8bd1795725bc9b714d0f92cbbc8053b84c19aa7d
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for 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 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 ;; Recursive :-(.
40 ;; (require 'gnus-art)
41 (require 'nnoo)
42 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
43 (autoload 'gnus-cache-write-active "gnus-cache")
44 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
45 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
46 (autoload 'mm-uu-dissect "mm-uu")
47
48 (defcustom gnus-kill-summary-on-exit t
49   "*If non-nil, kill the summary buffer when you exit from it.
50 If nil, the summary will become a \"*Dead Summary*\" buffer, and
51 it will be killed sometime later."
52   :group 'gnus-summary-exit
53   :type 'boolean)
54
55 (defcustom gnus-fetch-old-headers nil
56   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
57 If an unread article in the group refers to an older, already read (or
58 just marked as read) article, the old article will not normally be
59 displayed in the Summary buffer.  If this variable is non-nil, Gnus
60 will attempt to grab the headers to the old articles, and thereby
61 build complete threads.  If it has the value `some', only enough
62 headers to connect otherwise loose threads will be displayed.  This
63 variable can also be a number.  In that case, no more than that number
64 of old headers will be fetched.  If it has the value `invisible', all
65 old headers will be fetched, but none will be displayed.
66
67 The server has to support NOV for any of this to work."
68   :group 'gnus-thread
69   :type '(choice (const :tag "off" nil)
70                  (const some)
71                  number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-refer-thread-limit 200
75   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
76 If t, fetch all the available old headers."
77   :group 'gnus-thread
78   :type '(choice number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-summary-make-false-root 'adopt
82   "*nil means that Gnus won't gather loose threads.
83 If the root of a thread has expired or been read in a previous
84 session, the information necessary to build a complete thread has been
85 lost.  Instead of having many small sub-threads from this original thread
86 scattered all over the summary buffer, Gnus can gather them.
87
88 If non-nil, Gnus will try to gather all loose sub-threads from an
89 original thread into one large thread.
90
91 If this variable is non-nil, it should be one of `none', `adopt',
92 `dummy' or `empty'.
93
94 If this variable is `none', Gnus will not make a false root, but just
95 present the sub-threads after another.
96 If this variable is `dummy', Gnus will create a dummy root that will
97 have all the sub-threads as children.
98 If this variable is `adopt', Gnus will make one of the \"children\"
99 the parent and mark all the step-children as such.
100 If this variable is `empty', the \"children\" are printed with empty
101 subject fields.  (Or rather, they will be printed with a string
102 given by the `gnus-summary-same-subject' variable.)"
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const none)
106                  (const dummy)
107                  (const adopt)
108                  (const empty)))
109
110 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
111   "*A regexp to match subjects to be excluded from loose thread gathering.
112 As loose thread gathering is done on subjects only, that means that
113 there can be many false gatherings performed.  By rooting out certain
114 common subjects, gathering might become saner."
115   :group 'gnus-thread
116   :type 'regexp)
117
118 (defcustom gnus-summary-gather-subject-limit nil
119   "*Maximum length of subject comparisons when gathering loose threads.
120 Use nil to compare full subjects.  Setting this variable to a low
121 number will help gather threads that have been corrupted by
122 newsreaders chopping off subject lines, but it might also mean that
123 unrelated articles that have subject that happen to begin with the
124 same few characters will be incorrectly gathered.
125
126 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
127 comparing subjects."
128   :group 'gnus-thread
129   :type '(choice (const :tag "off" nil)
130                  (const fuzzy)
131                  (sexp :menu-tag "on" t)))
132
133 (defcustom gnus-simplify-subject-functions nil
134   "List of functions taking a string argument that simplify subjects.
135 The functions are applied recursively.
136
137 Useful functions to put in this list include: `gnus-simplify-subject-re',
138 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
139   :group 'gnus-thread
140   :type '(repeat function))
141
142 (defcustom gnus-simplify-ignored-prefixes nil
143   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  regexp))
147
148 (defcustom gnus-build-sparse-threads nil
149   "*If non-nil, fill in the gaps in threads.
150 If `some', only fill in the gaps that are needed to tie loose threads
151 together.  If `more', fill in all leaf nodes that Gnus can find.  If
152 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const some)
156                  (const more)
157                  (sexp :menu-tag "all" t)))
158
159 (defcustom gnus-summary-thread-gathering-function
160   'gnus-gather-threads-by-subject
161   "*Function used for gathering loose threads.
162 There are two pre-defined functions: `gnus-gather-threads-by-subject',
163 which only takes Subjects into consideration; and
164 `gnus-gather-threads-by-references', which compared the References
165 headers of the articles to find matches."
166   :group 'gnus-thread
167   :type '(radio (function-item gnus-gather-threads-by-subject)
168                 (function-item gnus-gather-threads-by-references)
169                 (function :tag "other")))
170
171 (defcustom gnus-summary-same-subject ""
172   "*String indicating that the current article has the same subject as the previous.
173 This variable will only be used if the value of
174 `gnus-summary-make-false-root' is `empty'."
175   :group 'gnus-summary-format
176   :type 'string)
177
178 (defcustom gnus-summary-goto-unread t
179   "*If t, many commands will go to the next unread article.
180 This applies to marking commands as well as other commands that
181 \"naturally\" select the next article, like, for instance, `SPC' at
182 the end of an article.
183
184 If nil, the marking commands do NOT go to the next unread article
185 (they go to the next article instead).  If `never', commands that
186 usually go to the next unread article, will go to the next article,
187 whether it is read or not."
188   :group 'gnus-summary-marks
189   :link '(custom-manual "(gnus)Setting Marks")
190   :type '(choice (const :tag "off" nil)
191                  (const never)
192                  (sexp :menu-tag "on" t)))
193
194 (defcustom gnus-summary-default-score 0
195   "*Default article score level.
196 All scores generated by the score files will be added to this score.
197 If this variable is nil, scoring will be disabled."
198   :group 'gnus-score-default
199   :type '(choice (const :tag "disable")
200                  integer))
201
202 (defcustom gnus-summary-default-high-score 0
203   "*Default threshold for a high scored article.
204 An article will be highlighted as high scored if its score is greater
205 than this score."
206   :group 'gnus-score-default
207   :type 'integer)
208
209 (defcustom gnus-summary-default-low-score 0
210   "*Default threshold for a low scored article.
211 An article will be highlighted as low scored if its score is smaller
212 than this score."
213   :group 'gnus-score-default
214   :type '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-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-move-split-methods nil
368   "*Variable used to suggest where articles are to be moved to.
369 It uses the same syntax as the `gnus-split-methods' variable.
370 However, whereas `gnus-split-methods' specifies file names as targets,
371 this variable specifies group names."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ?           ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for souped articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-forwarded-mark ?F
438   "*Mark used for articles that have been forwarded."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-recent-mark ?N
443   "*Mark used for articles that are recent."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-cached-mark ?*
448   "*Mark used for articles that are in the cache."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-saved-mark ?S
453   "*Mark used for articles that have been saved."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-unseen-mark ?.
458   "*Mark used for articles that haven't been seen."
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 gnus-pick-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 nil
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-display-arrow
811   (and (fboundp 'display-graphic-p)
812        (display-graphic-p))
813   "*If non-nil, display an arrow highlighting the current article."
814   :version "21.1"
815   :group 'gnus-summary
816   :type 'boolean)
817
818 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
819   "Face used for highlighting the current article in the summary buffer."
820   :group 'gnus-summary-visual
821   :type 'face)
822
823 (defcustom gnus-summary-highlight
824   '(((= mark gnus-canceled-mark)
825      . gnus-summary-cancelled-face)
826     ((and (> score default-high)
827           (or (= mark gnus-dormant-mark)
828               (= mark gnus-ticked-mark)))
829      . gnus-summary-high-ticked-face)
830     ((and (< score default-low)
831           (or (= mark gnus-dormant-mark)
832               (= mark gnus-ticked-mark)))
833      . gnus-summary-low-ticked-face)
834     ((or (= mark gnus-dormant-mark)
835          (= mark gnus-ticked-mark))
836      . gnus-summary-normal-ticked-face)
837     ((and (> score default-high) (= mark gnus-ancient-mark))
838      . gnus-summary-high-ancient-face)
839     ((and (< score default-low) (= mark gnus-ancient-mark))
840      . gnus-summary-low-ancient-face)
841     ((= mark gnus-ancient-mark)
842      . gnus-summary-normal-ancient-face)
843     ((and (> score default-high) (= mark gnus-unread-mark))
844      . gnus-summary-high-unread-face)
845     ((and (< score default-low) (= mark gnus-unread-mark))
846      . gnus-summary-low-unread-face)
847     ((= mark gnus-unread-mark)
848      . gnus-summary-normal-unread-face)
849     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
850                                                   gnus-undownloaded-mark)))
851      . gnus-summary-high-unread-face)
852     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
853                                                  gnus-undownloaded-mark)))
854      . gnus-summary-low-unread-face)
855     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856           (memq article gnus-newsgroup-unreads))
857      . gnus-summary-normal-unread-face)
858     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
859      . gnus-summary-normal-read-face)
860     ((> score default-high)
861      . gnus-summary-high-read-face)
862     ((< score default-low)
863      . gnus-summary-low-read-face)
864     (t
865      . gnus-summary-normal-read-face))
866   "*Controls the highlighting of summary buffer lines.
867
868 A list of (FORM . FACE) pairs.  When deciding how a a particular
869 summary line should be displayed, each form is evaluated.  The content
870 of the face field after the first true form is used.  You can change
871 how those summary lines are displayed, by editing the face field.
872
873 You can use the following variables in the FORM field.
874
875 score:        The article's score
876 default:      The default article score.
877 default-high: The default score for high scored articles.
878 default-low:  The default score for low scored articles.
879 below:        The score below which articles are automatically marked as read.
880 mark:         The articles mark."
881   :group 'gnus-summary-visual
882   :type '(repeat (cons (sexp :tag "Form" nil)
883                        face)))
884
885 (defcustom gnus-alter-header-function nil
886   "Function called to allow alteration of article header structures.
887 The function is called with one parameter, the article header vector,
888 which it may alter in any way.")
889
890 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
891   "Variable that says which function should be used to decode a string with encoded words.")
892
893 (defcustom gnus-extra-headers nil
894   "*Extra headers to parse."
895   :version "21.1"
896   :group 'gnus-summary
897   :type '(repeat symbol))
898
899 (defcustom gnus-ignored-from-addresses
900   (and user-mail-address (regexp-quote user-mail-address))
901   "*Regexp of From headers that may be suppressed in favor of To headers."
902   :version "21.1"
903   :group 'gnus-summary
904   :type 'regexp)
905
906 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
907   "List of charsets that should be ignored.
908 When these charsets are used in the \"charset\" parameter, the
909 default charset will be used instead."
910   :version "21.1"
911   :type '(repeat symbol)
912   :group 'gnus-charset)
913
914 (gnus-define-group-parameter
915  ignored-charsets
916  :type list
917  :function-document
918  "Return the ignored charsets of GROUP."
919  :variable gnus-group-ignored-charsets-alist
920  :variable-default
921  '(("alt\\.chinese\\.text" iso-8859-1))
922  :variable-document
923  "Alist of regexps (to match group names) and charsets that should be ignored.
924 When these charsets are used in the \"charset\" parameter, the
925 default charset will be used instead."
926  :variable-group gnus-charset
927  :variable-type '(repeat (cons (regexp :tag "Group")
928                                (repeat symbol)))
929  :parameter-type '(choice :tag "Ignored charsets"
930                           :value nil
931                           (repeat (symbol)))
932  :parameter-document       "\
933 List of charsets that should be ignored.
934
935 When these charsets are used in the \"charset\" parameter, the
936 default charset will be used instead.")
937
938 (defcustom gnus-group-highlight-words-alist nil
939   "Alist of group regexps and highlight regexps.
940 This variable uses the same syntax as `gnus-emphasis-alist'."
941   :version "21.1"
942   :type '(repeat (cons (regexp :tag "Group")
943                        (repeat (list (regexp :tag "Highlight regexp")
944                                      (number :tag "Group for entire word" 0)
945                                      (number :tag "Group for displayed part" 0)
946                                      (symbol :tag "Face"
947                                              gnus-emphasis-highlight-words)))))
948   :group 'gnus-summary-visual)
949
950 (defcustom gnus-summary-show-article-charset-alist
951   nil
952   "Alist of number and charset.
953 The article will be shown with the charset corresponding to the
954 numbered argument.
955 For example: ((1 . cn-gb-2312) (2 . big5))."
956   :version "21.1"
957   :type '(repeat (cons (number :tag "Argument" 1)
958                        (symbol :tag "Charset")))
959   :group 'gnus-charset)
960
961 (defcustom gnus-preserve-marks t
962   "Whether marks are preserved when moving, copying and respooling messages."
963   :version "21.1"
964   :type 'boolean
965   :group 'gnus-summary-marks)
966
967 (defcustom gnus-alter-articles-to-read-function nil
968   "Function to be called to alter the list of articles to be selected."
969   :type '(choice (const nil) function)
970   :group 'gnus-summary)
971
972 (defcustom gnus-orphan-score nil
973   "*All orphans get this score added.  Set in the score file."
974   :group 'gnus-score-default
975   :type '(choice (const nil)
976                  integer))
977
978 (defcustom gnus-summary-save-parts-default-mime "image/.*"
979   "*A regexp to match MIME parts when saving multiple parts of a message
980 with gnus-summary-save-parts (X m). This regexp will be used by default
981 when prompting the user for which type of files to save."
982   :group 'gnus-summary
983   :type 'regexp)
984
985
986 (defcustom gnus-summary-save-parts-default-mime "image/.*"
987   "*A regexp to match MIME parts when saving multiple parts of a message
988 with gnus-summary-save-parts (X m). This regexp will be used by default
989 when prompting the user for which type of files to save."
990   :group 'gnus-summary
991   :type 'regexp)
992
993 (defcustom gnus-read-all-available-headers nil
994   "Whether Gnus should parse all headers made available to it.
995 This is mostly relevant for slow backends where the user may
996 wish to widen the summary buffer to include all headers
997 that were fetched.  Say, for nnultimate groups."
998   :group 'gnus-summary
999   :type '(choice boolean regexp))
1000
1001 ;;; Internal variables
1002
1003 (defvar gnus-summary-display-cache nil)
1004 (defvar gnus-article-mime-handles nil)
1005 (defvar gnus-article-decoded-p nil)
1006 (defvar gnus-article-charset nil)
1007 (defvar gnus-article-ignored-charsets nil)
1008 (defvar gnus-scores-exclude-files nil)
1009 (defvar gnus-page-broken nil)
1010 (defvar gnus-inhibit-mime-unbuttonizing nil)
1011
1012 (defvar gnus-original-article nil)
1013 (defvar gnus-article-internal-prepare-hook nil)
1014 (defvar gnus-newsgroup-process-stack nil)
1015
1016 (defvar gnus-thread-indent-array nil)
1017 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1018 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1019   "Function called to sort the articles within a thread after it has been gathered together.")
1020
1021 (defvar gnus-summary-save-parts-type-history nil)
1022 (defvar gnus-summary-save-parts-last-directory nil)
1023
1024 (defvar gnus-summary-save-parts-type-history nil)
1025 (defvar gnus-summary-save-parts-last-directory nil)
1026
1027 ;; Avoid highlighting in kill files.
1028 (defvar gnus-summary-inhibit-highlight nil)
1029 (defvar gnus-newsgroup-selected-overlay nil)
1030 (defvar gnus-inhibit-limiting nil)
1031 (defvar gnus-newsgroup-adaptive-score-file nil)
1032 (defvar gnus-current-score-file nil)
1033 (defvar gnus-current-move-group nil)
1034 (defvar gnus-current-copy-group nil)
1035 (defvar gnus-current-crosspost-group nil)
1036 (defvar gnus-newsgroup-display nil)
1037
1038 (defvar gnus-newsgroup-dependencies nil)
1039 (defvar gnus-newsgroup-adaptive nil)
1040 (defvar gnus-summary-display-article-function nil)
1041 (defvar gnus-summary-highlight-line-function nil
1042   "Function called after highlighting a summary line.")
1043
1044 (defvar gnus-summary-line-format-alist
1045   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1046     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1047     (?s gnus-tmp-subject-or-nil ?s)
1048     (?n gnus-tmp-name ?s)
1049     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1050         ?s)
1051     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1052             gnus-tmp-from) ?s)
1053     (?F gnus-tmp-from ?s)
1054     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1055     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1056     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1057     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1058     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1059     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1060     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1061     (?L gnus-tmp-lines ?s)
1062     (?I gnus-tmp-indentation ?s)
1063     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1064     (?R gnus-tmp-replied ?c)
1065     (?\[ gnus-tmp-opening-bracket ?c)
1066     (?\] gnus-tmp-closing-bracket ?c)
1067     (?\> (make-string gnus-tmp-level ? ) ?s)
1068     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1069     (?i gnus-tmp-score ?d)
1070     (?z gnus-tmp-score-char ?c)
1071     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1072     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1073     (?U gnus-tmp-unread ?c)
1074     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1075         ?s)
1076     (?t (gnus-summary-number-of-articles-in-thread
1077          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1078         ?d)
1079     (?e (gnus-summary-number-of-articles-in-thread
1080          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1081         ?c)
1082     (?u gnus-tmp-user-defined ?s)
1083     (?P (gnus-pick-line-number) ?d)
1084     (?B gnus-tmp-thread-tree-header-string ?s)
1085     (user-date (gnus-user-date 
1086                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1087   "An alist of format specifications that can appear in summary lines.
1088 These are paired with what variables they correspond with, along with
1089 the type of the variable (string, integer, character, etc).")
1090
1091 (defvar gnus-summary-dummy-line-format-alist
1092   `((?S gnus-tmp-subject ?s)
1093     (?N gnus-tmp-number ?d)
1094     (?u gnus-tmp-user-defined ?s)))
1095
1096 (defvar gnus-summary-mode-line-format-alist
1097   `((?G gnus-tmp-group-name ?s)
1098     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1099     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1100     (?A gnus-tmp-article-number ?d)
1101     (?Z gnus-tmp-unread-and-unselected ?s)
1102     (?V gnus-version ?s)
1103     (?U gnus-tmp-unread-and-unticked ?d)
1104     (?S gnus-tmp-subject ?s)
1105     (?e gnus-tmp-unselected ?d)
1106     (?u gnus-tmp-user-defined ?s)
1107     (?d (length gnus-newsgroup-dormant) ?d)
1108     (?t (length gnus-newsgroup-marked) ?d)
1109     (?r (length gnus-newsgroup-reads) ?d)
1110     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1111     (?E gnus-newsgroup-expunged-tally ?d)
1112     (?s (gnus-current-score-file-nondirectory) ?s)))
1113
1114 (defvar gnus-last-search-regexp nil
1115   "Default regexp for article search command.")
1116
1117 (defvar gnus-last-shell-command nil
1118   "Default shell command on article.")
1119
1120 (defvar gnus-newsgroup-begin nil)
1121 (defvar gnus-newsgroup-end nil)
1122 (defvar gnus-newsgroup-last-rmail nil)
1123 (defvar gnus-newsgroup-last-mail nil)
1124 (defvar gnus-newsgroup-last-folder nil)
1125 (defvar gnus-newsgroup-last-file nil)
1126 (defvar gnus-newsgroup-auto-expire nil)
1127 (defvar gnus-newsgroup-active nil)
1128
1129 (defvar gnus-newsgroup-data nil)
1130 (defvar gnus-newsgroup-data-reverse nil)
1131 (defvar gnus-newsgroup-limit nil)
1132 (defvar gnus-newsgroup-limits nil)
1133
1134 (defvar gnus-newsgroup-unreads nil
1135   "List of unread articles in the current newsgroup.")
1136
1137 (defvar gnus-newsgroup-unselected nil
1138   "List of unselected unread articles in the current newsgroup.")
1139
1140 (defvar gnus-newsgroup-reads nil
1141   "Alist of read articles and article marks in the current newsgroup.")
1142
1143 (defvar gnus-newsgroup-expunged-tally nil)
1144
1145 (defvar gnus-newsgroup-marked nil
1146   "List of ticked articles in the current newsgroup (a subset of unread art).")
1147
1148 (defvar gnus-newsgroup-killed nil
1149   "List of ranges of articles that have been through the scoring process.")
1150
1151 (defvar gnus-newsgroup-cached nil
1152   "List of articles that come from the article cache.")
1153
1154 (defvar gnus-newsgroup-saved nil
1155   "List of articles that have been saved.")
1156
1157 (defvar gnus-newsgroup-kill-headers nil)
1158
1159 (defvar gnus-newsgroup-replied nil
1160   "List of articles that have been replied to in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-forwarded nil
1163   "List of articles that have been forwarded in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-recent nil
1166   "List of articles that have are recent in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-expirable nil
1169   "List of articles in the current newsgroup that can be expired.")
1170
1171 (defvar gnus-newsgroup-processable nil
1172   "List of articles in the current newsgroup that can be processed.")
1173
1174 (defvar gnus-newsgroup-downloadable nil
1175   "List of articles in the current newsgroup that can be processed.")
1176
1177 (defvar gnus-newsgroup-undownloaded nil
1178   "List of articles in the current newsgroup that haven't been downloaded..")
1179
1180 (defvar gnus-newsgroup-unsendable nil
1181   "List of articles in the current newsgroup that won't be sent.")
1182
1183 (defvar gnus-newsgroup-bookmarks nil
1184   "List of articles in the current newsgroup that have bookmarks.")
1185
1186 (defvar gnus-newsgroup-dormant nil
1187   "List of dormant articles in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-unseen nil
1190   "List of unseen articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-seen nil
1193   "Range of seen articles in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-articles nil
1196   "List of articles in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-scored nil
1199   "List of scored articles in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-headers nil
1202   "List of article headers in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-threads nil)
1205
1206 (defvar gnus-newsgroup-prepared nil
1207   "Whether the current group has been prepared properly.")
1208
1209 (defvar gnus-newsgroup-ancient nil
1210   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-sparse nil)
1213
1214 (defvar gnus-current-article nil)
1215 (defvar gnus-article-current nil)
1216 (defvar gnus-current-headers nil)
1217 (defvar gnus-have-all-headers nil)
1218 (defvar gnus-last-article nil)
1219 (defvar gnus-newsgroup-history nil)
1220 (defvar gnus-newsgroup-charset nil)
1221 (defvar gnus-newsgroup-ephemeral-charset nil)
1222 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1223
1224 (defvar gnus-article-before-search nil)
1225
1226 (defconst gnus-summary-local-variables
1227   '(gnus-newsgroup-name
1228     gnus-newsgroup-begin gnus-newsgroup-end
1229     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1230     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1231     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1232     gnus-newsgroup-unselected gnus-newsgroup-marked
1233     gnus-newsgroup-reads gnus-newsgroup-saved
1234     gnus-newsgroup-replied gnus-newsgroup-forwarded
1235     gnus-newsgroup-recent
1236     gnus-newsgroup-expirable
1237     gnus-newsgroup-processable gnus-newsgroup-killed
1238     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1239     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1240     gnus-newsgroup-seen gnus-newsgroup-articles
1241     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1242     gnus-newsgroup-headers gnus-newsgroup-threads
1243     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1244     gnus-current-article gnus-current-headers gnus-have-all-headers
1245     gnus-last-article gnus-article-internal-prepare-hook
1246     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1247     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1248     gnus-thread-expunge-below
1249     gnus-score-alist gnus-current-score-file
1250     (gnus-summary-expunge-below . global)
1251     (gnus-summary-mark-below . global)
1252     (gnus-orphan-score . global)
1253     gnus-newsgroup-active gnus-scores-exclude-files
1254     gnus-newsgroup-history gnus-newsgroup-ancient
1255     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1256     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1257     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1258     (gnus-newsgroup-expunged-tally . 0)
1259     gnus-cache-removable-articles gnus-newsgroup-cached
1260     gnus-newsgroup-data gnus-newsgroup-data-reverse
1261     gnus-newsgroup-limit gnus-newsgroup-limits
1262     gnus-newsgroup-charset gnus-newsgroup-display)
1263   "Variables that are buffer-local to the summary buffers.")
1264
1265 (defvar gnus-newsgroup-variables nil
1266   "A list of variables that have separate values in different newsgroups.
1267 A list of newsgroup (summary buffer) local variables, or cons of
1268 variables and their default values (when the default values are not
1269 nil), that should be made global while the summary buffer is active.
1270 These variables can be used to set variables in the group parameters
1271 while still allowing them to affect operations done in other
1272 buffers. For example:
1273
1274 (setq gnus-newsgroup-variables 
1275      '(message-use-followup-to
1276        (gnus-visible-headers . 
1277          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1278 ")
1279
1280 ;; Byte-compiler warning.
1281 (eval-when-compile (defvar gnus-article-mode-map))
1282
1283 ;; MIME stuff.
1284
1285 (defvar gnus-decode-encoded-word-methods
1286   '(mail-decode-encoded-word-string)
1287   "List of methods used to decode encoded words.
1288
1289 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1290 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1291 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1292 whose names match REGEXP.
1293
1294 For example:
1295 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1296  mail-decode-encoded-word-string
1297  (\"chinese\" . rfc1843-decode-string))")
1298
1299 (defvar gnus-decode-encoded-word-methods-cache nil)
1300
1301 (defun gnus-multi-decode-encoded-word-string (string)
1302   "Apply the functions from `gnus-encoded-word-methods' that match."
1303   (unless (and gnus-decode-encoded-word-methods-cache
1304                (eq gnus-newsgroup-name
1305                    (car gnus-decode-encoded-word-methods-cache)))
1306     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1307     (mapcar (lambda (x)
1308               (if (symbolp x)
1309                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1310                 (if (and gnus-newsgroup-name
1311                          (string-match (car x) gnus-newsgroup-name))
1312                     (nconc gnus-decode-encoded-word-methods-cache
1313                            (list (cdr x))))))
1314             gnus-decode-encoded-word-methods))
1315   (let ((xlist gnus-decode-encoded-word-methods-cache))
1316     (pop xlist)
1317     (while xlist
1318       (setq string (funcall (pop xlist) string))))
1319   string)
1320
1321 ;; Subject simplification.
1322
1323 (defun gnus-simplify-whitespace (str)
1324   "Remove excessive whitespace from STR."
1325   (let ((mystr str))
1326     ;; Multiple spaces.
1327     (while (string-match "[ \t][ \t]+" mystr)
1328       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1329                           " "
1330                           (substring mystr (match-end 0)))))
1331     ;; Leading spaces.
1332     (when (string-match "^[ \t]+" mystr)
1333       (setq mystr (substring mystr (match-end 0))))
1334     ;; Trailing spaces.
1335     (when (string-match "[ \t]+$" mystr)
1336       (setq mystr (substring mystr 0 (match-beginning 0))))
1337     mystr))
1338
1339 (defsubst gnus-simplify-subject-re (subject)
1340   "Remove \"Re:\" from subject lines."
1341   (if (string-match message-subject-re-regexp subject)
1342       (substring subject (match-end 0))
1343     subject))
1344
1345 (defun gnus-simplify-subject (subject &optional re-only)
1346   "Remove `Re:' and words in parentheses.
1347 If RE-ONLY is non-nil, strip leading `Re:'s only."
1348   (let ((case-fold-search t))           ;Ignore case.
1349     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1350     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1351       (setq subject (substring subject (match-end 0))))
1352     ;; Remove uninteresting prefixes.
1353     (when (and (not re-only)
1354                gnus-simplify-ignored-prefixes
1355                (string-match gnus-simplify-ignored-prefixes subject))
1356       (setq subject (substring subject (match-end 0))))
1357     ;; Remove words in parentheses from end.
1358     (unless re-only
1359       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1360         (setq subject (substring subject 0 (match-beginning 0)))))
1361     ;; Return subject string.
1362     subject))
1363
1364 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1365 ;; all whitespace.
1366 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1367   (goto-char (point-min))
1368   (while (re-search-forward regexp nil t)
1369     (replace-match (or newtext ""))))
1370
1371 (defun gnus-simplify-buffer-fuzzy ()
1372   "Simplify string in the buffer fuzzily.
1373 The string in the accessible portion of the current buffer is simplified.
1374 It is assumed to be a single-line subject.
1375 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1376 matter is removed.  Additional things can be deleted by setting
1377 `gnus-simplify-subject-fuzzy-regexp'."
1378   (let ((case-fold-search t)
1379         (modified-tick))
1380     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1381
1382     (while (not (eq modified-tick (buffer-modified-tick)))
1383       (setq modified-tick (buffer-modified-tick))
1384       (cond
1385        ((listp gnus-simplify-subject-fuzzy-regexp)
1386         (mapcar 'gnus-simplify-buffer-fuzzy-step
1387                 gnus-simplify-subject-fuzzy-regexp))
1388        (gnus-simplify-subject-fuzzy-regexp
1389         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1390       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1391       (gnus-simplify-buffer-fuzzy-step
1392        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1393       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1394
1395     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1396     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1397     (gnus-simplify-buffer-fuzzy-step " $")
1398     (gnus-simplify-buffer-fuzzy-step "^ +")))
1399
1400 (defun gnus-simplify-subject-fuzzy (subject)
1401   "Simplify a subject string fuzzily.
1402 See `gnus-simplify-buffer-fuzzy' for details."
1403   (save-excursion
1404     (gnus-set-work-buffer)
1405     (let ((case-fold-search t))
1406       ;; Remove uninteresting prefixes.
1407       (when (and gnus-simplify-ignored-prefixes
1408                  (string-match gnus-simplify-ignored-prefixes subject))
1409         (setq subject (substring subject (match-end 0))))
1410       (insert subject)
1411       (inline (gnus-simplify-buffer-fuzzy))
1412       (buffer-string))))
1413
1414 (defsubst gnus-simplify-subject-fully (subject)
1415   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1416   (cond
1417    (gnus-simplify-subject-functions
1418     (gnus-map-function gnus-simplify-subject-functions subject))
1419    ((null gnus-summary-gather-subject-limit)
1420     (gnus-simplify-subject-re subject))
1421    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1422     (gnus-simplify-subject-fuzzy subject))
1423    ((numberp gnus-summary-gather-subject-limit)
1424     (gnus-limit-string (gnus-simplify-subject-re subject)
1425                        gnus-summary-gather-subject-limit))
1426    (t
1427     subject)))
1428
1429 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1430   "Check whether two subjects are equal.
1431 If optional argument simple-first is t, first argument is already
1432 simplified."
1433   (cond
1434    ((null simple-first)
1435     (equal (gnus-simplify-subject-fully s1)
1436            (gnus-simplify-subject-fully s2)))
1437    (t
1438     (equal s1
1439            (gnus-simplify-subject-fully s2)))))
1440
1441 (defun gnus-summary-bubble-group ()
1442   "Increase the score of the current group.
1443 This is a handy function to add to `gnus-summary-exit-hook' to
1444 increase the score of each group you read."
1445   (gnus-group-add-score gnus-newsgroup-name))
1446
1447 \f
1448 ;;;
1449 ;;; Gnus summary mode
1450 ;;;
1451
1452 (put 'gnus-summary-mode 'mode-class 'special)
1453
1454 (defvar gnus-article-commands-menu)
1455
1456 (when t
1457   ;; Non-orthogonal keys
1458
1459   (gnus-define-keys gnus-summary-mode-map
1460     " " gnus-summary-next-page
1461     "\177" gnus-summary-prev-page
1462     [delete] gnus-summary-prev-page
1463     [backspace] gnus-summary-prev-page
1464     "\r" gnus-summary-scroll-up
1465     "\M-\r" gnus-summary-scroll-down
1466     "n" gnus-summary-next-unread-article
1467     "p" gnus-summary-prev-unread-article
1468     "N" gnus-summary-next-article
1469     "P" gnus-summary-prev-article
1470     "\M-\C-n" gnus-summary-next-same-subject
1471     "\M-\C-p" gnus-summary-prev-same-subject
1472     "\M-n" gnus-summary-next-unread-subject
1473     "\M-p" gnus-summary-prev-unread-subject
1474     "." gnus-summary-first-unread-article
1475     "," gnus-summary-best-unread-article
1476     "\M-s" gnus-summary-search-article-forward
1477     "\M-r" gnus-summary-search-article-backward
1478     "<" gnus-summary-beginning-of-article
1479     ">" gnus-summary-end-of-article
1480     "j" gnus-summary-goto-article
1481     "^" gnus-summary-refer-parent-article
1482     "\M-^" gnus-summary-refer-article
1483     "u" gnus-summary-tick-article-forward
1484     "!" gnus-summary-tick-article-forward
1485     "U" gnus-summary-tick-article-backward
1486     "d" gnus-summary-mark-as-read-forward
1487     "D" gnus-summary-mark-as-read-backward
1488     "E" gnus-summary-mark-as-expirable
1489     "\M-u" gnus-summary-clear-mark-forward
1490     "\M-U" gnus-summary-clear-mark-backward
1491     "k" gnus-summary-kill-same-subject-and-select
1492     "\C-k" gnus-summary-kill-same-subject
1493     "\M-\C-k" gnus-summary-kill-thread
1494     "\M-\C-l" gnus-summary-lower-thread
1495     "e" gnus-summary-edit-article
1496     "#" gnus-summary-mark-as-processable
1497     "\M-#" gnus-summary-unmark-as-processable
1498     "\M-\C-t" gnus-summary-toggle-threads
1499     "\M-\C-s" gnus-summary-show-thread
1500     "\M-\C-h" gnus-summary-hide-thread
1501     "\M-\C-f" gnus-summary-next-thread
1502     "\M-\C-b" gnus-summary-prev-thread
1503     [(meta down)] gnus-summary-next-thread
1504     [(meta up)] gnus-summary-prev-thread
1505     "\M-\C-u" gnus-summary-up-thread
1506     "\M-\C-d" gnus-summary-down-thread
1507     "&" gnus-summary-execute-command
1508     "c" gnus-summary-catchup-and-exit
1509     "\C-w" gnus-summary-mark-region-as-read
1510     "\C-t" gnus-summary-toggle-truncation
1511     "?" gnus-summary-mark-as-dormant
1512     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1513     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1514     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1515     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1516     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1517     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1518     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1519     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1520     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1521     "=" gnus-summary-expand-window
1522     "\C-x\C-s" gnus-summary-reselect-current-group
1523     "\M-g" gnus-summary-rescan-group
1524     "w" gnus-summary-stop-page-breaking
1525     "\C-c\C-r" gnus-summary-caesar-message
1526     "f" gnus-summary-followup
1527     "F" gnus-summary-followup-with-original
1528     "C" gnus-summary-cancel-article
1529     "r" gnus-summary-reply
1530     "R" gnus-summary-reply-with-original
1531     "\C-c\C-f" gnus-summary-mail-forward
1532     "o" gnus-summary-save-article
1533     "\C-o" gnus-summary-save-article-mail
1534     "|" gnus-summary-pipe-output
1535     "\M-k" gnus-summary-edit-local-kill
1536     "\M-K" gnus-summary-edit-global-kill
1537     ;; "V" gnus-version
1538     "\C-c\C-d" gnus-summary-describe-group
1539     "q" gnus-summary-exit
1540     "Q" gnus-summary-exit-no-update
1541     "\C-c\C-i" gnus-info-find-node
1542     gnus-mouse-2 gnus-mouse-pick-article
1543     "m" gnus-summary-mail-other-window
1544     "a" gnus-summary-post-news
1545     "x" gnus-summary-limit-to-unread
1546     "s" gnus-summary-isearch-article
1547     "t" gnus-summary-toggle-header
1548     "g" gnus-summary-show-article
1549     "l" gnus-summary-goto-last-article
1550     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1551     "\C-d" gnus-summary-enter-digest-group
1552     "\M-\C-d" gnus-summary-read-document
1553     "\M-\C-e" gnus-summary-edit-parameters
1554     "\M-\C-a" gnus-summary-customize-parameters
1555     "\C-c\C-b" gnus-bug
1556     "*" gnus-cache-enter-article
1557     "\M-*" gnus-cache-remove-article
1558     "\M-&" gnus-summary-universal-argument
1559     "\C-l" gnus-recenter
1560     "I" gnus-summary-increase-score
1561     "L" gnus-summary-lower-score
1562     "\M-i" gnus-symbolic-argument
1563     "h" gnus-summary-select-article-buffer
1564
1565     "b" gnus-article-view-part
1566     "\M-t" gnus-summary-toggle-display-buttonized
1567
1568     "V" gnus-summary-score-map
1569     "X" gnus-uu-extract-map
1570     "S" gnus-summary-send-map)
1571
1572   ;; Sort of orthogonal keymap
1573   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1574     "t" gnus-summary-tick-article-forward
1575     "!" gnus-summary-tick-article-forward
1576     "d" gnus-summary-mark-as-read-forward
1577     "r" gnus-summary-mark-as-read-forward
1578     "c" gnus-summary-clear-mark-forward
1579     " " gnus-summary-clear-mark-forward
1580     "e" gnus-summary-mark-as-expirable
1581     "x" gnus-summary-mark-as-expirable
1582     "?" gnus-summary-mark-as-dormant
1583     "b" gnus-summary-set-bookmark
1584     "B" gnus-summary-remove-bookmark
1585     "#" gnus-summary-mark-as-processable
1586     "\M-#" gnus-summary-unmark-as-processable
1587     "S" gnus-summary-limit-include-expunged
1588     "C" gnus-summary-catchup
1589     "H" gnus-summary-catchup-to-here
1590     "h" gnus-summary-catchup-from-here
1591     "\C-c" gnus-summary-catchup-all
1592     "k" gnus-summary-kill-same-subject-and-select
1593     "K" gnus-summary-kill-same-subject
1594     "P" gnus-uu-mark-map)
1595
1596   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1597     "c" gnus-summary-clear-above
1598     "u" gnus-summary-tick-above
1599     "m" gnus-summary-mark-above
1600     "k" gnus-summary-kill-below)
1601
1602   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1603     "/" gnus-summary-limit-to-subject
1604     "n" gnus-summary-limit-to-articles
1605     "w" gnus-summary-pop-limit
1606     "s" gnus-summary-limit-to-subject
1607     "a" gnus-summary-limit-to-author
1608     "u" gnus-summary-limit-to-unread
1609     "m" gnus-summary-limit-to-marks
1610     "M" gnus-summary-limit-exclude-marks
1611     "v" gnus-summary-limit-to-score
1612     "*" gnus-summary-limit-include-cached
1613     "D" gnus-summary-limit-include-dormant
1614     "T" gnus-summary-limit-include-thread
1615     "d" gnus-summary-limit-exclude-dormant
1616     "t" gnus-summary-limit-to-age
1617     "x" gnus-summary-limit-to-extra
1618     "p" gnus-summary-limit-to-display-predicate
1619     "E" gnus-summary-limit-include-expunged
1620     "c" gnus-summary-limit-exclude-childless-dormant
1621     "C" gnus-summary-limit-mark-excluded-as-read
1622     "o" gnus-summary-insert-old-articles
1623     "N" gnus-summary-insert-new-articles)
1624
1625   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1626     "n" gnus-summary-next-unread-article
1627     "p" gnus-summary-prev-unread-article
1628     "N" gnus-summary-next-article
1629     "P" gnus-summary-prev-article
1630     "\C-n" gnus-summary-next-same-subject
1631     "\C-p" gnus-summary-prev-same-subject
1632     "\M-n" gnus-summary-next-unread-subject
1633     "\M-p" gnus-summary-prev-unread-subject
1634     "f" gnus-summary-first-unread-article
1635     "b" gnus-summary-best-unread-article
1636     "j" gnus-summary-goto-article
1637     "g" gnus-summary-goto-subject
1638     "l" gnus-summary-goto-last-article
1639     "o" gnus-summary-pop-article)
1640
1641   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1642     "k" gnus-summary-kill-thread
1643     "l" gnus-summary-lower-thread
1644     "i" gnus-summary-raise-thread
1645     "T" gnus-summary-toggle-threads
1646     "t" gnus-summary-rethread-current
1647     "^" gnus-summary-reparent-thread
1648     "s" gnus-summary-show-thread
1649     "S" gnus-summary-show-all-threads
1650     "h" gnus-summary-hide-thread
1651     "H" gnus-summary-hide-all-threads
1652     "n" gnus-summary-next-thread
1653     "p" gnus-summary-prev-thread
1654     "u" gnus-summary-up-thread
1655     "o" gnus-summary-top-thread
1656     "d" gnus-summary-down-thread
1657     "#" gnus-uu-mark-thread
1658     "\M-#" gnus-uu-unmark-thread)
1659
1660   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1661     "g" gnus-summary-prepare
1662     "c" gnus-summary-insert-cached-articles)
1663
1664   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1665     "c" gnus-summary-catchup-and-exit
1666     "C" gnus-summary-catchup-all-and-exit
1667     "E" gnus-summary-exit-no-update
1668     "Q" gnus-summary-exit
1669     "Z" gnus-summary-exit
1670     "n" gnus-summary-catchup-and-goto-next-group
1671     "R" gnus-summary-reselect-current-group
1672     "G" gnus-summary-rescan-group
1673     "N" gnus-summary-next-group
1674     "s" gnus-summary-save-newsrc
1675     "P" gnus-summary-prev-group)
1676
1677   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1678     " " gnus-summary-next-page
1679     "n" gnus-summary-next-page
1680     "\177" gnus-summary-prev-page
1681     [delete] gnus-summary-prev-page
1682     "p" gnus-summary-prev-page
1683     "\r" gnus-summary-scroll-up
1684     "\M-\r" gnus-summary-scroll-down
1685     "<" gnus-summary-beginning-of-article
1686     ">" gnus-summary-end-of-article
1687     "b" gnus-summary-beginning-of-article
1688     "e" gnus-summary-end-of-article
1689     "^" gnus-summary-refer-parent-article
1690     "r" gnus-summary-refer-parent-article
1691     "D" gnus-summary-enter-digest-group
1692     "R" gnus-summary-refer-references
1693     "T" gnus-summary-refer-thread
1694     "g" gnus-summary-show-article
1695     "s" gnus-summary-isearch-article
1696     "P" gnus-summary-print-article
1697     "M" gnus-mailing-list-insinuate
1698     "t" gnus-article-babel)
1699
1700   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1701     "b" gnus-article-add-buttons
1702     "B" gnus-article-add-buttons-to-head
1703     "o" gnus-article-treat-overstrike
1704     "e" gnus-article-emphasize
1705     "w" gnus-article-fill-cited-article
1706     "Q" gnus-article-fill-long-lines
1707     "C" gnus-article-capitalize-sentences
1708     "c" gnus-article-remove-cr
1709     "q" gnus-article-de-quoted-unreadable
1710     "6" gnus-article-de-base64-unreadable
1711     "Z" gnus-article-decode-HZ
1712     "h" gnus-article-wash-html
1713     "s" gnus-summary-force-verify-and-decrypt
1714     "f" gnus-article-display-x-face
1715     "l" gnus-summary-stop-page-breaking
1716     "r" gnus-summary-caesar-message
1717     "t" gnus-summary-toggle-header
1718     "g" gnus-summary-toggle-smiley
1719     "v" gnus-summary-verbose-headers
1720     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1721     "p" gnus-article-verify-x-pgp-sig
1722     "d" gnus-article-treat-dumbquotes)
1723
1724   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1725     "a" gnus-article-hide
1726     "h" gnus-article-hide-headers
1727     "b" gnus-article-hide-boring-headers
1728     "s" gnus-article-hide-signature
1729     "c" gnus-article-hide-citation
1730     "C" gnus-article-hide-citation-in-followups
1731     "l" gnus-article-hide-list-identifiers
1732     "p" gnus-article-hide-pgp
1733     "B" gnus-article-strip-banner
1734     "P" gnus-article-hide-pem
1735     "\C-c" gnus-article-hide-citation-maybe)
1736
1737   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1738     "a" gnus-article-highlight
1739     "h" gnus-article-highlight-headers
1740     "c" gnus-article-highlight-citation
1741     "s" gnus-article-highlight-signature)
1742
1743   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1744     "w" gnus-article-decode-mime-words
1745     "c" gnus-article-decode-charset
1746     "v" gnus-mime-view-all-parts
1747     "b" gnus-article-view-part)
1748
1749   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1750     "z" gnus-article-date-ut
1751     "u" gnus-article-date-ut
1752     "l" gnus-article-date-local
1753     "p" gnus-article-date-english
1754     "e" gnus-article-date-lapsed
1755     "o" gnus-article-date-original
1756     "i" gnus-article-date-iso8601
1757     "s" gnus-article-date-user)
1758
1759   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1760     "t" gnus-article-remove-trailing-blank-lines
1761     "l" gnus-article-strip-leading-blank-lines
1762     "m" gnus-article-strip-multiple-blank-lines
1763     "a" gnus-article-strip-blank-lines
1764     "A" gnus-article-strip-all-blank-lines
1765     "s" gnus-article-strip-leading-space
1766     "e" gnus-article-strip-trailing-space
1767     "w" gnus-article-remove-leading-whitespace)
1768
1769   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1770     "v" gnus-version
1771     "f" gnus-summary-fetch-faq
1772     "d" gnus-summary-describe-group
1773     "h" gnus-summary-describe-briefly
1774     "i" gnus-info-find-node)
1775
1776   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1777     "e" gnus-summary-expire-articles
1778     "\M-\C-e" gnus-summary-expire-articles-now
1779     "\177" gnus-summary-delete-article
1780     [delete] gnus-summary-delete-article
1781     [backspace] gnus-summary-delete-article
1782     "m" gnus-summary-move-article
1783     "r" gnus-summary-respool-article
1784     "w" gnus-summary-edit-article
1785     "c" gnus-summary-copy-article
1786     "B" gnus-summary-crosspost-article
1787     "q" gnus-summary-respool-query
1788     "t" gnus-summary-respool-trace
1789     "i" gnus-summary-import-article
1790     "I" gnus-summary-create-article
1791     "p" gnus-summary-article-posted-p)
1792
1793   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1794     "o" gnus-summary-save-article
1795     "m" gnus-summary-save-article-mail
1796     "F" gnus-summary-write-article-file
1797     "r" gnus-summary-save-article-rmail
1798     "f" gnus-summary-save-article-file
1799     "b" gnus-summary-save-article-body-file
1800     "h" gnus-summary-save-article-folder
1801     "v" gnus-summary-save-article-vm
1802     "p" gnus-summary-pipe-output
1803     "s" gnus-soup-add-article)
1804
1805   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1806     "b" gnus-summary-display-buttonized
1807     "m" gnus-summary-repair-multipart
1808     "v" gnus-article-view-part
1809     "o" gnus-article-save-part
1810     "c" gnus-article-copy-part
1811     "C" gnus-article-view-part-as-charset
1812     "e" gnus-article-externalize-part
1813     "E" gnus-article-encrypt-body
1814     "i" gnus-article-inline-part
1815     "|" gnus-article-pipe-part))
1816
1817 (defvar gnus-article-post-menu nil)
1818
1819 (defun gnus-summary-make-menu-bar ()
1820   (gnus-turn-off-edit-menu 'summary)
1821
1822   (unless (boundp 'gnus-summary-misc-menu)
1823
1824     (easy-menu-define
1825       gnus-summary-kill-menu gnus-summary-mode-map ""
1826       (cons
1827        "Score"
1828        (nconc
1829         (list
1830          ["Customize" gnus-score-customize t])
1831         (gnus-make-score-map 'increase)
1832         (gnus-make-score-map 'lower)
1833         '(("Mark"
1834            ["Kill below" gnus-summary-kill-below t]
1835            ["Mark above" gnus-summary-mark-above t]
1836            ["Tick above" gnus-summary-tick-above t]
1837            ["Clear above" gnus-summary-clear-above t])
1838           ["Current score" gnus-summary-current-score t]
1839           ["Set score" gnus-summary-set-score t]
1840           ["Switch current score file..." gnus-score-change-score-file t]
1841           ["Set mark below..." gnus-score-set-mark-below t]
1842           ["Set expunge below..." gnus-score-set-expunge-below t]
1843           ["Edit current score file" gnus-score-edit-current-scores t]
1844           ["Edit score file" gnus-score-edit-file t]
1845           ["Trace score" gnus-score-find-trace t]
1846           ["Find words" gnus-score-find-favourite-words t]
1847           ["Rescore buffer" gnus-summary-rescore t]
1848           ["Increase score..." gnus-summary-increase-score t]
1849           ["Lower score..." gnus-summary-lower-score t]))))
1850
1851 ;; Define both the Article menu in the summary buffer and the equivalent
1852     ;; Commands menu in the article buffer here for consistency.
1853     (let ((innards
1854            `(("Hide"
1855               ["All" gnus-article-hide t]
1856               ["Headers" gnus-article-hide-headers t]
1857               ["Signature" gnus-article-hide-signature t]
1858               ["Citation" gnus-article-hide-citation t]
1859               ["List identifiers" gnus-article-hide-list-identifiers t]
1860               ["PGP" gnus-article-hide-pgp t]
1861               ["Banner" gnus-article-strip-banner t]
1862               ["Boring headers" gnus-article-hide-boring-headers t])
1863              ("Highlight"
1864               ["All" gnus-article-highlight t]
1865               ["Headers" gnus-article-highlight-headers t]
1866               ["Signature" gnus-article-highlight-signature t]
1867               ["Citation" gnus-article-highlight-citation t])
1868              ("MIME"
1869               ["Words" gnus-article-decode-mime-words t]
1870               ["Charset" gnus-article-decode-charset t]
1871               ["QP" gnus-article-de-quoted-unreadable t]
1872               ["Base64" gnus-article-de-base64-unreadable t]
1873               ["View all" gnus-mime-view-all-parts t]
1874               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1875               ["Encrypt body" gnus-article-encrypt-body t])
1876              ("Date"
1877               ["Local" gnus-article-date-local t]
1878               ["ISO8601" gnus-article-date-iso8601 t]
1879               ["UT" gnus-article-date-ut t]
1880               ["Original" gnus-article-date-original t]
1881               ["Lapsed" gnus-article-date-lapsed t]
1882               ["User-defined" gnus-article-date-user t])
1883              ("Washing"
1884               ("Remove Blanks"
1885                ["Leading" gnus-article-strip-leading-blank-lines t]
1886                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1887                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1888                ["All of the above" gnus-article-strip-blank-lines t]
1889                ["All" gnus-article-strip-all-blank-lines t]
1890                ["Leading space" gnus-article-strip-leading-space t]
1891                ["Trailing space" gnus-article-strip-trailing-space t]
1892                ["Leading space in headers"
1893                 gnus-article-remove-leading-whitespace t])
1894               ["Overstrike" gnus-article-treat-overstrike t]
1895               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1896               ["Emphasis" gnus-article-emphasize t]
1897               ["Word wrap" gnus-article-fill-cited-article t]
1898               ["Fill long lines" gnus-article-fill-long-lines t]
1899               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1900               ["CR" gnus-article-remove-cr t]
1901               ["Show X-Face" gnus-article-display-x-face t]
1902               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1903               ["Base64" gnus-article-de-base64-unreadable t]
1904               ["Rot 13" gnus-summary-caesar-message
1905                ,@(if (featurep 'xemacs) '(t)
1906                    '(:help "\"Caesar rotate\" article by 13"))]
1907               ["Unix pipe" gnus-summary-pipe-message t]
1908               ["Add buttons" gnus-article-add-buttons t]
1909               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1910               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1911               ["Verbose header" gnus-summary-verbose-headers t]
1912               ["Toggle header" gnus-summary-toggle-header t]
1913               ["Toggle smiley" gnus-summary-toggle-smiley t]
1914               ["Html" gnus-article-wash-html t]
1915               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1916               ["HZ" gnus-article-decode-HZ t])
1917              ("Output"
1918               ["Save in default format" gnus-summary-save-article
1919                ,@(if (featurep 'xemacs) '(t)
1920                    '(:help "Save article using default method"))]
1921               ["Save in file" gnus-summary-save-article-file
1922                ,@(if (featurep 'xemacs) '(t)
1923                    '(:help "Save article in file"))]
1924               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1925               ["Save in MH folder" gnus-summary-save-article-folder t]
1926               ["Save in VM folder" gnus-summary-save-article-vm t]
1927               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1928               ["Save body in file" gnus-summary-save-article-body-file t]
1929               ["Pipe through a filter" gnus-summary-pipe-output t]
1930               ["Add to SOUP packet" gnus-soup-add-article t]
1931               ["Print" gnus-summary-print-article t])
1932              ("Backend"
1933               ["Respool article..." gnus-summary-respool-article t]
1934               ["Move article..." gnus-summary-move-article
1935                (gnus-check-backend-function
1936                 'request-move-article gnus-newsgroup-name)]
1937               ["Copy article..." gnus-summary-copy-article t]
1938               ["Crosspost article..." gnus-summary-crosspost-article
1939                (gnus-check-backend-function
1940                 'request-replace-article gnus-newsgroup-name)]
1941               ["Import file..." gnus-summary-import-article t]
1942               ["Create article..." gnus-summary-create-article t]
1943               ["Check if posted" gnus-summary-article-posted-p t]
1944               ["Edit article" gnus-summary-edit-article
1945                (not (gnus-group-read-only-p))]
1946               ["Delete article" gnus-summary-delete-article
1947                (gnus-check-backend-function
1948                 'request-expire-articles gnus-newsgroup-name)]
1949               ["Query respool" gnus-summary-respool-query t]
1950               ["Trace respool" gnus-summary-respool-trace t]
1951               ["Delete expirable articles" gnus-summary-expire-articles-now
1952                (gnus-check-backend-function
1953                 'request-expire-articles gnus-newsgroup-name)])
1954              ("Extract"
1955               ["Uudecode" gnus-uu-decode-uu
1956                ,@(if (featurep 'xemacs) '(t)
1957                    '(:help "Decode uuencoded article(s)"))]
1958               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1959               ["Unshar" gnus-uu-decode-unshar t]
1960               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1961               ["Save" gnus-uu-decode-save t]
1962               ["Binhex" gnus-uu-decode-binhex t]
1963               ["Postscript" gnus-uu-decode-postscript t])
1964              ("Cache"
1965               ["Enter article" gnus-cache-enter-article t]
1966               ["Remove article" gnus-cache-remove-article t])
1967              ["Translate" gnus-article-babel t]
1968              ["Select article buffer" gnus-summary-select-article-buffer t]
1969              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1970              ["Isearch article..." gnus-summary-isearch-article t]
1971              ["Beginning of the article" gnus-summary-beginning-of-article t]
1972              ["End of the article" gnus-summary-end-of-article t]
1973              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1974              ["Fetch referenced articles" gnus-summary-refer-references t]
1975              ["Fetch current thread" gnus-summary-refer-thread t]
1976              ["Fetch article with id..." gnus-summary-refer-article t]
1977              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1978              ["Redisplay" gnus-summary-show-article t]
1979              ["Raw article" gnus-summary-show-raw-article t])))
1980       (easy-menu-define
1981         gnus-summary-article-menu gnus-summary-mode-map ""
1982         (cons "Article" innards))
1983
1984       (if (not (keymapp gnus-summary-article-menu))
1985           (easy-menu-define
1986             gnus-article-commands-menu gnus-article-mode-map ""
1987             (cons "Commands" innards))
1988         ;; in Emacs, don't share menu.
1989         (setq gnus-article-commands-menu
1990               (copy-keymap gnus-summary-article-menu))
1991         (define-key gnus-article-mode-map [menu-bar commands]
1992           (cons "Commands" gnus-article-commands-menu))))
1993
1994     (easy-menu-define
1995       gnus-summary-thread-menu gnus-summary-mode-map ""
1996       '("Threads"
1997         ["Toggle threading" gnus-summary-toggle-threads t]
1998         ["Hide threads" gnus-summary-hide-all-threads t]
1999         ["Show threads" gnus-summary-show-all-threads t]
2000         ["Hide thread" gnus-summary-hide-thread t]
2001         ["Show thread" gnus-summary-show-thread t]
2002         ["Go to next thread" gnus-summary-next-thread t]
2003         ["Go to previous thread" gnus-summary-prev-thread t]
2004         ["Go down thread" gnus-summary-down-thread t]
2005         ["Go up thread" gnus-summary-up-thread t]
2006         ["Top of thread" gnus-summary-top-thread t]
2007         ["Mark thread as read" gnus-summary-kill-thread t]
2008         ["Lower thread score" gnus-summary-lower-thread t]
2009         ["Raise thread score" gnus-summary-raise-thread t]
2010         ["Rethread current" gnus-summary-rethread-current t]))
2011
2012     (easy-menu-define
2013       gnus-summary-post-menu gnus-summary-mode-map ""
2014       `("Post"
2015         ["Post an article" gnus-summary-post-news
2016          ,@(if (featurep 'xemacs) '(t)
2017              '(:help "Post an article"))]
2018         ["Followup" gnus-summary-followup
2019          ,@(if (featurep 'xemacs) '(t)
2020              '(:help "Post followup to this article"))]
2021         ["Followup and yank" gnus-summary-followup-with-original
2022          ,@(if (featurep 'xemacs) '(t)
2023              '(:help "Post followup to this article, quoting its contents"))]
2024         ["Supersede article" gnus-summary-supersede-article t]
2025         ["Cancel article" gnus-summary-cancel-article
2026          ,@(if (featurep 'xemacs) '(t)
2027              '(:help "Cancel an article you posted"))]
2028         ["Reply" gnus-summary-reply t]
2029         ["Reply and yank" gnus-summary-reply-with-original t]
2030         ["Wide reply" gnus-summary-wide-reply t]
2031         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2032          ,@(if (featurep 'xemacs) '(t)
2033              '(:help "Mail a reply, quoting this article"))]
2034         ["Mail forward" gnus-summary-mail-forward t]
2035         ["Post forward" gnus-summary-post-forward t]
2036         ["Digest and mail" gnus-uu-digest-mail-forward t]
2037         ["Digest and post" gnus-uu-digest-post-forward t]
2038         ["Resend message" gnus-summary-resend-message t]
2039         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2040         ["Send a mail" gnus-summary-mail-other-window t]
2041         ["Uuencode and post" gnus-uu-post-news
2042          ,@(if (featurep 'xemacs) '(t)
2043              '(:help "Post a uuencoded article"))]
2044         ["Followup via news" gnus-summary-followup-to-mail t]
2045         ["Followup via news and yank"
2046          gnus-summary-followup-to-mail-with-original t]
2047         ;;("Draft"
2048         ;;["Send" gnus-summary-send-draft t]
2049         ;;["Send bounced" gnus-resend-bounced-mail t])
2050         ))
2051
2052     (cond
2053      ((not (keymapp gnus-summary-post-menu))
2054       (setq gnus-article-post-menu gnus-summary-post-menu))
2055      ((not gnus-article-post-menu)
2056       ;; Don't share post menu.
2057       (setq gnus-article-post-menu
2058             (copy-keymap gnus-summary-post-menu))))
2059     (define-key gnus-article-mode-map [menu-bar post]
2060       (cons "Post" gnus-article-post-menu))
2061
2062     (easy-menu-define
2063       gnus-summary-misc-menu gnus-summary-mode-map ""
2064       `("Gnus"
2065         ("Mark Read"
2066          ["Mark as read" gnus-summary-mark-as-read-forward t]
2067          ["Mark same subject and select"
2068           gnus-summary-kill-same-subject-and-select t]
2069          ["Mark same subject" gnus-summary-kill-same-subject t]
2070          ["Catchup" gnus-summary-catchup
2071           ,@(if (featurep 'xemacs) '(t)
2072               '(:help "Mark unread articles in this group as read"))]
2073          ["Catchup all" gnus-summary-catchup-all t]
2074          ["Catchup to here" gnus-summary-catchup-to-here t]
2075          ["Catchup from here" gnus-summary-catchup-from-here t]
2076          ["Catchup region" gnus-summary-mark-region-as-read t]
2077          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2078         ("Mark Various"
2079          ["Tick" gnus-summary-tick-article-forward t]
2080          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2081          ["Remove marks" gnus-summary-clear-mark-forward t]
2082          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2083          ["Set bookmark" gnus-summary-set-bookmark t]
2084          ["Remove bookmark" gnus-summary-remove-bookmark t])
2085         ("Mark Limit"
2086          ["Marks..." gnus-summary-limit-to-marks t]
2087          ["Subject..." gnus-summary-limit-to-subject t]
2088          ["Author..." gnus-summary-limit-to-author t]
2089          ["Age..." gnus-summary-limit-to-age t]
2090          ["Extra..." gnus-summary-limit-to-extra t]
2091          ["Score" gnus-summary-limit-to-score t]
2092          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2093          ["Unread" gnus-summary-limit-to-unread t]
2094          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2095          ["Articles" gnus-summary-limit-to-articles t]
2096          ["Pop limit" gnus-summary-pop-limit t]
2097          ["Show dormant" gnus-summary-limit-include-dormant t]
2098          ["Hide childless dormant"
2099           gnus-summary-limit-exclude-childless-dormant t]
2100          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2101          ["Hide marked" gnus-summary-limit-exclude-marks t]
2102          ["Show expunged" gnus-summary-limit-include-expunged t])
2103         ("Process Mark"
2104          ["Set mark" gnus-summary-mark-as-processable t]
2105          ["Remove mark" gnus-summary-unmark-as-processable t]
2106          ["Remove all marks" gnus-summary-unmark-all-processable t]
2107          ["Mark above" gnus-uu-mark-over t]
2108          ["Mark series" gnus-uu-mark-series t]
2109          ["Mark region" gnus-uu-mark-region t]
2110          ["Unmark region" gnus-uu-unmark-region t]
2111          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2112          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2113          ["Mark all" gnus-uu-mark-all t]
2114          ["Mark buffer" gnus-uu-mark-buffer t]
2115          ["Mark sparse" gnus-uu-mark-sparse t]
2116          ["Mark thread" gnus-uu-mark-thread t]
2117          ["Unmark thread" gnus-uu-unmark-thread t]
2118          ("Process Mark Sets"
2119           ["Kill" gnus-summary-kill-process-mark t]
2120           ["Yank" gnus-summary-yank-process-mark
2121            gnus-newsgroup-process-stack]
2122           ["Save" gnus-summary-save-process-mark t]))
2123         ("Scroll article"
2124          ["Page forward" gnus-summary-next-page
2125           ,@(if (featurep 'xemacs) '(t)
2126               '(:help "Show next page of article"))]
2127          ["Page backward" gnus-summary-prev-page
2128           ,@(if (featurep 'xemacs) '(t)
2129               '(:help "Show previous page of article"))]
2130          ["Line forward" gnus-summary-scroll-up t])
2131         ("Move"
2132          ["Next unread article" gnus-summary-next-unread-article t]
2133          ["Previous unread article" gnus-summary-prev-unread-article t]
2134          ["Next article" gnus-summary-next-article t]
2135          ["Previous article" gnus-summary-prev-article t]
2136          ["Next unread subject" gnus-summary-next-unread-subject t]
2137          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2138          ["Next article same subject" gnus-summary-next-same-subject t]
2139          ["Previous article same subject" gnus-summary-prev-same-subject t]
2140          ["First unread article" gnus-summary-first-unread-article t]
2141          ["Best unread article" gnus-summary-best-unread-article t]
2142          ["Go to subject number..." gnus-summary-goto-subject t]
2143          ["Go to article number..." gnus-summary-goto-article t]
2144          ["Go to the last article" gnus-summary-goto-last-article t]
2145          ["Pop article off history" gnus-summary-pop-article t])
2146         ("Sort"
2147          ["Sort by number" gnus-summary-sort-by-number t]
2148          ["Sort by author" gnus-summary-sort-by-author t]
2149          ["Sort by subject" gnus-summary-sort-by-subject t]
2150          ["Sort by date" gnus-summary-sort-by-date t]
2151          ["Sort by score" gnus-summary-sort-by-score t]
2152          ["Sort by lines" gnus-summary-sort-by-lines t]
2153          ["Sort by characters" gnus-summary-sort-by-chars t]
2154          ["Original sort" gnus-summary-sort-by-original t])
2155         ("Help"
2156          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2157          ["Describe group" gnus-summary-describe-group t]
2158          ["Read manual" gnus-info-find-node t])
2159         ("Modes"
2160          ["Pick and read" gnus-pick-mode t]
2161          ["Binary" gnus-binary-mode t])
2162         ("Regeneration"
2163          ["Regenerate" gnus-summary-prepare t]
2164          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2165          ["Toggle threading" gnus-summary-toggle-threads t])
2166         ["See old articles" gnus-summary-insert-old-articles t]
2167         ["See new articles" gnus-summary-insert-new-articles t]
2168         ["Filter articles..." gnus-summary-execute-command t]
2169         ["Run command on subjects..." gnus-summary-universal-argument t]
2170         ["Search articles forward..." gnus-summary-search-article-forward t]
2171         ["Search articles backward..." gnus-summary-search-article-backward t]
2172         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2173         ["Expand window" gnus-summary-expand-window t]
2174         ["Expire expirable articles" gnus-summary-expire-articles
2175          (gnus-check-backend-function
2176           'request-expire-articles gnus-newsgroup-name)]
2177         ["Edit local kill file" gnus-summary-edit-local-kill t]
2178         ["Edit main kill file" gnus-summary-edit-global-kill t]
2179         ["Edit group parameters" gnus-summary-edit-parameters t]
2180         ["Customize group parameters" gnus-summary-customize-parameters t]
2181         ["Send a bug report" gnus-bug t]
2182         ("Exit"
2183          ["Catchup and exit" gnus-summary-catchup-and-exit
2184           ,@(if (featurep 'xemacs) '(t)
2185               '(:help "Mark unread articles in this group as read, then exit"))]
2186          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2187          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2188          ["Exit group" gnus-summary-exit
2189           ,@(if (featurep 'xemacs) '(t)
2190               '(:help "Exit current group, return to group selection mode"))]
2191          ["Exit group without updating" gnus-summary-exit-no-update t]
2192          ["Exit and goto next group" gnus-summary-next-group t]
2193          ["Exit and goto prev group" gnus-summary-prev-group t]
2194          ["Reselect group" gnus-summary-reselect-current-group t]
2195          ["Rescan group" gnus-summary-rescan-group t]
2196          ["Update dribble" gnus-summary-save-newsrc t])))
2197
2198     (gnus-run-hooks 'gnus-summary-menu-hook)))
2199
2200 (defvar gnus-summary-tool-bar-map nil)
2201
2202 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2203 (defun gnus-summary-make-tool-bar ()
2204   (if (and (fboundp 'tool-bar-add-item-from-menu)
2205            (default-value 'tool-bar-mode)
2206            (not gnus-summary-tool-bar-map))
2207       (setq gnus-summary-tool-bar-map
2208             (let ((tool-bar-map (make-sparse-keymap))
2209                   (load-path (mm-image-load-path)))
2210               (tool-bar-add-item-from-menu
2211                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2212               (tool-bar-add-item-from-menu
2213                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-summary-post-news "post" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-summary-followup "followup" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-reply "reply" gnus-summary-mode-map)
2224               (tool-bar-add-item-from-menu
2225                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2226               (tool-bar-add-item-from-menu
2227                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2228               (tool-bar-add-item-from-menu
2229                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2230               (tool-bar-add-item-from-menu
2231                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2232               (tool-bar-add-item-from-menu
2233                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2234               (tool-bar-add-item-from-menu
2235                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2236               (tool-bar-add-item-from-menu
2237                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2238               (tool-bar-add-item-from-menu
2239                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2240               tool-bar-map)))
2241   (if gnus-summary-tool-bar-map
2242       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2243
2244 (defun gnus-score-set-default (var value)
2245   "A version of set that updates the GNU Emacs menu-bar."
2246   (set var value)
2247   ;; It is the message that forces the active status to be updated.
2248   (message ""))
2249
2250 (defun gnus-make-score-map (type)
2251   "Make a summary score map of type TYPE."
2252   (if t
2253       nil
2254     (let ((headers '(("author" "from" string)
2255                      ("subject" "subject" string)
2256                      ("article body" "body" string)
2257                      ("article head" "head" string)
2258                      ("xref" "xref" string)
2259                      ("extra header" "extra" string)
2260                      ("lines" "lines" number)
2261                      ("followups to author" "followup" string)))
2262           (types '((number ("less than" <)
2263                            ("greater than" >)
2264                            ("equal" =))
2265                    (string ("substring" s)
2266                            ("exact string" e)
2267                            ("fuzzy string" f)
2268                            ("regexp" r))))
2269           (perms '(("temporary" (current-time-string))
2270                    ("permanent" nil)
2271                    ("immediate" now)))
2272           header)
2273       (list
2274        (apply
2275         'nconc
2276         (list
2277          (if (eq type 'lower)
2278              "Lower score"
2279            "Increase score"))
2280         (let (outh)
2281           (while headers
2282             (setq header (car headers))
2283             (setq outh
2284                   (cons
2285                    (apply
2286                     'nconc
2287                     (list (car header))
2288                     (let ((ts (cdr (assoc (nth 2 header) types)))
2289                           outt)
2290                       (while ts
2291                         (setq outt
2292                               (cons
2293                                (apply
2294                                 'nconc
2295                                 (list (caar ts))
2296                                 (let ((ps perms)
2297                                       outp)
2298                                   (while ps
2299                                     (setq outp
2300                                           (cons
2301                                            (vector
2302                                             (caar ps)
2303                                             (list
2304                                              'gnus-summary-score-entry
2305                                              (nth 1 header)
2306                                              (if (or (string= (nth 1 header)
2307                                                               "head")
2308                                                      (string= (nth 1 header)
2309                                                               "body"))
2310                                                  ""
2311                                                (list 'gnus-summary-header
2312                                                      (nth 1 header)))
2313                                              (list 'quote (nth 1 (car ts)))
2314                                              (list 'gnus-score-delta-default
2315                                                    nil)
2316                                              (nth 1 (car ps))
2317                                              t)
2318                                             t)
2319                                            outp))
2320                                     (setq ps (cdr ps)))
2321                                   (list (nreverse outp))))
2322                                outt))
2323                         (setq ts (cdr ts)))
2324                       (list (nreverse outt))))
2325                    outh))
2326             (setq headers (cdr headers)))
2327           (list (nreverse outh))))))))
2328
2329 \f
2330
2331 (defun gnus-summary-mode (&optional group)
2332   "Major mode for reading articles.
2333
2334 All normal editing commands are switched off.
2335 \\<gnus-summary-mode-map>
2336 Each line in this buffer represents one article.  To read an
2337 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2338 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2339 respectively.
2340
2341 You can also post articles and send mail from this buffer.  To
2342 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2343 of an article, type `\\[gnus-summary-reply]'.
2344
2345 There are approx. one gazillion commands you can execute in this
2346 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2347
2348 The following commands are available:
2349
2350 \\{gnus-summary-mode-map}"
2351   (interactive)
2352   (kill-all-local-variables)
2353   (when (gnus-visual-p 'summary-menu 'menu)
2354     (gnus-summary-make-menu-bar)
2355     (gnus-summary-make-tool-bar))
2356   (gnus-summary-make-local-variables)
2357   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2358     (gnus-summary-make-local-variables))
2359   (gnus-make-thread-indent-array)
2360   (gnus-simplify-mode-line)
2361   (setq major-mode 'gnus-summary-mode)
2362   (setq mode-name "Summary")
2363   (make-local-variable 'minor-mode-alist)
2364   (use-local-map gnus-summary-mode-map)
2365   (buffer-disable-undo)
2366   (setq buffer-read-only t)             ;Disable modification
2367   (setq truncate-lines t)
2368   (setq selective-display t)
2369   (setq selective-display-ellipses t)   ;Display `...'
2370   (gnus-summary-set-display-table)
2371   (gnus-set-default-directory)
2372   (setq gnus-newsgroup-name group)
2373   (make-local-variable 'gnus-summary-line-format)
2374   (make-local-variable 'gnus-summary-line-format-spec)
2375   (make-local-variable 'gnus-summary-dummy-line-format)
2376   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2377   (make-local-variable 'gnus-summary-mark-positions)
2378   (make-local-hook 'pre-command-hook)
2379   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2380   (gnus-run-hooks 'gnus-summary-mode-hook)
2381   (turn-on-gnus-mailing-list-mode)
2382   (mm-enable-multibyte-mule4)
2383   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2384   (gnus-update-summary-mark-positions))
2385
2386 (defun gnus-summary-make-local-variables ()
2387   "Make all the local summary buffer variables."
2388   (let (global)
2389     (dolist (local gnus-summary-local-variables)
2390       (if (consp local)
2391           (progn
2392             (if (eq (cdr local) 'global)
2393                 ;; Copy the global value of the variable.
2394                 (setq global (symbol-value (car local)))
2395               ;; Use the value from the list.
2396               (setq global (eval (cdr local))))
2397             (set (make-local-variable (car local)) global))
2398         ;; Simple nil-valued local variable.
2399         (set (make-local-variable local) nil)))))
2400
2401 (defun gnus-summary-clear-local-variables ()
2402   (let ((locals gnus-summary-local-variables))
2403     (while locals
2404       (if (consp (car locals))
2405           (and (vectorp (caar locals))
2406                (set (caar locals) nil))
2407         (and (vectorp (car locals))
2408              (set (car locals) nil)))
2409       (setq locals (cdr locals)))))
2410
2411 ;; Summary data functions.
2412
2413 (defmacro gnus-data-number (data)
2414   `(car ,data))
2415
2416 (defmacro gnus-data-set-number (data number)
2417   `(setcar ,data ,number))
2418
2419 (defmacro gnus-data-mark (data)
2420   `(nth 1 ,data))
2421
2422 (defmacro gnus-data-set-mark (data mark)
2423   `(setcar (nthcdr 1 ,data) ,mark))
2424
2425 (defmacro gnus-data-pos (data)
2426   `(nth 2 ,data))
2427
2428 (defmacro gnus-data-set-pos (data pos)
2429   `(setcar (nthcdr 2 ,data) ,pos))
2430
2431 (defmacro gnus-data-header (data)
2432   `(nth 3 ,data))
2433
2434 (defmacro gnus-data-set-header (data header)
2435   `(setf (nth 3 ,data) ,header))
2436
2437 (defmacro gnus-data-level (data)
2438   `(nth 4 ,data))
2439
2440 (defmacro gnus-data-unread-p (data)
2441   `(= (nth 1 ,data) gnus-unread-mark))
2442
2443 (defmacro gnus-data-read-p (data)
2444   `(/= (nth 1 ,data) gnus-unread-mark))
2445
2446 (defmacro gnus-data-pseudo-p (data)
2447   `(consp (nth 3 ,data)))
2448
2449 (defmacro gnus-data-find (number)
2450   `(assq ,number gnus-newsgroup-data))
2451
2452 (defmacro gnus-data-find-list (number &optional data)
2453   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2454      (memq (assq ,number bdata)
2455            bdata)))
2456
2457 (defmacro gnus-data-make (number mark pos header level)
2458   `(list ,number ,mark ,pos ,header ,level))
2459
2460 (defun gnus-data-enter (after-article number mark pos header level offset)
2461   (let ((data (gnus-data-find-list after-article)))
2462     (unless data
2463       (error "No such article: %d" after-article))
2464     (setcdr data (cons (gnus-data-make number mark pos header level)
2465                        (cdr data)))
2466     (setq gnus-newsgroup-data-reverse nil)
2467     (gnus-data-update-list (cddr data) offset)))
2468
2469 (defun gnus-data-enter-list (after-article list &optional offset)
2470   (when list
2471     (let ((data (and after-article (gnus-data-find-list after-article)))
2472           (ilist list))
2473       (if (not (or data
2474                    after-article))
2475           (let ((odata gnus-newsgroup-data))
2476             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2477             (when offset
2478               (gnus-data-update-list odata offset)))
2479       ;; Find the last element in the list to be spliced into the main
2480         ;; list.
2481         (while (cdr list)
2482           (setq list (cdr list)))
2483         (if (not data)
2484             (progn
2485               (setcdr list gnus-newsgroup-data)
2486               (setq gnus-newsgroup-data ilist)
2487               (when offset
2488                 (gnus-data-update-list (cdr list) offset)))
2489           (setcdr list (cdr data))
2490           (setcdr data ilist)
2491           (when offset
2492             (gnus-data-update-list (cdr list) offset))))
2493       (setq gnus-newsgroup-data-reverse nil))))
2494
2495 (defun gnus-data-remove (article &optional offset)
2496   (let ((data gnus-newsgroup-data))
2497     (if (= (gnus-data-number (car data)) article)
2498         (progn
2499           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2500                 gnus-newsgroup-data-reverse nil)
2501           (when offset
2502             (gnus-data-update-list gnus-newsgroup-data offset)))
2503       (while (cdr data)
2504         (when (= (gnus-data-number (cadr data)) article)
2505           (setcdr data (cddr data))
2506           (when offset
2507             (gnus-data-update-list (cdr data) offset))
2508           (setq data nil
2509                 gnus-newsgroup-data-reverse nil))
2510         (setq data (cdr data))))))
2511
2512 (defmacro gnus-data-list (backward)
2513   `(if ,backward
2514        (or gnus-newsgroup-data-reverse
2515            (setq gnus-newsgroup-data-reverse
2516                  (reverse gnus-newsgroup-data)))
2517      gnus-newsgroup-data))
2518
2519 (defun gnus-data-update-list (data offset)
2520   "Add OFFSET to the POS of all data entries in DATA."
2521   (setq gnus-newsgroup-data-reverse nil)
2522   (while data
2523     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2524     (setq data (cdr data))))
2525
2526 (defun gnus-summary-article-pseudo-p (article)
2527   "Say whether this article is a pseudo article or not."
2528   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2529
2530 (defmacro gnus-summary-article-sparse-p (article)
2531   "Say whether this article is a sparse article or not."
2532   `(memq ,article gnus-newsgroup-sparse))
2533
2534 (defmacro gnus-summary-article-ancient-p (article)
2535   "Say whether this article is a sparse article or not."
2536   `(memq ,article gnus-newsgroup-ancient))
2537
2538 (defun gnus-article-parent-p (number)
2539   "Say whether this article is a parent or not."
2540   (let ((data (gnus-data-find-list number)))
2541     (and (cdr data)              ; There has to be an article after...
2542          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2543             (gnus-data-level (nth 1 data))))))
2544
2545 (defun gnus-article-children (number)
2546   "Return a list of all children to NUMBER."
2547   (let* ((data (gnus-data-find-list number))
2548          (level (gnus-data-level (car data)))
2549          children)
2550     (setq data (cdr data))
2551     (while (and data
2552                 (= (gnus-data-level (car data)) (1+ level)))
2553       (push (gnus-data-number (car data)) children)
2554       (setq data (cdr data)))
2555     children))
2556
2557 (defmacro gnus-summary-skip-intangible ()
2558   "If the current article is intangible, then jump to a different article."
2559   '(let ((to (get-text-property (point) 'gnus-intangible)))
2560      (and to (gnus-summary-goto-subject to))))
2561
2562 (defmacro gnus-summary-article-intangible-p ()
2563   "Say whether this article is intangible or not."
2564   '(get-text-property (point) 'gnus-intangible))
2565
2566 (defun gnus-article-read-p (article)
2567   "Say whether ARTICLE is read or not."
2568   (not (or (memq article gnus-newsgroup-marked)
2569            (memq article gnus-newsgroup-unreads)
2570            (memq article gnus-newsgroup-unselected)
2571            (memq article gnus-newsgroup-dormant))))
2572
2573 ;; Some summary mode macros.
2574
2575 (defmacro gnus-summary-article-number ()
2576   "The article number of the article on the current line.
2577 If there isn's an article number here, then we return the current
2578 article number."
2579   '(progn
2580      (gnus-summary-skip-intangible)
2581      (or (get-text-property (point) 'gnus-number)
2582          (gnus-summary-last-subject))))
2583
2584 (defmacro gnus-summary-article-header (&optional number)
2585   "Return the header of article NUMBER."
2586   `(gnus-data-header (gnus-data-find
2587                       ,(or number '(gnus-summary-article-number)))))
2588
2589 (defmacro gnus-summary-thread-level (&optional number)
2590   "Return the level of thread that starts with article NUMBER."
2591   `(if (and (eq gnus-summary-make-false-root 'dummy)
2592             (get-text-property (point) 'gnus-intangible))
2593        0
2594      (gnus-data-level (gnus-data-find
2595                        ,(or number '(gnus-summary-article-number))))))
2596
2597 (defmacro gnus-summary-article-mark (&optional number)
2598   "Return the mark of article NUMBER."
2599   `(gnus-data-mark (gnus-data-find
2600                     ,(or number '(gnus-summary-article-number)))))
2601
2602 (defmacro gnus-summary-article-pos (&optional number)
2603   "Return the position of the line of article NUMBER."
2604   `(gnus-data-pos (gnus-data-find
2605                    ,(or number '(gnus-summary-article-number)))))
2606
2607 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2608 (defmacro gnus-summary-article-subject (&optional number)
2609   "Return current subject string or nil if nothing."
2610   `(let ((headers
2611           ,(if number
2612                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2613              '(gnus-data-header (assq (gnus-summary-article-number)
2614                                       gnus-newsgroup-data)))))
2615      (and headers
2616           (vectorp headers)
2617           (mail-header-subject headers))))
2618
2619 (defmacro gnus-summary-article-score (&optional number)
2620   "Return current article score."
2621   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2622                   gnus-newsgroup-scored))
2623        gnus-summary-default-score 0))
2624
2625 (defun gnus-summary-article-children (&optional number)
2626   "Return a list of article numbers that are children of article NUMBER."
2627   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2628          (level (gnus-data-level (car data)))
2629          l children)
2630     (while (and (setq data (cdr data))
2631                 (> (setq l (gnus-data-level (car data))) level))
2632       (and (= (1+ level) l)
2633            (push (gnus-data-number (car data))
2634                  children)))
2635     (nreverse children)))
2636
2637 (defun gnus-summary-article-parent (&optional number)
2638   "Return the article number of the parent of article NUMBER."
2639   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2640                                     (gnus-data-list t)))
2641          (level (gnus-data-level (car data))))
2642     (if (zerop level)
2643         ()                              ; This is a root.
2644       ;; We search until we find an article with a level less than
2645       ;; this one.  That function has to be the parent.
2646       (while (and (setq data (cdr data))
2647                   (not (< (gnus-data-level (car data)) level))))
2648       (and data (gnus-data-number (car data))))))
2649
2650 (defun gnus-unread-mark-p (mark)
2651   "Say whether MARK is the unread mark."
2652   (= mark gnus-unread-mark))
2653
2654 (defun gnus-read-mark-p (mark)
2655   "Say whether MARK is one of the marks that mark as read.
2656 This is all marks except unread, ticked, dormant, and expirable."
2657   (not (or (= mark gnus-unread-mark)
2658            (= mark gnus-ticked-mark)
2659            (= mark gnus-dormant-mark)
2660            (= mark gnus-expirable-mark))))
2661
2662 (defmacro gnus-article-mark (number)
2663   "Return the MARK of article NUMBER.
2664 This macro should only be used when computing the mark the \"first\"
2665 time; i.e., when generating the summary lines.  After that,
2666 `gnus-summary-article-mark' should be used to examine the
2667 marks of articles."
2668   `(cond
2669     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2670     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2671     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2672     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2673     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2674     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2675     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2676     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2677            gnus-ancient-mark))))
2678
2679 ;; Saving hidden threads.
2680
2681 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2682 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2683
2684 (defmacro gnus-save-hidden-threads (&rest forms)
2685   "Save hidden threads, eval FORMS, and restore the hidden threads."
2686   (let ((config (make-symbol "config")))
2687     `(let ((,config (gnus-hidden-threads-configuration)))
2688        (unwind-protect
2689            (save-excursion
2690              ,@forms)
2691          (gnus-restore-hidden-threads-configuration ,config)))))
2692
2693 (defun gnus-data-compute-positions ()
2694   "Compute the positions of all articles."
2695   (setq gnus-newsgroup-data-reverse nil)
2696   (let ((data gnus-newsgroup-data))
2697     (save-excursion
2698       (gnus-save-hidden-threads
2699         (gnus-summary-show-all-threads)
2700         (goto-char (point-min))
2701         (while data
2702           (while (get-text-property (point) 'gnus-intangible)
2703             (forward-line 1))
2704           (gnus-data-set-pos (car data) (+ (point) 3))
2705           (setq data (cdr data))
2706           (forward-line 1))))))
2707
2708 (defun gnus-hidden-threads-configuration ()
2709   "Return the current hidden threads configuration."
2710   (save-excursion
2711     (let (config)
2712       (goto-char (point-min))
2713       (while (search-forward "\r" nil t)
2714         (push (1- (point)) config))
2715       config)))
2716
2717 (defun gnus-restore-hidden-threads-configuration (config)
2718   "Restore hidden threads configuration from CONFIG."
2719   (save-excursion
2720     (let (point buffer-read-only)
2721       (while (setq point (pop config))
2722         (when (and (< point (point-max))
2723                    (goto-char point)
2724                    (eq (char-after) ?\n))
2725           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2726
2727 ;; Various summary mode internalish functions.
2728
2729 (defun gnus-mouse-pick-article (e)
2730   (interactive "e")
2731   (mouse-set-point e)
2732   (gnus-summary-next-page nil t))
2733
2734 (defun gnus-summary-set-display-table ()
2735   "Change the display table.
2736 Odd characters have a tendency to mess
2737 up nicely formatted displays - we make all possible glyphs
2738 display only a single character."
2739
2740   ;; We start from the standard display table, if any.
2741   (let ((table (or (copy-sequence standard-display-table)
2742                    (make-display-table)))
2743         (i 32))
2744     ;; Nix out all the control chars...
2745     (while (>= (setq i (1- i)) 0)
2746       (aset table i [??]))
2747    ;; ... but not newline and cr, of course.  (cr is necessary for the
2748     ;; selective display).
2749     (aset table ?\n nil)
2750     (aset table ?\r nil)
2751     ;; We keep TAB as well.
2752     (aset table ?\t nil)
2753     ;; We nix out any glyphs over 126 that are not set already.
2754     (let ((i 256))
2755       (while (>= (setq i (1- i)) 127)
2756         ;; Only modify if the entry is nil.
2757         (unless (aref table i)
2758           (aset table i [??]))))
2759     (setq buffer-display-table table)))
2760
2761 (defun gnus-summary-set-article-display-arrow (pos)
2762   "Update the overlay arrow to point to line at position POS."
2763   (when (and gnus-summary-display-arrow
2764              (boundp 'overlay-arrow-position)
2765              (boundp 'overlay-arrow-string))
2766     (save-excursion
2767       (goto-char pos)
2768       (beginning-of-line)
2769       (unless overlay-arrow-position
2770         (setq overlay-arrow-position (make-marker)))
2771       (setq overlay-arrow-string "=>"
2772             overlay-arrow-position (set-marker overlay-arrow-position
2773                                                (point)
2774                                                (current-buffer))))))
2775
2776 (defun gnus-summary-buffer-name (group)
2777   "Return the summary buffer name of GROUP."
2778   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2779
2780 (defun gnus-summary-setup-buffer (group)
2781   "Initialize summary buffer."
2782   (let ((buffer (gnus-summary-buffer-name group)))
2783     (if (get-buffer buffer)
2784         (progn
2785           (set-buffer buffer)
2786           (setq gnus-summary-buffer (current-buffer))
2787           (not gnus-newsgroup-prepared))
2788       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2789       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2790       (gnus-summary-mode group)
2791       (when gnus-carpal
2792         (gnus-carpal-setup-buffer 'summary))
2793       (unless gnus-single-article-buffer
2794         (make-local-variable 'gnus-article-buffer)
2795         (make-local-variable 'gnus-article-current)
2796         (make-local-variable 'gnus-original-article-buffer))
2797       (setq gnus-newsgroup-name group)
2798       ;; Set any local variables in the group parameters.
2799       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2800       t)))
2801
2802 (defun gnus-set-global-variables ()
2803   "Set the global equivalents of the buffer-local variables.
2804 They are set to the latest values they had.  These reflect the summary
2805 buffer that was in action when the last article was fetched."
2806   (when (eq major-mode 'gnus-summary-mode)
2807     (setq gnus-summary-buffer (current-buffer))
2808     (let ((name gnus-newsgroup-name)
2809           (marked gnus-newsgroup-marked)
2810           (unread gnus-newsgroup-unreads)
2811           (headers gnus-current-headers)
2812           (data gnus-newsgroup-data)
2813           (summary gnus-summary-buffer)
2814           (article-buffer gnus-article-buffer)
2815           (original gnus-original-article-buffer)
2816           (gac gnus-article-current)
2817           (reffed gnus-reffed-article-number)
2818           (score-file gnus-current-score-file)
2819           (default-charset gnus-newsgroup-charset)
2820           vlist)
2821       (let ((locals gnus-newsgroup-variables))
2822         (while locals
2823           (if (consp (car locals))
2824               (push (eval (caar locals)) vlist)
2825             (push (eval (car locals)) vlist))
2826           (setq locals (cdr locals)))
2827         (setq vlist (nreverse vlist)))
2828       (save-excursion
2829         (set-buffer gnus-group-buffer)
2830         (setq gnus-newsgroup-name name
2831               gnus-newsgroup-marked marked
2832               gnus-newsgroup-unreads unread
2833               gnus-current-headers headers
2834               gnus-newsgroup-data data
2835               gnus-article-current gac
2836               gnus-summary-buffer summary
2837               gnus-article-buffer article-buffer
2838               gnus-original-article-buffer original
2839               gnus-reffed-article-number reffed
2840               gnus-current-score-file score-file
2841               gnus-newsgroup-charset default-charset)
2842         (let ((locals gnus-newsgroup-variables))
2843           (while locals
2844             (if (consp (car locals))
2845                 (set (caar locals) (pop vlist))
2846               (set (car locals) (pop vlist)))
2847             (setq locals (cdr locals))))
2848         ;; The article buffer also has local variables.
2849         (when (gnus-buffer-live-p gnus-article-buffer)
2850           (set-buffer gnus-article-buffer)
2851           (setq gnus-summary-buffer summary))))))
2852
2853 (defun gnus-summary-article-unread-p (article)
2854   "Say whether ARTICLE is unread or not."
2855   (memq article gnus-newsgroup-unreads))
2856
2857 (defun gnus-summary-first-article-p (&optional article)
2858   "Return whether ARTICLE is the first article in the buffer."
2859   (if (not (setq article (or article (gnus-summary-article-number))))
2860       nil
2861     (eq article (caar gnus-newsgroup-data))))
2862
2863 (defun gnus-summary-last-article-p (&optional article)
2864   "Return whether ARTICLE is the last article in the buffer."
2865   (if (not (setq article (or article (gnus-summary-article-number))))
2866       ;; All non-existent numbers are the last article.  :-)
2867       t
2868     (not (cdr (gnus-data-find-list article)))))
2869
2870 (defun gnus-make-thread-indent-array ()
2871   (let ((n 200))
2872     (unless (and gnus-thread-indent-array
2873                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2874       (setq gnus-thread-indent-array (make-vector 201 "")
2875             gnus-thread-indent-array-level gnus-thread-indent-level)
2876       (while (>= n 0)
2877         (aset gnus-thread-indent-array n
2878               (make-string (* n gnus-thread-indent-level) ? ))
2879         (setq n (1- n))))))
2880
2881 (defun gnus-update-summary-mark-positions ()
2882   "Compute where the summary marks are to go."
2883   (save-excursion
2884     (when (gnus-buffer-exists-p gnus-summary-buffer)
2885       (set-buffer gnus-summary-buffer))
2886     (let ((gnus-replied-mark 129)
2887           (gnus-score-below-mark 130)
2888           (gnus-score-over-mark 130)
2889           (gnus-download-mark 131)
2890           (spec gnus-summary-line-format-spec)
2891           gnus-visual pos)
2892       (save-excursion
2893         (gnus-set-work-buffer)
2894         (let ((gnus-summary-line-format-spec spec)
2895               (gnus-newsgroup-downloadable '((0 . t))))
2896           (gnus-summary-insert-line
2897            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2898            0 nil 128 t nil "" nil 1)
2899           (goto-char (point-min))
2900           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2901                                              (- (point) 2)))))
2902           (goto-char (point-min))
2903           (push (cons 'replied (and (search-forward "\201" nil t)
2904                                     (- (point) 2)))
2905                 pos)
2906           (goto-char (point-min))
2907           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2908                 pos)
2909           (goto-char (point-min))
2910           (push (cons 'download
2911                       (and (search-forward "\203" nil t) (- (point) 2)))
2912                 pos)))
2913       (setq gnus-summary-mark-positions pos))))
2914
2915 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2916   "Insert a dummy root in the summary buffer."
2917   (beginning-of-line)
2918   (gnus-add-text-properties
2919    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2920    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2921
2922 (defun gnus-summary-extract-address-component (from)
2923   (or (car (funcall gnus-extract-address-components from))
2924       from))
2925
2926 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2927   (let ((mail-parse-charset gnus-newsgroup-charset)
2928         ; Is it really necessary to do this next part for each summary line?
2929         ; Luckily, doesn't seem to slow things down much.
2930         (mail-parse-ignored-charsets
2931          (save-excursion (set-buffer gnus-summary-buffer)
2932                          gnus-newsgroup-ignored-charsets)))
2933     (or
2934      (and gnus-ignored-from-addresses
2935           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2936           (let ((extra-headers (mail-header-extra header))
2937                 to
2938                 newsgroups)
2939             (cond
2940              ((setq to (cdr (assq 'To extra-headers)))
2941               (concat "-> "
2942                       (gnus-summary-extract-address-component
2943                        (funcall gnus-decode-encoded-word-function to))))
2944              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2945               (concat "=> " newsgroups)))))
2946      (gnus-summary-extract-address-component gnus-tmp-from))))
2947
2948 (defun gnus-summary-insert-line (gnus-tmp-header
2949                                  gnus-tmp-level gnus-tmp-current
2950                                  gnus-tmp-unread gnus-tmp-replied
2951                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2952                                  &optional gnus-tmp-dummy gnus-tmp-score
2953                                  gnus-tmp-process)
2954   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2955          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2956          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2957          (gnus-tmp-score-char
2958           (if (or (null gnus-summary-default-score)
2959                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2960                       gnus-summary-zcore-fuzz))
2961               ?                         ;Whitespace
2962             (if (< gnus-tmp-score gnus-summary-default-score)
2963                 gnus-score-below-mark gnus-score-over-mark)))
2964          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2965          (gnus-tmp-replied
2966           (cond (gnus-tmp-process gnus-process-mark)
2967                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2968                  gnus-cached-mark)
2969                 (gnus-tmp-replied gnus-replied-mark)
2970                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2971                  gnus-forwarded-mark)
2972                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2973                  gnus-saved-mark)
2974                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2975                  gnus-recent-mark)
2976                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2977                  gnus-unseen-mark)
2978                 (t gnus-no-mark)))
2979          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2980          (gnus-tmp-name
2981           (cond
2982            ((string-match "<[^>]+> *$" gnus-tmp-from)
2983             (let ((beg (match-beginning 0)))
2984               (or (and (string-match "^\".+\"" gnus-tmp-from)
2985                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2986                   (substring gnus-tmp-from 0 beg))))
2987            ((string-match "(.+)" gnus-tmp-from)
2988             (substring gnus-tmp-from
2989                        (1+ (match-beginning 0)) (1- (match-end 0))))
2990            (t gnus-tmp-from)))
2991          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2992          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2993          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2994          (buffer-read-only nil))
2995     (when (string= gnus-tmp-name "")
2996       (setq gnus-tmp-name gnus-tmp-from))
2997     (unless (numberp gnus-tmp-lines)
2998       (setq gnus-tmp-lines -1))
2999     (if (= gnus-tmp-lines -1)
3000         (setq gnus-tmp-lines "?")
3001       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3002     (gnus-put-text-property
3003      (point)
3004      (progn (eval gnus-summary-line-format-spec) (point))
3005      'gnus-number gnus-tmp-number)
3006     (when (gnus-visual-p 'summary-highlight 'highlight)
3007       (forward-line -1)
3008       (gnus-run-hooks 'gnus-summary-update-hook)
3009       (forward-line 1))))
3010
3011 (defun gnus-summary-update-line (&optional dont-update)
3012   "Update summary line after change."
3013   (when (and gnus-summary-default-score
3014              (not gnus-summary-inhibit-highlight))
3015     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3016            (article (gnus-summary-article-number))
3017            (score (gnus-summary-article-score article)))
3018       (unless dont-update
3019         (if (and gnus-summary-mark-below
3020                  (< (gnus-summary-article-score)
3021                     gnus-summary-mark-below))
3022             ;; This article has a low score, so we mark it as read.
3023             (when (memq article gnus-newsgroup-unreads)
3024               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3025           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3026             ;; This article was previously marked as read on account
3027             ;; of a low score, but now it has risen, so we mark it as
3028             ;; unread.
3029             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3030         (gnus-summary-update-mark
3031          (if (or (null gnus-summary-default-score)
3032                  (<= (abs (- score gnus-summary-default-score))
3033                      gnus-summary-zcore-fuzz))
3034              ?                          ;Whitespace
3035            (if (< score gnus-summary-default-score)
3036                gnus-score-below-mark gnus-score-over-mark))
3037          'score))
3038       ;; Do visual highlighting.
3039       (when (gnus-visual-p 'summary-highlight 'highlight)
3040         (gnus-run-hooks 'gnus-summary-update-hook)))))
3041
3042 (defvar gnus-tmp-new-adopts nil)
3043
3044 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3045   "Return the number of articles in THREAD.
3046 This may be 0 in some cases -- if none of the articles in
3047 the thread are to be displayed."
3048   (let* ((number
3049          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3050           (cond
3051            ((not (listp thread))
3052             1)
3053            ((and (consp thread) (cdr thread))
3054             (apply
3055              '+ 1 (mapcar
3056                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3057            ((null thread)
3058             1)
3059            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3060             1)
3061            (t 0))))
3062     (when (and level (zerop level) gnus-tmp-new-adopts)
3063       (incf number
3064             (apply '+ (mapcar
3065                        'gnus-summary-number-of-articles-in-thread
3066                        gnus-tmp-new-adopts))))
3067     (if char
3068         (if (> number 1) gnus-not-empty-thread-mark
3069           gnus-empty-thread-mark)
3070       number)))
3071
3072 (defun gnus-summary-set-local-parameters (group)
3073   "Go through the local params of GROUP and set all variable specs in that list."
3074   (let ((params (gnus-group-find-parameter group))
3075         (vars '(quit-config))           ; Ignore quit-config.
3076         elem)
3077     (while params
3078       (setq elem (car params)
3079             params (cdr params))
3080       (and (consp elem)                 ; Has to be a cons.
3081            (consp (cdr elem))           ; The cdr has to be a list.
3082            (symbolp (car elem))         ; Has to be a symbol in there.
3083            (not (memq (car elem) vars))
3084            (ignore-errors               ; So we set it.
3085              (push (car elem) vars)
3086              (make-local-variable (car elem))
3087              (set (car elem) (eval (nth 1 elem))))))))
3088
3089 (defun gnus-summary-read-group (group &optional show-all no-article
3090                                       kill-buffer no-display backward
3091                                       select-articles)
3092   "Start reading news in newsgroup GROUP.
3093 If SHOW-ALL is non-nil, already read articles are also listed.
3094 If NO-ARTICLE is non-nil, no article is selected initially.
3095 If NO-DISPLAY, don't generate a summary buffer."
3096   (let (result)
3097     (while (and group
3098                 (null (setq result
3099                             (let ((gnus-auto-select-next nil))
3100                               (or (gnus-summary-read-group-1
3101                                    group show-all no-article
3102                                    kill-buffer no-display
3103                                    select-articles)
3104                                   (setq show-all nil
3105                                         select-articles nil)))))
3106                 (eq gnus-auto-select-next 'quietly))
3107       (set-buffer gnus-group-buffer)
3108       ;; The entry function called above goes to the next
3109       ;; group automatically, so we go two groups back
3110       ;; if we are searching for the previous group.
3111       (when backward
3112         (gnus-group-prev-unread-group 2))
3113       (if (not (equal group (gnus-group-group-name)))
3114           (setq group (gnus-group-group-name))
3115         (setq group nil)))
3116     result))
3117
3118 (defun gnus-summary-read-group-1 (group show-all no-article
3119                                         kill-buffer no-display
3120                                         &optional select-articles)
3121   ;; Killed foreign groups can't be entered.
3122   ;;  (when (and (not (gnus-group-native-p group))
3123   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3124   ;;    (error "Dead non-native groups can't be entered"))
3125   (gnus-message 5 "Retrieving newsgroup: %s..."
3126                 (gnus-group-decoded-name group))
3127   (let* ((new-group (gnus-summary-setup-buffer group))
3128          (quit-config (gnus-group-quit-config group))
3129          (did-select (and new-group (gnus-select-newsgroup
3130                                      group show-all select-articles))))
3131     (cond
3132      ;; This summary buffer exists already, so we just select it.
3133      ((not new-group)
3134       (gnus-set-global-variables)
3135       (when kill-buffer
3136         (gnus-kill-or-deaden-summary kill-buffer))
3137       (gnus-configure-windows 'summary 'force)
3138       (gnus-set-mode-line 'summary)
3139       (gnus-summary-position-point)
3140       (message "")
3141       t)
3142      ;; We couldn't select this group.
3143      ((null did-select)
3144       (when (and (eq major-mode 'gnus-summary-mode)
3145                  (not (equal (current-buffer) kill-buffer)))
3146         (kill-buffer (current-buffer))
3147         (if (not quit-config)
3148             (progn
3149               ;; Update the info -- marks might need to be removed,
3150               ;; for instance.
3151               (gnus-summary-update-info)
3152               (set-buffer gnus-group-buffer)
3153               (gnus-group-jump-to-group group)
3154               (gnus-group-next-unread-group 1))
3155           (gnus-handle-ephemeral-exit quit-config)))
3156       (let ((grpinfo (gnus-get-info group)))
3157         (if (null (gnus-info-read grpinfo))
3158             (gnus-message 3 "Group %s contains no messages"
3159                           (gnus-group-decoded-name group))
3160           (gnus-message 3 "Can't select group")))
3161       nil)
3162      ;; The user did a `C-g' while prompting for number of articles,
3163      ;; so we exit this group.
3164      ((eq did-select 'quit)
3165       (and (eq major-mode 'gnus-summary-mode)
3166            (not (equal (current-buffer) kill-buffer))
3167            (kill-buffer (current-buffer)))
3168       (when kill-buffer
3169         (gnus-kill-or-deaden-summary kill-buffer))
3170       (if (not quit-config)
3171           (progn
3172             (set-buffer gnus-group-buffer)
3173             (gnus-group-jump-to-group group)
3174             (gnus-group-next-unread-group 1)
3175             (gnus-configure-windows 'group 'force))
3176         (gnus-handle-ephemeral-exit quit-config))
3177       ;; Finally signal the quit.
3178       (signal 'quit nil))
3179      ;; The group was successfully selected.
3180      (t
3181       (gnus-set-global-variables)
3182       ;; Save the active value in effect when the group was entered.
3183       (setq gnus-newsgroup-active
3184             (gnus-copy-sequence
3185              (gnus-active gnus-newsgroup-name)))
3186       ;; You can change the summary buffer in some way with this hook.
3187       (gnus-run-hooks 'gnus-select-group-hook)
3188       (gnus-update-format-specifications
3189        nil 'summary 'summary-mode 'summary-dummy)
3190       (gnus-update-summary-mark-positions)
3191       ;; Do score processing.
3192       (when gnus-use-scoring
3193         (gnus-possibly-score-headers))
3194       ;; Check whether to fill in the gaps in the threads.
3195       (when gnus-build-sparse-threads
3196         (gnus-build-sparse-threads))
3197       ;; Find the initial limit.
3198       (if gnus-show-threads
3199           (if show-all
3200               (let ((gnus-newsgroup-dormant nil))
3201                 (gnus-summary-initial-limit show-all))
3202             (gnus-summary-initial-limit show-all))
3203         ;; When untreaded, all articles are always shown.
3204         (setq gnus-newsgroup-limit
3205               (mapcar
3206                (lambda (header) (mail-header-number header))
3207                gnus-newsgroup-headers)))
3208       ;; Generate the summary buffer.
3209       (unless no-display
3210         (gnus-summary-prepare))
3211       (when gnus-use-trees
3212         (gnus-tree-open group)
3213         (setq gnus-summary-highlight-line-function
3214               'gnus-tree-highlight-article))
3215       ;; If the summary buffer is empty, but there are some low-scored
3216       ;; articles or some excluded dormants, we include these in the
3217       ;; buffer.
3218       (when (and (zerop (buffer-size))
3219                  (not no-display))
3220         (cond (gnus-newsgroup-dormant
3221                (gnus-summary-limit-include-dormant))
3222               ((and gnus-newsgroup-scored show-all)
3223                (gnus-summary-limit-include-expunged t))))
3224       ;; Function `gnus-apply-kill-file' must be called in this hook.
3225       (gnus-run-hooks 'gnus-apply-kill-hook)
3226       (if (and (zerop (buffer-size))
3227                (not no-display))
3228           (progn
3229             ;; This newsgroup is empty.
3230             (gnus-summary-catchup-and-exit nil t)
3231             (gnus-message 6 "No unread news")
3232             (when kill-buffer
3233               (gnus-kill-or-deaden-summary kill-buffer))
3234             ;; Return nil from this function.
3235             nil)
3236         ;; Hide conversation thread subtrees.  We cannot do this in
3237         ;; gnus-summary-prepare-hook since kill processing may not
3238         ;; work with hidden articles.
3239         (and gnus-show-threads
3240              gnus-thread-hide-subtree
3241              (gnus-summary-hide-all-threads))
3242         (when kill-buffer
3243           (gnus-kill-or-deaden-summary kill-buffer))
3244         ;; Show first unread article if requested.
3245         (if (and (not no-article)
3246                  (not no-display)
3247                  gnus-newsgroup-unreads
3248                  gnus-auto-select-first)
3249             (progn
3250               (gnus-configure-windows 'summary)
3251               (cond
3252                ((eq gnus-auto-select-first 'best)
3253                 (gnus-summary-best-unread-article))
3254                ((eq gnus-auto-select-first t)
3255                 (gnus-summary-first-unread-article))
3256                ((gnus-functionp gnus-auto-select-first)
3257                 (funcall gnus-auto-select-first))))
3258           ;; Don't select any articles, just move point to the first
3259           ;; article in the group.
3260           (goto-char (point-min))
3261           (gnus-summary-position-point)
3262           (gnus-configure-windows 'summary 'force)
3263           (gnus-set-mode-line 'summary))
3264         (when (get-buffer-window gnus-group-buffer t)
3265           ;; Gotta use windows, because recenter does weird stuff if
3266           ;; the current buffer ain't the displayed window.
3267           (let ((owin (selected-window)))
3268             (select-window (get-buffer-window gnus-group-buffer t))
3269             (when (gnus-group-goto-group group)
3270               (recenter))
3271             (select-window owin)))
3272         ;; Mark this buffer as "prepared".
3273         (setq gnus-newsgroup-prepared t)
3274         (gnus-run-hooks 'gnus-summary-prepared-hook)
3275         t)))))
3276
3277 (defun gnus-summary-prepare ()
3278   "Generate the summary buffer."
3279   (interactive)
3280   (let ((buffer-read-only nil))
3281     (erase-buffer)
3282     (setq gnus-newsgroup-data nil
3283           gnus-newsgroup-data-reverse nil)
3284     (gnus-run-hooks 'gnus-summary-generate-hook)
3285     ;; Generate the buffer, either with threads or without.
3286     (when gnus-newsgroup-headers
3287       (gnus-summary-prepare-threads
3288        (if gnus-show-threads
3289            (gnus-sort-gathered-threads
3290             (funcall gnus-summary-thread-gathering-function
3291                      (gnus-sort-threads
3292                       (gnus-cut-threads (gnus-make-threads)))))
3293          ;; Unthreaded display.
3294          (gnus-sort-articles gnus-newsgroup-headers))))
3295     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3296     ;; Call hooks for modifying summary buffer.
3297     (goto-char (point-min))
3298     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3299
3300 (defsubst gnus-general-simplify-subject (subject)
3301   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3302   (setq subject
3303         (cond
3304          ;; Truncate the subject.
3305          (gnus-simplify-subject-functions
3306           (gnus-map-function gnus-simplify-subject-functions subject))
3307          ((numberp gnus-summary-gather-subject-limit)
3308           (setq subject (gnus-simplify-subject-re subject))
3309           (if (> (length subject) gnus-summary-gather-subject-limit)
3310               (substring subject 0 gnus-summary-gather-subject-limit)
3311             subject))
3312          ;; Fuzzily simplify it.
3313          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3314           (gnus-simplify-subject-fuzzy subject))
3315          ;; Just remove the leading "Re:".
3316          (t
3317           (gnus-simplify-subject-re subject))))
3318
3319   (if (and gnus-summary-gather-exclude-subject
3320            (string-match gnus-summary-gather-exclude-subject subject))
3321       nil                         ; This article shouldn't be gathered
3322     subject))
3323
3324 (defun gnus-summary-simplify-subject-query ()
3325   "Query where the respool algorithm would put this article."
3326   (interactive)
3327   (gnus-summary-select-article)
3328   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3329
3330 (defun gnus-gather-threads-by-subject (threads)
3331   "Gather threads by looking at Subject headers."
3332   (if (not gnus-summary-make-false-root)
3333       threads
3334     (let ((hashtb (gnus-make-hashtable 1024))
3335           (prev threads)
3336           (result threads)
3337           subject hthread whole-subject)
3338       (while threads
3339         (setq subject (gnus-general-simplify-subject
3340                        (setq whole-subject (mail-header-subject
3341                                             (caar threads)))))
3342         (when subject
3343           (if (setq hthread (gnus-gethash subject hashtb))
3344               (progn
3345                 ;; We enter a dummy root into the thread, if we
3346                 ;; haven't done that already.
3347                 (unless (stringp (caar hthread))
3348                   (setcar hthread (list whole-subject (car hthread))))
3349                 ;; We add this new gathered thread to this gathered
3350                 ;; thread.
3351                 (setcdr (car hthread)
3352                         (nconc (cdar hthread) (list (car threads))))
3353                 ;; Remove it from the list of threads.
3354                 (setcdr prev (cdr threads))
3355                 (setq threads prev))
3356             ;; Enter this thread into the hash table.
3357             (gnus-sethash subject threads hashtb)))
3358         (setq prev threads)
3359         (setq threads (cdr threads)))
3360       result)))
3361
3362 (defun gnus-gather-threads-by-references (threads)
3363   "Gather threads by looking at References headers."
3364   (let ((idhashtb (gnus-make-hashtable 1024))
3365         (thhashtb (gnus-make-hashtable 1024))
3366         (prev threads)
3367         (result threads)
3368         ids references id gthread gid entered ref)
3369     (while threads
3370       (when (setq references (mail-header-references (caar threads)))
3371         (setq id (mail-header-id (caar threads))
3372               ids (gnus-split-references references)
3373               entered nil)
3374         (while (setq ref (pop ids))
3375           (setq ids (delete ref ids))
3376           (if (not (setq gid (gnus-gethash ref idhashtb)))
3377               (progn
3378                 (gnus-sethash ref id idhashtb)
3379                 (gnus-sethash id threads thhashtb))
3380             (setq gthread (gnus-gethash gid thhashtb))
3381             (unless entered
3382               ;; We enter a dummy root into the thread, if we
3383               ;; haven't done that already.
3384               (unless (stringp (caar gthread))
3385                 (setcar gthread (list (mail-header-subject (caar gthread))
3386                                       (car gthread))))
3387               ;; We add this new gathered thread to this gathered
3388               ;; thread.
3389               (setcdr (car gthread)
3390                       (nconc (cdar gthread) (list (car threads)))))
3391             ;; Add it into the thread hash table.
3392             (gnus-sethash id gthread thhashtb)
3393             (setq entered t)
3394             ;; Remove it from the list of threads.
3395             (setcdr prev (cdr threads))
3396             (setq threads prev))))
3397       (setq prev threads)
3398       (setq threads (cdr threads)))
3399     result))
3400
3401 (defun gnus-sort-gathered-threads (threads)
3402   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3403   (let ((result threads))
3404     (while threads
3405       (when (stringp (caar threads))
3406         (setcdr (car threads)
3407                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3408       (setq threads (cdr threads)))
3409     result))
3410
3411 (defun gnus-thread-loop-p (root thread)
3412   "Say whether ROOT is in THREAD."
3413   (let ((stack (list thread))
3414         (infloop 0)
3415         th)
3416     (while (setq thread (pop stack))
3417       (setq th (cdr thread))
3418       (while (and th
3419                   (not (eq (caar th) root)))
3420         (pop th))
3421       (if th
3422           ;; We have found a loop.
3423           (let (ref-dep)
3424             (setcdr thread (delq (car th) (cdr thread)))
3425             (if (boundp (setq ref-dep (intern "none"
3426                                               gnus-newsgroup-dependencies)))
3427                 (setcdr (symbol-value ref-dep)
3428                         (nconc (cdr (symbol-value ref-dep))
3429                                (list (car th))))
3430               (set ref-dep (list nil (car th))))
3431             (setq infloop 1
3432                   stack nil))
3433         ;; Push all the subthreads onto the stack.
3434         (push (cdr thread) stack)))
3435     infloop))
3436
3437 (defun gnus-make-threads ()
3438   "Go through the dependency hashtb and find the roots.  Return all threads."
3439   (let (threads)
3440     (while (catch 'infloop
3441              (mapatoms
3442               (lambda (refs)
3443                 ;; Deal with self-referencing References loops.
3444                 (when (and (car (symbol-value refs))
3445                            (not (zerop
3446                                  (apply
3447                                   '+
3448                                   (mapcar
3449                                    (lambda (thread)
3450                                      (gnus-thread-loop-p
3451                                       (car (symbol-value refs)) thread))
3452                                    (cdr (symbol-value refs)))))))
3453                   (setq threads nil)
3454                   (throw 'infloop t))
3455                 (unless (car (symbol-value refs))
3456              ;; These threads do not refer back to any other articles,
3457                   ;; so they're roots.
3458                   (setq threads (append (cdr (symbol-value refs)) threads))))
3459               gnus-newsgroup-dependencies)))
3460     threads))
3461
3462 ;; Build the thread tree.
3463 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3464   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3465
3466 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3467 if it was already present.
3468
3469 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3470 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3471 Message-IDs will be renamed be renamed to a unique Message-ID before
3472 being entered.
3473
3474 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3475   (let* ((id (mail-header-id header))
3476          (id-dep (and id (intern id dependencies)))
3477          ref ref-dep ref-header)
3478     ;; Enter this `header' in the `dependencies' table.
3479     (cond
3480      ((not id-dep)
3481       (setq header nil))
3482      ;; The first two cases do the normal part: enter a new `header'
3483      ;; in the `dependencies' table.
3484      ((not (boundp id-dep))
3485       (set id-dep (list header)))
3486      ((null (car (symbol-value id-dep)))
3487       (setcar (symbol-value id-dep) header))
3488
3489      ;; From here the `header' was already present in the
3490      ;; `dependencies' table.
3491      (force-new
3492       ;; Overrides an existing entry;
3493       ;; just set the header part of the entry.
3494       (setcar (symbol-value id-dep) header))
3495
3496      ;; Renames the existing `header' to a unique Message-ID.
3497      ((not gnus-summary-ignore-duplicates)
3498       ;; An article with this Message-ID has already been seen.
3499       ;; We rename the Message-ID.
3500       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3501            (list header))
3502       (mail-header-set-id header id))
3503
3504      ;; The last case ignores an existing entry, except it adds any
3505      ;; additional Xrefs (in case the two articles came from different
3506      ;; servers.
3507      ;; Also sets `header' to `nil' meaning that the `dependencies'
3508      ;; table was *not* modified.
3509      (t
3510       (mail-header-set-xref
3511        (car (symbol-value id-dep))
3512        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3513                    "")
3514                (or (mail-header-xref header) "")))
3515       (setq header nil)))
3516
3517     (when header
3518       ;; First check if that we are not creating a References loop.
3519       (setq ref (gnus-parent-id (mail-header-references header)))
3520       (while (and ref
3521                   (setq ref-dep (intern-soft ref dependencies))
3522                   (boundp ref-dep)
3523                   (setq ref-header (car (symbol-value ref-dep))))
3524         (if (string= id ref)
3525             ;; Yuk!  This is a reference loop.  Make the article be a
3526             ;; root article.
3527             (progn
3528               (mail-header-set-references (car (symbol-value id-dep)) "none")
3529               (setq ref nil))
3530           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3531       (setq ref (gnus-parent-id (mail-header-references header)))
3532       (setq ref-dep (intern (or ref "none") dependencies))
3533       (if (boundp ref-dep)
3534           (setcdr (symbol-value ref-dep)
3535                   (nconc (cdr (symbol-value ref-dep))
3536                          (list (symbol-value id-dep))))
3537         (set ref-dep (list nil (symbol-value id-dep)))))
3538     header))
3539
3540 (defun gnus-build-sparse-threads ()
3541   (let ((headers gnus-newsgroup-headers)
3542         (mail-parse-charset gnus-newsgroup-charset)
3543         (gnus-summary-ignore-duplicates t)
3544         header references generation relations
3545         subject child end new-child date)
3546     ;; First we create an alist of generations/relations, where
3547     ;; generations is how much we trust the relation, and the relation
3548     ;; is parent/child.
3549     (gnus-message 7 "Making sparse threads...")
3550     (save-excursion
3551       (nnheader-set-temp-buffer " *gnus sparse threads*")
3552       (while (setq header (pop headers))
3553         (when (and (setq references (mail-header-references header))
3554                    (not (string= references "")))
3555           (insert references)
3556           (setq child (mail-header-id header)
3557                 subject (mail-header-subject header)
3558                 date (mail-header-date header)
3559                 generation 0)
3560           (while (search-backward ">" nil t)
3561             (setq end (1+ (point)))
3562             (when (search-backward "<" nil t)
3563               (setq new-child (buffer-substring (point) end))
3564               (push (list (incf generation)
3565                           child (setq child new-child)
3566                           subject date)
3567                     relations)))
3568           (when child
3569             (push (list (1+ generation) child nil subject) relations))
3570           (erase-buffer)))
3571       (kill-buffer (current-buffer)))
3572     ;; Sort over trustworthiness.
3573     (mapcar
3574      (lambda (relation)
3575        (when (gnus-dependencies-add-header
3576               (make-full-mail-header
3577                gnus-reffed-article-number
3578                (nth 3 relation) "" (or (nth 4 relation) "")
3579                (nth 1 relation)
3580                (or (nth 2 relation) "") 0 0 "")
3581               gnus-newsgroup-dependencies nil)
3582          (push gnus-reffed-article-number gnus-newsgroup-limit)
3583          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3584          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3585                gnus-newsgroup-reads)
3586          (decf gnus-reffed-article-number)))
3587      (sort relations 'car-less-than-car))
3588     (gnus-message 7 "Making sparse threads...done")))
3589
3590 (defun gnus-build-old-threads ()
3591   ;; Look at all the articles that refer back to old articles, and
3592   ;; fetch the headers for the articles that aren't there.  This will
3593   ;; build complete threads - if the roots haven't been expired by the
3594   ;; server, that is.
3595   (let ((mail-parse-charset gnus-newsgroup-charset)
3596         id heads)
3597     (mapatoms
3598      (lambda (refs)
3599        (when (not (car (symbol-value refs)))
3600          (setq heads (cdr (symbol-value refs)))
3601          (while heads
3602            (if (memq (mail-header-number (caar heads))
3603                      gnus-newsgroup-dormant)
3604                (setq heads (cdr heads))
3605              (setq id (symbol-name refs))
3606              (while (and (setq id (gnus-build-get-header id))
3607                          (not (car (gnus-id-to-thread id)))))
3608              (setq heads nil)))))
3609      gnus-newsgroup-dependencies)))
3610
3611 ;; This function has to be called with point after the article number
3612 ;; on the beginning of the line.
3613 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3614   (let ((eol (gnus-point-at-eol))
3615         (buffer (current-buffer))
3616         header)
3617
3618     ;; overview: [num subject from date id refs chars lines misc]
3619     (unwind-protect
3620         (progn
3621           (narrow-to-region (point) eol)
3622           (unless (eobp)
3623             (forward-char))
3624
3625           (setq header
3626                 (make-full-mail-header
3627                  number                 ; number
3628                  (funcall gnus-decode-encoded-word-function
3629                           (nnheader-nov-field)) ; subject
3630                  (funcall gnus-decode-encoded-word-function
3631                           (nnheader-nov-field)) ; from
3632                  (nnheader-nov-field)   ; date
3633                  (nnheader-nov-read-message-id) ; id
3634                  (nnheader-nov-field)   ; refs
3635                  (nnheader-nov-read-integer) ; chars
3636                  (nnheader-nov-read-integer) ; lines
3637                  (unless (eobp)
3638                    (if (looking-at "Xref: ")
3639                        (goto-char (match-end 0)))
3640                    (nnheader-nov-field)) ; Xref
3641                  (nnheader-nov-parse-extra)))) ; extra
3642
3643       (widen))
3644
3645     (when gnus-alter-header-function
3646       (funcall gnus-alter-header-function header))
3647     (gnus-dependencies-add-header header dependencies force-new)))
3648
3649 (defun gnus-build-get-header (id)
3650   "Look through the buffer of NOV lines and find the header to ID.
3651 Enter this line into the dependencies hash table, and return
3652 the id of the parent article (if any)."
3653   (let ((deps gnus-newsgroup-dependencies)
3654         found header)
3655     (prog1
3656         (save-excursion
3657           (set-buffer nntp-server-buffer)
3658           (let ((case-fold-search nil))
3659             (goto-char (point-min))
3660             (while (and (not found)
3661                         (search-forward id nil t))
3662               (beginning-of-line)
3663               (setq found (looking-at
3664                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3665                                    (regexp-quote id))))
3666               (or found (beginning-of-line 2)))
3667             (when found
3668               (beginning-of-line)
3669               (and
3670                (setq header (gnus-nov-parse-line
3671                              (read (current-buffer)) deps))
3672                (gnus-parent-id (mail-header-references header))))))
3673       (when header
3674         (let ((number (mail-header-number header)))
3675           (push number gnus-newsgroup-limit)
3676           (push header gnus-newsgroup-headers)
3677           (if (memq number gnus-newsgroup-unselected)
3678               (progn
3679                 (push number gnus-newsgroup-unreads)
3680                 (setq gnus-newsgroup-unselected
3681                       (delq number gnus-newsgroup-unselected)))
3682             (push number gnus-newsgroup-ancient)))))))
3683
3684 (defun gnus-build-all-threads ()
3685   "Read all the headers."
3686   (let ((gnus-summary-ignore-duplicates t)
3687         (mail-parse-charset gnus-newsgroup-charset)
3688         (dependencies gnus-newsgroup-dependencies)
3689         header article)
3690     (save-excursion
3691       (set-buffer nntp-server-buffer)
3692       (let ((case-fold-search nil))
3693         (goto-char (point-min))
3694         (while (not (eobp))
3695           (ignore-errors
3696             (setq article (read (current-buffer))
3697                   header (gnus-nov-parse-line article dependencies)))
3698           (when header
3699             (save-excursion
3700               (set-buffer gnus-summary-buffer)
3701               (push header gnus-newsgroup-headers)
3702               (if (memq (setq article (mail-header-number header))
3703                         gnus-newsgroup-unselected)
3704                   (progn
3705                     (push article gnus-newsgroup-unreads)
3706                     (setq gnus-newsgroup-unselected
3707                           (delq article gnus-newsgroup-unselected)))
3708                 (push article gnus-newsgroup-ancient)))
3709             (forward-line 1)))))))
3710
3711 (defun gnus-summary-update-article-line (article header)
3712   "Update the line for ARTICLE using HEADERS."
3713   (let* ((id (mail-header-id header))
3714          (thread (gnus-id-to-thread id)))
3715     (unless thread
3716       (error "Article in no thread"))
3717     ;; Update the thread.
3718     (setcar thread header)
3719     (gnus-summary-goto-subject article)
3720     (let* ((datal (gnus-data-find-list article))
3721            (data (car datal))
3722            (length (when (cdr datal)
3723                      (- (gnus-data-pos data)
3724                         (gnus-data-pos (cadr datal)))))
3725            (buffer-read-only nil)
3726            (level (gnus-summary-thread-level)))
3727       (gnus-delete-line)
3728       (gnus-summary-insert-line
3729        header level nil (gnus-article-mark article)
3730        (memq article gnus-newsgroup-replied)
3731        (memq article gnus-newsgroup-expirable)
3732        ;; Only insert the Subject string when it's different
3733        ;; from the previous Subject string.
3734        (if (and
3735             gnus-show-threads
3736             (gnus-subject-equal
3737              (condition-case ()
3738                  (mail-header-subject
3739                   (gnus-data-header
3740                    (cadr
3741                     (gnus-data-find-list
3742                      article
3743                      (gnus-data-list t)))))
3744                ;; Error on the side of excessive subjects.
3745                (error ""))
3746              (mail-header-subject header)))
3747            ""
3748          (mail-header-subject header))
3749        nil (cdr (assq article gnus-newsgroup-scored))
3750        (memq article gnus-newsgroup-processable))
3751       (when length
3752         (gnus-data-update-list
3753          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3754
3755 (defun gnus-summary-update-article (article &optional iheader)
3756   "Update ARTICLE in the summary buffer."
3757   (set-buffer gnus-summary-buffer)
3758   (let* ((header (gnus-summary-article-header article))
3759          (id (mail-header-id header))
3760          (data (gnus-data-find article))
3761          (thread (gnus-id-to-thread id))
3762          (references (mail-header-references header))
3763          (parent
3764           (gnus-id-to-thread
3765            (or (gnus-parent-id
3766                 (when (and references
3767                            (not (equal "" references)))
3768                   references))
3769                "none")))
3770          (buffer-read-only nil)
3771          (old (car thread)))
3772     (when thread
3773       (unless iheader
3774         (setcar thread nil)
3775         (when parent
3776           (delq thread parent)))
3777       (if (gnus-summary-insert-subject id header)
3778        ;; Set the (possibly) new article number in the data structure.
3779           (gnus-data-set-number data (gnus-id-to-article id))
3780         (setcar thread old)
3781         nil))))
3782
3783 (defun gnus-rebuild-thread (id &optional line)
3784   "Rebuild the thread containing ID.
3785 If LINE, insert the rebuilt thread starting on line LINE."
3786   (let ((buffer-read-only nil)
3787         old-pos current thread data)
3788     (if (not gnus-show-threads)
3789         (setq thread (list (car (gnus-id-to-thread id))))
3790       ;; Get the thread this article is part of.
3791       (setq thread (gnus-remove-thread id)))
3792     (setq old-pos (gnus-point-at-bol))
3793     (setq current (save-excursion
3794                     (and (re-search-backward "[\r\n]" nil t)
3795                          (gnus-summary-article-number))))
3796     ;; If this is a gathered thread, we have to go some re-gathering.
3797     (when (stringp (car thread))
3798       (let ((subject (car thread))
3799             roots thr)
3800         (setq thread (cdr thread))
3801         (while thread
3802           (unless (memq (setq thr (gnus-id-to-thread
3803                                    (gnus-root-id
3804                                     (mail-header-id (caar thread)))))
3805                         roots)
3806             (push thr roots))
3807           (setq thread (cdr thread)))
3808         ;; We now have all (unique) roots.
3809         (if (= (length roots) 1)
3810             ;; All the loose roots are now one solid root.
3811             (setq thread (car roots))
3812           (setq thread (cons subject (gnus-sort-threads roots))))))
3813     (let (threads)
3814       ;; We then insert this thread into the summary buffer.
3815       (when line
3816         (goto-char (point-min))
3817         (forward-line (1- line)))
3818       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3819         (if gnus-show-threads
3820             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3821           (gnus-summary-prepare-unthreaded thread))
3822         (setq data (nreverse gnus-newsgroup-data))
3823         (setq threads gnus-newsgroup-threads))
3824       ;; We splice the new data into the data structure.
3825       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3826       ;;!!! then we want to insert at the beginning of the buffer.
3827       ;;!!! That happens to be true with Gnus now, but that may
3828       ;;!!! change in the future.  Perhaps.
3829       (gnus-data-enter-list
3830        (if line nil current) data (- (point) old-pos))
3831       (setq gnus-newsgroup-threads
3832             (nconc threads gnus-newsgroup-threads))
3833       (gnus-data-compute-positions))))
3834
3835 (defun gnus-number-to-header (number)
3836   "Return the header for article NUMBER."
3837   (let ((headers gnus-newsgroup-headers))
3838     (while (and headers
3839                 (not (= number (mail-header-number (car headers)))))
3840       (pop headers))
3841     (when headers
3842       (car headers))))
3843
3844 (defun gnus-parent-headers (in-headers &optional generation)
3845   "Return the headers of the GENERATIONeth parent of HEADERS."
3846   (unless generation
3847     (setq generation 1))
3848   (let ((parent t)
3849         (headers in-headers)
3850         references)
3851     (while (and parent
3852                 (not (zerop generation))
3853                 (setq references (mail-header-references headers)))
3854       (setq headers (if (and references
3855                              (setq parent (gnus-parent-id references)))
3856                         (car (gnus-id-to-thread parent))
3857                       nil))
3858       (decf generation))
3859     (and (not (eq headers in-headers))
3860          headers)))
3861
3862 (defun gnus-id-to-thread (id)
3863   "Return the (sub-)thread where ID appears."
3864   (gnus-gethash id gnus-newsgroup-dependencies))
3865
3866 (defun gnus-id-to-article (id)
3867   "Return the article number of ID."
3868   (let ((thread (gnus-id-to-thread id)))
3869     (when (and thread
3870                (car thread))
3871       (mail-header-number (car thread)))))
3872
3873 (defun gnus-id-to-header (id)
3874   "Return the article headers of ID."
3875   (car (gnus-id-to-thread id)))
3876
3877 (defun gnus-article-displayed-root-p (article)
3878   "Say whether ARTICLE is a root(ish) article."
3879   (let ((level (gnus-summary-thread-level article))
3880         (refs (mail-header-references  (gnus-summary-article-header article)))
3881         particle)
3882     (cond
3883      ((null level) nil)
3884      ((zerop level) t)
3885      ((null refs) t)
3886      ((null (gnus-parent-id refs)) t)
3887      ((and (= 1 level)
3888            (null (setq particle (gnus-id-to-article
3889                                  (gnus-parent-id refs))))
3890            (null (gnus-summary-thread-level particle)))))))
3891
3892 (defun gnus-root-id (id)
3893   "Return the id of the root of the thread where ID appears."
3894   (let (last-id prev)
3895     (while (and id (setq prev (car (gnus-id-to-thread id))))
3896       (setq last-id id
3897             id (gnus-parent-id (mail-header-references prev))))
3898     last-id))
3899
3900 (defun gnus-articles-in-thread (thread)
3901   "Return the list of articles in THREAD."
3902   (cons (mail-header-number (car thread))
3903         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3904
3905 (defun gnus-remove-thread (id &optional dont-remove)
3906   "Remove the thread that has ID in it."
3907   (let (headers thread last-id)
3908     ;; First go up in this thread until we find the root.
3909     (setq last-id (gnus-root-id id)
3910           headers (message-flatten-list (gnus-id-to-thread last-id)))
3911     ;; We have now found the real root of this thread.  It might have
3912     ;; been gathered into some loose thread, so we have to search
3913     ;; through the threads to find the thread we wanted.
3914     (let ((threads gnus-newsgroup-threads)
3915           sub)
3916       (while threads
3917         (setq sub (car threads))
3918         (if (stringp (car sub))
3919             ;; This is a gathered thread, so we look at the roots
3920             ;; below it to find whether this article is in this
3921             ;; gathered root.
3922             (progn
3923               (setq sub (cdr sub))
3924               (while sub
3925                 (when (member (caar sub) headers)
3926                   (setq thread (car threads)
3927                         threads nil
3928                         sub nil))
3929                 (setq sub (cdr sub))))
3930           ;; It's an ordinary thread, so we check it.
3931           (when (eq (car sub) (car headers))
3932             (setq thread sub
3933                   threads nil)))
3934         (setq threads (cdr threads)))
3935       ;; If this article is in no thread, then it's a root.
3936       (if thread
3937           (unless dont-remove
3938             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3939         (setq thread (gnus-id-to-thread last-id)))
3940       (when thread
3941         (prog1
3942             thread                      ; We return this thread.
3943           (unless dont-remove
3944             (if (stringp (car thread))
3945                 (progn
3946                   ;; If we use dummy roots, then we have to remove the
3947                   ;; dummy root as well.
3948                   (when (eq gnus-summary-make-false-root 'dummy)
3949                     ;; We go to the dummy root by going to
3950                     ;; the first sub-"thread", and then one line up.
3951                     (gnus-summary-goto-article
3952                      (mail-header-number (caadr thread)))
3953                     (forward-line -1)
3954                     (gnus-delete-line)
3955                     (gnus-data-compute-positions))
3956                   (setq thread (cdr thread))
3957                   (while thread
3958                     (gnus-remove-thread-1 (car thread))
3959                     (setq thread (cdr thread))))
3960               (gnus-remove-thread-1 thread))))))))
3961
3962 (defun gnus-remove-thread-1 (thread)
3963   "Remove the thread THREAD recursively."
3964   (let ((number (mail-header-number (pop thread)))
3965         d)
3966     (setq thread (reverse thread))
3967     (while thread
3968       (gnus-remove-thread-1 (pop thread)))
3969     (when (setq d (gnus-data-find number))
3970       (goto-char (gnus-data-pos d))
3971       (gnus-summary-show-thread)
3972       (gnus-data-remove
3973        number
3974        (- (gnus-point-at-bol)
3975           (prog1
3976               (1+ (gnus-point-at-eol))
3977             (gnus-delete-line)))))))
3978
3979 (defun gnus-sort-threads-1 (threads func)
3980   (sort (mapcar (lambda (thread)
3981                   (cons (car thread)
3982                         (and (cdr thread)
3983                              (gnus-sort-threads-1 (cdr thread) func))))
3984                 threads) func))
3985
3986 (defun gnus-sort-threads (threads)
3987   "Sort THREADS."
3988   (if (not gnus-thread-sort-functions)
3989       threads
3990     (gnus-message 8 "Sorting threads...")
3991     (prog1
3992         (gnus-sort-threads-1
3993          threads
3994          (gnus-make-sort-function gnus-thread-sort-functions))
3995       (gnus-message 8 "Sorting threads...done"))))
3996
3997 (defun gnus-sort-articles (articles)
3998   "Sort ARTICLES."
3999   (when gnus-article-sort-functions
4000     (gnus-message 7 "Sorting articles...")
4001     (prog1
4002         (setq gnus-newsgroup-headers
4003               (sort articles (gnus-make-sort-function
4004                               gnus-article-sort-functions)))
4005       (gnus-message 7 "Sorting articles...done"))))
4006
4007 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4008 (defmacro gnus-thread-header (thread)
4009   "Return header of first article in THREAD.
4010 Note that THREAD must never, ever be anything else than a variable -
4011 using some other form will lead to serious barfage."
4012   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4013   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4014   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4015         (vector thread) 2))
4016
4017 (defsubst gnus-article-sort-by-number (h1 h2)
4018   "Sort articles by article number."
4019   (< (mail-header-number h1)
4020      (mail-header-number h2)))
4021
4022 (defun gnus-thread-sort-by-number (h1 h2)
4023   "Sort threads by root article number."
4024   (gnus-article-sort-by-number
4025    (gnus-thread-header h1) (gnus-thread-header h2)))
4026
4027 (defsubst gnus-article-sort-by-lines (h1 h2)
4028   "Sort articles by article Lines header."
4029   (< (mail-header-lines h1)
4030      (mail-header-lines h2)))
4031
4032 (defun gnus-thread-sort-by-lines (h1 h2)
4033   "Sort threads by root article Lines header."
4034   (gnus-article-sort-by-lines
4035    (gnus-thread-header h1) (gnus-thread-header h2)))
4036
4037 (defsubst gnus-article-sort-by-chars (h1 h2)
4038   "Sort articles by octet length."
4039   (< (mail-header-chars h1)
4040      (mail-header-chars h2)))
4041
4042 (defun gnus-thread-sort-by-chars (h1 h2)
4043   "Sort threads by root article octet length."
4044   (gnus-article-sort-by-chars
4045    (gnus-thread-header h1) (gnus-thread-header h2)))
4046
4047 (defsubst gnus-article-sort-by-author (h1 h2)
4048   "Sort articles by root author."
4049   (string-lessp
4050    (let ((extract (funcall
4051                    gnus-extract-address-components
4052                    (mail-header-from h1))))
4053      (or (car extract) (cadr extract) ""))
4054    (let ((extract (funcall
4055                    gnus-extract-address-components
4056                    (mail-header-from h2))))
4057      (or (car extract) (cadr extract) ""))))
4058
4059 (defun gnus-thread-sort-by-author (h1 h2)
4060   "Sort threads by root author."
4061   (gnus-article-sort-by-author
4062    (gnus-thread-header h1)  (gnus-thread-header h2)))
4063
4064 (defsubst gnus-article-sort-by-subject (h1 h2)
4065   "Sort articles by root subject."
4066   (string-lessp
4067    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4068    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4069
4070 (defun gnus-thread-sort-by-subject (h1 h2)
4071   "Sort threads by root subject."
4072   (gnus-article-sort-by-subject
4073    (gnus-thread-header h1) (gnus-thread-header h2)))
4074
4075 (defsubst gnus-article-sort-by-date (h1 h2)
4076   "Sort articles by root article date."
4077   (time-less-p
4078    (gnus-date-get-time (mail-header-date h1))
4079    (gnus-date-get-time (mail-header-date h2))))
4080
4081 (defun gnus-thread-sort-by-date (h1 h2)
4082   "Sort threads by root article date."
4083   (gnus-article-sort-by-date
4084    (gnus-thread-header h1) (gnus-thread-header h2)))
4085
4086 (defsubst gnus-article-sort-by-score (h1 h2)
4087   "Sort articles by root article score.
4088 Unscored articles will be counted as having a score of zero."
4089   (> (or (cdr (assq (mail-header-number h1)
4090                     gnus-newsgroup-scored))
4091          gnus-summary-default-score 0)
4092      (or (cdr (assq (mail-header-number h2)
4093                     gnus-newsgroup-scored))
4094          gnus-summary-default-score 0)))
4095
4096 (defun gnus-thread-sort-by-score (h1 h2)
4097   "Sort threads by root article score."
4098   (gnus-article-sort-by-score
4099    (gnus-thread-header h1) (gnus-thread-header h2)))
4100
4101 (defun gnus-thread-sort-by-total-score (h1 h2)
4102   "Sort threads by the sum of all scores in the thread.
4103 Unscored articles will be counted as having a score of zero."
4104   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4105
4106 (defun gnus-thread-total-score (thread)
4107   ;; This function find the total score of THREAD.
4108   (cond ((null thread)
4109          0)
4110         ((consp thread)
4111          (if (stringp (car thread))
4112              (apply gnus-thread-score-function 0
4113                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4114            (gnus-thread-total-score-1 thread)))
4115         (t
4116          (gnus-thread-total-score-1 (list thread)))))
4117
4118 (defun gnus-thread-total-score-1 (root)
4119   ;; This function find the total score of the thread below ROOT.
4120   (setq root (car root))
4121   (apply gnus-thread-score-function
4122          (or (append
4123               (mapcar 'gnus-thread-total-score
4124                       (cdr (gnus-id-to-thread (mail-header-id root))))
4125               (when (> (mail-header-number root) 0)
4126                 (list (or (cdr (assq (mail-header-number root)
4127                                      gnus-newsgroup-scored))
4128                           gnus-summary-default-score 0))))
4129              (list gnus-summary-default-score)
4130              '(0))))
4131
4132 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4133 (defvar gnus-tmp-prev-subject nil)
4134 (defvar gnus-tmp-false-parent nil)
4135 (defvar gnus-tmp-root-expunged nil)
4136 (defvar gnus-tmp-dummy-line nil)
4137
4138 (eval-when-compile (defvar gnus-tmp-header))
4139 (defun gnus-extra-header (type &optional header)
4140   "Return the extra header of TYPE."
4141   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4142       ""))
4143
4144 (defvar gnus-tmp-thread-tree-header-string "")
4145
4146 (defvar gnus-sum-thread-tree-root "> "
4147   "With %B spec, used for the root of a thread.
4148 If nil, use subject instead.")
4149 (defvar gnus-sum-thread-tree-single-indent ""
4150   "With %B spec, used for a thread with just one message.
4151 If nil, use subject instead.")
4152 (defvar gnus-sum-thread-tree-vertical "| "
4153   "With %B spec, used for drawing a vertical line.")
4154 (defvar gnus-sum-thread-tree-indent "  "
4155   "With %B spec, used for indenting.")
4156 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4157   "With %B spec, used for a leaf with brothers.")
4158 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4159   "With %B spec, used for a leaf without brothers.")
4160
4161 (defun gnus-summary-prepare-threads (threads)
4162   "Prepare summary buffer from THREADS and indentation LEVEL.
4163 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4164 or a straight list of headers."
4165   (gnus-message 7 "Generating summary...")
4166
4167   (setq gnus-newsgroup-threads threads)
4168   (beginning-of-line)
4169
4170   (let ((gnus-tmp-level 0)
4171         (default-score (or gnus-summary-default-score 0))
4172         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4173         thread number subject stack state gnus-tmp-gathered beg-match
4174         new-roots gnus-tmp-new-adopts thread-end
4175         gnus-tmp-header gnus-tmp-unread
4176         gnus-tmp-replied gnus-tmp-subject-or-nil
4177         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4178         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4179         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4180         tree-stack)
4181
4182     (setq gnus-tmp-prev-subject nil)
4183
4184     (if (vectorp (car threads))
4185         ;; If this is a straight (sic) list of headers, then a
4186         ;; threaded summary display isn't required, so we just create
4187         ;; an unthreaded one.
4188         (gnus-summary-prepare-unthreaded threads)
4189
4190       ;; Do the threaded display.
4191
4192       (while (or threads stack gnus-tmp-new-adopts new-roots)
4193
4194         (if (and (= gnus-tmp-level 0)
4195                  (or (not stack)
4196                      (= (caar stack) 0))
4197                  (not gnus-tmp-false-parent)
4198                  (or gnus-tmp-new-adopts new-roots))
4199             (if gnus-tmp-new-adopts
4200                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4201                       thread (list (car gnus-tmp-new-adopts))
4202                       gnus-tmp-header (caar thread)
4203                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4204               (when new-roots
4205                 (setq thread (list (car new-roots))
4206                       gnus-tmp-header (caar thread)
4207                       new-roots (cdr new-roots))))
4208
4209           (if threads
4210               ;; If there are some threads, we do them before the
4211               ;; threads on the stack.
4212               (setq thread threads
4213                     gnus-tmp-header (caar thread))
4214             ;; There were no current threads, so we pop something off
4215             ;; the stack.
4216             (setq state (car stack)
4217                   gnus-tmp-level (car state)
4218                   tree-stack (cadr state)
4219                   thread (caddr state)
4220                   stack (cdr stack)
4221                   gnus-tmp-header (caar thread))))
4222
4223         (setq gnus-tmp-false-parent nil)
4224         (setq gnus-tmp-root-expunged nil)
4225         (setq thread-end nil)
4226
4227         (if (stringp gnus-tmp-header)
4228             ;; The header is a dummy root.
4229             (cond
4230              ((eq gnus-summary-make-false-root 'adopt)
4231               ;; We let the first article adopt the rest.
4232               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4233                                                (cddar thread)))
4234               (setq gnus-tmp-gathered
4235                     (nconc (mapcar
4236                             (lambda (h) (mail-header-number (car h)))
4237                             (cddar thread))
4238                            gnus-tmp-gathered))
4239               (setq thread (cons (list (caar thread)
4240                                        (cadar thread))
4241                                  (cdr thread)))
4242               (setq gnus-tmp-level -1
4243                     gnus-tmp-false-parent t))
4244              ((eq gnus-summary-make-false-root 'empty)
4245               ;; We print adopted articles with empty subject fields.
4246               (setq gnus-tmp-gathered
4247                     (nconc (mapcar
4248                             (lambda (h) (mail-header-number (car h)))
4249                             (cddar thread))
4250                            gnus-tmp-gathered))
4251               (setq gnus-tmp-level -1))
4252              ((eq gnus-summary-make-false-root 'dummy)
4253               ;; We remember that we probably want to output a dummy
4254               ;; root.
4255               (setq gnus-tmp-dummy-line gnus-tmp-header)
4256               (setq gnus-tmp-prev-subject gnus-tmp-header))
4257              (t
4258               ;; We do not make a root for the gathered
4259               ;; sub-threads at all.
4260               (setq gnus-tmp-level -1)))
4261
4262           (setq number (mail-header-number gnus-tmp-header)
4263                 subject (mail-header-subject gnus-tmp-header))
4264
4265           (cond
4266            ;; If the thread has changed subject, we might want to make
4267            ;; this subthread into a root.
4268            ((and (null gnus-thread-ignore-subject)
4269                  (not (zerop gnus-tmp-level))
4270                  gnus-tmp-prev-subject
4271                  (not (inline
4272                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4273             (setq new-roots (nconc new-roots (list (car thread)))
4274                   thread-end t
4275                   gnus-tmp-header nil))
4276            ;; If the article lies outside the current limit,
4277            ;; then we do not display it.
4278            ((not (memq number gnus-newsgroup-limit))
4279             (setq gnus-tmp-gathered
4280                   (nconc (mapcar
4281                           (lambda (h) (mail-header-number (car h)))
4282                           (cdar thread))
4283                          gnus-tmp-gathered))
4284             (setq gnus-tmp-new-adopts (if (cdar thread)
4285                                           (append gnus-tmp-new-adopts
4286                                                   (cdar thread))
4287                                         gnus-tmp-new-adopts)
4288                   thread-end t
4289                   gnus-tmp-header nil)
4290             (when (zerop gnus-tmp-level)
4291               (setq gnus-tmp-root-expunged t)))
4292            ;; Perhaps this article is to be marked as read?
4293            ((and gnus-summary-mark-below
4294                  (< (or (cdr (assq number gnus-newsgroup-scored))
4295                         default-score)
4296                     gnus-summary-mark-below)
4297                  ;; Don't touch sparse articles.
4298                  (not (gnus-summary-article-sparse-p number))
4299                  (not (gnus-summary-article-ancient-p number)))
4300             (setq gnus-newsgroup-unreads
4301                   (delq number gnus-newsgroup-unreads))
4302             (if gnus-newsgroup-auto-expire
4303                 (push number gnus-newsgroup-expirable)
4304               (push (cons number gnus-low-score-mark)
4305                     gnus-newsgroup-reads))))
4306
4307           (when gnus-tmp-header
4308             ;; We may have an old dummy line to output before this
4309             ;; article.
4310             (when (and gnus-tmp-dummy-line
4311                        (gnus-subject-equal
4312                         gnus-tmp-dummy-line
4313                         (mail-header-subject gnus-tmp-header)))
4314               (gnus-summary-insert-dummy-line
4315                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4316               (setq gnus-tmp-dummy-line nil))
4317
4318             ;; Compute the mark.
4319             (setq gnus-tmp-unread (gnus-article-mark number))
4320
4321             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4322                                   gnus-tmp-header gnus-tmp-level)
4323                   gnus-newsgroup-data)
4324
4325             ;; Actually insert the line.
4326             (setq
4327              gnus-tmp-subject-or-nil
4328              (cond
4329               ((and gnus-thread-ignore-subject
4330                     gnus-tmp-prev-subject
4331                     (not (inline (gnus-subject-equal
4332                                   gnus-tmp-prev-subject subject))))
4333                subject)
4334               ((zerop gnus-tmp-level)
4335                (if (and (eq gnus-summary-make-false-root 'empty)
4336                         (memq number gnus-tmp-gathered)
4337                         gnus-tmp-prev-subject
4338                         (inline (gnus-subject-equal
4339                                  gnus-tmp-prev-subject subject)))
4340                    gnus-summary-same-subject
4341                  subject))
4342               (t gnus-summary-same-subject)))
4343             (if (and (eq gnus-summary-make-false-root 'adopt)
4344                      (= gnus-tmp-level 1)
4345                      (memq number gnus-tmp-gathered))
4346                 (setq gnus-tmp-opening-bracket ?\<
4347                       gnus-tmp-closing-bracket ?\>)
4348               (setq gnus-tmp-opening-bracket ?\[
4349                     gnus-tmp-closing-bracket ?\]))
4350             (setq
4351              gnus-tmp-indentation
4352              (aref gnus-thread-indent-array gnus-tmp-level)
4353              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4354              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4355                                 gnus-summary-default-score 0)
4356              gnus-tmp-score-char
4357              (if (or (null gnus-summary-default-score)
4358                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4359                          gnus-summary-zcore-fuzz))
4360                  ?                      ;Whitespace
4361                (if (< gnus-tmp-score gnus-summary-default-score)
4362                    gnus-score-below-mark gnus-score-over-mark))
4363              gnus-tmp-replied
4364              (cond ((memq number gnus-newsgroup-processable)
4365                     gnus-process-mark)
4366                    ((memq number gnus-newsgroup-cached)
4367                     gnus-cached-mark)
4368                    ((memq number gnus-newsgroup-replied)
4369                     gnus-replied-mark)
4370                    ((memq number gnus-newsgroup-forwarded)
4371                     gnus-forwarded-mark)
4372                    ((memq number gnus-newsgroup-saved)
4373                     gnus-saved-mark)
4374                    ((memq number gnus-newsgroup-recent)
4375                     gnus-recent-mark)
4376                    ((memq number gnus-newsgroup-unseen)
4377                     gnus-unseen-mark)
4378                    (t gnus-no-mark))
4379              gnus-tmp-from (mail-header-from gnus-tmp-header)
4380              gnus-tmp-name
4381              (cond
4382               ((string-match "<[^>]+> *$" gnus-tmp-from)
4383                (setq beg-match (match-beginning 0))
4384                (or (and (string-match "^\".+\"" gnus-tmp-from)
4385                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4386                    (substring gnus-tmp-from 0 beg-match)))
4387               ((string-match "(.+)" gnus-tmp-from)
4388                (substring gnus-tmp-from
4389                           (1+ (match-beginning 0)) (1- (match-end 0))))
4390               (t gnus-tmp-from))
4391              gnus-tmp-thread-tree-header-string
4392              (cond 
4393               ((not gnus-show-threads) "")
4394               ((zerop gnus-tmp-level)
4395                (if (cdar thread) 
4396                    (or gnus-sum-thread-tree-root subject)
4397                  (or gnus-sum-thread-tree-single-indent subject)))
4398               (t
4399                (concat (apply 'concat
4400                               (mapcar (lambda (item) 
4401                                         (if (= item 1) 
4402                                             gnus-sum-thread-tree-vertical
4403                                           gnus-sum-thread-tree-indent))
4404                                       (cdr (reverse tree-stack))))
4405                        (if (nth 1 thread) 
4406                            gnus-sum-thread-tree-leaf-with-other
4407                          gnus-sum-thread-tree-single-leaf)))))
4408             (when (string= gnus-tmp-name "")
4409               (setq gnus-tmp-name gnus-tmp-from))
4410             (unless (numberp gnus-tmp-lines)
4411               (setq gnus-tmp-lines -1))
4412             (if (= gnus-tmp-lines -1)
4413                 (setq gnus-tmp-lines "?")
4414               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4415             (gnus-put-text-property
4416              (point)
4417              (progn (eval gnus-summary-line-format-spec) (point))
4418              'gnus-number number)
4419             (when gnus-visual-p
4420               (forward-line -1)
4421               (gnus-run-hooks 'gnus-summary-update-hook)
4422               (forward-line 1))
4423
4424             (setq gnus-tmp-prev-subject subject)))
4425
4426         (when (nth 1 thread)
4427           (push (list (max 0 gnus-tmp-level)
4428                       (copy-list tree-stack)
4429                       (nthcdr 1 thread))
4430                 stack))
4431         (push (if (nth 1 thread) 1 0) tree-stack)
4432         (incf gnus-tmp-level)
4433         (setq threads (if thread-end nil (cdar thread)))
4434         (unless threads
4435           (setq gnus-tmp-level 0)))))
4436   (gnus-message 7 "Generating summary...done"))
4437
4438 (defun gnus-summary-prepare-unthreaded (headers)
4439   "Generate an unthreaded summary buffer based on HEADERS."
4440   (let (header number mark)
4441
4442     (beginning-of-line)
4443
4444     (while headers
4445       ;; We may have to root out some bad articles...
4446       (when (memq (setq number (mail-header-number
4447                                 (setq header (pop headers))))
4448                   gnus-newsgroup-limit)
4449         ;; Mark article as read when it has a low score.
4450         (when (and gnus-summary-mark-below
4451                    (< (or (cdr (assq number gnus-newsgroup-scored))
4452                           gnus-summary-default-score 0)
4453                       gnus-summary-mark-below)
4454                    (not (gnus-summary-article-ancient-p number)))
4455           (setq gnus-newsgroup-unreads
4456                 (delq number gnus-newsgroup-unreads))
4457           (if gnus-newsgroup-auto-expire
4458               (push number gnus-newsgroup-expirable)
4459             (push (cons number gnus-low-score-mark)
4460                   gnus-newsgroup-reads)))
4461
4462         (setq mark (gnus-article-mark number))
4463         (push (gnus-data-make number mark (1+ (point)) header 0)
4464               gnus-newsgroup-data)
4465         (gnus-summary-insert-line
4466          header 0 number
4467          mark (memq number gnus-newsgroup-replied)
4468          (memq number gnus-newsgroup-expirable)
4469          (mail-header-subject header) nil
4470          (cdr (assq number gnus-newsgroup-scored))
4471          (memq number gnus-newsgroup-processable))))))
4472
4473 (defun gnus-summary-remove-list-identifiers ()
4474   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4475   (let ((regexp (if (consp gnus-list-identifiers)
4476                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4477                   gnus-list-identifiers))
4478         changed subject)
4479     (when regexp
4480       (dolist (header gnus-newsgroup-headers)
4481         (setq subject (mail-header-subject header)
4482               changed nil)
4483         (while (string-match
4484                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4485                 subject)
4486           (setq subject
4487                 (concat (substring subject 0 (match-beginning 2))
4488                         (substring subject (match-end 0)))
4489                 changed t))
4490         (when (and changed
4491                    (string-match
4492                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4493           (setq subject
4494                 (concat (substring subject 0 (match-beginning 1))
4495                         (substring subject (match-end 1)))))
4496         (when changed
4497           (mail-header-set-subject header subject))))))
4498
4499 (defun gnus-fetch-headers (articles)
4500   "Fetch headers of ARTICLES."
4501   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4502     (gnus-message 5 "Fetching headers for %s..." name)
4503     (prog1
4504         (if (eq 'nov
4505                 (setq gnus-headers-retrieved-by
4506                       (gnus-retrieve-headers
4507                        articles gnus-newsgroup-name
4508                        ;; We might want to fetch old headers, but
4509                        ;; not if there is only 1 article.
4510                        (and (or (and
4511                                  (not (eq gnus-fetch-old-headers 'some))
4512                                  (not (numberp gnus-fetch-old-headers)))
4513                                 (> (length articles) 1))
4514                             gnus-fetch-old-headers))))
4515             (gnus-get-newsgroup-headers-xover
4516              articles nil nil gnus-newsgroup-name t)
4517           (gnus-get-newsgroup-headers))
4518       (gnus-message 5 "Fetching headers for %s...done" name))))
4519
4520 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4521   "Select newsgroup GROUP.
4522 If READ-ALL is non-nil, all articles in the group are selected.
4523 If SELECT-ARTICLES, only select those articles from GROUP."
4524   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4525          ;;!!! Dirty hack; should be removed.
4526          (gnus-summary-ignore-duplicates
4527           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4528               t
4529             gnus-summary-ignore-duplicates))
4530          (info (nth 2 entry))
4531          articles fetched-articles cached)
4532
4533     (unless (gnus-check-server
4534              (set (make-local-variable 'gnus-current-select-method)
4535                   (gnus-find-method-for-group group)))
4536       (error "Couldn't open server"))
4537
4538     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4539         (gnus-activate-group group)     ; Or we can activate it...
4540         (progn                          ; Or we bug out.
4541           (when (equal major-mode 'gnus-summary-mode)
4542             (kill-buffer (current-buffer)))
4543           (error "Couldn't activate group %s: %s"
4544                  group (gnus-status-message group))))
4545
4546     (unless (gnus-request-group group t)
4547       (when (equal major-mode 'gnus-summary-mode)
4548         (kill-buffer (current-buffer)))
4549       (error "Couldn't request group %s: %s"
4550              group (gnus-status-message group)))
4551
4552     (setq gnus-newsgroup-name group
4553           gnus-newsgroup-unselected nil
4554           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4555     
4556     (let ((display (gnus-group-find-parameter group 'display)))
4557       (setq gnus-newsgroup-display
4558             (cond
4559              ((not (zerop (or (car-safe read-all) 0)))
4560               ;; The user entered the group with C-u SPC/RET, let's show
4561               ;; all articles.
4562               'gnus-not-ignore)
4563              ((eq display 'all)
4564               'gnus-not-ignore)
4565              ((arrayp display)
4566               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4567              ((numberp display)
4568               ;; The following is probably the "correct" solution, but
4569               ;; it makes Gnus fetch all headers and then limit the
4570               ;; articles (which is slow), so instead we hack the
4571               ;; select-articles parameter instead. -- Simon Josefsson
4572               ;; <jas@kth.se>
4573               ;;
4574               ;; (gnus-byte-compile
4575               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4576               ;;                         display)))))
4577               (setq select-articles
4578                     (gnus-uncompress-range
4579                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4580                              (if (> tmp 0)
4581                                  tmp
4582                                1))
4583                            (cdr (gnus-active group)))))
4584               nil)
4585              (t
4586               nil))))
4587       
4588     (gnus-summary-setup-default-charset)
4589
4590     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4591     (when (gnus-virtual-group-p group)
4592       (setq cached gnus-newsgroup-cached))
4593
4594     (setq gnus-newsgroup-unreads
4595           (gnus-set-difference
4596            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4597            gnus-newsgroup-dormant))
4598
4599     (setq gnus-newsgroup-processable nil)
4600
4601     (gnus-update-read-articles group gnus-newsgroup-unreads)
4602
4603     ;; Adjust and set lists of article marks.
4604     (when info
4605       (gnus-adjust-marked-articles info))
4606     
4607     (if (setq articles select-articles)
4608         (setq gnus-newsgroup-unselected
4609               (gnus-sorted-intersection
4610                gnus-newsgroup-unreads
4611                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4612       (setq articles (gnus-articles-to-read group read-all)))
4613
4614     (cond
4615      ((null articles)
4616       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4617       'quit)
4618      ((eq articles 0) nil)
4619      (t
4620       ;; Init the dependencies hash table.
4621       (setq gnus-newsgroup-dependencies
4622             (gnus-make-hashtable (length articles)))
4623       (gnus-set-global-variables)
4624       ;; Retrieve the headers and read them in.
4625       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4626
4627       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4628       (when cached
4629         (setq gnus-newsgroup-cached cached))
4630
4631       ;; Suppress duplicates?
4632       (when gnus-suppress-duplicates
4633         (gnus-dup-suppress-articles))
4634
4635       ;; Set the initial limit.
4636       (setq gnus-newsgroup-limit (copy-sequence articles))
4637       ;; Remove canceled articles from the list of unread articles.
4638       (setq fetched-articles
4639             (mapcar (lambda (headers) (mail-header-number headers))
4640                     gnus-newsgroup-headers))
4641       (setq gnus-newsgroup-articles fetched-articles)
4642       (setq gnus-newsgroup-unreads
4643             (gnus-set-sorted-intersection
4644              gnus-newsgroup-unreads fetched-articles))
4645
4646       (let ((marks (assq 'seen (gnus-info-marks info))))
4647         ;; The `seen' marks are treated specially.
4648         (when (setq gnus-newsgroup-seen (cdr marks))
4649           (dolist (article gnus-newsgroup-articles)
4650             (unless (gnus-member-of-range
4651                      article gnus-newsgroup-seen)
4652               (push article gnus-newsgroup-unseen)))))
4653
4654       ;; Removed marked articles that do not exist.
4655       (gnus-update-missing-marks
4656        (gnus-sorted-complement fetched-articles articles))
4657       ;; We might want to build some more threads first.
4658       (when (and gnus-fetch-old-headers
4659                  (eq gnus-headers-retrieved-by 'nov))
4660         (if (eq gnus-fetch-old-headers 'invisible)
4661             (gnus-build-all-threads)
4662           (gnus-build-old-threads)))
4663       ;; Let the Gnus agent mark articles as read.
4664       (when gnus-agent
4665         (gnus-agent-get-undownloaded-list))
4666       ;; Remove list identifiers from subject
4667       (when gnus-list-identifiers
4668         (gnus-summary-remove-list-identifiers))
4669       ;; Check whether auto-expire is to be done in this group.
4670       (setq gnus-newsgroup-auto-expire
4671             (gnus-group-auto-expirable-p group))
4672       ;; Set up the article buffer now, if necessary.
4673       (unless gnus-single-article-buffer
4674         (gnus-article-setup-buffer))
4675       ;; First and last article in this newsgroup.
4676       (when gnus-newsgroup-headers
4677         (setq gnus-newsgroup-begin
4678               (mail-header-number (car gnus-newsgroup-headers))
4679               gnus-newsgroup-end
4680               (mail-header-number
4681                (gnus-last-element gnus-newsgroup-headers))))
4682       ;; GROUP is successfully selected.
4683       (or gnus-newsgroup-headers t)))))
4684
4685 (defun gnus-summary-display-make-predicate (display)
4686   (require 'gnus-agent)
4687   (when (= (length display) 1)
4688     (setq display (car display)))
4689   (unless gnus-summary-display-cache
4690     (dolist (elem (append (list (cons 'read 'read)
4691                                 (cons 'unseen 'unseen))
4692                           gnus-article-mark-lists))
4693       (push (cons (cdr elem)
4694                   (gnus-byte-compile
4695                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4696             gnus-summary-display-cache)))
4697   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4698     (gnus-get-predicate display)))
4699
4700 ;; Uses the dynamically bound `number' variable.
4701 (defvar number)
4702 (defun gnus-article-marked-p (type &optional article)
4703   (let ((article (or article number)))
4704     (cond
4705      ((eq type 'tick)
4706       (memq article gnus-newsgroup-marked))
4707      ((eq type 'unsend)
4708       (memq article gnus-newsgroup-unsendable))
4709      ((eq type 'undownload)
4710       (memq article gnus-newsgroup-undownloaded))
4711      ((eq type 'download)
4712       (memq article gnus-newsgroup-downloadable))
4713      ((eq type 'unread)
4714       (memq article gnus-newsgroup-unreads))
4715      ((eq type 'read)
4716       (memq article gnus-newsgroup-reads))
4717      ((eq type 'dormant)
4718       (memq article gnus-newsgroup-dormant) )
4719      ((eq type 'expire)
4720       (memq article gnus-newsgroup-expirable))
4721      ((eq type 'reply)
4722       (memq article gnus-newsgroup-replied))
4723      ((eq type 'killed)
4724       (memq article gnus-newsgroup-killed))
4725      ((eq type 'bookmark)
4726       (assq article gnus-newsgroup-bookmarks))
4727      ((eq type 'score)
4728       (assq article gnus-newsgroup-scored))
4729      ((eq type 'save)
4730       (memq article gnus-newsgroup-saved))
4731      ((eq type 'cache)
4732       (memq article gnus-newsgroup-cached))
4733      ((eq type 'forward)
4734       (memq article gnus-newsgroup-forwarded))
4735      ((eq type 'seen)
4736       (not (memq article gnus-newsgroup-unseen)))
4737      ((eq type 'recent)
4738       (memq article gnus-newsgroup-recent))
4739      (t t))))
4740
4741 (defun gnus-articles-to-read (group &optional read-all)
4742   "Find out what articles the user wants to read."
4743   (let* ((articles
4744           ;; Select all articles if `read-all' is non-nil, or if there
4745           ;; are no unread articles.
4746           (if (or read-all
4747                   (and (zerop (length gnus-newsgroup-marked))
4748                        (zerop (length gnus-newsgroup-unreads)))
4749                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4750               ;; We want to select the headers for all the articles in
4751               ;; the group, so we select either all the active
4752               ;; articles in the group, or (if that's nil), the
4753               ;; articles in the cache.
4754               (or
4755                (gnus-uncompress-range (gnus-active group))
4756                (gnus-cache-articles-in-group group))
4757             ;; Select only the "normal" subset of articles.
4758             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4759                           (copy-sequence gnus-newsgroup-unreads))
4760                   '<)))
4761          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4762          (scored (length scored-list))
4763          (number (length articles))
4764          (marked (+ (length gnus-newsgroup-marked)
4765                     (length gnus-newsgroup-dormant)))
4766          (select
4767           (cond
4768            ((numberp read-all)
4769             read-all)
4770            (t
4771             (condition-case ()
4772                 (cond
4773                  ((and (or (<= scored marked) (= scored number))
4774                        (numberp gnus-large-newsgroup)
4775                        (> number gnus-large-newsgroup))
4776                   (let ((input
4777                          (read-string
4778                           (format
4779                            "How many articles from %s (default %d): "
4780                            (gnus-limit-string
4781                             (gnus-group-decoded-name gnus-newsgroup-name)
4782                             35)
4783                            number))))
4784                     (if (string-match "^[ \t]*$" input) number input)))
4785                  ((and (> scored marked) (< scored number)
4786                        (> (- scored number) 20))
4787                   (let ((input
4788                          (read-string
4789                           (format "%s %s (%d scored, %d total): "
4790                                   "How many articles from"
4791                                   (gnus-group-decoded-name group)
4792                                   scored number))))
4793                     (if (string-match "^[ \t]*$" input)
4794                         number input)))
4795                  (t number))
4796               (quit
4797                (message "Quit getting the articles to read")
4798                nil))))))
4799     (setq select (if (stringp select) (string-to-number select) select))
4800     (if (or (null select) (zerop select))
4801         select
4802       (if (and (not (zerop scored)) (<= (abs select) scored))
4803           (progn
4804             (setq articles (sort scored-list '<))
4805             (setq number (length articles)))
4806         (setq articles (copy-sequence articles)))
4807
4808       (when (< (abs select) number)
4809         (if (< select 0)
4810             ;; Select the N oldest articles.
4811             (setcdr (nthcdr (1- (abs select)) articles) nil)
4812           ;; Select the N most recent articles.
4813           (setq articles (nthcdr (- number select) articles))))
4814       (setq gnus-newsgroup-unselected
4815             (gnus-sorted-intersection
4816              gnus-newsgroup-unreads
4817              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4818       (when gnus-alter-articles-to-read-function
4819         (setq gnus-newsgroup-unreads
4820               (sort
4821                (funcall gnus-alter-articles-to-read-function
4822                         gnus-newsgroup-name gnus-newsgroup-unreads)
4823                '<)))
4824       articles)))
4825
4826 (defun gnus-killed-articles (killed articles)
4827   (let (out)
4828     (while articles
4829       (when (inline (gnus-member-of-range (car articles) killed))
4830         (push (car articles) out))
4831       (setq articles (cdr articles)))
4832     out))
4833
4834 (defun gnus-uncompress-marks (marks)
4835   "Uncompress the mark ranges in MARKS."
4836   (let ((uncompressed '(score bookmark))
4837         out)
4838     (while marks
4839       (if (memq (caar marks) uncompressed)
4840           (push (car marks) out)
4841         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4842       (setq marks (cdr marks)))
4843     out))
4844
4845 (defun gnus-article-mark-to-type (mark)
4846   "Return the type of MARK."
4847   (or (cadr (assq mark gnus-article-special-mark-lists))
4848       'list))
4849
4850 (defun gnus-article-unpropagatable-p (mark)
4851   "Return whether MARK should be propagated to backend."
4852   (memq mark gnus-article-unpropagated-mark-lists))
4853
4854 (defun gnus-adjust-marked-articles (info)
4855   "Set all article lists and remove all marks that are no longer valid."
4856   (let* ((marked-lists (gnus-info-marks info))
4857          (active (gnus-active (gnus-info-group info)))
4858          (min (car active))
4859          (max (cdr active))
4860          (types gnus-article-mark-lists)
4861          marks var articles article mark mark-type)
4862
4863     (dolist (marks marked-lists)
4864       (setq mark (car marks)
4865             mark-type (gnus-article-mark-to-type mark)
4866             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4867
4868       ;; We set the variable according to the type of the marks list,
4869       ;; and then adjust the marks to a subset of the active articles.
4870       (cond
4871        ;; Adjust "simple" lists.
4872        ((eq mark-type 'list)
4873         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4874         (when (memq mark '(tick dormant expire reply save))
4875           (while articles
4876             (when (or (< (setq article (pop articles)) min) (> article max))
4877               (set var (delq article (symbol-value var)))))))
4878        ;; Adjust assocs.
4879        ((eq mark-type 'tuple)
4880         (set var (setq articles (cdr marks)))
4881         (when (not (listp (cdr (symbol-value var))))
4882           (set var (list (symbol-value var))))
4883         (when (not (listp (cdr articles)))
4884           (setq articles (list articles)))
4885         (while articles
4886           (when (or (not (consp (setq article (pop articles))))
4887                     (< (car article) min)
4888                     (> (car article) max))
4889             (set var (delq article (symbol-value var))))))
4890        ((eq mark-type 'range)
4891         (cond
4892          ((eq mark 'seen))))))))
4893
4894 (defun gnus-update-missing-marks (missing)
4895   "Go through the list of MISSING articles and remove them from the mark lists."
4896   (when missing
4897     (let (var m)
4898       ;; Go through all types.
4899       (dolist (elem gnus-article-mark-lists)
4900         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4901           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4902           (when (symbol-value var)
4903             ;; This list has articles.  So we delete all missing
4904             ;; articles from it.
4905             (setq m missing)
4906             (while m
4907               (set var (delq (pop m) (symbol-value var))))))))))
4908
4909 (defun gnus-update-marks ()
4910   "Enter the various lists of marked articles into the newsgroup info list."
4911   (let ((types gnus-article-mark-lists)
4912         (info (gnus-get-info gnus-newsgroup-name))
4913         type list newmarked symbol delta-marks)
4914     (when info
4915       ;; Add all marks lists to the list of marks lists.
4916       (while (setq type (pop types))
4917         (setq list (symbol-value
4918                     (setq symbol
4919                           (intern (format "gnus-newsgroup-%s" (car type))))))
4920
4921         (when list
4922           ;; Get rid of the entries of the articles that have the
4923           ;; default score.
4924           (when (and (eq (cdr type) 'score)
4925                      gnus-save-score
4926                      list)
4927             (let* ((arts list)
4928                    (prev (cons nil list))
4929                    (all prev))
4930               (while arts
4931                 (if (or (not (consp (car arts)))
4932                         (= (cdar arts) gnus-summary-default-score))
4933                     (setcdr prev (cdr arts))
4934                   (setq prev arts))
4935                 (setq arts (cdr arts)))
4936               (setq list (cdr all)))))
4937
4938         (when (eq (cdr type) 'seen)
4939           (setq list 
4940                 (if list
4941                     (gnus-add-to-range list gnus-newsgroup-unseen)
4942                   (gnus-compress-sequence gnus-newsgroup-articles))))
4943
4944         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4945           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4946
4947         (when (and (gnus-check-backend-function
4948                     'request-set-mark gnus-newsgroup-name)
4949                    (not (gnus-article-unpropagatable-p (cdr type))))
4950           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4951                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4952                  (add (gnus-remove-from-range
4953                        (gnus-copy-sequence list) old)))
4954             (when add
4955               (push (list add 'add (list (cdr type))) delta-marks))
4956             (when del
4957               (push (list del 'del (list (cdr type))) delta-marks))))
4958
4959         (when list
4960           (push (cons (cdr type) list) newmarked)))
4961
4962       (when delta-marks
4963         (unless (gnus-check-group gnus-newsgroup-name)
4964           (error "Can't open server for %s" gnus-newsgroup-name))
4965         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4966
4967       ;; Enter these new marks into the info of the group.
4968       (if (nthcdr 3 info)
4969           (setcar (nthcdr 3 info) newmarked)
4970         ;; Add the marks lists to the end of the info.
4971         (when newmarked
4972           (setcdr (nthcdr 2 info) (list newmarked))))
4973
4974       ;; Cut off the end of the info if there's nothing else there.
4975       (let ((i 5))
4976         (while (and (> i 2)
4977                     (not (nth i info)))
4978           (when (nthcdr (decf i) info)
4979             (setcdr (nthcdr i info) nil)))))))
4980
4981 (defun gnus-set-mode-line (where)
4982   "Set the mode line of the article or summary buffers.
4983 If WHERE is `summary', the summary mode line format will be used."
4984   ;; Is this mode line one we keep updated?
4985   (when (and (memq where gnus-updated-mode-lines)
4986              (symbol-value
4987               (intern (format "gnus-%s-mode-line-format-spec" where))))
4988     (let (mode-string)
4989       (save-excursion
4990         ;; We evaluate this in the summary buffer since these
4991         ;; variables are buffer-local to that buffer.
4992         (set-buffer gnus-summary-buffer)
4993        ;; We bind all these variables that are used in the `eval' form
4994         ;; below.
4995         (let* ((mformat (symbol-value
4996                          (intern
4997                           (format "gnus-%s-mode-line-format-spec" where))))
4998                (gnus-tmp-group-name (gnus-group-decoded-name
4999                                      gnus-newsgroup-name))
5000                (gnus-tmp-article-number (or gnus-current-article 0))
5001                (gnus-tmp-unread gnus-newsgroup-unreads)
5002                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5003                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5004                (gnus-tmp-unread-and-unselected
5005                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5006                             (zerop gnus-tmp-unselected))
5007                        "")
5008                       ((zerop gnus-tmp-unselected)
5009                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5010                       (t (format "{%d(+%d) more}"
5011                                  gnus-tmp-unread-and-unticked
5012                                  gnus-tmp-unselected))))
5013                (gnus-tmp-subject
5014                 (if (and gnus-current-headers
5015                          (vectorp gnus-current-headers))
5016                     (gnus-mode-string-quote
5017                      (mail-header-subject gnus-current-headers))
5018                   ""))
5019                bufname-length max-len
5020                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5021           (setq mode-string (eval mformat))
5022           (setq bufname-length (if (string-match "%b" mode-string)
5023                                    (- (length
5024                                        (buffer-name
5025                                         (if (eq where 'summary)
5026                                             nil
5027                                           (get-buffer gnus-article-buffer))))
5028                                       2)
5029                                  0))
5030           (setq max-len (max 4 (if gnus-mode-non-string-length
5031                                    (- (window-width)
5032                                       gnus-mode-non-string-length
5033                                       bufname-length)
5034                                  (length mode-string))))
5035           ;; We might have to chop a bit of the string off...
5036           (when (> (length mode-string) max-len)
5037             (setq mode-string
5038                   (concat (truncate-string-to-width mode-string (- max-len 3))
5039                           "...")))
5040           ;; Pad the mode string a bit.
5041           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5042       ;; Update the mode line.
5043       (setq mode-line-buffer-identification
5044             (gnus-mode-line-buffer-identification (list mode-string)))
5045       (set-buffer-modified-p t))))
5046
5047 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5048   "Go through the HEADERS list and add all Xrefs to a hash table.
5049 The resulting hash table is returned, or nil if no Xrefs were found."
5050   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5051          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5052          (xref-hashtb (gnus-make-hashtable))
5053          start group entry number xrefs header)
5054     (while headers
5055       (setq header (pop headers))
5056       (when (and (setq xrefs (mail-header-xref header))
5057                  (not (memq (setq number (mail-header-number header))
5058                             unreads)))
5059         (setq start 0)
5060         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5061           (setq start (match-end 0))
5062           (setq group (if prefix
5063                           (concat prefix (substring xrefs (match-beginning 1)
5064                                                     (match-end 1)))
5065                         (substring xrefs (match-beginning 1) (match-end 1))))
5066           (setq number
5067                 (string-to-int (substring xrefs (match-beginning 2)
5068                                           (match-end 2))))
5069           (if (setq entry (gnus-gethash group xref-hashtb))
5070               (setcdr entry (cons number (cdr entry)))
5071             (gnus-sethash group (cons number nil) xref-hashtb)))))
5072     (and start xref-hashtb)))
5073
5074 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5075   "Look through all the headers and mark the Xrefs as read."
5076   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5077         name entry info xref-hashtb idlist method nth4)
5078     (save-excursion
5079       (set-buffer gnus-group-buffer)
5080       (when (setq xref-hashtb
5081                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5082         (mapatoms
5083          (lambda (group)
5084            (unless (string= from-newsgroup (setq name (symbol-name group)))
5085              (setq idlist (symbol-value group))
5086              ;; Dead groups are not updated.
5087              (and (prog1
5088                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5089                             info (nth 2 entry))
5090                     (when (stringp (setq nth4 (gnus-info-method info)))
5091                       (setq nth4 (gnus-server-to-method nth4))))
5092                   ;; Only do the xrefs if the group has the same
5093                   ;; select method as the group we have just read.
5094                   (or (gnus-methods-equal-p
5095                        nth4 (gnus-find-method-for-group from-newsgroup))
5096                       virtual
5097                       (equal nth4 (setq method (gnus-find-method-for-group
5098                                                 from-newsgroup)))
5099                       (and (equal (car nth4) (car method))
5100                            (equal (nth 1 nth4) (nth 1 method))))
5101                   gnus-use-cross-reference
5102                   (or (not (eq gnus-use-cross-reference t))
5103                       virtual
5104                       ;; Only do cross-references on subscribed
5105                       ;; groups, if that is what is wanted.
5106                       (<= (gnus-info-level info) gnus-level-subscribed))
5107                   (gnus-group-make-articles-read name idlist))))
5108          xref-hashtb)))))
5109
5110 (defun gnus-compute-read-articles (group articles)
5111   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5112          (info (nth 2 entry))
5113          (active (gnus-active group))
5114          ninfo)
5115     (when entry
5116       ;; First peel off all invalid article numbers.
5117       (when active
5118         (let ((ids articles)
5119               id first)
5120           (while (setq id (pop ids))
5121             (when (and first (> id (cdr active)))
5122               ;; We'll end up in this situation in one particular
5123               ;; obscure situation.  If you re-scan a group and get
5124               ;; a new article that is cross-posted to a different
5125               ;; group that has not been re-scanned, you might get
5126               ;; crossposted article that has a higher number than
5127               ;; Gnus believes possible.  So we re-activate this
5128               ;; group as well.  This might mean doing the
5129               ;; crossposting thingy will *increase* the number
5130               ;; of articles in some groups.  Tsk, tsk.
5131               (setq active (or (gnus-activate-group group) active)))
5132             (when (or (> id (cdr active))
5133                       (< id (car active)))
5134               (setq articles (delq id articles))))))
5135       ;; If the read list is nil, we init it.
5136       (if (and active
5137                (null (gnus-info-read info))
5138                (> (car active) 1))
5139           (setq ninfo (cons 1 (1- (car active))))
5140         (setq ninfo (gnus-info-read info)))
5141       ;; Then we add the read articles to the range.
5142       (gnus-add-to-range
5143        ninfo (setq articles (sort articles '<))))))
5144
5145 (defun gnus-group-make-articles-read (group articles)
5146   "Update the info of GROUP to say that ARTICLES are read."
5147   (let* ((num 0)
5148          (entry (gnus-gethash group gnus-newsrc-hashtb))
5149          (info (nth 2 entry))
5150          (active (gnus-active group))
5151          range)
5152     (when entry
5153       (setq range (gnus-compute-read-articles group articles))
5154       (save-excursion
5155         (set-buffer gnus-group-buffer)
5156         (gnus-undo-register
5157           `(progn
5158              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5159              (gnus-info-set-read ',info ',(gnus-info-read info))
5160              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5161              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5162              (gnus-group-update-group ,group t))))
5163       ;; Add the read articles to the range.
5164       (gnus-info-set-read info range)
5165       (gnus-request-set-mark group (list (list range 'add '(read))))
5166       ;; Then we have to re-compute how many unread
5167       ;; articles there are in this group.
5168       (when active
5169         (cond
5170          ((not range)
5171           (setq num (- (1+ (cdr active)) (car active))))
5172          ((not (listp (cdr range)))
5173           (setq num (- (cdr active) (- (1+ (cdr range))
5174                                        (car range)))))
5175          (t
5176           (while range
5177             (if (numberp (car range))
5178                 (setq num (1+ num))
5179               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5180             (setq range (cdr range)))
5181           (setq num (- (cdr active) num))))
5182         ;; Update the number of unread articles.
5183         (setcar entry num)
5184         ;; Update the group buffer.
5185         (gnus-group-update-group group t)))))
5186
5187 (defvar gnus-newsgroup-none-id 0)
5188
5189 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5190   (let ((cur nntp-server-buffer)
5191         (dependencies
5192          (or dependencies
5193              (save-excursion (set-buffer gnus-summary-buffer)
5194                              gnus-newsgroup-dependencies)))
5195         headers id end ref
5196         (mail-parse-charset gnus-newsgroup-charset)
5197         (mail-parse-ignored-charsets
5198          (save-excursion (condition-case nil
5199                              (set-buffer gnus-summary-buffer)
5200                            (error))
5201                          gnus-newsgroup-ignored-charsets)))
5202     (save-excursion
5203       (set-buffer nntp-server-buffer)
5204       ;; Translate all TAB characters into SPACE characters.
5205       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5206       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5207       (gnus-run-hooks 'gnus-parse-headers-hook)
5208       (let ((case-fold-search t)
5209             in-reply-to header p lines chars)
5210         (goto-char (point-min))
5211         ;; Search to the beginning of the next header.  Error messages
5212         ;; do not begin with 2 or 3.
5213         (while (re-search-forward "^[23][0-9]+ " nil t)
5214           (setq id nil
5215                 ref nil)
5216           ;; This implementation of this function, with nine
5217           ;; search-forwards instead of the one re-search-forward and
5218           ;; a case (which basically was the old function) is actually
5219           ;; about twice as fast, even though it looks messier.  You
5220           ;; can't have everything, I guess.  Speed and elegance
5221           ;; doesn't always go hand in hand.
5222           (setq
5223            header
5224            (vector
5225             ;; Number.
5226             (prog1
5227                 (read cur)
5228               (end-of-line)
5229               (setq p (point))
5230               (narrow-to-region (point)
5231                                 (or (and (search-forward "\n.\n" nil t)
5232                                          (- (point) 2))
5233                                     (point))))
5234             ;; Subject.
5235             (progn
5236               (goto-char p)
5237               (if (search-forward "\nsubject:" nil t)
5238                   (funcall gnus-decode-encoded-word-function
5239                            (nnheader-header-value))
5240                 "(none)"))
5241             ;; From.
5242             (progn
5243               (goto-char p)
5244               (if (search-forward "\nfrom:" nil t)
5245                   (funcall gnus-decode-encoded-word-function
5246                            (nnheader-header-value))
5247                 "(nobody)"))
5248             ;; Date.
5249             (progn
5250               (goto-char p)
5251               (if (search-forward "\ndate:" nil t)
5252                   (nnheader-header-value) ""))
5253             ;; Message-ID.
5254             (progn
5255               (goto-char p)
5256               (setq id (if (re-search-forward
5257                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5258                       ;; We do it this way to make sure the Message-ID
5259                            ;; is (somewhat) syntactically valid.
5260                            (buffer-substring (match-beginning 1)
5261                                              (match-end 1))
5262                        ;; If there was no message-id, we just fake one
5263                          ;; to make subsequent routines simpler.
5264                          (nnheader-generate-fake-message-id))))
5265             ;; References.
5266             (progn
5267               (goto-char p)
5268               (if (search-forward "\nreferences:" nil t)
5269                   (progn
5270                     (setq end (point))
5271                     (prog1
5272                         (nnheader-header-value)
5273                       (setq ref
5274                             (buffer-substring
5275                              (progn
5276                                (end-of-line)
5277                                (search-backward ">" end t)
5278                                (1+ (point)))
5279                              (progn
5280                                (search-backward "<" end t)
5281                                (point))))))
5282             ;; Get the references from the in-reply-to header if there
5283                 ;; were no references and the in-reply-to header looks
5284                 ;; promising.
5285                 (if (and (search-forward "\nin-reply-to:" nil t)
5286                          (setq in-reply-to (nnheader-header-value))
5287                          (string-match "<[^>]+>" in-reply-to))
5288                     (let (ref2)
5289                       (setq ref (substring in-reply-to (match-beginning 0)
5290                                            (match-end 0)))
5291                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5292                         (setq ref2 (substring in-reply-to (match-beginning 0)
5293                                               (match-end 0)))
5294                         (when (> (length ref2) (length ref))
5295                           (setq ref ref2)))
5296                       ref)
5297                   (setq ref nil))))
5298             ;; Chars.
5299             (progn
5300               (goto-char p)
5301               (if (search-forward "\nchars: " nil t)
5302                   (if (numberp (setq chars (ignore-errors (read cur))))
5303                       chars -1)
5304                 -1))
5305             ;; Lines.
5306             (progn
5307               (goto-char p)
5308               (if (search-forward "\nlines: " nil t)
5309                   (if (numberp (setq lines (ignore-errors (read cur))))
5310                       lines -1)
5311                 -1))
5312             ;; Xref.
5313             (progn
5314               (goto-char p)
5315               (and (search-forward "\nxref:" nil t)
5316                    (nnheader-header-value)))
5317             ;; Extra.
5318             (when gnus-extra-headers
5319               (let ((extra gnus-extra-headers)
5320                     out)
5321                 (while extra
5322                   (goto-char p)
5323                   (when (search-forward
5324                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5325                     (push (cons (car extra) (nnheader-header-value))
5326                           out))
5327                   (pop extra))
5328                 out))))
5329           (when (equal id ref)
5330             (setq ref nil))
5331
5332           (when gnus-alter-header-function
5333             (funcall gnus-alter-header-function header)
5334             (setq id (mail-header-id header)
5335                   ref (gnus-parent-id (mail-header-references header))))
5336
5337           (when (setq header
5338                       (gnus-dependencies-add-header
5339                        header dependencies force-new))
5340             (push header headers))
5341           (goto-char (point-max))
5342           (widen))
5343         (nreverse headers)))))
5344
5345 ;; Goes through the xover lines and returns a list of vectors
5346 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5347                                                   force-new dependencies
5348                                                   group also-fetch-heads)
5349   "Parse the news overview data in the server buffer.
5350 Return a list of headers that match SEQUENCE (see
5351 `nntp-retrieve-headers')."
5352   ;; Get the Xref when the users reads the articles since most/some
5353   ;; NNTP servers do not include Xrefs when using XOVER.
5354   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5355   (let ((mail-parse-charset gnus-newsgroup-charset)
5356         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5357         (cur nntp-server-buffer)
5358         (dependencies (or dependencies gnus-newsgroup-dependencies))
5359         (allp (cond
5360                ((eq gnus-read-all-available-headers t)
5361                 t)
5362                ((stringp gnus-read-all-available-headers)
5363                 (string-match gnus-read-all-available-headers group))
5364                (t
5365                 nil)))
5366         number headers header)
5367     (save-excursion
5368       (set-buffer nntp-server-buffer)
5369       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5370       ;; Allow the user to mangle the headers before parsing them.
5371       (gnus-run-hooks 'gnus-parse-headers-hook)
5372       (goto-char (point-min))
5373       (while (not (eobp))
5374         (condition-case ()
5375             (while (and (or sequence allp)
5376                         (not (eobp)))
5377               (setq number (read cur))
5378               (when (not allp)
5379                 (while (and sequence
5380                             (< (car sequence) number))
5381                   (setq sequence (cdr sequence))))
5382               (when (and (or allp
5383                              (and sequence
5384                                   (eq number (car sequence))))
5385                          (progn
5386                            (setq sequence (cdr sequence))
5387                            (setq header (inline
5388                                           (gnus-nov-parse-line
5389                                            number dependencies force-new)))))
5390                 (push header headers))
5391               (forward-line 1))
5392           (error
5393            (gnus-error 4 "Strange nov line (%d)"
5394                        (count-lines (point-min) (point)))))
5395         (forward-line 1))
5396       ;; A common bug in inn is that if you have posted an article and
5397       ;; then retrieves the active file, it will answer correctly --
5398       ;; the new article is included.  However, a NOV entry for the
5399       ;; article may not have been generated yet, so this may fail.
5400       ;; We work around this problem by retrieving the last few
5401       ;; headers using HEAD.
5402       (if (or (not also-fetch-heads)
5403               (not sequence))
5404           ;; We (probably) got all the headers.
5405           (nreverse headers)
5406         (let ((gnus-nov-is-evil t))
5407           (nconc
5408            (nreverse headers)
5409            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5410              (gnus-get-newsgroup-headers))))))))
5411
5412 (defun gnus-article-get-xrefs ()
5413   "Fill in the Xref value in `gnus-current-headers', if necessary.
5414 This is meant to be called in `gnus-article-internal-prepare-hook'."
5415   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5416                                  gnus-current-headers)))
5417     (or (not gnus-use-cross-reference)
5418         (not headers)
5419         (and (mail-header-xref headers)
5420              (not (string= (mail-header-xref headers) "")))
5421         (let ((case-fold-search t)
5422               xref)
5423           (save-restriction
5424             (nnheader-narrow-to-headers)
5425             (goto-char (point-min))
5426             (when (or (and (not (eobp))
5427                            (eq (downcase (char-after)) ?x)
5428                            (looking-at "Xref:"))
5429                       (search-forward "\nXref:" nil t))
5430               (goto-char (1+ (match-end 0)))
5431               (setq xref (buffer-substring (point)
5432                                            (progn (end-of-line) (point))))
5433               (mail-header-set-xref headers xref)))))))
5434
5435 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5436   "Find article ID and insert the summary line for that article.
5437 OLD-HEADER can either be a header or a line number to insert
5438 the subject line on."
5439   (let* ((line (and (numberp old-header) old-header))
5440          (old-header (and (vectorp old-header) old-header))
5441          (header (cond ((and old-header use-old-header)
5442                         old-header)
5443                        ((and (numberp id)
5444                              (gnus-number-to-header id))
5445                         (gnus-number-to-header id))
5446                        (t
5447                         (gnus-read-header id))))
5448          (number (and (numberp id) id))
5449          d)
5450     (when header
5451       ;; Rebuild the thread that this article is part of and go to the
5452       ;; article we have fetched.
5453       (when (and (not gnus-show-threads)
5454                  old-header)
5455         (when (and number
5456                    (setq d (gnus-data-find (mail-header-number old-header))))
5457           (goto-char (gnus-data-pos d))
5458           (gnus-data-remove
5459            number
5460            (- (gnus-point-at-bol)
5461               (prog1
5462                   (1+ (gnus-point-at-eol))
5463                 (gnus-delete-line))))))
5464       (when old-header
5465         (mail-header-set-number header (mail-header-number old-header)))
5466       (setq gnus-newsgroup-sparse
5467             (delq (setq number (mail-header-number header))
5468                   gnus-newsgroup-sparse))
5469       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5470       (push number gnus-newsgroup-limit)
5471       (gnus-rebuild-thread (mail-header-id header) line)
5472       (gnus-summary-goto-subject number nil t))
5473     (when (and (numberp number)
5474                (> number 0))
5475       ;; We have to update the boundaries even if we can't fetch the
5476       ;; article if ID is a number -- so that the next `P' or `N'
5477       ;; command will fetch the previous (or next) article even
5478       ;; if the one we tried to fetch this time has been canceled.
5479       (when (> number gnus-newsgroup-end)
5480         (setq gnus-newsgroup-end number))
5481       (when (< number gnus-newsgroup-begin)
5482         (setq gnus-newsgroup-begin number))
5483       (setq gnus-newsgroup-unselected
5484             (delq number gnus-newsgroup-unselected)))
5485     ;; Report back a success?
5486     (and header (mail-header-number header))))
5487
5488 ;;; Process/prefix in the summary buffer
5489
5490 (defun gnus-summary-work-articles (n)
5491   "Return a list of articles to be worked upon.
5492 The prefix argument, the list of process marked articles, and the
5493 current article will be taken into consideration."
5494   (save-excursion
5495     (set-buffer gnus-summary-buffer)
5496     (cond
5497      (n
5498       ;; A numerical prefix has been given.
5499       (setq n (prefix-numeric-value n))
5500       (let ((backward (< n 0))
5501             (n (abs (prefix-numeric-value n)))
5502             articles article)
5503         (save-excursion
5504           (while
5505               (and (> n 0)
5506                    (push (setq article (gnus-summary-article-number))
5507                          articles)
5508                    (if backward
5509                        (gnus-summary-find-prev nil article)
5510                      (gnus-summary-find-next nil article)))
5511             (decf n)))
5512         (nreverse articles)))
5513      ((and (gnus-region-active-p) (mark))
5514       (message "region active")
5515       ;; Work on the region between point and mark.
5516       (let ((max (max (point) (mark)))
5517             articles article)
5518         (save-excursion
5519           (goto-char (min (min (point) (mark))))
5520           (while
5521               (and
5522                (push (setq article (gnus-summary-article-number)) articles)
5523                (gnus-summary-find-next nil article)
5524                (< (point) max)))
5525           (nreverse articles))))
5526      (gnus-newsgroup-processable
5527       ;; There are process-marked articles present.
5528       ;; Save current state.
5529       (gnus-summary-save-process-mark)
5530       ;; Return the list.
5531       (reverse gnus-newsgroup-processable))
5532      (t
5533       ;; Just return the current article.
5534       (list (gnus-summary-article-number))))))
5535
5536 (defmacro gnus-summary-iterate (arg &rest forms)
5537   "Iterate over the process/prefixed articles and do FORMS.
5538 ARG is the interactive prefix given to the command.  FORMS will be
5539 executed with point over the summary line of the articles."
5540   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5541     `(let ((,articles (gnus-summary-work-articles ,arg)))
5542        (while ,articles
5543          (gnus-summary-goto-subject (car ,articles))
5544          ,@forms
5545          (pop ,articles)))))
5546
5547 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5548 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5549
5550 (defun gnus-summary-save-process-mark ()
5551   "Push the current set of process marked articles on the stack."
5552   (interactive)
5553   (push (copy-sequence gnus-newsgroup-processable)
5554         gnus-newsgroup-process-stack))
5555
5556 (defun gnus-summary-kill-process-mark ()
5557   "Push the current set of process marked articles on the stack and unmark."
5558   (interactive)
5559   (gnus-summary-save-process-mark)
5560   (gnus-summary-unmark-all-processable))
5561
5562 (defun gnus-summary-yank-process-mark ()
5563   "Pop the last process mark state off the stack and restore it."
5564   (interactive)
5565   (unless gnus-newsgroup-process-stack
5566     (error "Empty mark stack"))
5567   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5568
5569 (defun gnus-summary-process-mark-set (set)
5570   "Make SET into the current process marked articles."
5571   (gnus-summary-unmark-all-processable)
5572   (while set
5573     (gnus-summary-set-process-mark (pop set))))
5574
5575 ;;; Searching and stuff
5576
5577 (defun gnus-summary-search-group (&optional backward use-level)
5578   "Search for next unread newsgroup.
5579 If optional argument BACKWARD is non-nil, search backward instead."
5580   (save-excursion
5581     (set-buffer gnus-group-buffer)
5582     (when (gnus-group-search-forward
5583            backward nil (if use-level (gnus-group-group-level) nil))
5584       (gnus-group-group-name))))
5585
5586 (defun gnus-summary-best-group (&optional exclude-group)
5587   "Find the name of the best unread group.
5588 If EXCLUDE-GROUP, do not go to this group."
5589   (save-excursion
5590     (set-buffer gnus-group-buffer)
5591     (save-excursion
5592       (gnus-group-best-unread-group exclude-group))))
5593
5594 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5595   (if backward (gnus-summary-find-prev)
5596     (let* ((dummy (gnus-summary-article-intangible-p))
5597            (article (or article (gnus-summary-article-number)))
5598            (arts (gnus-data-find-list article))
5599            result)
5600       (when (and (not dummy)
5601                  (or (not gnus-summary-check-current)
5602                      (not unread)
5603                      (not (gnus-data-unread-p (car arts)))))
5604         (setq arts (cdr arts)))
5605       (when (setq result
5606                   (if unread
5607                       (progn
5608                         (while arts
5609                           (when (or (and undownloaded
5610                                          (eq gnus-undownloaded-mark
5611                                              (gnus-data-mark (car arts))))
5612                                     (gnus-data-unread-p (car arts)))
5613                             (setq result (car arts)
5614                                   arts nil))
5615                           (setq arts (cdr arts)))
5616                         result)
5617                     (car arts)))
5618         (goto-char (gnus-data-pos result))
5619         (gnus-data-number result)))))
5620
5621 (defun gnus-summary-find-prev (&optional unread article)
5622   (let* ((eobp (eobp))
5623          (article (or article (gnus-summary-article-number)))
5624          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5625          result)
5626     (when (and (not eobp)
5627                (or (not gnus-summary-check-current)
5628                    (not unread)
5629                    (not (gnus-data-unread-p (car arts)))))
5630       (setq arts (cdr arts)))
5631     (when (setq result
5632                 (if unread
5633                     (progn
5634                       (while arts
5635                         (when (gnus-data-unread-p (car arts))
5636                           (setq result (car arts)
5637                                 arts nil))
5638                         (setq arts (cdr arts)))
5639                       result)
5640                   (car arts)))
5641       (goto-char (gnus-data-pos result))
5642       (gnus-data-number result))))
5643
5644 (defun gnus-summary-find-subject (subject &optional unread backward article)
5645   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5646          (article (or article (gnus-summary-article-number)))
5647          (articles (gnus-data-list backward))
5648          (arts (gnus-data-find-list article articles))
5649          result)
5650     (when (or (not gnus-summary-check-current)
5651               (not unread)
5652               (not (gnus-data-unread-p (car arts))))
5653       (setq arts (cdr arts)))
5654     (while arts
5655       (and (or (not unread)
5656                (gnus-data-unread-p (car arts)))
5657            (vectorp (gnus-data-header (car arts)))
5658            (gnus-subject-equal
5659             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5660            (setq result (car arts)
5661                  arts nil))
5662       (setq arts (cdr arts)))
5663     (and result
5664          (goto-char (gnus-data-pos result))
5665          (gnus-data-number result))))
5666
5667 (defun gnus-summary-search-forward (&optional unread subject backward)
5668   "Search forward for an article.
5669 If UNREAD, look for unread articles.  If SUBJECT, look for
5670 articles with that subject.  If BACKWARD, search backward instead."
5671   (cond (subject (gnus-summary-find-subject subject unread backward))
5672         (backward (gnus-summary-find-prev unread))
5673         (t (gnus-summary-find-next unread))))
5674
5675 (defun gnus-recenter (&optional n)
5676   "Center point in window and redisplay frame.
5677 Also do horizontal recentering."
5678   (interactive "P")
5679   (when (and gnus-auto-center-summary
5680              (not (eq gnus-auto-center-summary 'vertical)))
5681     (gnus-horizontal-recenter))
5682   (recenter n))
5683
5684 (defun gnus-summary-recenter ()
5685   "Center point in the summary window.
5686 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5687 displayed, no centering will be performed."
5688   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5689 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5690   (interactive)
5691   (let* ((top (cond ((< (window-height) 4) 0)
5692                     ((< (window-height) 7) 1)
5693                     (t (if (numberp gnus-auto-center-summary)
5694                            gnus-auto-center-summary
5695                          2))))
5696          (height (1- (window-height)))
5697          (bottom (save-excursion (goto-char (point-max))
5698                                  (forward-line (- height))
5699                                  (point)))
5700          (window (get-buffer-window (current-buffer))))
5701     ;; The user has to want it.
5702     (when gnus-auto-center-summary
5703       (when (get-buffer-window gnus-article-buffer)
5704         ;; Only do recentering when the article buffer is displayed,
5705       ;; Set the window start to either `bottom', which is the biggest
5706         ;; possible valid number, or the second line from the top,
5707         ;; whichever is the least.
5708         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5709           (if (> bottom top-pos)
5710               ;; Keep the second line from the top visible
5711               (set-window-start window top-pos t)
5712             ;; Try to keep the bottom line visible; if it's partially
5713             ;; obscured, either scroll one more line to make it fully
5714             ;; visible, or revert to using TOP-POS.
5715             (save-excursion
5716               (goto-char (point-max))
5717               (forward-line -1)
5718               (let ((last-line-start (point)))
5719                 (goto-char bottom)
5720                 (set-window-start window (point) t)
5721                 (when (not (pos-visible-in-window-p last-line-start window))
5722                   (forward-line 1)
5723                   (set-window-start window (min (point) top-pos) t)))))))
5724       ;; Do horizontal recentering while we're at it.
5725       (when (and (get-buffer-window (current-buffer) t)
5726                  (not (eq gnus-auto-center-summary 'vertical)))
5727         (let ((selected (selected-window)))
5728           (select-window (get-buffer-window (current-buffer) t))
5729           (gnus-summary-position-point)
5730           (gnus-horizontal-recenter)
5731           (select-window selected))))))
5732
5733 (defun gnus-summary-jump-to-group (newsgroup)
5734   "Move point to NEWSGROUP in group mode buffer."
5735   ;; Keep update point of group mode buffer if visible.
5736   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5737       (save-window-excursion
5738         ;; Take care of tree window mode.
5739         (when (get-buffer-window gnus-group-buffer)
5740           (pop-to-buffer gnus-group-buffer))
5741         (gnus-group-jump-to-group newsgroup))
5742     (save-excursion
5743       ;; Take care of tree window mode.
5744       (if (get-buffer-window gnus-group-buffer)
5745           (pop-to-buffer gnus-group-buffer)
5746         (set-buffer gnus-group-buffer))
5747       (gnus-group-jump-to-group newsgroup))))
5748
5749 ;; This function returns a list of article numbers based on the
5750 ;; difference between the ranges of read articles in this group and
5751 ;; the range of active articles.
5752 (defun gnus-list-of-unread-articles (group)
5753   (let* ((read (gnus-info-read (gnus-get-info group)))
5754          (active (or (gnus-active group) (gnus-activate-group group)))
5755          (last (cdr active))
5756          first nlast unread)
5757     ;; If none are read, then all are unread.
5758     (if (not read)
5759         (setq first (car active))
5760       ;; If the range of read articles is a single range, then the
5761       ;; first unread article is the article after the last read
5762       ;; article.  Sounds logical, doesn't it?
5763       (if (and (not (listp (cdr read)))
5764                (or (< (car read) (car active))
5765                    (progn (setq read (list read))
5766                           nil)))
5767           (setq first (max (car active) (1+ (cdr read))))
5768         ;; `read' is a list of ranges.
5769         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5770                                   (caar read)))
5771                   1)
5772           (setq first (car active)))
5773         (while read
5774           (when first
5775             (while (< first nlast)
5776               (push first unread)
5777               (setq first (1+ first))))
5778           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5779           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5780           (setq read (cdr read)))))
5781     ;; And add the last unread articles.
5782     (while (<= first last)
5783       (push first unread)
5784       (setq first (1+ first)))
5785     ;; Return the list of unread articles.
5786     (delq 0 (nreverse unread))))
5787
5788 (defun gnus-list-of-read-articles (group)
5789   "Return a list of unread, unticked and non-dormant articles."
5790   (let* ((info (gnus-get-info group))
5791          (marked (gnus-info-marks info))
5792          (active (gnus-active group)))
5793     (and info active
5794          (gnus-set-difference
5795           (gnus-sorted-complement
5796            (gnus-uncompress-range active)
5797            (gnus-list-of-unread-articles group))
5798           (append
5799            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5800            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5801
5802 ;; Various summary commands
5803
5804 (defun gnus-summary-select-article-buffer ()
5805   "Reconfigure windows to show article buffer."
5806   (interactive)
5807   (if (not (gnus-buffer-live-p gnus-article-buffer))
5808       (error "There is no article buffer for this summary buffer")
5809     (gnus-configure-windows 'article)
5810     (select-window (get-buffer-window gnus-article-buffer))))
5811
5812 (defun gnus-summary-universal-argument (arg)
5813   "Perform any operation on all articles that are process/prefixed."
5814   (interactive "P")
5815   (let ((articles (gnus-summary-work-articles arg))
5816         func article)
5817     (if (eq
5818          (setq
5819           func
5820           (key-binding
5821            (read-key-sequence
5822             (substitute-command-keys
5823              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5824          'undefined)
5825         (gnus-error 1 "Undefined key")
5826       (save-excursion
5827         (while articles
5828           (gnus-summary-goto-subject (setq article (pop articles)))
5829           (let (gnus-newsgroup-processable)
5830             (command-execute func))
5831           (gnus-summary-remove-process-mark article)))))
5832   (gnus-summary-position-point))
5833
5834 (defun gnus-summary-toggle-truncation (&optional arg)
5835   "Toggle truncation of summary lines.
5836 With arg, turn line truncation on iff arg is positive."
5837   (interactive "P")
5838   (setq truncate-lines
5839         (if (null arg) (not truncate-lines)
5840           (> (prefix-numeric-value arg) 0)))
5841   (redraw-display))
5842
5843 (defun gnus-summary-reselect-current-group (&optional all rescan)
5844   "Exit and then reselect the current newsgroup.
5845 The prefix argument ALL means to select all articles."
5846   (interactive "P")
5847   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5848     (error "Ephemeral groups can't be reselected"))
5849   (let ((current-subject (gnus-summary-article-number))
5850         (group gnus-newsgroup-name))
5851     (setq gnus-newsgroup-begin nil)
5852     (gnus-summary-exit)
5853     ;; We have to adjust the point of group mode buffer because
5854     ;; point was moved to the next unread newsgroup by exiting.
5855     (gnus-summary-jump-to-group group)
5856     (when rescan
5857       (save-excursion
5858         (gnus-group-get-new-news-this-group 1)))
5859     (gnus-group-read-group all t)
5860     (gnus-summary-goto-subject current-subject nil t)))
5861
5862 (defun gnus-summary-rescan-group (&optional all)
5863   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5864   (interactive "P")
5865   (gnus-summary-reselect-current-group all t))
5866
5867 (defun gnus-summary-update-info (&optional non-destructive)
5868   (save-excursion
5869     (let ((group gnus-newsgroup-name))
5870       (when group
5871         (when gnus-newsgroup-kill-headers
5872           (setq gnus-newsgroup-killed
5873                 (gnus-compress-sequence
5874                  (nconc
5875                   (gnus-set-sorted-intersection
5876                    (gnus-uncompress-range gnus-newsgroup-killed)
5877                    (setq gnus-newsgroup-unselected
5878                          (sort gnus-newsgroup-unselected '<)))
5879                   (setq gnus-newsgroup-unreads
5880                         (sort gnus-newsgroup-unreads '<)))
5881                  t)))
5882         (unless (listp (cdr gnus-newsgroup-killed))
5883           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5884         (let ((headers gnus-newsgroup-headers))
5885           ;; Set the new ranges of read articles.
5886           (save-excursion
5887             (set-buffer gnus-group-buffer)
5888             (gnus-undo-force-boundary))
5889           (gnus-update-read-articles
5890            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5891           ;; Set the current article marks.
5892           (let ((gnus-newsgroup-scored
5893                  (if (and (not gnus-save-score)
5894                           (not non-destructive))
5895                      nil
5896                    gnus-newsgroup-scored)))
5897             (save-excursion
5898               (gnus-update-marks)))
5899           ;; Do the cross-ref thing.
5900           (when gnus-use-cross-reference
5901             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5902           ;; Do not switch windows but change the buffer to work.
5903           (set-buffer gnus-group-buffer)
5904           (unless (gnus-ephemeral-group-p group)
5905             (gnus-group-update-group group)))))))
5906
5907 (defun gnus-summary-save-newsrc (&optional force)
5908   "Save the current number of read/marked articles in the dribble buffer.
5909 The dribble buffer will then be saved.
5910 If FORCE (the prefix), also save the .newsrc file(s)."
5911   (interactive "P")
5912   (gnus-summary-update-info t)
5913   (if force
5914       (gnus-save-newsrc-file)
5915     (gnus-dribble-save)))
5916
5917 (defun gnus-summary-exit (&optional temporary)
5918   "Exit reading current newsgroup, and then return to group selection mode.
5919 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5920   (interactive)
5921   (gnus-set-global-variables)
5922   (when (gnus-buffer-live-p gnus-article-buffer)
5923     (save-excursion
5924       (set-buffer gnus-article-buffer)
5925       (mm-destroy-parts gnus-article-mime-handles)
5926       ;; Set it to nil for safety reason.
5927       (setq gnus-article-mime-handle-alist nil)
5928       (setq gnus-article-mime-handles nil)))
5929   (gnus-kill-save-kill-buffer)
5930   (gnus-async-halt-prefetch)
5931   (let* ((group gnus-newsgroup-name)
5932          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5933          (mode major-mode)
5934          (group-point nil)
5935          (buf (current-buffer)))
5936     (unless quit-config
5937       ;; Do adaptive scoring, and possibly save score files.
5938       (when gnus-newsgroup-adaptive
5939         (gnus-score-adaptive))
5940       (when gnus-use-scoring
5941         (gnus-score-save)))
5942     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5943     ;; If we have several article buffers, we kill them at exit.
5944     (unless gnus-single-article-buffer
5945       (gnus-kill-buffer gnus-original-article-buffer)
5946       (setq gnus-article-current nil))
5947     (when gnus-use-cache
5948       (gnus-cache-possibly-remove-articles)
5949       (gnus-cache-save-buffers))
5950     (gnus-async-prefetch-remove-group group)
5951     (when gnus-suppress-duplicates
5952       (gnus-dup-enter-articles))
5953     (when gnus-use-trees
5954       (gnus-tree-close group))
5955     (when gnus-use-cache
5956       (gnus-cache-write-active))
5957     ;; Remove entries for this group.
5958     (nnmail-purge-split-history (gnus-group-real-name group))
5959     ;; Make all changes in this group permanent.
5960     (unless quit-config
5961       (gnus-run-hooks 'gnus-exit-group-hook)
5962       (gnus-summary-update-info))
5963     (gnus-close-group group)
5964     ;; Make sure where we were, and go to next newsgroup.
5965     (set-buffer gnus-group-buffer)
5966     (unless quit-config
5967       (gnus-group-jump-to-group group))
5968     (gnus-run-hooks 'gnus-summary-exit-hook)
5969     (unless (or quit-config
5970                 ;; If this group has disappeared from the summary
5971                 ;; buffer, don't skip forwards.
5972                 (not (string= group (gnus-group-group-name))))
5973       (gnus-group-next-unread-group 1))
5974     (setq group-point (point))
5975     (if temporary
5976         nil                             ;Nothing to do.
5977       ;; If we have several article buffers, we kill them at exit.
5978       (unless gnus-single-article-buffer
5979         (gnus-kill-buffer gnus-article-buffer)
5980         (gnus-kill-buffer gnus-original-article-buffer)
5981         (setq gnus-article-current nil))
5982       (set-buffer buf)
5983       (if (not gnus-kill-summary-on-exit)
5984           (progn
5985             (gnus-deaden-summary)
5986             (setq mode nil))
5987        ;; We set all buffer-local variables to nil.  It is unclear why
5988         ;; this is needed, but if we don't, buffer-local variables are
5989         ;; not garbage-collected, it seems.  This would the lead to en
5990         ;; ever-growing Emacs.
5991         (gnus-summary-clear-local-variables)
5992         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5993           (gnus-summary-clear-local-variables))
5994         (when (get-buffer gnus-article-buffer)
5995           (bury-buffer gnus-article-buffer))
5996         ;; We clear the global counterparts of the buffer-local
5997         ;; variables as well, just to be on the safe side.
5998         (set-buffer gnus-group-buffer)
5999         (gnus-summary-clear-local-variables)
6000         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6001           (gnus-summary-clear-local-variables)))
6002       (setq gnus-current-select-method gnus-select-method)
6003       (pop-to-buffer gnus-group-buffer)
6004       (if (not quit-config)
6005           (progn
6006             (goto-char group-point)
6007             (gnus-configure-windows 'group 'force))
6008         (gnus-handle-ephemeral-exit quit-config))
6009       ;; Return to group mode buffer.
6010       (when (eq mode 'gnus-summary-mode)
6011         (gnus-kill-buffer buf))
6012       ;; Clear the current group name.
6013       (unless quit-config
6014         (setq gnus-newsgroup-name nil)))))
6015
6016 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6017 (defun gnus-summary-exit-no-update (&optional no-questions)
6018   "Quit reading current newsgroup without updating read article info."
6019   (interactive)
6020   (let* ((group gnus-newsgroup-name)
6021          (quit-config (gnus-group-quit-config group)))
6022     (when (or no-questions
6023               gnus-expert-user
6024               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6025       (gnus-async-halt-prefetch)
6026       (mapcar 'funcall
6027               (delq 'gnus-summary-expire-articles
6028                     (copy-sequence gnus-summary-prepare-exit-hook)))
6029       (when (gnus-buffer-live-p gnus-article-buffer)
6030         (save-excursion
6031           (set-buffer gnus-article-buffer)
6032           (mm-destroy-parts gnus-article-mime-handles)
6033           ;; Set it to nil for safety reason.
6034           (setq gnus-article-mime-handle-alist nil)
6035           (setq gnus-article-mime-handles nil)))
6036       ;; If we have several article buffers, we kill them at exit.
6037       (unless gnus-single-article-buffer
6038         (gnus-kill-buffer gnus-article-buffer)
6039         (gnus-kill-buffer gnus-original-article-buffer)
6040         (setq gnus-article-current nil))
6041       (if (not gnus-kill-summary-on-exit)
6042           (gnus-deaden-summary)
6043         (gnus-close-group group)
6044         (gnus-summary-clear-local-variables)
6045         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6046           (gnus-summary-clear-local-variables))
6047         (set-buffer gnus-group-buffer)
6048         (gnus-summary-clear-local-variables)
6049         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6050           (gnus-summary-clear-local-variables))
6051         (when (get-buffer gnus-summary-buffer)
6052           (kill-buffer gnus-summary-buffer)))
6053       (unless gnus-single-article-buffer
6054         (setq gnus-article-current nil))
6055       (when gnus-use-trees
6056         (gnus-tree-close group))
6057       (gnus-async-prefetch-remove-group group)
6058       (when (get-buffer gnus-article-buffer)
6059         (bury-buffer gnus-article-buffer))
6060       ;; Return to the group buffer.
6061       (gnus-configure-windows 'group 'force)
6062       ;; Clear the current group name.
6063       (setq gnus-newsgroup-name nil)
6064       (when (equal (gnus-group-group-name) group)
6065         (gnus-group-next-unread-group 1))
6066       (when quit-config
6067         (gnus-handle-ephemeral-exit quit-config)))))
6068
6069 (defun gnus-handle-ephemeral-exit (quit-config)
6070   "Handle movement when leaving an ephemeral group.
6071 The state which existed when entering the ephemeral is reset."
6072   (if (not (buffer-name (car quit-config)))
6073       (gnus-configure-windows 'group 'force)
6074     (set-buffer (car quit-config))
6075     (cond ((eq major-mode 'gnus-summary-mode)
6076            (gnus-set-global-variables))
6077           ((eq major-mode 'gnus-article-mode)
6078            (save-excursion
6079              ;; The `gnus-summary-buffer' variable may point
6080              ;; to the old summary buffer when using a single
6081              ;; article buffer.
6082              (unless (gnus-buffer-live-p gnus-summary-buffer)
6083                (set-buffer gnus-group-buffer))
6084              (set-buffer gnus-summary-buffer)
6085              (gnus-set-global-variables))))
6086     (if (or (eq (cdr quit-config) 'article)
6087             (eq (cdr quit-config) 'pick))
6088         (progn
6089           ;; The current article may be from the ephemeral group
6090           ;; thus it is best that we reload this article
6091           (gnus-summary-show-article)
6092           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6093               (gnus-configure-windows 'pick 'force)
6094             (gnus-configure-windows (cdr quit-config) 'force)))
6095       (gnus-configure-windows (cdr quit-config) 'force))
6096     (when (eq major-mode 'gnus-summary-mode)
6097       (gnus-summary-next-subject 1 nil t)
6098       (gnus-summary-recenter)
6099       (gnus-summary-position-point))))
6100
6101 ;;; Dead summaries.
6102
6103 (defvar gnus-dead-summary-mode-map nil)
6104
6105 (unless gnus-dead-summary-mode-map
6106   (setq gnus-dead-summary-mode-map (make-keymap))
6107   (suppress-keymap gnus-dead-summary-mode-map)
6108   (substitute-key-definition
6109    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6110   (let ((keys '("\C-d" "\r" "\177" [delete])))
6111     (while keys
6112       (define-key gnus-dead-summary-mode-map
6113         (pop keys) 'gnus-summary-wake-up-the-dead))))
6114
6115 (defvar gnus-dead-summary-mode nil
6116   "Minor mode for Gnus summary buffers.")
6117
6118 (defun gnus-dead-summary-mode (&optional arg)
6119   "Minor mode for Gnus summary buffers."
6120   (interactive "P")
6121   (when (eq major-mode 'gnus-summary-mode)
6122     (make-local-variable 'gnus-dead-summary-mode)
6123     (setq gnus-dead-summary-mode
6124           (if (null arg) (not gnus-dead-summary-mode)
6125             (> (prefix-numeric-value arg) 0)))
6126     (when gnus-dead-summary-mode
6127       (gnus-add-minor-mode
6128        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6129
6130 (defun gnus-deaden-summary ()
6131   "Make the current summary buffer into a dead summary buffer."
6132   ;; Kill any previous dead summary buffer.
6133   (when (and gnus-dead-summary
6134              (buffer-name gnus-dead-summary))
6135     (save-excursion
6136       (set-buffer gnus-dead-summary)
6137       (when gnus-dead-summary-mode
6138         (kill-buffer (current-buffer)))))
6139   ;; Make this the current dead summary.
6140   (setq gnus-dead-summary (current-buffer))
6141   (gnus-dead-summary-mode 1)
6142   (let ((name (buffer-name)))
6143     (when (string-match "Summary" name)
6144       (rename-buffer
6145        (concat (substring name 0 (match-beginning 0)) "Dead "
6146                (substring name (match-beginning 0)))
6147        t)
6148       (bury-buffer))))
6149
6150 (defun gnus-kill-or-deaden-summary (buffer)
6151   "Kill or deaden the summary BUFFER."
6152   (save-excursion
6153     (when (and (buffer-name buffer)
6154                (not gnus-single-article-buffer))
6155       (save-excursion
6156         (set-buffer buffer)
6157         (gnus-kill-buffer gnus-article-buffer)
6158         (gnus-kill-buffer gnus-original-article-buffer)))
6159     (cond (gnus-kill-summary-on-exit
6160            (when (and gnus-use-trees
6161                       (gnus-buffer-exists-p buffer))
6162              (save-excursion
6163                (set-buffer buffer)
6164                (gnus-tree-close gnus-newsgroup-name)))
6165            (gnus-kill-buffer buffer))
6166           ((gnus-buffer-exists-p buffer)
6167            (save-excursion
6168              (set-buffer buffer)
6169              (gnus-deaden-summary))))))
6170
6171 (defun gnus-summary-wake-up-the-dead (&rest args)
6172   "Wake up the dead summary buffer."
6173   (interactive)
6174   (gnus-dead-summary-mode -1)
6175   (let ((name (buffer-name)))
6176     (when (string-match "Dead " name)
6177       (rename-buffer
6178        (concat (substring name 0 (match-beginning 0))
6179                (substring name (match-end 0)))
6180        t)))
6181   (gnus-message 3 "This dead summary is now alive again"))
6182
6183 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6184 (defun gnus-summary-fetch-faq (&optional faq-dir)
6185   "Fetch the FAQ for the current group.
6186 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6187 in."
6188   (interactive
6189    (list
6190     (when current-prefix-arg
6191       (completing-read
6192        "Faq dir: " (and (listp gnus-group-faq-directory)
6193                         (mapcar (lambda (file) (list file))
6194                                 gnus-group-faq-directory))))))
6195   (let (gnus-faq-buffer)
6196     (when (setq gnus-faq-buffer
6197                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6198       (gnus-configure-windows 'summary-faq))))
6199
6200 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6201 (defun gnus-summary-describe-group (&optional force)
6202   "Describe the current newsgroup."
6203   (interactive "P")
6204   (gnus-group-describe-group force gnus-newsgroup-name))
6205
6206 (defun gnus-summary-describe-briefly ()
6207   "Describe summary mode commands briefly."
6208   (interactive)
6209   (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")))
6210
6211 ;; Walking around group mode buffer from summary mode.
6212
6213 (defun gnus-summary-next-group (&optional no-article target-group backward)
6214   "Exit current newsgroup and then select next unread newsgroup.
6215 If prefix argument NO-ARTICLE is non-nil, no article is selected
6216 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6217 previous group instead."
6218   (interactive "P")
6219   ;; Stop pre-fetching.
6220   (gnus-async-halt-prefetch)
6221   (let ((current-group gnus-newsgroup-name)
6222         (current-buffer (current-buffer))
6223         entered)
6224    ;; First we semi-exit this group to update Xrefs and all variables.
6225     ;; We can't do a real exit, because the window conf must remain
6226     ;; the same in case the user is prompted for info, and we don't
6227     ;; want the window conf to change before that...
6228     (gnus-summary-exit t)
6229     (while (not entered)
6230       ;; Then we find what group we are supposed to enter.
6231       (set-buffer gnus-group-buffer)
6232       (gnus-group-jump-to-group current-group)
6233       (setq target-group
6234             (or target-group
6235                 (if (eq gnus-keep-same-level 'best)
6236                     (gnus-summary-best-group gnus-newsgroup-name)
6237                   (gnus-summary-search-group backward gnus-keep-same-level))))
6238       (if (not target-group)
6239           ;; There are no further groups, so we return to the group
6240           ;; buffer.
6241           (progn
6242             (gnus-message 5 "Returning to the group buffer")
6243             (setq entered t)
6244             (when (gnus-buffer-live-p current-buffer)
6245               (set-buffer current-buffer)
6246               (gnus-summary-exit))
6247             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6248         ;; We try to enter the target group.
6249         (gnus-group-jump-to-group target-group)
6250         (let ((unreads (gnus-group-group-unread)))
6251           (if (and (or (eq t unreads)
6252                        (and unreads (not (zerop unreads))))
6253                    (gnus-summary-read-group
6254                     target-group nil no-article
6255                     (and (buffer-name current-buffer) current-buffer)
6256                     nil backward))
6257               (setq entered t)
6258             (setq current-group target-group
6259                   target-group nil)))))))
6260
6261 (defun gnus-summary-prev-group (&optional no-article)
6262   "Exit current newsgroup and then select previous unread newsgroup.
6263 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6264   (interactive "P")
6265   (gnus-summary-next-group no-article nil t))
6266
6267 ;; Walking around summary lines.
6268
6269 (defun gnus-summary-first-subject (&optional unread undownloaded)
6270   "Go to the first unread subject.
6271 If UNREAD is non-nil, go to the first unread article.
6272 Returns the article selected or nil if there are no unread articles."
6273   (interactive "P")
6274   (prog1
6275       (cond
6276        ;; Empty summary.
6277        ((null gnus-newsgroup-data)
6278         (gnus-message 3 "No articles in the group")
6279         nil)
6280        ;; Pick the first article.
6281        ((not unread)
6282         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6283         (gnus-data-number (car gnus-newsgroup-data)))
6284        ;; No unread articles.
6285        ((null gnus-newsgroup-unreads)
6286         (gnus-message 3 "No more unread articles")
6287         nil)
6288        ;; Find the first unread article.
6289        (t
6290         (let ((data gnus-newsgroup-data))
6291           (while (and data
6292                       (and (not (and undownloaded
6293                                      (eq gnus-undownloaded-mark
6294                                          (gnus-data-mark (car data)))))
6295                            (not (gnus-data-unread-p (car data)))))
6296             (setq data (cdr data)))
6297           (when data
6298             (goto-char (gnus-data-pos (car data)))
6299             (gnus-data-number (car data))))))
6300     (gnus-summary-position-point)))
6301
6302 (defun gnus-summary-next-subject (n &optional unread dont-display)
6303   "Go to next N'th summary line.
6304 If N is negative, go to the previous N'th subject line.
6305 If UNREAD is non-nil, only unread articles are selected.
6306 The difference between N and the actual number of steps taken is
6307 returned."
6308   (interactive "p")
6309   (let ((backward (< n 0))
6310         (n (abs n)))
6311     (while (and (> n 0)
6312                 (if backward
6313                     (gnus-summary-find-prev unread)
6314                   (gnus-summary-find-next unread)))
6315       (unless (zerop (setq n (1- n)))
6316         (gnus-summary-show-thread)))
6317     (when (/= 0 n)
6318       (gnus-message 7 "No more%s articles"
6319                     (if unread " unread" "")))
6320     (unless dont-display
6321       (gnus-summary-recenter)
6322       (gnus-summary-position-point))
6323     n))
6324
6325 (defun gnus-summary-next-unread-subject (n)
6326   "Go to next N'th unread summary line."
6327   (interactive "p")
6328   (gnus-summary-next-subject n t))
6329
6330 (defun gnus-summary-prev-subject (n &optional unread)
6331   "Go to previous N'th summary line.
6332 If optional argument UNREAD is non-nil, only unread article is selected."
6333   (interactive "p")
6334   (gnus-summary-next-subject (- n) unread))
6335
6336 (defun gnus-summary-prev-unread-subject (n)
6337   "Go to previous N'th unread summary line."
6338   (interactive "p")
6339   (gnus-summary-next-subject (- n) t))
6340
6341 (defun gnus-summary-goto-subject (article &optional force silent)
6342   "Go the subject line of ARTICLE.
6343 If FORCE, also allow jumping to articles not currently shown."
6344   (interactive "nArticle number: ")
6345   (let ((b (point))
6346         (data (gnus-data-find article)))
6347     ;; We read in the article if we have to.
6348     (and (not data)
6349          force
6350          (gnus-summary-insert-subject
6351           article
6352           (if (or (numberp force) (vectorp force)) force)
6353           t)
6354          (setq data (gnus-data-find article)))
6355     (goto-char b)
6356     (if (not data)
6357         (progn
6358           (unless silent
6359             (gnus-message 3 "Can't find article %d" article))
6360           nil)
6361       (let ((pt (gnus-data-pos data)))
6362         (goto-char pt)
6363         (gnus-summary-set-article-display-arrow pt))
6364       (gnus-summary-position-point)
6365       article)))
6366
6367 ;; Walking around summary lines with displaying articles.
6368
6369 (defun gnus-summary-expand-window (&optional arg)
6370   "Make the summary buffer take up the entire Emacs frame.
6371 Given a prefix, will force an `article' buffer configuration."
6372   (interactive "P")
6373   (if arg
6374       (gnus-configure-windows 'article 'force)
6375     (gnus-configure-windows 'summary 'force)))
6376
6377 (defun gnus-summary-display-article (article &optional all-header)
6378   "Display ARTICLE in article buffer."
6379   (when (gnus-buffer-live-p gnus-article-buffer)
6380     (with-current-buffer gnus-article-buffer
6381       (mm-enable-multibyte-mule4)))
6382   (gnus-set-global-variables)
6383   (when (gnus-buffer-live-p gnus-article-buffer)
6384     (with-current-buffer gnus-article-buffer
6385       (setq gnus-article-charset gnus-newsgroup-charset)
6386       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6387       (mm-enable-multibyte-mule4)))
6388   (if (null article)
6389       nil
6390     (prog1
6391         (if gnus-summary-display-article-function
6392             (funcall gnus-summary-display-article-function article all-header)
6393           (gnus-article-prepare article all-header))
6394       (gnus-run-hooks 'gnus-select-article-hook)
6395       (when (and gnus-current-article
6396                  (not (zerop gnus-current-article)))
6397         (gnus-summary-goto-subject gnus-current-article))
6398       (gnus-summary-recenter)
6399       (when (and gnus-use-trees gnus-show-threads)
6400         (gnus-possibly-generate-tree article)
6401         (gnus-highlight-selected-tree article))
6402       ;; Successfully display article.
6403       (gnus-article-set-window-start
6404        (cdr (assq article gnus-newsgroup-bookmarks))))))
6405
6406 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6407   "Select the current article.
6408 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6409 non-nil, the article will be re-fetched even if it already present in
6410 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6411 be displayed."
6412   ;; Make sure we are in the summary buffer to work around bbdb bug.
6413   (unless (eq major-mode 'gnus-summary-mode)
6414     (set-buffer gnus-summary-buffer))
6415   (let ((article (or article (gnus-summary-article-number)))
6416         (all-headers (not (not all-headers))) ;Must be T or NIL.
6417         gnus-summary-display-article-function)
6418     (and (not pseudo)
6419          (gnus-summary-article-pseudo-p article)
6420          (error "This is a pseudo-article"))
6421     (save-excursion
6422       (set-buffer gnus-summary-buffer)
6423       (if (or (and gnus-single-article-buffer
6424                    (or (null gnus-current-article)
6425                        (null gnus-article-current)
6426                        (null (get-buffer gnus-article-buffer))
6427                        (not (eq article (cdr gnus-article-current)))
6428                        (not (equal (car gnus-article-current)
6429                                    gnus-newsgroup-name))))
6430               (and (not gnus-single-article-buffer)
6431                    (or (null gnus-current-article)
6432                        (not (eq gnus-current-article article))))
6433               force)
6434        ;; The requested article is different from the current article.
6435           (progn
6436             (gnus-summary-display-article article all-headers)
6437             (when (gnus-buffer-live-p gnus-article-buffer)
6438               (with-current-buffer gnus-article-buffer
6439                 (if (not gnus-article-decoded-p) ;; a local variable
6440                     (mm-disable-multibyte-mule4))))
6441             (when (or all-headers gnus-show-all-headers)
6442               (gnus-article-show-all-headers))
6443             (gnus-article-set-window-start
6444              (cdr (assq article gnus-newsgroup-bookmarks)))
6445             article)
6446         (when (or all-headers gnus-show-all-headers)
6447           (gnus-article-show-all-headers))
6448         'old))))
6449
6450 (defun gnus-summary-force-verify-and-decrypt ()
6451   (interactive)
6452   (let ((mm-verify-option 'known)
6453         (mm-decrypt-option 'known))
6454     (gnus-summary-select-article nil 'force)))
6455
6456 (defun gnus-summary-set-current-mark (&optional current-mark)
6457   "Obsolete function."
6458   nil)
6459
6460 (defun gnus-summary-next-article (&optional unread subject backward push)
6461   "Select the next article.
6462 If UNREAD, only unread articles are selected.
6463 If SUBJECT, only articles with SUBJECT are selected.
6464 If BACKWARD, the previous article is selected instead of the next."
6465   (interactive "P")
6466   (cond
6467    ;; Is there such an article?
6468    ((and (gnus-summary-search-forward unread subject backward)
6469          (or (gnus-summary-display-article (gnus-summary-article-number))
6470              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6471     (gnus-summary-position-point))
6472    ;; If not, we try the first unread, if that is wanted.
6473    ((and subject
6474          gnus-auto-select-same
6475          (gnus-summary-first-unread-article))
6476     (gnus-summary-position-point)
6477     (gnus-message 6 "Wrapped"))
6478    ;; Try to get next/previous article not displayed in this group.
6479    ((and gnus-auto-extend-newsgroup
6480          (not unread) (not subject))
6481     (gnus-summary-goto-article
6482      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6483      nil (count-lines (point-min) (point))))
6484    ;; Go to next/previous group.
6485    (t
6486     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6487       (gnus-summary-jump-to-group gnus-newsgroup-name))
6488     (let ((cmd last-command-char)
6489           (point
6490            (save-excursion
6491              (set-buffer gnus-group-buffer)
6492              (point)))
6493           (group
6494            (if (eq gnus-keep-same-level 'best)
6495                (gnus-summary-best-group gnus-newsgroup-name)
6496              (gnus-summary-search-group backward gnus-keep-same-level))))
6497       ;; For some reason, the group window gets selected.  We change
6498       ;; it back.
6499       (select-window (get-buffer-window (current-buffer)))
6500       ;; Select next unread newsgroup automagically.
6501       (cond
6502        ((or (not gnus-auto-select-next)
6503             (not cmd))
6504         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6505        ((or (eq gnus-auto-select-next 'quietly)
6506             (and (eq gnus-auto-select-next 'slightly-quietly)
6507                  push)
6508             (and (eq gnus-auto-select-next 'almost-quietly)
6509                  (gnus-summary-last-article-p)))
6510         ;; Select quietly.
6511         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6512             (gnus-summary-exit)
6513           (gnus-message 7 "No more%s articles (%s)..."
6514                         (if unread " unread" "")
6515                         (if group (concat "selecting " group)
6516                           "exiting"))
6517           (gnus-summary-next-group nil group backward)))
6518        (t
6519         (when (gnus-key-press-event-p last-input-event)
6520           (gnus-summary-walk-group-buffer
6521            gnus-newsgroup-name cmd unread backward point))))))))
6522
6523 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6524   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6525                       (?\C-p (gnus-group-prev-unread-group 1))))
6526         (cursor-in-echo-area t)
6527         keve key group ended)
6528     (save-excursion
6529       (set-buffer gnus-group-buffer)
6530       (goto-char start)
6531       (setq group
6532             (if (eq gnus-keep-same-level 'best)
6533                 (gnus-summary-best-group gnus-newsgroup-name)
6534               (gnus-summary-search-group backward gnus-keep-same-level))))
6535     (while (not ended)
6536       (gnus-message
6537        5 "No more%s articles%s" (if unread " unread" "")
6538        (if (and group
6539                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6540            (format " (Type %s for %s [%s])"
6541                    (single-key-description cmd) group
6542                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6543          (format " (Type %s to exit %s)"
6544                  (single-key-description cmd)
6545                  gnus-newsgroup-name)))
6546       ;; Confirm auto selection.
6547       (setq key (car (setq keve (gnus-read-event-char))))
6548       (setq ended t)
6549       (cond
6550        ((assq key keystrokes)
6551         (let ((obuf (current-buffer)))
6552           (switch-to-buffer gnus-group-buffer)
6553           (when group
6554             (gnus-group-jump-to-group group))
6555           (eval (cadr (assq key keystrokes)))
6556           (setq group (gnus-group-group-name))
6557           (switch-to-buffer obuf))
6558         (setq ended nil))
6559        ((equal key cmd)
6560         (if (or (not group)
6561                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6562             (gnus-summary-exit)
6563           (gnus-summary-next-group nil group backward)))
6564        (t
6565         (push (cdr keve) unread-command-events))))))
6566
6567 (defun gnus-summary-next-unread-article ()
6568   "Select unread article after current one."
6569   (interactive)
6570   (gnus-summary-next-article
6571    (or (not (eq gnus-summary-goto-unread 'never))
6572        (gnus-summary-last-article-p (gnus-summary-article-number)))
6573    (and gnus-auto-select-same
6574         (gnus-summary-article-subject))))
6575
6576 (defun gnus-summary-prev-article (&optional unread subject)
6577   "Select the article after the current one.
6578 If UNREAD is non-nil, only unread articles are selected."
6579   (interactive "P")
6580   (gnus-summary-next-article unread subject t))
6581
6582 (defun gnus-summary-prev-unread-article ()
6583   "Select unread article before current one."
6584   (interactive)
6585   (gnus-summary-prev-article
6586    (or (not (eq gnus-summary-goto-unread 'never))
6587        (gnus-summary-first-article-p (gnus-summary-article-number)))
6588    (and gnus-auto-select-same
6589         (gnus-summary-article-subject))))
6590
6591 (defun gnus-summary-next-page (&optional lines circular)
6592   "Show next page of the selected article.
6593 If at the end of the current article, select the next article.
6594 LINES says how many lines should be scrolled up.
6595
6596 If CIRCULAR is non-nil, go to the start of the article instead of
6597 selecting the next article when reaching the end of the current
6598 article."
6599   (interactive "P")
6600   (setq gnus-summary-buffer (current-buffer))
6601   (gnus-set-global-variables)
6602   (let ((article (gnus-summary-article-number))
6603         (article-window (get-buffer-window gnus-article-buffer t))
6604         endp)
6605     ;; If the buffer is empty, we have no article.
6606     (unless article
6607       (error "No article to select"))
6608     (gnus-configure-windows 'article)
6609     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6610         (if (and (eq gnus-summary-goto-unread 'never)
6611                  (not (gnus-summary-last-article-p article)))
6612             (gnus-summary-next-article)
6613           (gnus-summary-next-unread-article))
6614       (if (or (null gnus-current-article)
6615               (null gnus-article-current)
6616               (/= article (cdr gnus-article-current))
6617               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6618           ;; Selected subject is different from current article's.
6619           (gnus-summary-display-article article)
6620         (when article-window
6621           (gnus-eval-in-buffer-window gnus-article-buffer
6622             (setq endp (gnus-article-next-page lines)))
6623           (when endp
6624             (cond (circular
6625                    (gnus-summary-beginning-of-article))
6626                   (lines
6627                    (gnus-message 3 "End of message"))
6628                   ((null lines)
6629                    (if (and (eq gnus-summary-goto-unread 'never)
6630                             (not (gnus-summary-last-article-p article)))
6631                        (gnus-summary-next-article)
6632                      (gnus-summary-next-unread-article))))))))
6633     (gnus-summary-recenter)
6634     (gnus-summary-position-point)))
6635
6636 (defun gnus-summary-prev-page (&optional lines move)
6637   "Show previous page of selected article.
6638 Argument LINES specifies lines to be scrolled down.
6639 If MOVE, move to the previous unread article if point is at
6640 the beginning of the buffer."
6641   (interactive "P")
6642   (let ((article (gnus-summary-article-number))
6643         (article-window (get-buffer-window gnus-article-buffer t))
6644         endp)
6645     (gnus-configure-windows 'article)
6646     (if (or (null gnus-current-article)
6647             (null gnus-article-current)
6648             (/= article (cdr gnus-article-current))
6649             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6650         ;; Selected subject is different from current article's.
6651         (gnus-summary-display-article article)
6652       (gnus-summary-recenter)
6653       (when article-window
6654         (gnus-eval-in-buffer-window gnus-article-buffer
6655           (setq endp (gnus-article-prev-page lines)))
6656         (when (and move endp)
6657           (cond (lines
6658                  (gnus-message 3 "Beginning of message"))
6659                 ((null lines)
6660                  (if (and (eq gnus-summary-goto-unread 'never)
6661                           (not (gnus-summary-first-article-p article)))
6662                      (gnus-summary-prev-article)
6663                    (gnus-summary-prev-unread-article))))))))
6664   (gnus-summary-position-point))
6665
6666 (defun gnus-summary-prev-page-or-article (&optional lines)
6667   "Show previous page of selected article.
6668 Argument LINES specifies lines to be scrolled down.
6669 If at the beginning of the article, go to the next article."
6670   (interactive "P")
6671   (gnus-summary-prev-page lines t))
6672
6673 (defun gnus-summary-scroll-up (lines)
6674   "Scroll up (or down) one line current article.
6675 Argument LINES specifies lines to be scrolled up (or down if negative)."
6676   (interactive "p")
6677   (gnus-configure-windows 'article)
6678   (gnus-summary-show-thread)
6679   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6680     (gnus-eval-in-buffer-window gnus-article-buffer
6681       (cond ((> lines 0)
6682              (when (gnus-article-next-page lines)
6683                (gnus-message 3 "End of message")))
6684             ((< lines 0)
6685              (gnus-article-prev-page (- lines))))))
6686   (gnus-summary-recenter)
6687   (gnus-summary-position-point))
6688
6689 (defun gnus-summary-scroll-down (lines)
6690   "Scroll down (or up) one line current article.
6691 Argument LINES specifies lines to be scrolled down (or up if negative)."
6692   (interactive "p")
6693   (gnus-summary-scroll-up (- lines)))
6694
6695 (defun gnus-summary-next-same-subject ()
6696   "Select next article which has the same subject as current one."
6697   (interactive)
6698   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6699
6700 (defun gnus-summary-prev-same-subject ()
6701   "Select previous article which has the same subject as current one."
6702   (interactive)
6703   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6704
6705 (defun gnus-summary-next-unread-same-subject ()
6706   "Select next unread article which has the same subject as current one."
6707   (interactive)
6708   (gnus-summary-next-article t (gnus-summary-article-subject)))
6709
6710 (defun gnus-summary-prev-unread-same-subject ()
6711   "Select previous unread article which has the same subject as current one."
6712   (interactive)
6713   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6714
6715 (defun gnus-summary-first-unread-article ()
6716   "Select the first unread article.
6717 Return nil if there are no unread articles."
6718   (interactive)
6719   (prog1
6720       (when (gnus-summary-first-subject t)
6721         (gnus-summary-show-thread)
6722         (gnus-summary-first-subject t)
6723         (gnus-summary-display-article (gnus-summary-article-number)))
6724     (gnus-summary-position-point)))
6725
6726 (defun gnus-summary-first-unread-subject ()
6727   "Place the point on the subject line of the first unread article.
6728 Return nil if there are no unread articles."
6729   (interactive)
6730   (prog1
6731       (when (gnus-summary-first-subject t)
6732         (gnus-summary-show-thread)
6733         (gnus-summary-first-subject t))
6734     (gnus-summary-position-point)))
6735
6736 (defun gnus-summary-first-article ()
6737   "Select the first article.
6738 Return nil if there are no articles."
6739   (interactive)
6740   (prog1
6741       (when (gnus-summary-first-subject)
6742         (gnus-summary-show-thread)
6743         (gnus-summary-first-subject)
6744         (gnus-summary-display-article (gnus-summary-article-number)))
6745     (gnus-summary-position-point)))
6746
6747 (defun gnus-summary-best-unread-article ()
6748   "Select the unread article with the highest score."
6749   (interactive)
6750   (let ((best -1000000)
6751         (data gnus-newsgroup-data)
6752         article score)
6753     (while data
6754       (and (gnus-data-unread-p (car data))
6755            (> (setq score
6756                     (gnus-summary-article-score (gnus-data-number (car data))))
6757               best)
6758            (setq best score
6759                  article (gnus-data-number (car data))))
6760       (setq data (cdr data)))
6761     (prog1
6762         (if article
6763             (gnus-summary-goto-article article)
6764           (error "No unread articles"))
6765       (gnus-summary-position-point))))
6766
6767 (defun gnus-summary-last-subject ()
6768   "Go to the last displayed subject line in the group."
6769   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6770     (when article
6771       (gnus-summary-goto-subject article))))
6772
6773 (defun gnus-summary-goto-article (article &optional all-headers force)
6774   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6775 If ALL-HEADERS is non-nil, no header lines are hidden.
6776 If FORCE, go to the article even if it isn't displayed.  If FORCE
6777 is a number, it is the line the article is to be displayed on."
6778   (interactive
6779    (list
6780     (completing-read
6781      "Article number or Message-ID: "
6782      (mapcar (lambda (number) (list (int-to-string number)))
6783              gnus-newsgroup-limit))
6784     current-prefix-arg
6785     t))
6786   (prog1
6787       (if (and (stringp article)
6788                (string-match "@" article))
6789           (gnus-summary-refer-article article)
6790         (when (stringp article)
6791           (setq article (string-to-number article)))
6792         (if (gnus-summary-goto-subject article force)
6793             (gnus-summary-display-article article all-headers)
6794           (gnus-message 4 "Couldn't go to article %s" article) nil))
6795     (gnus-summary-position-point)))
6796
6797 (defun gnus-summary-goto-last-article ()
6798   "Go to the previously read article."
6799   (interactive)
6800   (prog1
6801       (when gnus-last-article
6802         (gnus-summary-goto-article gnus-last-article nil t))
6803     (gnus-summary-position-point)))
6804
6805 (defun gnus-summary-pop-article (number)
6806   "Pop one article off the history and go to the previous.
6807 NUMBER articles will be popped off."
6808   (interactive "p")
6809   (let (to)
6810     (setq gnus-newsgroup-history
6811           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6812     (if to
6813         (gnus-summary-goto-article (car to) nil t)
6814       (error "Article history empty")))
6815   (gnus-summary-position-point))
6816
6817 ;; Summary commands and functions for limiting the summary buffer.
6818
6819 (defun gnus-summary-limit-to-articles (n)
6820   "Limit the summary buffer to the next N articles.
6821 If not given a prefix, use the process marked articles instead."
6822   (interactive "P")
6823   (prog1
6824       (let ((articles (gnus-summary-work-articles n)))
6825         (setq gnus-newsgroup-processable nil)
6826         (gnus-summary-limit articles))
6827     (gnus-summary-position-point)))
6828
6829 (defun gnus-summary-pop-limit (&optional total)
6830   "Restore the previous limit.
6831 If given a prefix, remove all limits."
6832   (interactive "P")
6833   (when total
6834     (setq gnus-newsgroup-limits
6835           (list (mapcar (lambda (h) (mail-header-number h))
6836                         gnus-newsgroup-headers))))
6837   (unless gnus-newsgroup-limits
6838     (error "No limit to pop"))
6839   (prog1
6840       (gnus-summary-limit nil 'pop)
6841     (gnus-summary-position-point)))
6842
6843 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
6844   "Limit the summary buffer to articles that have subjects that match a regexp.
6845 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
6846   (interactive 
6847    (list (read-string (if current-prefix-arg
6848                           "Exclude subject (regexp): "
6849                         "Limit to subject (regexp): ")) 
6850          nil current-prefix-arg))
6851   (unless header
6852     (setq header "subject"))
6853   (when (not (equal "" subject))
6854     (prog1
6855         (let ((articles (gnus-summary-find-matching
6856                          (or header "subject") subject 'all nil nil 
6857                          not-matching)))
6858           (unless articles
6859             (error "Found no matches for \"%s\"" subject))
6860           (gnus-summary-limit articles))
6861       (gnus-summary-position-point))))
6862
6863 (defun gnus-summary-limit-to-author (from &optional not-matching)
6864   "Limit the summary buffer to articles that have authors that match a regexp.
6865 If NOT-MATCHING, excluding articles that have authors that match a regexp."
6866   (interactive 
6867    (list (read-string (if current-prefix-arg
6868                           "Exclude author (regexp): "
6869                         "Limit to author (regexp): ")) 
6870          current-prefix-arg))
6871   (gnus-summary-limit-to-subject from "from" not-matching))
6872
6873 (defun gnus-summary-limit-to-age (age &optional younger-p)
6874   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6875 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6876 articles that are younger than AGE days."
6877   (interactive
6878    (let ((younger current-prefix-arg)
6879          (days-got nil)
6880          days)
6881      (while (not days-got)
6882        (setq days (if younger
6883                       (read-string "Limit to articles within (in days): ")
6884                     (read-string "Limit to articles older than (in days): ")))
6885        (when (> (length days) 0)
6886          (setq days (read days)))
6887        (if (numberp days)
6888            (setq days-got t)
6889          (message "Please enter a number.")
6890          (sleep-for 1)))
6891      (list days younger)))
6892   (prog1
6893       (let ((data gnus-newsgroup-data)
6894             (cutoff (days-to-time age))
6895             articles d date is-younger)
6896         (while (setq d (pop data))
6897           (when (and (vectorp (gnus-data-header d))
6898                      (setq date (mail-header-date (gnus-data-header d))))
6899             (setq is-younger (time-less-p
6900                               (time-since (condition-case ()
6901                                               (date-to-time date)
6902                                             (error '(0 0))))
6903                               cutoff))
6904             (when (if younger-p
6905                       is-younger
6906                     (not is-younger))
6907               (push (gnus-data-number d) articles))))
6908         (gnus-summary-limit (nreverse articles)))
6909     (gnus-summary-position-point)))
6910
6911 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
6912   "Limit the summary buffer to articles that match an 'extra' header."
6913   (interactive
6914    (let ((header
6915           (intern
6916            (gnus-completing-read
6917             (symbol-name (car gnus-extra-headers))
6918             (if current-prefix-arg
6919                 "Exclude extra header:"
6920               "Limit extra header:")
6921             (mapcar (lambda (x)
6922                       (cons (symbol-name x) x))
6923                     gnus-extra-headers)
6924             nil
6925             t))))
6926      (list header
6927            (read-string (format "%s header %s (regexp): " 
6928                                 (if current-prefix-arg "Exclude" "Limit to")
6929                                 header))
6930            current-prefix-arg)))
6931   (when (not (equal "" regexp))
6932     (prog1
6933         (let ((articles (gnus-summary-find-matching
6934                          (cons 'extra header) regexp 'all nil nil 
6935                          not-matching)))
6936           (unless articles
6937             (error "Found no matches for \"%s\"" regexp))
6938           (gnus-summary-limit articles))
6939       (gnus-summary-position-point))))
6940
6941 (defun gnus-summary-limit-to-display-predicate ()
6942   "Limit the summary buffer to the predicated in the `display' group parameter."
6943   (interactive)
6944   (unless gnus-newsgroup-display
6945     (error "There is no `display' group parameter"))
6946   (let (articles)
6947     (dolist (number gnus-newsgroup-articles)
6948       (when (funcall gnus-newsgroup-display)
6949         (push number articles)))
6950     (gnus-summary-limit articles))
6951   (gnus-summary-position-point))
6952
6953 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6954 (make-obsolete
6955  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6956
6957 (defun gnus-summary-limit-to-unread (&optional all)
6958   "Limit the summary buffer to articles that are not marked as read.
6959 If ALL is non-nil, limit strictly to unread articles."
6960   (interactive "P")
6961   (if all
6962       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6963     (gnus-summary-limit-to-marks
6964      ;; Concat all the marks that say that an article is read and have
6965      ;; those removed.
6966      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6967            gnus-killed-mark gnus-kill-file-mark
6968            gnus-low-score-mark gnus-expirable-mark
6969            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6970            gnus-duplicate-mark gnus-souped-mark)
6971      'reverse)))
6972
6973 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6974 (make-obsolete 'gnus-summary-delete-marked-with
6975                'gnus-summary-limit-exlude-marks)
6976
6977 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6978   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6979 If REVERSE, limit the summary buffer to articles that are marked
6980 with MARKS.  MARKS can either be a string of marks or a list of marks.
6981 Returns how many articles were removed."
6982   (interactive "sMarks: ")
6983   (gnus-summary-limit-to-marks marks t))
6984
6985 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6986   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6987 If REVERSE (the prefix), limit the summary buffer to articles that are
6988 not marked with MARKS.  MARKS can either be a string of marks or a
6989 list of marks.
6990 Returns how many articles were removed."
6991   (interactive "sMarks: \nP")
6992   (prog1
6993       (let ((data gnus-newsgroup-data)
6994             (marks (if (listp marks) marks
6995                      (append marks nil))) ; Transform to list.
6996             articles)
6997         (while data
6998           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6999                   (memq (gnus-data-mark (car data)) marks))
7000             (push (gnus-data-number (car data)) articles))
7001           (setq data (cdr data)))
7002         (gnus-summary-limit articles))
7003     (gnus-summary-position-point)))
7004
7005 (defun gnus-summary-limit-to-score (score)
7006   "Limit to articles with score at or above SCORE."
7007   (interactive "NLimit to articles with score of at least: ")
7008   (let ((data gnus-newsgroup-data)
7009         articles)
7010     (while data
7011       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7012                 score)
7013         (push (gnus-data-number (car data)) articles))
7014       (setq data (cdr data)))
7015     (prog1
7016         (gnus-summary-limit articles)
7017       (gnus-summary-position-point))))
7018
7019 (defun gnus-summary-limit-include-thread (id)
7020   "Display all the hidden articles that is in the thread with ID in it.
7021 When called interactively, ID is the Message-ID of the current
7022 article."
7023   (interactive (list (mail-header-id (gnus-summary-article-header))))
7024   (let ((articles (gnus-articles-in-thread
7025                    (gnus-id-to-thread (gnus-root-id id)))))
7026     (prog1
7027         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7028       (gnus-summary-limit-include-matching-articles
7029        "subject"
7030        (regexp-quote (gnus-simplify-subject-re
7031                       (mail-header-subject (gnus-id-to-header id)))))
7032       (gnus-summary-position-point))))
7033
7034 (defun gnus-summary-limit-include-matching-articles (header regexp)
7035   "Display all the hidden articles that have HEADERs that match REGEXP."
7036   (interactive (list (read-string "Match on header: ")
7037                      (read-string "Regexp: ")))
7038   (let ((articles (gnus-find-matching-articles header regexp)))
7039     (prog1
7040         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7041       (gnus-summary-position-point))))
7042
7043 (defun gnus-summary-limit-include-dormant ()
7044   "Display all the hidden articles that are marked as dormant.
7045 Note that this command only works on a subset of the articles currently
7046 fetched for this group."
7047   (interactive)
7048   (unless gnus-newsgroup-dormant
7049     (error "There are no dormant articles in this group"))
7050   (prog1
7051       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7052     (gnus-summary-position-point)))
7053
7054 (defun gnus-summary-limit-exclude-dormant ()
7055   "Hide all dormant articles."
7056   (interactive)
7057   (prog1
7058       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7059     (gnus-summary-position-point)))
7060
7061 (defun gnus-summary-limit-exclude-childless-dormant ()
7062   "Hide all dormant articles that have no children."
7063   (interactive)
7064   (let ((data (gnus-data-list t))
7065         articles d children)
7066     ;; Find all articles that are either not dormant or have
7067     ;; children.
7068     (while (setq d (pop data))
7069       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7070                 (and (setq children
7071                            (gnus-article-children (gnus-data-number d)))
7072                      (let (found)
7073                        (while children
7074                          (when (memq (car children) articles)
7075                            (setq children nil
7076                                  found t))
7077                          (pop children))
7078                        found)))
7079         (push (gnus-data-number d) articles)))
7080     ;; Do the limiting.
7081     (prog1
7082         (gnus-summary-limit articles)
7083       (gnus-summary-position-point))))
7084
7085 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7086   "Mark all unread excluded articles as read.
7087 If ALL, mark even excluded ticked and dormants as read."
7088   (interactive "P")
7089   (let ((articles (gnus-sorted-complement
7090                    (sort
7091                     (mapcar (lambda (h) (mail-header-number h))
7092                             gnus-newsgroup-headers)
7093                     '<)
7094                    (sort gnus-newsgroup-limit '<)))
7095         article)
7096     (setq gnus-newsgroup-unreads
7097           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7098     (if all
7099         (setq gnus-newsgroup-dormant nil
7100               gnus-newsgroup-marked nil
7101               gnus-newsgroup-reads
7102               (nconc
7103                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7104                gnus-newsgroup-reads))
7105       (while (setq article (pop articles))
7106         (unless (or (memq article gnus-newsgroup-dormant)
7107                     (memq article gnus-newsgroup-marked))
7108           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7109
7110 (defun gnus-summary-limit (articles &optional pop)
7111   (if pop
7112       ;; We pop the previous limit off the stack and use that.
7113       (setq articles (car gnus-newsgroup-limits)
7114             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7115     ;; We use the new limit, so we push the old limit on the stack.
7116     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7117   ;; Set the limit.
7118   (setq gnus-newsgroup-limit articles)
7119   (let ((total (length gnus-newsgroup-data))
7120         (data (gnus-data-find-list (gnus-summary-article-number)))
7121         (gnus-summary-mark-below nil)   ; Inhibit this.
7122         found)
7123     ;; This will do all the work of generating the new summary buffer
7124     ;; according to the new limit.
7125     (gnus-summary-prepare)
7126     ;; Hide any threads, possibly.
7127     (and gnus-show-threads
7128          gnus-thread-hide-subtree
7129          (gnus-summary-hide-all-threads))
7130     ;; Try to return to the article you were at, or one in the
7131     ;; neighborhood.
7132     (when data
7133       ;; We try to find some article after the current one.
7134       (while data
7135         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7136           (setq data nil
7137                 found t))
7138         (setq data (cdr data))))
7139     (unless found
7140       ;; If there is no data, that means that we were after the last
7141       ;; article.  The same goes when we can't find any articles
7142       ;; after the current one.
7143       (goto-char (point-max))
7144       (gnus-summary-find-prev))
7145     (gnus-set-mode-line 'summary)
7146     ;; We return how many articles were removed from the summary
7147     ;; buffer as a result of the new limit.
7148     (- total (length gnus-newsgroup-data))))
7149
7150 (defsubst gnus-invisible-cut-children (threads)
7151   (let ((num 0))
7152     (while threads
7153       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7154         (incf num))
7155       (pop threads))
7156     (< num 2)))
7157
7158 (defsubst gnus-cut-thread (thread)
7159   "Go forwards in the thread until we find an article that we want to display."
7160   (when (or (eq gnus-fetch-old-headers 'some)
7161             (eq gnus-fetch-old-headers 'invisible)
7162             (numberp gnus-fetch-old-headers)
7163             (eq gnus-build-sparse-threads 'some)
7164             (eq gnus-build-sparse-threads 'more))
7165     ;; Deal with old-fetched headers and sparse threads.
7166     (while (and
7167             thread
7168             (or
7169              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7170              (gnus-summary-article-ancient-p
7171               (mail-header-number (car thread))))
7172             (if (or (<= (length (cdr thread)) 1)
7173                     (eq gnus-fetch-old-headers 'invisible))
7174                 (setq gnus-newsgroup-limit
7175                       (delq (mail-header-number (car thread))
7176                             gnus-newsgroup-limit)
7177                       thread (cadr thread))
7178               (when (gnus-invisible-cut-children (cdr thread))
7179                 (let ((th (cdr thread)))
7180                   (while th
7181                     (if (memq (mail-header-number (caar th))
7182                               gnus-newsgroup-limit)
7183                         (setq thread (car th)
7184                               th nil)
7185                       (setq th (cdr th))))))))))
7186   thread)
7187
7188 (defun gnus-cut-threads (threads)
7189   "Cut off all uninteresting articles from the beginning of threads."
7190   (when (or (eq gnus-fetch-old-headers 'some)
7191             (eq gnus-fetch-old-headers 'invisible)
7192             (numberp gnus-fetch-old-headers)
7193             (eq gnus-build-sparse-threads 'some)
7194             (eq gnus-build-sparse-threads 'more))
7195     (let ((th threads))
7196       (while th
7197         (setcar th (gnus-cut-thread (car th)))
7198         (setq th (cdr th)))))
7199   ;; Remove nixed out threads.
7200   (delq nil threads))
7201
7202 (defun gnus-summary-initial-limit (&optional show-if-empty)
7203   "Figure out what the initial limit is supposed to be on group entry.
7204 This entails weeding out unwanted dormants, low-scored articles,
7205 fetch-old-headers verbiage, and so on."
7206   ;; Most groups have nothing to remove.
7207   (if (or gnus-inhibit-limiting
7208           (and (null gnus-newsgroup-dormant)
7209                (eq gnus-newsgroup-display 'gnus-not-ignore)
7210                (not (eq gnus-fetch-old-headers 'some))
7211                (not (numberp gnus-fetch-old-headers))
7212                (not (eq gnus-fetch-old-headers 'invisible))
7213                (null gnus-summary-expunge-below)
7214                (not (eq gnus-build-sparse-threads 'some))
7215                (not (eq gnus-build-sparse-threads 'more))
7216                (null gnus-thread-expunge-below)
7217                (not gnus-use-nocem)))
7218       ()                                ; Do nothing.
7219     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7220     (setq gnus-newsgroup-limit nil)
7221     (mapatoms
7222      (lambda (node)
7223        (unless (car (symbol-value node))
7224          ;; These threads have no parents -- they are roots.
7225          (let ((nodes (cdr (symbol-value node)))
7226                thread)
7227            (while nodes
7228              (if (and gnus-thread-expunge-below
7229                       (< (gnus-thread-total-score (car nodes))
7230                          gnus-thread-expunge-below))
7231                  (gnus-expunge-thread (pop nodes))
7232                (setq thread (pop nodes))
7233                (gnus-summary-limit-children thread))))))
7234      gnus-newsgroup-dependencies)
7235     ;; If this limitation resulted in an empty group, we might
7236     ;; pop the previous limit and use it instead.
7237     (when (and (not gnus-newsgroup-limit)
7238                show-if-empty)
7239       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7240     gnus-newsgroup-limit))
7241
7242 (defun gnus-summary-limit-children (thread)
7243   "Return 1 if this subthread is visible and 0 if it is not."
7244   ;; First we get the number of visible children to this thread.  This
7245   ;; is done by recursing down the thread using this function, so this
7246   ;; will really go down to a leaf article first, before slowly
7247   ;; working its way up towards the root.
7248   (when thread
7249     (let ((children
7250            (if (cdr thread)
7251                (apply '+ (mapcar 'gnus-summary-limit-children
7252                                  (cdr thread)))
7253              0))
7254           (number (mail-header-number (car thread)))
7255           score)
7256       (if (and
7257            (not (memq number gnus-newsgroup-marked))
7258            (or
7259             ;; If this article is dormant and has absolutely no visible
7260             ;; children, then this article isn't visible.
7261             (and (memq number gnus-newsgroup-dormant)
7262                  (zerop children))
7263             ;; If this is "fetch-old-headered" and there is no
7264             ;; visible children, then we don't want this article.
7265             (and (or (eq gnus-fetch-old-headers 'some)
7266                      (numberp gnus-fetch-old-headers))
7267                  (gnus-summary-article-ancient-p number)
7268                  (zerop children))
7269             ;; If this is "fetch-old-headered" and `invisible', then
7270             ;; we don't want this article.
7271             (and (eq gnus-fetch-old-headers 'invisible)
7272                  (gnus-summary-article-ancient-p number))
7273             ;; If this is a sparsely inserted article with no children,
7274             ;; we don't want it.
7275             (and (eq gnus-build-sparse-threads 'some)
7276                  (gnus-summary-article-sparse-p number)
7277                  (zerop children))
7278             ;; If we use expunging, and this article is really
7279             ;; low-scored, then we don't want this article.
7280             (when (and gnus-summary-expunge-below
7281                        (< (setq score
7282                                 (or (cdr (assq number gnus-newsgroup-scored))
7283                                     gnus-summary-default-score))
7284                           gnus-summary-expunge-below))
7285               ;; We increase the expunge-tally here, but that has
7286               ;; nothing to do with the limits, really.
7287               (incf gnus-newsgroup-expunged-tally)
7288               ;; We also mark as read here, if that's wanted.
7289               (when (and gnus-summary-mark-below
7290                          (< score gnus-summary-mark-below))
7291                 (setq gnus-newsgroup-unreads
7292                       (delq number gnus-newsgroup-unreads))
7293                 (if gnus-newsgroup-auto-expire
7294                     (push number gnus-newsgroup-expirable)
7295                   (push (cons number gnus-low-score-mark)
7296                         gnus-newsgroup-reads)))
7297               t)
7298             ;; Do the `display' group parameter.
7299             (and gnus-newsgroup-display
7300                  (not (funcall gnus-newsgroup-display)))
7301             ;; Check NoCeM things.
7302             (if (and gnus-use-nocem
7303                      (gnus-nocem-unwanted-article-p
7304                       (mail-header-id (car thread))))
7305                 (progn
7306                   (setq gnus-newsgroup-unreads
7307                         (delq number gnus-newsgroup-unreads))
7308                   t))))
7309           ;; Nope, invisible article.
7310           0
7311         ;; Ok, this article is to be visible, so we add it to the limit
7312         ;; and return 1.
7313         (push number gnus-newsgroup-limit)
7314         1))))
7315
7316 (defun gnus-expunge-thread (thread)
7317   "Mark all articles in THREAD as read."
7318   (let* ((number (mail-header-number (car thread))))
7319     (incf gnus-newsgroup-expunged-tally)
7320     ;; We also mark as read here, if that's wanted.
7321     (setq gnus-newsgroup-unreads
7322           (delq number gnus-newsgroup-unreads))
7323     (if gnus-newsgroup-auto-expire
7324         (push number gnus-newsgroup-expirable)
7325       (push (cons number gnus-low-score-mark)
7326             gnus-newsgroup-reads)))
7327   ;; Go recursively through all subthreads.
7328   (mapcar 'gnus-expunge-thread (cdr thread)))
7329
7330 ;; Summary article oriented commands
7331
7332 (defun gnus-summary-refer-parent-article (n)
7333   "Refer parent article N times.
7334 If N is negative, go to ancestor -N instead.
7335 The difference between N and the number of articles fetched is returned."
7336   (interactive "p")
7337   (let ((skip 1)
7338         error header ref)
7339     (when (not (natnump n))
7340       (setq skip (abs n)
7341             n 1))
7342     (while (and (> n 0)
7343                 (not error))
7344       (setq header (gnus-summary-article-header))
7345       (if (and (eq (mail-header-number header)
7346                    (cdr gnus-article-current))
7347                (equal gnus-newsgroup-name
7348                       (car gnus-article-current)))
7349           ;; If we try to find the parent of the currently
7350           ;; displayed article, then we take a look at the actual
7351           ;; References header, since this is slightly more
7352           ;; reliable than the References field we got from the
7353           ;; server.
7354           (save-excursion
7355             (set-buffer gnus-original-article-buffer)
7356             (nnheader-narrow-to-headers)
7357             (unless (setq ref (message-fetch-field "references"))
7358               (setq ref (message-fetch-field "in-reply-to")))
7359             (widen))
7360         (setq ref
7361               ;; It's not the current article, so we take a bet on
7362               ;; the value we got from the server.
7363               (mail-header-references header)))
7364       (if (and ref
7365                (not (equal ref "")))
7366           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7367             (gnus-message 1 "Couldn't find parent"))
7368         (gnus-message 1 "No references in article %d"
7369                       (gnus-summary-article-number))
7370         (setq error t))
7371       (decf n))
7372     (gnus-summary-position-point)
7373     n))
7374
7375 (defun gnus-summary-refer-references ()
7376   "Fetch all articles mentioned in the References header.
7377 Return the number of articles fetched."
7378   (interactive)
7379   (let ((ref (mail-header-references (gnus-summary-article-header)))
7380         (current (gnus-summary-article-number))
7381         (n 0))
7382     (if (or (not ref)
7383             (equal ref ""))
7384         (error "No References in the current article")
7385       ;; For each Message-ID in the References header...
7386       (while (string-match "<[^>]*>" ref)
7387         (incf n)
7388         ;; ... fetch that article.
7389         (gnus-summary-refer-article
7390          (prog1 (match-string 0 ref)
7391            (setq ref (substring ref (match-end 0))))))
7392       (gnus-summary-goto-subject current)
7393       (gnus-summary-position-point)
7394       n)))
7395
7396 (defun gnus-summary-refer-thread (&optional limit)
7397   "Fetch all articles in the current thread.
7398 If LIMIT (the numerical prefix), fetch that many old headers instead
7399 of what's specified by the `gnus-refer-thread-limit' variable."
7400   (interactive "P")
7401   (let ((id (mail-header-id (gnus-summary-article-header)))
7402         (limit (if limit (prefix-numeric-value limit)
7403                  gnus-refer-thread-limit)))
7404     ;; We want to fetch LIMIT *old* headers, but we also have to
7405     ;; re-fetch all the headers in the current buffer, because many of
7406     ;; them may be undisplayed.  So we adjust LIMIT.
7407     (when (numberp limit)
7408       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7409     (unless (eq gnus-fetch-old-headers 'invisible)
7410       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7411       ;; Retrieve the headers and read them in.
7412       (if (eq (gnus-retrieve-headers
7413                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7414               'nov)
7415           (gnus-build-all-threads)
7416         (error "Can't fetch thread from backends that don't support NOV"))
7417       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7418     (gnus-summary-limit-include-thread id)))
7419
7420 (defun gnus-summary-refer-article (message-id)
7421   "Fetch an article specified by MESSAGE-ID."
7422   (interactive "sMessage-ID: ")
7423   (when (and (stringp message-id)
7424              (not (zerop (length message-id))))
7425     ;; Construct the correct Message-ID if necessary.
7426     ;; Suggested by tale@pawl.rpi.edu.
7427     (unless (string-match "^<" message-id)
7428       (setq message-id (concat "<" message-id)))
7429     (unless (string-match ">$" message-id)
7430       (setq message-id (concat message-id ">")))
7431     (let* ((header (gnus-id-to-header message-id))
7432            (sparse (and header
7433                         (gnus-summary-article-sparse-p
7434                          (mail-header-number header))
7435                         (memq (mail-header-number header)
7436                               gnus-newsgroup-limit)))
7437            number)
7438       (cond
7439        ;; If the article is present in the buffer we just go to it.
7440        ((and header
7441              (or (not (gnus-summary-article-sparse-p
7442                        (mail-header-number header)))
7443                  sparse))
7444         (prog1
7445             (gnus-summary-goto-article
7446              (mail-header-number header) nil t)
7447           (when sparse
7448             (gnus-summary-update-article (mail-header-number header)))))
7449        (t
7450         ;; We fetch the article.
7451         (catch 'found
7452           (dolist (gnus-override-method (gnus-refer-article-methods))
7453             (gnus-check-server gnus-override-method)
7454             ;; Fetch the header, and display the article.
7455             (when (setq number (gnus-summary-insert-subject message-id))
7456               (gnus-summary-select-article nil nil nil number)
7457               (throw 'found t)))
7458           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7459
7460 (defun gnus-refer-article-methods ()
7461   "Return a list of referrable methods."
7462   (cond
7463    ;; No method, so we default to current and native.
7464    ((null gnus-refer-article-method)
7465     (list gnus-current-select-method gnus-select-method))
7466    ;; Current.
7467    ((eq 'current gnus-refer-article-method)
7468     (list gnus-current-select-method))
7469    ;; List of select methods.
7470    ((not (and (symbolp (car gnus-refer-article-method))
7471               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7472     (let (out)
7473       (dolist (method gnus-refer-article-method)
7474         (push (if (eq 'current method)
7475                   gnus-current-select-method
7476                 method)
7477               out))
7478       (nreverse out)))
7479    ;; One single select method.
7480    (t
7481     (list gnus-refer-article-method))))
7482
7483 (defun gnus-summary-edit-parameters ()
7484   "Edit the group parameters of the current group."
7485   (interactive)
7486   (gnus-group-edit-group gnus-newsgroup-name 'params))
7487
7488 (defun gnus-summary-customize-parameters ()
7489   "Customize the group parameters of the current group."
7490   (interactive)
7491   (gnus-group-customize gnus-newsgroup-name))
7492
7493 (defun gnus-summary-enter-digest-group (&optional force)
7494   "Enter an nndoc group based on the current article.
7495 If FORCE, force a digest interpretation.  If not, try
7496 to guess what the document format is."
7497   (interactive "P")
7498   (let ((conf gnus-current-window-configuration))
7499     (save-excursion
7500       (gnus-summary-select-article))
7501     (setq gnus-current-window-configuration conf)
7502     (let* ((name (format "%s-%d"
7503                          (gnus-group-prefixed-name
7504                           gnus-newsgroup-name (list 'nndoc ""))
7505                          (save-excursion
7506                            (set-buffer gnus-summary-buffer)
7507                            gnus-current-article)))
7508            (ogroup gnus-newsgroup-name)
7509            (params (append (gnus-info-params (gnus-get-info ogroup))
7510                            (list (cons 'to-group ogroup))
7511                            (list (cons 'save-article-group ogroup))))
7512            (case-fold-search t)
7513            (buf (current-buffer))
7514            dig to-address)
7515       (save-excursion
7516         (set-buffer gnus-original-article-buffer)
7517         ;; Have the digest group inherit the main mail address of
7518         ;; the parent article.
7519         (when (setq to-address (or (message-fetch-field "reply-to")
7520                                    (message-fetch-field "from")))
7521           (setq params (append
7522                         (list (cons 'to-address
7523                                     (funcall gnus-decode-encoded-word-function
7524                                              to-address))))))
7525         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7526         (insert-buffer-substring gnus-original-article-buffer)
7527         ;; Remove lines that may lead nndoc to misinterpret the
7528         ;; document type.
7529         (narrow-to-region
7530          (goto-char (point-min))
7531          (or (search-forward "\n\n" nil t) (point)))
7532         (goto-char (point-min))
7533         (delete-matching-lines "^Path:\\|^From ")
7534         (widen))
7535       (unwind-protect
7536           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7537                     (gnus-newsgroup-ephemeral-ignored-charsets
7538                      gnus-newsgroup-ignored-charsets))
7539                 (gnus-group-read-ephemeral-group
7540                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7541                               (nndoc-article-type
7542                                ,(if force 'mbox 'guess))) t))
7543             ;; Make all postings to this group go to the parent group.
7544               (nconc (gnus-info-params (gnus-get-info name))
7545                      params)
7546             ;; Couldn't select this doc group.
7547             (switch-to-buffer buf)
7548             (gnus-set-global-variables)
7549             (gnus-configure-windows 'summary)
7550             (gnus-message 3 "Article couldn't be entered?"))
7551         (kill-buffer dig)))))
7552
7553 (defun gnus-summary-read-document (n)
7554   "Open a new group based on the current article(s).
7555 This will allow you to read digests and other similar
7556 documents as newsgroups.
7557 Obeys the standard process/prefix convention."
7558   (interactive "P")
7559   (let* ((articles (gnus-summary-work-articles n))
7560          (ogroup gnus-newsgroup-name)
7561          (params (append (gnus-info-params (gnus-get-info ogroup))
7562                          (list (cons 'to-group ogroup))))
7563          article group egroup groups vgroup)
7564     (while (setq article (pop articles))
7565       (setq group (format "%s-%d" gnus-newsgroup-name article))
7566       (gnus-summary-remove-process-mark article)
7567       (when (gnus-summary-display-article article)
7568         (save-excursion
7569           (with-temp-buffer
7570             (insert-buffer-substring gnus-original-article-buffer)
7571             ;; Remove some headers that may lead nndoc to make
7572             ;; the wrong guess.
7573             (message-narrow-to-head)
7574             (goto-char (point-min))
7575             (delete-matching-lines "^\\(Path\\):\\|^From ")
7576             (widen)
7577             (if (setq egroup
7578                       (gnus-group-read-ephemeral-group
7579                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7580                                      (nndoc-article-type guess))
7581                        t nil t))
7582                 (progn
7583             ;; Make all postings to this group go to the parent group.
7584                   (nconc (gnus-info-params (gnus-get-info egroup))
7585                          params)
7586                   (push egroup groups))
7587               ;; Couldn't select this doc group.
7588               (gnus-error 3 "Article couldn't be entered"))))))
7589     ;; Now we have selected all the documents.
7590     (cond
7591      ((not groups)
7592       (error "None of the articles could be interpreted as documents"))
7593      ((gnus-group-read-ephemeral-group
7594        (setq vgroup (format
7595                      "nnvirtual:%s-%s" gnus-newsgroup-name
7596                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7597        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7598        t
7599        (cons (current-buffer) 'summary)))
7600      (t
7601       (error "Couldn't select virtual nndoc group")))))
7602
7603 (defun gnus-summary-isearch-article (&optional regexp-p)
7604   "Do incremental search forward on the current article.
7605 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7606   (interactive "P")
7607   (gnus-summary-select-article)
7608   (gnus-configure-windows 'article)
7609   (gnus-eval-in-buffer-window gnus-article-buffer
7610     (save-restriction
7611       (widen)
7612       (isearch-forward regexp-p))))
7613
7614 (defun gnus-summary-search-article-forward (regexp &optional backward)
7615   "Search for an article containing REGEXP forward.
7616 If BACKWARD, search backward instead."
7617   (interactive
7618    (list (read-string
7619           (format "Search article %s (regexp%s): "
7620                   (if current-prefix-arg "backward" "forward")
7621                   (if gnus-last-search-regexp
7622                       (concat ", default " gnus-last-search-regexp)
7623                     "")))
7624          current-prefix-arg))
7625   (if (string-equal regexp "")
7626       (setq regexp (or gnus-last-search-regexp ""))
7627     (setq gnus-last-search-regexp regexp)
7628     (setq gnus-article-before-search gnus-current-article))
7629   ;; Intentionally set gnus-last-article.
7630   (setq gnus-last-article gnus-article-before-search)
7631   (let ((gnus-last-article gnus-last-article))
7632     (if (gnus-summary-search-article regexp backward)
7633         (gnus-summary-show-thread)
7634       (error "Search failed: \"%s\"" regexp))))
7635
7636 (defun gnus-summary-search-article-backward (regexp)
7637   "Search for an article containing REGEXP backward."
7638   (interactive
7639    (list (read-string
7640           (format "Search article backward (regexp%s): "
7641                   (if gnus-last-search-regexp
7642                       (concat ", default " gnus-last-search-regexp)
7643                     "")))))
7644   (gnus-summary-search-article-forward regexp 'backward))
7645
7646 (defun gnus-summary-search-article (regexp &optional backward)
7647   "Search for an article containing REGEXP.
7648 Optional argument BACKWARD means do search for backward.
7649 `gnus-select-article-hook' is not called during the search."
7650   ;; We have to require this here to make sure that the following
7651   ;; dynamic binding isn't shadowed by autoloading.
7652   (require 'gnus-async)
7653   (require 'gnus-art)
7654   (let ((gnus-select-article-hook nil)  ;Disable hook.
7655         (gnus-article-prepare-hook nil)
7656         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7657         (gnus-use-article-prefetch nil)
7658         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7659         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7660         (sum (current-buffer))
7661         (gnus-display-mime-function nil)
7662         (found nil)
7663         point)
7664     (gnus-save-hidden-threads
7665       (gnus-summary-select-article)
7666       (set-buffer gnus-article-buffer)
7667       (goto-char (window-point (get-buffer-window (current-buffer))))
7668       (when backward
7669         (forward-line -1))
7670       (while (not found)
7671         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7672         (if (if backward
7673                 (re-search-backward regexp nil t)
7674               (re-search-forward regexp nil t))
7675             ;; We found the regexp.
7676             (progn
7677               (setq found 'found)
7678               (beginning-of-line)
7679               (set-window-start
7680                (get-buffer-window (current-buffer))
7681                (point))
7682               (forward-line 1)
7683               (set-window-point
7684                (get-buffer-window (current-buffer))
7685                (point))
7686               (set-buffer sum)
7687               (setq point (point)))
7688           ;; We didn't find it, so we go to the next article.
7689           (set-buffer sum)
7690           (setq found 'not)
7691           (while (eq found 'not)
7692             (if (not (if backward (gnus-summary-find-prev)
7693                        (gnus-summary-find-next)))
7694                 ;; No more articles.
7695                 (setq found t)
7696               ;; Select the next article and adjust point.
7697               (unless (gnus-summary-article-sparse-p
7698                        (gnus-summary-article-number))
7699                 (setq found nil)
7700                 (gnus-summary-select-article)
7701                 (set-buffer gnus-article-buffer)
7702                 (widen)
7703                 (goto-char (if backward (point-max) (point-min))))))))
7704       (gnus-message 7 ""))
7705     ;; Return whether we found the regexp.
7706     (when (eq found 'found)
7707       (goto-char point)
7708       (gnus-summary-show-thread)
7709       (gnus-summary-goto-subject gnus-current-article)
7710       (gnus-summary-position-point)
7711       t)))
7712
7713 (defun gnus-find-matching-articles (header regexp)
7714   "Return a list of all articles that match REGEXP on HEADER.
7715 This search includes all articles in the current group that Gnus has
7716 fetched headers for, whether they are displayed or not."
7717   (let ((articles nil)
7718         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7719         (case-fold-search t))
7720     (dolist (header gnus-newsgroup-headers)
7721       (when (string-match regexp (funcall func header))
7722         (push (mail-header-number header) articles)))
7723     (nreverse articles)))
7724
7725 (defun gnus-summary-find-matching (header regexp &optional backward unread
7726                                           not-case-fold not-matching)
7727   "Return a list of all articles that match REGEXP on HEADER.
7728 The search stars on the current article and goes forwards unless
7729 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7730 If UNREAD is non-nil, only unread articles will
7731 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7732 in the comparisons. If NOT-MATCHING, return a list of all articles that 
7733 not match REGEXP on HEADER."
7734   (let ((case-fold-search (not not-case-fold))
7735         articles d func)
7736     (if (consp header)
7737         (if (eq (car header) 'extra)
7738             (setq func
7739                   `(lambda (h)
7740                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7741                          "")))
7742           (error "%s is an invalid header" header))
7743       (unless (fboundp (intern (concat "mail-header-" header)))
7744         (error "%s is not a valid header" header))
7745       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7746     (dolist (d (if (eq backward 'all)
7747                    gnus-newsgroup-data
7748                  (gnus-data-find-list
7749                   (gnus-summary-article-number)
7750                   (gnus-data-list backward))))
7751       (when (and (or (not unread)       ; We want all articles...
7752                      (gnus-data-unread-p d)) ; Or just unreads.
7753                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7754                  (if not-matching
7755                      (not (string-match 
7756                            regexp
7757                            (funcall func (gnus-data-header d))))
7758                    (string-match regexp
7759                                  (funcall func (gnus-data-header d)))))
7760         (push (gnus-data-number d) articles))) ; Success!
7761     (nreverse articles)))
7762
7763 (defun gnus-summary-execute-command (header regexp command &optional backward)
7764   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7765 If HEADER is an empty string (or nil), the match is done on the entire
7766 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7767   (interactive
7768    (list (let ((completion-ignore-case t))
7769            (completing-read
7770             "Header name: "
7771             (mapcar (lambda (header) (list (format "%s" header)))
7772                     (append
7773                      '("Number" "Subject" "From" "Lines" "Date"
7774                        "Message-ID" "Xref" "References" "Body")
7775                      gnus-extra-headers))
7776             nil 'require-match))
7777          (read-string "Regexp: ")
7778          (read-key-sequence "Command: ")
7779          current-prefix-arg))
7780   (when (equal header "Body")
7781     (setq header ""))
7782   ;; Hidden thread subtrees must be searched as well.
7783   (gnus-summary-show-all-threads)
7784   ;; We don't want to change current point nor window configuration.
7785   (save-excursion
7786     (save-window-excursion
7787       (gnus-message 6 "Executing %s..." (key-description command))
7788 ;; We'd like to execute COMMAND interactively so as to give arguments.
7789       (gnus-execute header regexp
7790                     `(call-interactively ',(key-binding command))
7791                     backward)
7792       (gnus-message 6 "Executing %s...done" (key-description command)))))
7793
7794 (defun gnus-summary-beginning-of-article ()
7795   "Scroll the article back to the beginning."
7796   (interactive)
7797   (gnus-summary-select-article)
7798   (gnus-configure-windows 'article)
7799   (gnus-eval-in-buffer-window gnus-article-buffer
7800     (widen)
7801     (goto-char (point-min))
7802     (when gnus-page-broken
7803       (gnus-narrow-to-page))))
7804
7805 (defun gnus-summary-end-of-article ()
7806   "Scroll to the end of the article."
7807   (interactive)
7808   (gnus-summary-select-article)
7809   (gnus-configure-windows 'article)
7810   (gnus-eval-in-buffer-window gnus-article-buffer
7811     (widen)
7812     (goto-char (point-max))
7813     (recenter -3)
7814     (when gnus-page-broken
7815       (gnus-narrow-to-page))))
7816
7817 (defun gnus-summary-print-truncate-and-quote (string &optional len)
7818   "Truncate to LEN and quote all \"(\"'s in STRING."
7819   (gnus-replace-in-string (if (and len (> (length string) len))
7820                               (substring string 0 len) 
7821                             string)
7822                           "[()]" "\\\\\\&"))
7823
7824 (defun gnus-summary-print-article (&optional filename n)
7825   "Generate and print a PostScript image of the N next (mail) articles.
7826
7827 If N is negative, print the N previous articles.  If N is nil and articles
7828 have been marked with the process mark, print these instead.
7829
7830 If the optional first argument FILENAME is nil, send the image to the
7831 printer.  If FILENAME is a string, save the PostScript image in a file with
7832 that name.  If FILENAME is a number, prompt the user for the name of the file
7833 to save in."
7834   (interactive (list (ps-print-preprint current-prefix-arg)))
7835   (dolist (article (gnus-summary-work-articles n))
7836     (gnus-summary-select-article nil nil 'pseudo article)
7837     (gnus-eval-in-buffer-window gnus-article-buffer
7838       (let ((buffer (generate-new-buffer " *print*")))
7839         (unwind-protect
7840             (progn
7841               (copy-to-buffer buffer (point-min) (point-max))
7842               (set-buffer buffer)
7843               (gnus-article-delete-invisible-text)
7844               (when (gnus-visual-p 'article-highlight 'highlight)
7845                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7846                 ;; highlight.
7847                 (let ((gnus-article-buffer buffer))
7848                   (gnus-article-highlight-citation t)
7849                   (gnus-article-highlight-signature)))
7850               (let ((ps-left-header
7851                      (list
7852                       (concat "("
7853                               (gnus-summary-print-truncate-and-quote
7854                                (mail-header-subject gnus-current-headers) 
7855                                66) ")")
7856                       (concat "("
7857                               (gnus-summary-print-truncate-and-quote
7858                                (mail-header-from gnus-current-headers) 
7859                                45) ")")))
7860                     (ps-right-header
7861                      (list
7862                       "/pagenumberstring load"
7863                       (concat "("
7864                               (mail-header-date gnus-current-headers) ")"))))
7865                 (gnus-run-hooks 'gnus-ps-print-hook)
7866                 (save-excursion
7867                   (if window-system
7868                       (ps-spool-buffer-with-faces)
7869                     (ps-spool-buffer)))))
7870           (kill-buffer buffer))))
7871     (gnus-summary-remove-process-mark article))
7872   (ps-despool filename))
7873
7874 (defun gnus-summary-show-article (&optional arg)
7875   "Force redisplaying of the current article.
7876 If ARG (the prefix) is a number, show the article with the charset
7877 defined in `gnus-summary-show-article-charset-alist', or the charset
7878 input.
7879 If ARG (the prefix) is non-nil and not a number, show the raw article
7880 without any article massaging functions being run.  Normally, the key strokes 
7881 are `C-u g'."
7882   (interactive "P")
7883   (cond
7884    ((numberp arg)
7885     (gnus-summary-show-article t)
7886     (let ((gnus-newsgroup-charset
7887            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7888                (mm-read-coding-system
7889                 "View as charset: "
7890                 (save-excursion
7891                   (set-buffer gnus-article-buffer)
7892                   (let ((coding-systems
7893                          (detect-coding-region (point) (point-max))))
7894                     (or (car-safe coding-systems)
7895                         coding-systems))))))
7896           (gnus-newsgroup-ignored-charsets 'gnus-all))
7897       (gnus-summary-select-article nil 'force)
7898       (let ((deps gnus-newsgroup-dependencies)
7899             head header lines)
7900         (save-excursion
7901           (set-buffer gnus-original-article-buffer)
7902           (save-restriction
7903             (message-narrow-to-head)
7904             (setq head (buffer-string))
7905             (goto-char (point-min))
7906             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
7907               (goto-char (point-max))
7908               (widen)
7909               (setq lines (1- (count-lines (point) (point-max))))))
7910           (with-temp-buffer
7911             (insert (format "211 %d Article retrieved.\n"
7912                             (cdr gnus-article-current)))
7913             (insert head)
7914             (if lines (insert (format "Lines: %d\n" lines)))
7915             (insert ".\n")
7916             (let ((nntp-server-buffer (current-buffer)))
7917               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7918         (gnus-data-set-header
7919          (gnus-data-find (cdr gnus-article-current))
7920          header)
7921         (gnus-summary-update-article-line
7922          (cdr gnus-article-current) header)
7923         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7924           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7925    ((not arg)
7926     ;; Select the article the normal way.
7927     (gnus-summary-select-article nil 'force))
7928    (t
7929     ;; We have to require this here to make sure that the following
7930     ;; dynamic binding isn't shadowed by autoloading.
7931     (require 'gnus-async)
7932     (require 'gnus-art)
7933     ;; Bind the article treatment functions to nil.
7934     (let ((gnus-have-all-headers t)
7935           gnus-article-prepare-hook
7936           gnus-article-decode-hook
7937           gnus-display-mime-function
7938           gnus-break-pages)
7939       ;; Destroy any MIME parts.
7940       (when (gnus-buffer-live-p gnus-article-buffer)
7941         (save-excursion
7942           (set-buffer gnus-article-buffer)
7943           (mm-destroy-parts gnus-article-mime-handles)
7944           ;; Set it to nil for safety reason.
7945           (setq gnus-article-mime-handle-alist nil)
7946           (setq gnus-article-mime-handles nil)))
7947       (gnus-summary-select-article nil 'force))))
7948   (gnus-summary-goto-subject gnus-current-article)
7949   (gnus-summary-position-point))
7950
7951 (defun gnus-summary-show-raw-article ()
7952   "Show the raw article without any article massaging functions being run."
7953   (interactive)
7954   (gnus-summary-show-article t))
7955
7956 (defun gnus-summary-verbose-headers (&optional arg)
7957   "Toggle permanent full header display.
7958 If ARG is a positive number, turn header display on.
7959 If ARG is a negative number, turn header display off."
7960   (interactive "P")
7961   (setq gnus-show-all-headers
7962         (cond ((or (not (numberp arg))
7963                    (zerop arg))
7964                (not gnus-show-all-headers))
7965               ((natnump arg)
7966                t)))
7967   (gnus-summary-show-article))
7968
7969 (defun gnus-summary-toggle-header (&optional arg)
7970   "Show the headers if they are hidden, or hide them if they are shown.
7971 If ARG is a positive number, show the entire header.
7972 If ARG is a negative number, hide the unwanted header lines."
7973   (interactive "P")
7974   (save-excursion
7975     (set-buffer gnus-article-buffer)
7976     (save-restriction
7977       (let* ((buffer-read-only nil)
7978              (inhibit-point-motion-hooks t)
7979              hidden e)
7980         (setq hidden
7981               (if (numberp arg)
7982                   (>= arg 0)
7983                 (save-restriction
7984                   (article-narrow-to-head)
7985                   (gnus-article-hidden-text-p 'headers))))
7986         (goto-char (point-min))
7987         (when (search-forward "\n\n" nil t)
7988           (delete-region (point-min) (1- (point))))
7989         (goto-char (point-min))
7990         (save-excursion
7991           (set-buffer gnus-original-article-buffer)
7992           (goto-char (point-min))
7993           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7994         (insert-buffer-substring gnus-original-article-buffer 1 e)
7995         (save-restriction
7996           (narrow-to-region (point-min) (point))
7997           (article-decode-encoded-words)
7998           (if  hidden
7999               (let ((gnus-treat-hide-headers nil)
8000                     (gnus-treat-hide-boring-headers nil))
8001                 (setq gnus-article-wash-types
8002                       (delq 'headers gnus-article-wash-types))
8003                 (gnus-treat-article 'head))
8004             (gnus-treat-article 'head)))
8005         (gnus-set-mode-line 'article)))))
8006
8007 (defun gnus-summary-show-all-headers ()
8008   "Make all header lines visible."
8009   (interactive)
8010   (gnus-summary-toggle-header 1))
8011
8012 (defun gnus-summary-caesar-message (&optional arg)
8013   "Caesar rotate the current article by 13.
8014 The numerical prefix specifies how many places to rotate each letter
8015 forward."
8016   (interactive "P")
8017   (gnus-summary-select-article)
8018   (let ((mail-header-separator ""))
8019     (gnus-eval-in-buffer-window gnus-article-buffer
8020       (save-restriction
8021         (widen)
8022         (let ((start (window-start))
8023               buffer-read-only)
8024           (message-caesar-buffer-body arg)
8025           (set-window-start (get-buffer-window (current-buffer)) start))))))
8026
8027 (defun gnus-summary-stop-page-breaking ()
8028   "Stop page breaking in the current article."
8029   (interactive)
8030   (gnus-summary-select-article)
8031   (gnus-eval-in-buffer-window gnus-article-buffer
8032     (widen)
8033     (when (gnus-visual-p 'page-marker)
8034       (let ((buffer-read-only nil))
8035         (gnus-remove-text-with-property 'gnus-prev)
8036         (gnus-remove-text-with-property 'gnus-next))
8037       (setq gnus-page-broken nil))))
8038
8039 (defun gnus-summary-move-article (&optional n to-newsgroup
8040                                             select-method action)
8041   "Move the current article to a different newsgroup.
8042 If N is a positive number, move the N next articles.
8043 If N is a negative number, move the N previous articles.
8044 If N is nil and any articles have been marked with the process mark,
8045 move those articles instead.
8046 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8047 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8048 re-spool using this method.
8049
8050 For this function to work, both the current newsgroup and the
8051 newsgroup that you want to move to have to support the `request-move'
8052 and `request-accept' functions.
8053
8054 ACTION can be either `move' (the default), `crosspost' or `copy'."
8055   (interactive "P")
8056   (unless action
8057     (setq action 'move))
8058   ;; Check whether the source group supports the required functions.
8059   (cond ((and (eq action 'move)
8060               (not (gnus-check-backend-function
8061                     'request-move-article gnus-newsgroup-name)))
8062          (error "The current group does not support article moving"))
8063         ((and (eq action 'crosspost)
8064               (not (gnus-check-backend-function
8065                     'request-replace-article gnus-newsgroup-name)))
8066          (error "The current group does not support article editing")))
8067   (let ((articles (gnus-summary-work-articles n))
8068         (prefix (if (gnus-check-backend-function
8069                      'request-move-article gnus-newsgroup-name)
8070                     (gnus-group-real-prefix gnus-newsgroup-name)
8071                   ""))
8072         (names '((move "Move" "Moving")
8073                  (copy "Copy" "Copying")
8074                  (crosspost "Crosspost" "Crossposting")))
8075         (copy-buf (save-excursion
8076                     (nnheader-set-temp-buffer " *copy article*")))
8077         art-group to-method new-xref article to-groups)
8078     (unless (assq action names)
8079       (error "Unknown action %s" action))
8080     ;; Read the newsgroup name.
8081     (when (and (not to-newsgroup)
8082                (not select-method))
8083       (setq to-newsgroup
8084             (gnus-read-move-group-name
8085              (cadr (assq action names))
8086              (symbol-value (intern (format "gnus-current-%s-group" action)))
8087              articles prefix))
8088       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8089     (setq to-method (or select-method
8090                         (gnus-server-to-method
8091                          (gnus-group-method to-newsgroup))))
8092     ;; Check the method we are to move this article to...
8093     (unless (gnus-check-backend-function
8094              'request-accept-article (car to-method))
8095       (error "%s does not support article copying" (car to-method)))
8096     (unless (gnus-check-server to-method)
8097       (error "Can't open server %s" (car to-method)))
8098     (gnus-message 6 "%s to %s: %s..."
8099                   (caddr (assq action names))
8100                   (or (car select-method) to-newsgroup) articles)
8101     (while articles
8102       (setq article (pop articles))
8103       (setq
8104        art-group
8105        (cond
8106         ;; Move the article.
8107         ((eq action 'move)
8108          ;; Remove this article from future suppression.
8109          (gnus-dup-unsuppress-article article)
8110          (gnus-request-move-article
8111           article                       ; Article to move
8112           gnus-newsgroup-name           ; From newsgroup
8113           (nth 1 (gnus-find-method-for-group
8114                   gnus-newsgroup-name)) ; Server
8115           (list 'gnus-request-accept-article
8116                 to-newsgroup (list 'quote select-method)
8117                 (not articles) t)       ; Accept form
8118           (not articles)))              ; Only save nov last time
8119         ;; Copy the article.
8120         ((eq action 'copy)
8121          (save-excursion
8122            (set-buffer copy-buf)
8123            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8124              (gnus-request-accept-article
8125               to-newsgroup select-method (not articles) t))))
8126         ;; Crosspost the article.
8127         ((eq action 'crosspost)
8128          (let ((xref (message-tokenize-header
8129                       (mail-header-xref (gnus-summary-article-header article))
8130                       " ")))
8131            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8132                                   ":" (number-to-string article)))
8133            (unless xref
8134              (setq xref (list (system-name))))
8135            (setq new-xref
8136                  (concat
8137                   (mapconcat 'identity
8138                              (delete "Xref:" (delete new-xref xref))
8139                              " ")
8140                   " " new-xref))
8141            (save-excursion
8142              (set-buffer copy-buf)
8143              ;; First put the article in the destination group.
8144              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8145              (when (consp (setq art-group
8146                                 (gnus-request-accept-article
8147                                  to-newsgroup select-method (not articles))))
8148                (setq new-xref (concat new-xref " " (car art-group)
8149                                       ":"
8150                                       (number-to-string (cdr art-group))))
8151                ;; Now we have the new Xrefs header, so we insert
8152                ;; it and replace the new article.
8153                (nnheader-replace-header "Xref" new-xref)
8154                (gnus-request-replace-article
8155                 (cdr art-group) to-newsgroup (current-buffer))
8156                art-group))))))
8157       (cond
8158        ((not art-group)
8159         (gnus-message 1 "Couldn't %s article %s: %s"
8160                       (cadr (assq action names)) article
8161                       (nnheader-get-report (car to-method))))
8162        ((eq art-group 'junk)
8163         (when (eq action 'move)
8164           (gnus-summary-mark-article article gnus-canceled-mark)
8165           (gnus-message 4 "Deleted article %s" article)))
8166        (t
8167         (let* ((pto-group (gnus-group-prefixed-name
8168                            (car art-group) to-method))
8169                (entry
8170                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8171                (info (nth 2 entry))
8172                (to-group (gnus-info-group info))
8173                to-marks)
8174           ;; Update the group that has been moved to.
8175           (when (and info
8176                      (memq action '(move copy)))
8177             (unless (member to-group to-groups)
8178               (push to-group to-groups))
8179
8180             (unless (memq article gnus-newsgroup-unreads)
8181               (push 'read to-marks)
8182               (gnus-info-set-read
8183                info (gnus-add-to-range (gnus-info-read info)
8184                                        (list (cdr art-group)))))
8185
8186             ;; See whether the article is to be put in the cache.
8187             (let ((marks gnus-article-mark-lists)
8188                   (to-article (cdr art-group)))
8189
8190               ;; Enter the article into the cache in the new group,
8191               ;; if that is required.
8192               (when gnus-use-cache
8193                 (gnus-cache-possibly-enter-article
8194                  to-group to-article
8195                  (memq article gnus-newsgroup-marked)
8196                  (memq article gnus-newsgroup-dormant)
8197                  (memq article gnus-newsgroup-unreads)))
8198
8199               (when gnus-preserve-marks
8200                 ;; Copy any marks over to the new group.
8201                 (when (and (equal to-group gnus-newsgroup-name)
8202                            (not (memq article gnus-newsgroup-unreads)))
8203                   ;; Mark this article as read in this group.
8204                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8205                   (setcdr (gnus-active to-group) to-article)
8206                   (setcdr gnus-newsgroup-active to-article))
8207
8208                 (while marks
8209                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8210                     (when (memq article (symbol-value
8211                                          (intern (format "gnus-newsgroup-%s"
8212                                                          (caar marks)))))
8213                       (push (cdar marks) to-marks)
8214                       ;; If the other group is the same as this group,
8215                       ;; then we have to add the mark to the list.
8216                       (when (equal to-group gnus-newsgroup-name)
8217                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8218                              (cons to-article
8219                                    (symbol-value
8220                                     (intern (format "gnus-newsgroup-%s"
8221                                                     (caar marks)))))))
8222                       ;; Copy the marks to other group.
8223                       (gnus-add-marked-articles
8224                        to-group (cdar marks) (list to-article) info)))
8225                   (setq marks (cdr marks)))
8226
8227                 (gnus-request-set-mark to-group (list (list (list to-article)
8228                                                             'add
8229                                                             to-marks))))
8230
8231               (gnus-dribble-enter
8232                (concat "(gnus-group-set-info '"
8233                        (gnus-prin1-to-string (gnus-get-info to-group))
8234                        ")"))))
8235
8236           ;; Update the Xref header in this article to point to
8237           ;; the new crossposted article we have just created.
8238           (when (eq action 'crosspost)
8239             (save-excursion
8240               (set-buffer copy-buf)
8241               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8242               (nnheader-replace-header "Xref" new-xref)
8243               (gnus-request-replace-article
8244                article gnus-newsgroup-name (current-buffer)))))
8245
8246         ;;;!!!Why is this necessary?
8247         (set-buffer gnus-summary-buffer)
8248
8249         (gnus-summary-goto-subject article)
8250         (when (eq action 'move)
8251           (gnus-summary-mark-article article gnus-canceled-mark))))
8252       (gnus-summary-remove-process-mark article))
8253     ;; Re-activate all groups that have been moved to.
8254     (save-excursion
8255       (set-buffer gnus-group-buffer)
8256       (let ((gnus-group-marked to-groups))
8257         (gnus-group-get-new-news-this-group nil t)))
8258
8259     (gnus-kill-buffer copy-buf)
8260     (gnus-summary-position-point)
8261     (gnus-set-mode-line 'summary)))
8262
8263 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8264   "Move the current article to a different newsgroup.
8265 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8266 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8267 re-spool using this method."
8268   (interactive "P")
8269   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8270
8271 (defun gnus-summary-crosspost-article (&optional n)
8272   "Crosspost the current article to some other group."
8273   (interactive "P")
8274   (gnus-summary-move-article n nil nil 'crosspost))
8275
8276 (defcustom gnus-summary-respool-default-method nil
8277   "Default method type for respooling an article.
8278 If nil, use to the current newsgroup method."
8279   :type 'symbol
8280   :group 'gnus-summary-mail)
8281
8282 (defun gnus-summary-respool-article (&optional n method)
8283   "Respool the current article.
8284 The article will be squeezed through the mail spooling process again,
8285 which means that it will be put in some mail newsgroup or other
8286 depending on `nnmail-split-methods'.
8287 If N is a positive number, respool the N next articles.
8288 If N is a negative number, respool the N previous articles.
8289 If N is nil and any articles have been marked with the process mark,
8290 respool those articles instead.
8291
8292 Respooling can be done both from mail groups and \"real\" newsgroups.
8293 In the former case, the articles in question will be moved from the
8294 current group into whatever groups they are destined to.  In the
8295 latter case, they will be copied into the relevant groups."
8296   (interactive
8297    (list current-prefix-arg
8298          (let* ((methods (gnus-methods-using 'respool))
8299                 (methname
8300                  (symbol-name (or gnus-summary-respool-default-method
8301                                   (car (gnus-find-method-for-group
8302                                         gnus-newsgroup-name)))))
8303                 (method
8304                  (gnus-completing-read
8305                   methname "What backend do you want to use when respooling?"
8306                   methods nil t nil 'gnus-mail-method-history))
8307                 ms)
8308            (cond
8309             ((zerop (length (setq ms (gnus-servers-using-backend
8310                                       (intern method)))))
8311              (list (intern method) ""))
8312             ((= 1 (length ms))
8313              (car ms))
8314             (t
8315              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8316                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8317                            ms-alist))))))))
8318   (unless method
8319     (error "No method given for respooling"))
8320   (if (assoc (symbol-name
8321               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8322              (gnus-methods-using 'respool))
8323       (gnus-summary-move-article n nil method)
8324     (gnus-summary-copy-article n nil method)))
8325
8326 (defun gnus-summary-import-article (file &optional edit)
8327   "Import an arbitrary file into a mail newsgroup."
8328   (interactive "fImport file: \nP")
8329   (let ((group gnus-newsgroup-name)
8330         (now (current-time))
8331         atts lines group-art)
8332     (unless (gnus-check-backend-function 'request-accept-article group)
8333       (error "%s does not support article importing" group))
8334     (or (file-readable-p file)
8335         (not (file-regular-p file))
8336         (error "Can't read %s" file))
8337     (save-excursion
8338       (set-buffer (gnus-get-buffer-create " *import file*"))
8339       (erase-buffer)
8340       (nnheader-insert-file-contents file)
8341       (goto-char (point-min))
8342       (if (nnheader-article-p)
8343           (save-restriction
8344             (goto-char (point-min))
8345             (search-forward "\n\n" nil t)
8346             (narrow-to-region (point-min) (1- (point)))
8347             (goto-char (point-min))
8348             (unless (re-search-forward "^date:" nil t)
8349               (goto-char (point-max))
8350               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8351        ;; This doesn't look like an article, so we fudge some headers.
8352         (setq atts (file-attributes file)
8353               lines (count-lines (point-min) (point-max)))
8354         (insert "From: " (read-string "From: ") "\n"
8355                 "Subject: " (read-string "Subject: ") "\n"
8356                 "Date: " (message-make-date (nth 5 atts)) "\n"
8357                 "Message-ID: " (message-make-message-id) "\n"
8358                 "Lines: " (int-to-string lines) "\n"
8359                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8360       (setq group-art (gnus-request-accept-article group nil t))
8361       (kill-buffer (current-buffer)))
8362     (setq gnus-newsgroup-active (gnus-activate-group group))
8363     (forward-line 1)
8364     (gnus-summary-goto-article (cdr group-art) nil t)
8365     (when edit
8366       (gnus-summary-edit-article))))
8367
8368 (defun gnus-summary-create-article ()
8369   "Create an article in a mail newsgroup."
8370   (interactive)
8371   (let ((group gnus-newsgroup-name)
8372         (now (current-time))
8373         group-art)
8374     (unless (gnus-check-backend-function 'request-accept-article group)
8375       (error "%s does not support article importing" group))
8376     (save-excursion
8377       (set-buffer (gnus-get-buffer-create " *import file*"))
8378       (erase-buffer)
8379       (goto-char (point-min))
8380       ;; This doesn't look like an article, so we fudge some headers.
8381       (insert "From: " (read-string "From: ") "\n"
8382               "Subject: " (read-string "Subject: ") "\n"
8383               "Date: " (message-make-date now) "\n"
8384               "Message-ID: " (message-make-message-id) "\n")
8385       (setq group-art (gnus-request-accept-article group nil t))
8386       (kill-buffer (current-buffer)))
8387     (setq gnus-newsgroup-active (gnus-activate-group group))
8388     (forward-line 1)
8389     (gnus-summary-goto-article (cdr group-art) nil t)
8390     (gnus-summary-edit-article)))
8391
8392 (defun gnus-summary-article-posted-p ()
8393   "Say whether the current (mail) article is available from news as well.
8394 This will be the case if the article has both been mailed and posted."
8395   (interactive)
8396   (let ((id (mail-header-references (gnus-summary-article-header)))
8397         (gnus-override-method (car (gnus-refer-article-methods))))
8398     (if (gnus-request-head id "")
8399         (gnus-message 2 "The current message was found on %s"
8400                       gnus-override-method)
8401       (gnus-message 2 "The current message couldn't be found on %s"
8402                     gnus-override-method)
8403       nil)))
8404
8405 (defun gnus-summary-expire-articles (&optional now)
8406   "Expire all articles that are marked as expirable in the current group."
8407   (interactive)
8408   (when (gnus-check-backend-function
8409          'request-expire-articles gnus-newsgroup-name)
8410     ;; This backend supports expiry.
8411     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8412            (expirable (if total
8413                           (progn
8414                             ;; We need to update the info for
8415                             ;; this group for `gnus-list-of-read-articles'
8416                             ;; to give us the right answer.
8417                             (gnus-run-hooks 'gnus-exit-group-hook)
8418                             (gnus-summary-update-info)
8419                             (gnus-list-of-read-articles gnus-newsgroup-name))
8420                         (setq gnus-newsgroup-expirable
8421                               (sort gnus-newsgroup-expirable '<))))
8422            (expiry-wait (if now 'immediate
8423                           (gnus-group-find-parameter
8424                            gnus-newsgroup-name 'expiry-wait)))
8425            (nnmail-expiry-target
8426             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8427                 nnmail-expiry-target))
8428            es)
8429       (when expirable
8430         ;; There are expirable articles in this group, so we run them
8431         ;; through the expiry process.
8432         (gnus-message 6 "Expiring articles...")
8433         (unless (gnus-check-group gnus-newsgroup-name)
8434           (error "Can't open server for %s" gnus-newsgroup-name))
8435         ;; The list of articles that weren't expired is returned.
8436         (save-excursion
8437           (if expiry-wait
8438               (let ((nnmail-expiry-wait-function nil)
8439                     (nnmail-expiry-wait expiry-wait))
8440                 (setq es (gnus-request-expire-articles
8441                           expirable gnus-newsgroup-name)))
8442             (setq es (gnus-request-expire-articles
8443                       expirable gnus-newsgroup-name)))
8444           (unless total
8445             (setq gnus-newsgroup-expirable es))
8446           ;; We go through the old list of expirable, and mark all
8447           ;; really expired articles as nonexistent.
8448           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8449             (let ((gnus-use-cache nil))
8450               (while expirable
8451                 (unless (memq (car expirable) es)
8452                   (when (gnus-data-find (car expirable))
8453                     (gnus-summary-mark-article
8454                      (car expirable) gnus-canceled-mark)))
8455                 (setq expirable (cdr expirable))))))
8456         (gnus-message 6 "Expiring articles...done")))))
8457
8458 (defun gnus-summary-expire-articles-now ()
8459   "Expunge all expirable articles in the current group.
8460 This means that *all* articles that are marked as expirable will be
8461 deleted forever, right now."
8462   (interactive)
8463   (or gnus-expert-user
8464       (gnus-yes-or-no-p
8465        "Are you really, really, really sure you want to delete all these messages? ")
8466       (error "Phew!"))
8467   (gnus-summary-expire-articles t))
8468
8469 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8470 (defun gnus-summary-delete-article (&optional n)
8471   "Delete the N next (mail) articles.
8472 This command actually deletes articles.  This is not a marking
8473 command.  The article will disappear forever from your life, never to
8474 return.
8475 If N is negative, delete backwards.
8476 If N is nil and articles have been marked with the process mark,
8477 delete these instead."
8478   (interactive "P")
8479   (unless (gnus-check-backend-function 'request-expire-articles
8480                                        gnus-newsgroup-name)
8481     (error "The current newsgroup does not support article deletion"))
8482   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8483     (error "Couldn't open server"))
8484   ;; Compute the list of articles to delete.
8485   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8486         not-deleted)
8487     (if (and gnus-novice-user
8488              (not (gnus-yes-or-no-p
8489                    (format "Do you really want to delete %s forever? "
8490                            (if (> (length articles) 1)
8491                                (format "these %s articles" (length articles))
8492                              "this article")))))
8493         ()
8494       ;; Delete the articles.
8495       (setq not-deleted (gnus-request-expire-articles
8496                          articles gnus-newsgroup-name 'force))
8497       (while articles
8498         (gnus-summary-remove-process-mark (car articles))
8499         ;; The backend might not have been able to delete the article
8500         ;; after all.
8501         (unless (memq (car articles) not-deleted)
8502           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8503         (setq articles (cdr articles)))
8504       (when not-deleted
8505         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8506     (gnus-summary-position-point)
8507     (gnus-set-mode-line 'summary)
8508     not-deleted))
8509
8510 (defun gnus-summary-edit-article (&optional arg)
8511   "Edit the current article.
8512 This will have permanent effect only in mail groups.
8513 If ARG is nil, edit the decoded articles.
8514 If ARG is 1, edit the raw articles.
8515 If ARG is 2, edit the raw articles even in read-only groups.
8516 If ARG is 3, edit the articles with the current handles.
8517 Otherwise, allow editing of articles even in read-only
8518 groups."
8519   (interactive "P")
8520   (let (force raw current-handles)
8521     (cond
8522      ((null arg))
8523      ((eq arg 1) (setq raw t))
8524      ((eq arg 2) (setq raw t
8525                        force t))
8526      ((eq arg 3) (setq current-handles
8527                        (and (gnus-buffer-live-p gnus-article-buffer)
8528                             (with-current-buffer gnus-article-buffer
8529                               (prog1
8530                                   gnus-article-mime-handles
8531                                 (setq gnus-article-mime-handles nil))))))
8532      (t (setq force t)))
8533     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8534         (error "Can't edit the raw article in group nndraft:drafts"))
8535     (save-excursion
8536       (set-buffer gnus-summary-buffer)
8537       (let ((mail-parse-charset gnus-newsgroup-charset)
8538             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8539         (gnus-set-global-variables)
8540         (when (and (not force)
8541                    (gnus-group-read-only-p))
8542           (error "The current newsgroup does not support article editing"))
8543         (gnus-summary-show-article t)
8544         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8545           (with-current-buffer gnus-article-buffer
8546             (mm-enable-multibyte-mule4)))
8547         (if (equal gnus-newsgroup-name "nndraft:drafts")
8548             (setq raw t))
8549         (gnus-article-edit-article
8550          (if raw 'ignore
8551            `(lambda ()
8552               (let ((mbl mml-buffer-list))
8553                 (setq mml-buffer-list nil)
8554                 (mime-to-mml ,'current-handles)
8555                 (let ((mbl1 mml-buffer-list))
8556                   (setq mml-buffer-list mbl)
8557                   (set (make-local-variable 'mml-buffer-list) mbl1))
8558                 (make-local-hook 'kill-buffer-hook)
8559                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8560          `(lambda (no-highlight)
8561             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8562                   (message-options message-options)
8563                   (message-options-set-recipient)
8564                   (mail-parse-ignored-charsets
8565                    ',gnus-newsgroup-ignored-charsets))
8566               ,(if (not raw) '(progn
8567                                 (mml-to-mime)
8568                                 (mml-destroy-buffers)
8569                                 (remove-hook 'kill-buffer-hook
8570                                              'mml-destroy-buffers t)
8571                                 (kill-local-variable 'mml-buffer-list)))
8572               (gnus-summary-edit-article-done
8573                ,(or (mail-header-references gnus-current-headers) "")
8574                ,(gnus-group-read-only-p)
8575                ,gnus-summary-buffer no-highlight))))))))
8576
8577 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8578
8579 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8580                                                  no-highlight)
8581   "Make edits to the current article permanent."
8582   (interactive)
8583   (save-excursion
8584    ;; The buffer restriction contains the entire article if it exists.
8585     (when (article-goto-body)
8586       (let ((lines (count-lines (point) (point-max)))
8587             (length (- (point-max) (point)))
8588             (case-fold-search t)
8589             (body (copy-marker (point))))
8590         (goto-char (point-min))
8591         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8592           (delete-region (match-beginning 1) (match-end 1))
8593           (insert (number-to-string length)))
8594         (goto-char (point-min))
8595         (when (re-search-forward
8596                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8597           (delete-region (match-beginning 1) (match-end 1))
8598           (insert (number-to-string length)))
8599         (goto-char (point-min))
8600         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8601           (delete-region (match-beginning 1) (match-end 1))
8602           (insert (number-to-string lines))))))
8603   ;; Replace the article.
8604   (let ((buf (current-buffer)))
8605     (with-temp-buffer
8606       (insert-buffer-substring buf)
8607
8608       (if (and (not read-only)
8609                (not (gnus-request-replace-article
8610                      (cdr gnus-article-current) (car gnus-article-current)
8611                      (current-buffer) t)))
8612           (error "Couldn't replace article")
8613         ;; Update the summary buffer.
8614         (if (and references
8615                  (equal (message-tokenize-header references " ")
8616                         (message-tokenize-header
8617                          (or (message-fetch-field "references") "") " ")))
8618             ;; We only have to update this line.
8619             (save-excursion
8620               (save-restriction
8621                 (message-narrow-to-head)
8622                 (let ((head (buffer-string))
8623                       header)
8624                   (with-temp-buffer
8625                     (insert (format "211 %d Article retrieved.\n"
8626                                     (cdr gnus-article-current)))
8627                     (insert head)
8628                     (insert ".\n")
8629                     (let ((nntp-server-buffer (current-buffer)))
8630                       (setq header (car (gnus-get-newsgroup-headers
8631                                          (save-excursion
8632                                            (set-buffer gnus-summary-buffer)
8633                                            gnus-newsgroup-dependencies)
8634                                          t))))
8635                     (save-excursion
8636                       (set-buffer gnus-summary-buffer)
8637                       (gnus-data-set-header
8638                        (gnus-data-find (cdr gnus-article-current))
8639                        header)
8640                       (gnus-summary-update-article-line
8641                        (cdr gnus-article-current) header)
8642                       (if (gnus-summary-goto-subject
8643                            (cdr gnus-article-current) nil t)
8644                           (gnus-summary-update-secondary-mark
8645                            (cdr gnus-article-current))))))))
8646           ;; Update threads.
8647           (set-buffer (or buffer gnus-summary-buffer))
8648           (gnus-summary-update-article (cdr gnus-article-current))
8649           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8650               (gnus-summary-update-secondary-mark
8651                (cdr gnus-article-current))))
8652         ;; Prettify the article buffer again.
8653         (unless no-highlight
8654           (save-excursion
8655             (set-buffer gnus-article-buffer)
8656             ;;;!!! Fix this -- article should be rehighlighted.
8657             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8658             (set-buffer gnus-original-article-buffer)
8659             (gnus-request-article
8660              (cdr gnus-article-current)
8661              (car gnus-article-current) (current-buffer))))
8662         ;; Prettify the summary buffer line.
8663         (when (gnus-visual-p 'summary-highlight 'highlight)
8664           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8665
8666 (defun gnus-summary-edit-wash (key)
8667   "Perform editing command KEY in the article buffer."
8668   (interactive
8669    (list
8670     (progn
8671       (message "%s" (concat (this-command-keys) "- "))
8672       (read-char))))
8673   (message "")
8674   (gnus-summary-edit-article)
8675   (execute-kbd-macro (concat (this-command-keys) key))
8676   (gnus-article-edit-done))
8677
8678
8679 (defun gnus-summary-toggle-smiley (&optional arg)
8680   "Toggle the display of smilies as small graphical icons."
8681   (interactive "P")
8682   (save-excursion
8683     (set-buffer gnus-article-buffer)
8684     (gnus-smiley-display arg)
8685     ))
8686
8687 ;;; Respooling
8688
8689 (defun gnus-summary-respool-query (&optional silent trace)
8690   "Query where the respool algorithm would put this article."
8691   (interactive)
8692   (let (gnus-mark-article-hook)
8693     (gnus-summary-select-article)
8694     (save-excursion
8695       (set-buffer gnus-original-article-buffer)
8696       (save-restriction
8697         (message-narrow-to-head)
8698         (let ((groups (nnmail-article-group 'identity trace)))
8699           (unless silent
8700             (if groups
8701                 (message "This message would go to %s"
8702                          (mapconcat 'car groups ", "))
8703               (message "This message would go to no groups"))
8704             groups))))))
8705
8706 (defun gnus-summary-respool-trace ()
8707   "Trace where the respool algorithm would put this article.
8708 Display a buffer showing all fancy splitting patterns which matched."
8709   (interactive)
8710   (gnus-summary-respool-query nil t))
8711
8712 ;; Summary marking commands.
8713
8714 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8715   "Mark articles which has the same subject as read, and then select the next.
8716 If UNMARK is positive, remove any kind of mark.
8717 If UNMARK is negative, tick articles."
8718   (interactive "P")
8719   (when unmark
8720     (setq unmark (prefix-numeric-value unmark)))
8721   (let ((count
8722          (gnus-summary-mark-same-subject
8723           (gnus-summary-article-subject) unmark)))
8724     ;; Select next unread article.  If auto-select-same mode, should
8725     ;; select the first unread article.
8726     (gnus-summary-next-article t (and gnus-auto-select-same
8727                                       (gnus-summary-article-subject)))
8728     (gnus-message 7 "%d article%s marked as %s"
8729                   count (if (= count 1) " is" "s are")
8730                   (if unmark "unread" "read"))))
8731
8732 (defun gnus-summary-kill-same-subject (&optional unmark)
8733   "Mark articles which has the same subject as read.
8734 If UNMARK is positive, remove any kind of mark.
8735 If UNMARK is negative, tick articles."
8736   (interactive "P")
8737   (when unmark
8738     (setq unmark (prefix-numeric-value unmark)))
8739   (let ((count
8740          (gnus-summary-mark-same-subject
8741           (gnus-summary-article-subject) unmark)))
8742     ;; If marked as read, go to next unread subject.
8743     (when (null unmark)
8744       ;; Go to next unread subject.
8745       (gnus-summary-next-subject 1 t))
8746     (gnus-message 7 "%d articles are marked as %s"
8747                   count (if unmark "unread" "read"))))
8748
8749 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8750   "Mark articles with same SUBJECT as read, and return marked number.
8751 If optional argument UNMARK is positive, remove any kinds of marks.
8752 If optional argument UNMARK is negative, mark articles as unread instead."
8753   (let ((count 1))
8754     (save-excursion
8755       (cond
8756        ((null unmark)                   ; Mark as read.
8757         (while (and
8758                 (progn
8759                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8760                   (gnus-summary-show-thread) t)
8761                 (gnus-summary-find-subject subject))
8762           (setq count (1+ count))))
8763        ((> unmark 0)                    ; Tick.
8764         (while (and
8765                 (progn
8766                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8767                   (gnus-summary-show-thread) t)
8768                 (gnus-summary-find-subject subject))
8769           (setq count (1+ count))))
8770        (t                               ; Mark as unread.
8771         (while (and
8772                 (progn
8773                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8774                   (gnus-summary-show-thread) t)
8775                 (gnus-summary-find-subject subject))
8776           (setq count (1+ count)))))
8777       (gnus-set-mode-line 'summary)
8778       ;; Return the number of marked articles.
8779       count)))
8780
8781 (defun gnus-summary-mark-as-processable (n &optional unmark)
8782   "Set the process mark on the next N articles.
8783 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8784 the process mark instead.  The difference between N and the actual
8785 number of articles marked is returned."
8786   (interactive "P")
8787   (if (and (null n) (gnus-region-active-p))
8788       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8789     (setq n (prefix-numeric-value n))
8790     (let ((backward (< n 0))
8791           (n (abs n)))
8792       (while (and
8793               (> n 0)
8794               (if unmark
8795                   (gnus-summary-remove-process-mark
8796                    (gnus-summary-article-number))
8797                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8798               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8799         (setq n (1- n)))
8800       (when (/= 0 n)
8801         (gnus-message 7 "No more articles"))
8802       (gnus-summary-recenter)
8803       (gnus-summary-position-point)
8804       n)))
8805
8806 (defun gnus-summary-unmark-as-processable (n)
8807   "Remove the process mark from the next N articles.
8808 If N is negative, unmark backward instead.  The difference between N and
8809 the actual number of articles unmarked is returned."
8810   (interactive "P")
8811   (gnus-summary-mark-as-processable n t))
8812
8813 (defun gnus-summary-unmark-all-processable ()
8814   "Remove the process mark from all articles."
8815   (interactive)
8816   (save-excursion
8817     (while gnus-newsgroup-processable
8818       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8819   (gnus-summary-position-point))
8820
8821 (defun gnus-summary-add-mark (article type)
8822   "Mark ARTICLE with a mark of TYPE."
8823   (let ((vtype (car (assq type gnus-article-mark-lists)))
8824         var)
8825     (if (not vtype)
8826         (error "No such mark type: %s" type)
8827       (setq var (intern (format "gnus-newsgroup-%s" type)))
8828       (set var (cons article (symbol-value var)))
8829       (if (memq type '(processable cached replied forwarded recent saved))
8830           (gnus-summary-update-secondary-mark article)
8831         ;;; !!! This is bogus.  We should find out what primary
8832         ;;; !!! mark we want to set.
8833         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8834
8835 (defun gnus-summary-mark-as-expirable (n)
8836   "Mark N articles forward as expirable.
8837 If N is negative, mark backward instead.  The difference between N and
8838 the actual number of articles marked is returned."
8839   (interactive "p")
8840   (gnus-summary-mark-forward n gnus-expirable-mark))
8841
8842 (defun gnus-summary-mark-article-as-replied (article)
8843   "Mark ARTICLE as replied to and update the summary line.
8844 ARTICLE can also be a list of articles."
8845   (interactive (list (gnus-summary-article-number)))
8846   (let ((articles (if (listp article) article (list article))))
8847     (dolist (article articles)
8848       (push article gnus-newsgroup-replied)
8849       (let ((buffer-read-only nil))
8850         (when (gnus-summary-goto-subject article nil t)
8851           (gnus-summary-update-secondary-mark article))))))
8852
8853 (defun gnus-summary-mark-article-as-forwarded (article)
8854   "Mark ARTICLE as forwarded and update the summary line.
8855 ARTICLE can also be a list of articles."
8856   (let ((articles (if (listp article) article (list article))))
8857     (dolist (article articles)
8858       (push article gnus-newsgroup-forwarded)
8859       (let ((buffer-read-only nil))
8860         (when (gnus-summary-goto-subject article nil t)
8861           (gnus-summary-update-secondary-mark article))))))
8862
8863 (defun gnus-summary-set-bookmark (article)
8864   "Set a bookmark in current article."
8865   (interactive (list (gnus-summary-article-number)))
8866   (when (or (not (get-buffer gnus-article-buffer))
8867             (not gnus-current-article)
8868             (not gnus-article-current)
8869             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8870     (error "No current article selected"))
8871   ;; Remove old bookmark, if one exists.
8872   (let ((old (assq article gnus-newsgroup-bookmarks)))
8873     (when old
8874       (setq gnus-newsgroup-bookmarks
8875             (delq old gnus-newsgroup-bookmarks))))
8876   ;; Set the new bookmark, which is on the form
8877   ;; (article-number . line-number-in-body).
8878   (push
8879    (cons article
8880          (save-excursion
8881            (set-buffer gnus-article-buffer)
8882            (count-lines
8883             (min (point)
8884                  (save-excursion
8885                    (goto-char (point-min))
8886                    (search-forward "\n\n" nil t)
8887                    (point)))
8888             (point))))
8889    gnus-newsgroup-bookmarks)
8890   (gnus-message 6 "A bookmark has been added to the current article."))
8891
8892 (defun gnus-summary-remove-bookmark (article)
8893   "Remove the bookmark from the current article."
8894   (interactive (list (gnus-summary-article-number)))
8895   ;; Remove old bookmark, if one exists.
8896   (let ((old (assq article gnus-newsgroup-bookmarks)))
8897     (if old
8898         (progn
8899           (setq gnus-newsgroup-bookmarks
8900                 (delq old gnus-newsgroup-bookmarks))
8901           (gnus-message 6 "Removed bookmark."))
8902       (gnus-message 6 "No bookmark in current article."))))
8903
8904 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8905 (defun gnus-summary-mark-as-dormant (n)
8906   "Mark N articles forward as dormant.
8907 If N is negative, mark backward instead.  The difference between N and
8908 the actual number of articles marked is returned."
8909   (interactive "p")
8910   (gnus-summary-mark-forward n gnus-dormant-mark))
8911
8912 (defun gnus-summary-set-process-mark (article)
8913   "Set the process mark on ARTICLE and update the summary line."
8914   (setq gnus-newsgroup-processable
8915         (cons article
8916               (delq article gnus-newsgroup-processable)))
8917   (when (gnus-summary-goto-subject article)
8918     (gnus-summary-show-thread)
8919     (gnus-summary-goto-subject article)
8920     (gnus-summary-update-secondary-mark article)))
8921
8922 (defun gnus-summary-remove-process-mark (article)
8923   "Remove the process mark from ARTICLE and update the summary line."
8924   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8925   (when (gnus-summary-goto-subject article)
8926     (gnus-summary-show-thread)
8927     (gnus-summary-goto-subject article)
8928     (gnus-summary-update-secondary-mark article)))
8929
8930 (defun gnus-summary-set-saved-mark (article)
8931   "Set the process mark on ARTICLE and update the summary line."
8932   (push article gnus-newsgroup-saved)
8933   (when (gnus-summary-goto-subject article)
8934     (gnus-summary-update-secondary-mark article)))
8935
8936 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8937   "Mark N articles as read forwards.
8938 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8939 The difference between N and the actual number of articles marked is
8940 returned.
8941 Iff NO-EXPIRE, auto-expiry will be inhibited."
8942   (interactive "p")
8943   (gnus-summary-show-thread)
8944   (let ((backward (< n 0))
8945         (gnus-summary-goto-unread
8946          (and gnus-summary-goto-unread
8947               (not (eq gnus-summary-goto-unread 'never))
8948               (not (memq mark (list gnus-unread-mark
8949                                     gnus-ticked-mark gnus-dormant-mark)))))
8950         (n (abs n))
8951         (mark (or mark gnus-del-mark)))
8952     (while (and (> n 0)
8953                 (gnus-summary-mark-article nil mark no-expire)
8954                 (zerop (gnus-summary-next-subject
8955                         (if backward -1 1)
8956                         (and gnus-summary-goto-unread
8957                              (not (eq gnus-summary-goto-unread 'never)))
8958                         t)))
8959       (setq n (1- n)))
8960     (when (/= 0 n)
8961       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8962     (gnus-summary-recenter)
8963     (gnus-summary-position-point)
8964     (gnus-set-mode-line 'summary)
8965     n))
8966
8967 (defun gnus-summary-mark-article-as-read (mark)
8968   "Mark the current article quickly as read with MARK."
8969   (let ((article (gnus-summary-article-number)))
8970     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8971     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8972     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8973     (push (cons article mark) gnus-newsgroup-reads)
8974     ;; Possibly remove from cache, if that is used.
8975     (when gnus-use-cache
8976       (gnus-cache-enter-remove-article article))
8977     ;; Allow the backend to change the mark.
8978     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8979     ;; Check for auto-expiry.
8980     (when (and gnus-newsgroup-auto-expire
8981                (memq mark gnus-auto-expirable-marks))
8982       (setq mark gnus-expirable-mark)
8983       ;; Let the backend know about the mark change.
8984       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8985       (push article gnus-newsgroup-expirable))
8986     ;; Set the mark in the buffer.
8987     (gnus-summary-update-mark mark 'unread)
8988     t))
8989
8990 (defun gnus-summary-mark-article-as-unread (mark)
8991   "Mark the current article quickly as unread with MARK."
8992   (let* ((article (gnus-summary-article-number))
8993          (old-mark (gnus-summary-article-mark article)))
8994     ;; Allow the backend to change the mark.
8995     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8996     (if (eq mark old-mark)
8997         t
8998       (if (<= article 0)
8999           (progn
9000             (gnus-error 1 "Can't mark negative article numbers")
9001             nil)
9002         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9003         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9004         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9005         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9006         (cond ((= mark gnus-ticked-mark)
9007                (push article gnus-newsgroup-marked))
9008               ((= mark gnus-dormant-mark)
9009                (push article gnus-newsgroup-dormant))
9010               (t
9011                (push article gnus-newsgroup-unreads)))
9012         (gnus-pull article gnus-newsgroup-reads)
9013
9014         ;; See whether the article is to be put in the cache.
9015         (and gnus-use-cache
9016              (vectorp (gnus-summary-article-header article))
9017              (save-excursion
9018                (gnus-cache-possibly-enter-article
9019                 gnus-newsgroup-name article
9020                 (= mark gnus-ticked-mark)
9021                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9022
9023         ;; Fix the mark.
9024         (gnus-summary-update-mark mark 'unread)
9025         t))))
9026
9027 (defun gnus-summary-mark-article (&optional article mark no-expire)
9028   "Mark ARTICLE with MARK.  MARK can be any character.
9029 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9030 `??' (dormant) and `?E' (expirable).
9031 If MARK is nil, then the default character `?r' is used.
9032 If ARTICLE is nil, then the article on the current line will be
9033 marked.
9034 Iff NO-EXPIRE, auto-expiry will be inhibited."
9035   ;; The mark might be a string.
9036   (when (stringp mark)
9037     (setq mark (aref mark 0)))
9038   ;; If no mark is given, then we check auto-expiring.
9039   (when (null mark)
9040     (setq mark gnus-del-mark))
9041   (when (and (not no-expire)
9042              gnus-newsgroup-auto-expire
9043              (memq mark gnus-auto-expirable-marks))
9044     (setq mark gnus-expirable-mark))
9045   (let ((article (or article (gnus-summary-article-number)))
9046         (old-mark (gnus-summary-article-mark article)))
9047     ;; Allow the backend to change the mark.
9048     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9049     (if (eq mark old-mark)
9050         t
9051       (unless article
9052         (error "No article on current line"))
9053       (if (not (if (or (= mark gnus-unread-mark)
9054                        (= mark gnus-ticked-mark)
9055                        (= mark gnus-dormant-mark))
9056                    (gnus-mark-article-as-unread article mark)
9057                  (gnus-mark-article-as-read article mark)))
9058           t
9059         ;; See whether the article is to be put in the cache.
9060         (and gnus-use-cache
9061              (not (= mark gnus-canceled-mark))
9062              (vectorp (gnus-summary-article-header article))
9063              (save-excursion
9064                (gnus-cache-possibly-enter-article
9065                 gnus-newsgroup-name article
9066                 (= mark gnus-ticked-mark)
9067                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9068
9069         (when (gnus-summary-goto-subject article nil t)
9070           (let ((buffer-read-only nil))
9071             (gnus-summary-show-thread)
9072             ;; Fix the mark.
9073             (gnus-summary-update-mark mark 'unread)
9074             t))))))
9075
9076 (defun gnus-summary-update-secondary-mark (article)
9077   "Update the secondary (read, process, cache) mark."
9078   (gnus-summary-update-mark
9079    (cond ((memq article gnus-newsgroup-processable)
9080           gnus-process-mark)
9081          ((memq article gnus-newsgroup-cached)
9082           gnus-cached-mark)
9083          ((memq article gnus-newsgroup-replied)
9084           gnus-replied-mark)
9085          ((memq article gnus-newsgroup-forwarded)
9086           gnus-forwarded-mark)
9087          ((memq article gnus-newsgroup-saved)
9088           gnus-saved-mark)
9089          ((memq article gnus-newsgroup-recent)
9090           gnus-recent-mark)
9091          ((memq article gnus-newsgroup-unseen)
9092           gnus-unseen-mark)
9093          (t gnus-no-mark))
9094    'replied)
9095   (when (gnus-visual-p 'summary-highlight 'highlight)
9096     (gnus-run-hooks 'gnus-summary-update-hook))
9097   t)
9098
9099 (defun gnus-summary-update-mark (mark type)
9100   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9101         (buffer-read-only nil))
9102     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9103     (when forward
9104       (when (looking-at "\r")
9105         (incf forward))
9106       (when (<= (+ forward (point)) (point-max))
9107         ;; Go to the right position on the line.
9108         (goto-char (+ forward (point)))
9109         ;; Replace the old mark with the new mark.
9110         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9111         ;; Optionally update the marks by some user rule.
9112         (when (eq type 'unread)
9113           (gnus-data-set-mark
9114            (gnus-data-find (gnus-summary-article-number)) mark)
9115           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9116
9117 (defun gnus-mark-article-as-read (article &optional mark)
9118   "Enter ARTICLE in the pertinent lists and remove it from others."
9119   ;; Make the article expirable.
9120   (let ((mark (or mark gnus-del-mark)))
9121     (if (= mark gnus-expirable-mark)
9122         (push article gnus-newsgroup-expirable)
9123       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9124     ;; Remove from unread and marked lists.
9125     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9126     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9127     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9128     (push (cons article mark) gnus-newsgroup-reads)
9129     ;; Possibly remove from cache, if that is used.
9130     (when gnus-use-cache
9131       (gnus-cache-enter-remove-article article))
9132     t))
9133
9134 (defun gnus-mark-article-as-unread (article &optional mark)
9135   "Enter ARTICLE in the pertinent lists and remove it from others."
9136   (let ((mark (or mark gnus-ticked-mark)))
9137     (if (<= article 0)
9138         (progn
9139           (gnus-error 1 "Can't mark negative article numbers")
9140           nil)
9141       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9142             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9143             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9144             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9145
9146       ;; Unsuppress duplicates?
9147       (when gnus-suppress-duplicates
9148         (gnus-dup-unsuppress-article article))
9149
9150       (cond ((= mark gnus-ticked-mark)
9151              (push article gnus-newsgroup-marked))
9152             ((= mark gnus-dormant-mark)
9153              (push article gnus-newsgroup-dormant))
9154             (t
9155              (push article gnus-newsgroup-unreads)))
9156       (gnus-pull article gnus-newsgroup-reads)
9157       t)))
9158
9159 (defalias 'gnus-summary-mark-as-unread-forward
9160   'gnus-summary-tick-article-forward)
9161 (make-obsolete 'gnus-summary-mark-as-unread-forward
9162                'gnus-summary-tick-article-forward)
9163 (defun gnus-summary-tick-article-forward (n)
9164   "Tick N articles forwards.
9165 If N is negative, tick backwards instead.
9166 The difference between N and the number of articles ticked is returned."
9167   (interactive "p")
9168   (gnus-summary-mark-forward n gnus-ticked-mark))
9169
9170 (defalias 'gnus-summary-mark-as-unread-backward
9171   'gnus-summary-tick-article-backward)
9172 (make-obsolete 'gnus-summary-mark-as-unread-backward
9173                'gnus-summary-tick-article-backward)
9174 (defun gnus-summary-tick-article-backward (n)
9175   "Tick N articles backwards.
9176 The difference between N and the number of articles ticked is returned."
9177   (interactive "p")
9178   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9179
9180 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9181 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9182 (defun gnus-summary-tick-article (&optional article clear-mark)
9183   "Mark current article as unread.
9184 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9185 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9186   (interactive)
9187   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9188                                        gnus-ticked-mark)))
9189
9190 (defun gnus-summary-mark-as-read-forward (n)
9191   "Mark N articles as read forwards.
9192 If N is negative, mark backwards instead.
9193 The difference between N and the actual number of articles marked is
9194 returned."
9195   (interactive "p")
9196   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9197
9198 (defun gnus-summary-mark-as-read-backward (n)
9199   "Mark the N articles as read backwards.
9200 The difference between N and the actual number of articles marked is
9201 returned."
9202   (interactive "p")
9203   (gnus-summary-mark-forward
9204    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9205
9206 (defun gnus-summary-mark-as-read (&optional article mark)
9207   "Mark current article as read.
9208 ARTICLE specifies the article to be marked as read.
9209 MARK specifies a string to be inserted at the beginning of the line."
9210   (gnus-summary-mark-article article mark))
9211
9212 (defun gnus-summary-clear-mark-forward (n)
9213   "Clear marks from N articles forward.
9214 If N is negative, clear backward instead.
9215 The difference between N and the number of marks cleared is returned."
9216   (interactive "p")
9217   (gnus-summary-mark-forward n gnus-unread-mark))
9218
9219 (defun gnus-summary-clear-mark-backward (n)
9220   "Clear marks from N articles backward.
9221 The difference between N and the number of marks cleared is returned."
9222   (interactive "p")
9223   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9224
9225 (defun gnus-summary-mark-unread-as-read ()
9226   "Intended to be used by `gnus-summary-mark-article-hook'."
9227   (when (memq gnus-current-article gnus-newsgroup-unreads)
9228     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9229
9230 (defun gnus-summary-mark-read-and-unread-as-read ()
9231   "Intended to be used by `gnus-summary-mark-article-hook'."
9232   (let ((mark (gnus-summary-article-mark)))
9233     (when (or (gnus-unread-mark-p mark)
9234               (gnus-read-mark-p mark))
9235       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9236
9237 (defun gnus-summary-mark-unread-as-ticked ()
9238   "Intended to be used by `gnus-summary-mark-article-hook'."
9239   (when (memq gnus-current-article gnus-newsgroup-unreads)
9240     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9241
9242 (defun gnus-summary-mark-region-as-read (point mark all)
9243   "Mark all unread articles between point and mark as read.
9244 If given a prefix, mark all articles between point and mark as read,
9245 even ticked and dormant ones."
9246   (interactive "r\nP")
9247   (save-excursion
9248     (let (article)
9249       (goto-char point)
9250       (beginning-of-line)
9251       (while (and
9252               (< (point) mark)
9253               (progn
9254                 (when (or all
9255                           (memq (setq article (gnus-summary-article-number))
9256                                 gnus-newsgroup-unreads))
9257                   (gnus-summary-mark-article article gnus-del-mark))
9258                 t)
9259               (gnus-summary-find-next))))))
9260
9261 (defun gnus-summary-mark-below (score mark)
9262   "Mark articles with score less than SCORE with MARK."
9263   (interactive "P\ncMark: ")
9264   (setq score (if score
9265                   (prefix-numeric-value score)
9266                 (or gnus-summary-default-score 0)))
9267   (save-excursion
9268     (set-buffer gnus-summary-buffer)
9269     (goto-char (point-min))
9270     (while
9271         (progn
9272           (and (< (gnus-summary-article-score) score)
9273                (gnus-summary-mark-article nil mark))
9274           (gnus-summary-find-next)))))
9275
9276 (defun gnus-summary-kill-below (&optional score)
9277   "Mark articles with score below SCORE as read."
9278   (interactive "P")
9279   (gnus-summary-mark-below score gnus-killed-mark))
9280
9281 (defun gnus-summary-clear-above (&optional score)
9282   "Clear all marks from articles with score above SCORE."
9283   (interactive "P")
9284   (gnus-summary-mark-above score gnus-unread-mark))
9285
9286 (defun gnus-summary-tick-above (&optional score)
9287   "Tick all articles with score above SCORE."
9288   (interactive "P")
9289   (gnus-summary-mark-above score gnus-ticked-mark))
9290
9291 (defun gnus-summary-mark-above (score mark)
9292   "Mark articles with score over SCORE with MARK."
9293   (interactive "P\ncMark: ")
9294   (setq score (if score
9295                   (prefix-numeric-value score)
9296                 (or gnus-summary-default-score 0)))
9297   (save-excursion
9298     (set-buffer gnus-summary-buffer)
9299     (goto-char (point-min))
9300     (while (and (progn
9301                   (when (> (gnus-summary-article-score) score)
9302                     (gnus-summary-mark-article nil mark))
9303                   t)
9304                 (gnus-summary-find-next)))))
9305
9306 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9307 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9308 (defun gnus-summary-limit-include-expunged (&optional no-error)
9309   "Display all the hidden articles that were expunged for low scores."
9310   (interactive)
9311   (let ((buffer-read-only nil))
9312     (let ((scored gnus-newsgroup-scored)
9313           headers h)
9314       (while scored
9315         (unless (gnus-summary-article-header (caar scored))
9316           (and (setq h (gnus-number-to-header (caar scored)))
9317                (< (cdar scored) gnus-summary-expunge-below)
9318                (push h headers)))
9319         (setq scored (cdr scored)))
9320       (if (not headers)
9321           (when (not no-error)
9322             (error "No expunged articles hidden"))
9323         (goto-char (point-min))
9324         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9325         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9326         (mapcar (lambda (x) (push (mail-header-number x)
9327                                   gnus-newsgroup-limit))
9328                 headers)
9329         (gnus-summary-prepare-unthreaded (nreverse headers))
9330         (goto-char (point-min))
9331         (gnus-summary-position-point)
9332         t))))
9333
9334 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9335   "Mark all unread articles in this newsgroup as read.
9336 If prefix argument ALL is non-nil, ticked and dormant articles will
9337 also be marked as read.
9338 If QUIETLY is non-nil, no questions will be asked.
9339 If TO-HERE is non-nil, it should be a point in the buffer.  All
9340 articles before (after, if REVERSE is set) this point will be marked as read.
9341 Note that this function will only catch up the unread article
9342 in the current summary buffer limitation.
9343 The number of articles marked as read is returned."
9344   (interactive "P")
9345   (prog1
9346       (save-excursion
9347         (when (or quietly
9348                   (not gnus-interactive-catchup) ;Without confirmation?
9349                   gnus-expert-user
9350                   (gnus-y-or-n-p
9351                    (if all
9352                        "Mark absolutely all articles as read? "
9353                      "Mark all unread articles as read? ")))
9354           (if (and not-mark
9355                    (not gnus-newsgroup-adaptive)
9356                    (not gnus-newsgroup-auto-expire)
9357                    (not gnus-suppress-duplicates)
9358                    (or (not gnus-use-cache)
9359                        (eq gnus-use-cache 'passive)))
9360               (progn
9361                 (when all
9362                   (setq gnus-newsgroup-marked nil
9363                         gnus-newsgroup-dormant nil))
9364                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9365             ;; We actually mark all articles as canceled, which we
9366             ;; have to do when using auto-expiry or adaptive scoring.
9367             (gnus-summary-show-all-threads)
9368             (if (and to-here reverse)
9369                 (progn
9370                   (goto-char to-here)
9371                   (while (and
9372                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9373                           (gnus-summary-find-next (not all) nil nil t))))
9374               (when (gnus-summary-first-subject (not all) t)
9375                 (while (and
9376                         (if to-here (< (point) to-here) t)
9377                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9378                         (gnus-summary-find-next (not all) nil nil t)))))
9379             (gnus-set-mode-line 'summary))
9380           t))
9381     (gnus-summary-position-point)))
9382
9383 (defun gnus-summary-catchup-to-here (&optional all)
9384   "Mark all unticked articles before the current one as read.
9385 If ALL is non-nil, also mark ticked and dormant articles as read."
9386   (interactive "P")
9387   (save-excursion
9388     (gnus-save-hidden-threads
9389       (let ((beg (point)))
9390         ;; We check that there are unread articles.
9391         (when (or all (gnus-summary-find-prev))
9392           (gnus-summary-catchup all t beg)))))
9393   (gnus-summary-position-point))
9394
9395 (defun gnus-summary-catchup-from-here (&optional all)
9396   "Mark all unticked articles after the current one as read.
9397 If ALL is non-nil, also mark ticked and dormant articles as read."
9398   (interactive "P")
9399   (save-excursion
9400     (gnus-save-hidden-threads
9401       (let ((beg (point)))
9402         ;; We check that there are unread articles.
9403         (when (or all (gnus-summary-find-next))
9404           (gnus-summary-catchup all t beg nil t)))))
9405
9406   (gnus-summary-position-point))
9407 (defun gnus-summary-catchup-all (&optional quietly)
9408   "Mark all articles in this newsgroup as read."
9409   (interactive "P")
9410   (gnus-summary-catchup t quietly))
9411
9412 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9413   "Mark all unread articles in this group as read, then exit.
9414 If prefix argument ALL is non-nil, all articles are marked as read.
9415 If QUIETLY is non-nil, no questions will be asked."
9416   (interactive "P")
9417   (when (gnus-summary-catchup all quietly nil 'fast)
9418     ;; Select next newsgroup or exit.
9419     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9420              (eq gnus-auto-select-next 'quietly))
9421         (gnus-summary-next-group nil)
9422       (gnus-summary-exit))))
9423
9424 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9425   "Mark all articles in this newsgroup as read, and then exit."
9426   (interactive "P")
9427   (gnus-summary-catchup-and-exit t quietly))
9428
9429 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9430   "Mark all articles in this group as read and select the next group.
9431 If given a prefix, mark all articles, unread as well as ticked, as
9432 read."
9433   (interactive "P")
9434   (save-excursion
9435     (gnus-summary-catchup all))
9436   (gnus-summary-next-group))
9437
9438 ;;;
9439 ;;; with article
9440 ;;;
9441
9442 (defmacro gnus-with-article (article &rest forms)
9443   "Select ARTICLE and perform FORMS in the original article buffer.
9444 Then replace the article with the result."
9445   `(progn
9446      ;; We don't want the article to be marked as read.
9447      (let (gnus-mark-article-hook)
9448        (gnus-summary-select-article t t nil ,article))
9449      (set-buffer gnus-original-article-buffer)
9450      ,@forms
9451      (if (not (gnus-check-backend-function
9452                'request-replace-article (car gnus-article-current)))
9453          (gnus-message 5 "Read-only group; not replacing")
9454        (unless (gnus-request-replace-article
9455                 ,article (car gnus-article-current)
9456                 (current-buffer) t)
9457          (error "Couldn't replace article")))
9458      ;; The cache and backlog have to be flushed somewhat.
9459      (when gnus-keep-backlog
9460        (gnus-backlog-remove-article
9461         (car gnus-article-current) (cdr gnus-article-current)))
9462      (when gnus-use-cache
9463        (gnus-cache-update-article
9464         (car gnus-article-current) (cdr gnus-article-current)))))
9465
9466 (put 'gnus-with-article 'lisp-indent-function 1)
9467 (put 'gnus-with-article 'edebug-form-spec '(form body))
9468
9469 ;; Thread-based commands.
9470
9471 (defun gnus-summary-articles-in-thread (&optional article)
9472   "Return a list of all articles in the current thread.
9473 If ARTICLE is non-nil, return all articles in the thread that starts
9474 with that article."
9475   (let* ((article (or article (gnus-summary-article-number)))
9476          (data (gnus-data-find-list article))
9477          (top-level (gnus-data-level (car data)))
9478          (top-subject
9479           (cond ((null gnus-thread-operation-ignore-subject)
9480                  (gnus-simplify-subject-re
9481                   (mail-header-subject (gnus-data-header (car data)))))
9482                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9483                  (gnus-simplify-subject-fuzzy
9484                   (mail-header-subject (gnus-data-header (car data)))))
9485                 (t nil)))
9486          (end-point (save-excursion
9487                       (if (gnus-summary-go-to-next-thread)
9488                           (point) (point-max))))
9489          articles)
9490     (while (and data
9491                 (< (gnus-data-pos (car data)) end-point))
9492       (when (or (not top-subject)
9493                 (string= top-subject
9494                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9495                              (gnus-simplify-subject-fuzzy
9496                               (mail-header-subject
9497                                (gnus-data-header (car data))))
9498                            (gnus-simplify-subject-re
9499                             (mail-header-subject
9500                              (gnus-data-header (car data)))))))
9501         (push (gnus-data-number (car data)) articles))
9502       (unless (and (setq data (cdr data))
9503                    (> (gnus-data-level (car data)) top-level))
9504         (setq data nil)))
9505     ;; Return the list of articles.
9506     (nreverse articles)))
9507
9508 (defun gnus-summary-rethread-current ()
9509   "Rethread the thread the current article is part of."
9510   (interactive)
9511   (let* ((gnus-show-threads t)
9512          (article (gnus-summary-article-number))
9513          (id (mail-header-id (gnus-summary-article-header)))
9514          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9515     (unless id
9516       (error "No article on the current line"))
9517     (gnus-rebuild-thread id)
9518     (gnus-summary-goto-subject article)))
9519
9520 (defun gnus-summary-reparent-thread ()
9521   "Make the current article child of the marked (or previous) article.
9522
9523 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9524 is non-nil or the Subject: of both articles are the same."
9525   (interactive)
9526   (unless (not (gnus-group-read-only-p))
9527     (error "The current newsgroup does not support article editing"))
9528   (unless (<= (length gnus-newsgroup-processable) 1)
9529     (error "No more than one article may be marked"))
9530   (save-window-excursion
9531     (let ((gnus-article-buffer " *reparent*")
9532           (current-article (gnus-summary-article-number))
9533           ;; First grab the marked article, otherwise one line up.
9534           (parent-article (if (not (null gnus-newsgroup-processable))
9535                               (car gnus-newsgroup-processable)
9536                             (save-excursion
9537                               (if (eq (forward-line -1) 0)
9538                                   (gnus-summary-article-number)
9539                                 (error "Beginning of summary buffer"))))))
9540       (unless (not (eq current-article parent-article))
9541         (error "An article may not be self-referential"))
9542       (let ((message-id (mail-header-id
9543                          (gnus-summary-article-header parent-article))))
9544         (unless (and message-id (not (equal message-id "")))
9545           (error "No message-id in desired parent"))
9546         (gnus-with-article current-article
9547           (save-restriction
9548             (goto-char (point-min))
9549             (message-narrow-to-head)
9550             (if (re-search-forward "^References: " nil t)
9551                 (progn
9552                   (re-search-forward "^[^ \t]" nil t)
9553                   (forward-line -1)
9554                   (end-of-line)
9555                   (insert " " message-id))
9556               (insert "References: " message-id "\n"))))
9557         (set-buffer gnus-summary-buffer)
9558         (gnus-summary-unmark-all-processable)
9559         (gnus-summary-update-article current-article)
9560         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9561             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9562         (gnus-summary-rethread-current)
9563         (gnus-message 3 "Article %d is now the child of article %d"
9564                       current-article parent-article)))))
9565
9566 (defun gnus-summary-toggle-threads (&optional arg)
9567   "Toggle showing conversation threads.
9568 If ARG is positive number, turn showing conversation threads on."
9569   (interactive "P")
9570   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9571     (setq gnus-show-threads
9572           (if (null arg) (not gnus-show-threads)
9573             (> (prefix-numeric-value arg) 0)))
9574     (gnus-summary-prepare)
9575     (gnus-summary-goto-subject current)
9576     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9577     (gnus-summary-position-point)))
9578
9579 (defun gnus-summary-show-all-threads ()
9580   "Show all threads."
9581   (interactive)
9582   (save-excursion
9583     (let ((buffer-read-only nil))
9584       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9585   (gnus-summary-position-point))
9586
9587 (defun gnus-summary-show-thread ()
9588   "Show thread subtrees.
9589 Returns nil if no thread was there to be shown."
9590   (interactive)
9591   (let ((buffer-read-only nil)
9592         (orig (point))
9593         ;; first goto end then to beg, to have point at beg after let
9594         (end (progn (end-of-line) (point)))
9595         (beg (progn (beginning-of-line) (point))))
9596     (prog1
9597         ;; Any hidden lines here?
9598         (search-forward "\r" end t)
9599       (subst-char-in-region beg end ?\^M ?\n t)
9600       (goto-char orig)
9601       (gnus-summary-position-point))))
9602
9603 (defun gnus-summary-hide-all-threads ()
9604   "Hide all thread subtrees."
9605   (interactive)
9606   (save-excursion
9607     (goto-char (point-min))
9608     (gnus-summary-hide-thread)
9609     (while (zerop (gnus-summary-next-thread 1 t))
9610       (gnus-summary-hide-thread)))
9611   (gnus-summary-position-point))
9612
9613 (defun gnus-summary-hide-thread ()
9614   "Hide thread subtrees.
9615 Returns nil if no threads were there to be hidden."
9616   (interactive)
9617   (let ((buffer-read-only nil)
9618         (start (point))
9619         (article (gnus-summary-article-number)))
9620     (goto-char start)
9621     ;; Go forward until either the buffer ends or the subthread
9622     ;; ends.
9623     (when (and (not (eobp))
9624                (or (zerop (gnus-summary-next-thread 1 t))
9625                    (goto-char (point-max))))
9626       (prog1
9627           (if (and (> (point) start)
9628                    (search-backward "\n" start t))
9629               (progn
9630                 (subst-char-in-region start (point) ?\n ?\^M)
9631                 (gnus-summary-goto-subject article))
9632             (goto-char start)
9633             nil)))))
9634
9635 (defun gnus-summary-go-to-next-thread (&optional previous)
9636   "Go to the same level (or less) next thread.
9637 If PREVIOUS is non-nil, go to previous thread instead.
9638 Return the article number moved to, or nil if moving was impossible."
9639   (let ((level (gnus-summary-thread-level))
9640         (way (if previous -1 1))
9641         (beg (point)))
9642     (forward-line way)
9643     (while (and (not (eobp))
9644                 (< level (gnus-summary-thread-level)))
9645       (forward-line way))
9646     (if (eobp)
9647         (progn
9648           (goto-char beg)
9649           nil)
9650       (setq beg (point))
9651       (prog1
9652           (gnus-summary-article-number)
9653         (goto-char beg)))))
9654
9655 (defun gnus-summary-next-thread (n &optional silent)
9656   "Go to the same level next N'th thread.
9657 If N is negative, search backward instead.
9658 Returns the difference between N and the number of skips actually
9659 done.
9660
9661 If SILENT, don't output messages."
9662   (interactive "p")
9663   (let ((backward (< n 0))
9664         (n (abs n)))
9665     (while (and (> n 0)
9666                 (gnus-summary-go-to-next-thread backward))
9667       (decf n))
9668     (unless silent
9669       (gnus-summary-position-point))
9670     (when (and (not silent) (/= 0 n))
9671       (gnus-message 7 "No more threads"))
9672     n))
9673
9674 (defun gnus-summary-prev-thread (n)
9675   "Go to the same level previous N'th thread.
9676 Returns the difference between N and the number of skips actually
9677 done."
9678   (interactive "p")
9679   (gnus-summary-next-thread (- n)))
9680
9681 (defun gnus-summary-go-down-thread ()
9682   "Go down one level in the current thread."
9683   (let ((children (gnus-summary-article-children)))
9684     (when children
9685       (gnus-summary-goto-subject (car children)))))
9686
9687 (defun gnus-summary-go-up-thread ()
9688   "Go up one level in the current thread."
9689   (let ((parent (gnus-summary-article-parent)))
9690     (when parent
9691       (gnus-summary-goto-subject parent))))
9692
9693 (defun gnus-summary-down-thread (n)
9694   "Go down thread N steps.
9695 If N is negative, go up instead.
9696 Returns the difference between N and how many steps down that were
9697 taken."
9698   (interactive "p")
9699   (let ((up (< n 0))
9700         (n (abs n)))
9701     (while (and (> n 0)
9702                 (if up (gnus-summary-go-up-thread)
9703                   (gnus-summary-go-down-thread)))
9704       (setq n (1- n)))
9705     (gnus-summary-position-point)
9706     (when (/= 0 n)
9707       (gnus-message 7 "Can't go further"))
9708     n))
9709
9710 (defun gnus-summary-up-thread (n)
9711   "Go up thread N steps.
9712 If N is negative, go down instead.
9713 Returns the difference between N and how many steps down that were
9714 taken."
9715   (interactive "p")
9716   (gnus-summary-down-thread (- n)))
9717
9718 (defun gnus-summary-top-thread ()
9719   "Go to the top of the thread."
9720   (interactive)
9721   (while (gnus-summary-go-up-thread))
9722   (gnus-summary-article-number))
9723
9724 (defun gnus-summary-kill-thread (&optional unmark)
9725   "Mark articles under current thread as read.
9726 If the prefix argument is positive, remove any kinds of marks.
9727 If the prefix argument is negative, tick articles instead."
9728   (interactive "P")
9729   (when unmark
9730     (setq unmark (prefix-numeric-value unmark)))
9731   (let ((articles (gnus-summary-articles-in-thread)))
9732     (save-excursion
9733       ;; Expand the thread.
9734       (gnus-summary-show-thread)
9735       ;; Mark all the articles.
9736       (while articles
9737         (gnus-summary-goto-subject (car articles))
9738         (cond ((null unmark)
9739                (gnus-summary-mark-article-as-read gnus-killed-mark))
9740               ((> unmark 0)
9741                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9742               (t
9743                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9744         (setq articles (cdr articles))))
9745     ;; Hide killed subtrees.
9746     (and (null unmark)
9747          gnus-thread-hide-killed
9748          (gnus-summary-hide-thread))
9749     ;; If marked as read, go to next unread subject.
9750     (when (null unmark)
9751       ;; Go to next unread subject.
9752       (gnus-summary-next-subject 1 t)))
9753   (gnus-set-mode-line 'summary))
9754
9755 ;; Summary sorting commands
9756
9757 (defun gnus-summary-sort-by-number (&optional reverse)
9758   "Sort the summary buffer by article number.
9759 Argument REVERSE means reverse order."
9760   (interactive "P")
9761   (gnus-summary-sort 'number reverse))
9762
9763 (defun gnus-summary-sort-by-author (&optional reverse)
9764   "Sort the summary buffer by author name alphabetically.
9765 If `case-fold-search' is non-nil, case of letters is ignored.
9766 Argument REVERSE means reverse order."
9767   (interactive "P")
9768   (gnus-summary-sort 'author reverse))
9769
9770 (defun gnus-summary-sort-by-subject (&optional reverse)
9771   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9772 If `case-fold-search' is non-nil, case of letters is ignored.
9773 Argument REVERSE means reverse order."
9774   (interactive "P")
9775   (gnus-summary-sort 'subject reverse))
9776
9777 (defun gnus-summary-sort-by-date (&optional reverse)
9778   "Sort the summary buffer by date.
9779 Argument REVERSE means reverse order."
9780   (interactive "P")
9781   (gnus-summary-sort 'date reverse))
9782
9783 (defun gnus-summary-sort-by-score (&optional reverse)
9784   "Sort the summary buffer by score.
9785 Argument REVERSE means reverse order."
9786   (interactive "P")
9787   (gnus-summary-sort 'score reverse))
9788
9789 (defun gnus-summary-sort-by-lines (&optional reverse)
9790   "Sort the summary buffer by the number of lines.
9791 Argument REVERSE means reverse order."
9792   (interactive "P")
9793   (gnus-summary-sort 'lines reverse))
9794
9795 (defun gnus-summary-sort-by-chars (&optional reverse)
9796   "Sort the summary buffer by article length.
9797 Argument REVERSE means reverse order."
9798   (interactive "P")
9799   (gnus-summary-sort 'chars reverse))
9800
9801 (defun gnus-summary-sort-by-original (&optional reverse)
9802   "Sort the summary buffer using the default sorting method.
9803 Argument REVERSE means reverse order."
9804   (interactive "P")
9805   (let* ((buffer-read-only)
9806          (gnus-summary-prepare-hook nil))
9807     ;; We do the sorting by regenerating the threads.
9808     (gnus-summary-prepare)
9809     ;; Hide subthreads if needed.
9810     (when (and gnus-show-threads gnus-thread-hide-subtree)
9811       (gnus-summary-hide-all-threads))))
9812
9813 (defun gnus-summary-sort (predicate reverse)
9814   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9815   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9816          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9817          (gnus-thread-sort-functions
9818           (if (not reverse)
9819               thread
9820             `(lambda (t1 t2)
9821                (,thread t2 t1))))
9822          (gnus-sort-gathered-threads-function
9823           gnus-thread-sort-functions)
9824          (gnus-article-sort-functions
9825           (if (not reverse)
9826               article
9827             `(lambda (t1 t2)
9828                (,article t2 t1))))
9829          (buffer-read-only)
9830          (gnus-summary-prepare-hook nil))
9831     ;; We do the sorting by regenerating the threads.
9832     (gnus-summary-prepare)
9833     ;; Hide subthreads if needed.
9834     (when (and gnus-show-threads gnus-thread-hide-subtree)
9835       (gnus-summary-hide-all-threads))))
9836
9837 ;; Summary saving commands.
9838
9839 (defun gnus-summary-save-article (&optional n not-saved)
9840   "Save the current article using the default saver function.
9841 If N is a positive number, save the N next articles.
9842 If N is a negative number, save the N previous articles.
9843 If N is nil and any articles have been marked with the process mark,
9844 save those articles instead.
9845 The variable `gnus-default-article-saver' specifies the saver function."
9846   (interactive "P")
9847   (let* ((articles (gnus-summary-work-articles n))
9848          (save-buffer (save-excursion
9849                         (nnheader-set-temp-buffer " *Gnus Save*")))
9850          (num (length articles))
9851          header file)
9852     (dolist (article articles)
9853       (setq header (gnus-summary-article-header article))
9854       (if (not (vectorp header))
9855           ;; This is a pseudo-article.
9856           (if (assq 'name header)
9857               (gnus-copy-file (cdr (assq 'name header)))
9858             (gnus-message 1 "Article %d is unsaveable" article))
9859         ;; This is a real article.
9860         (save-window-excursion
9861           (let ((gnus-display-mime-function nil)
9862                 (gnus-article-prepare-hook nil))
9863             (gnus-summary-select-article t nil nil article)))
9864         (save-excursion
9865           (set-buffer save-buffer)
9866           (erase-buffer)
9867           (insert-buffer-substring gnus-original-article-buffer))
9868         (setq file (gnus-article-save save-buffer file num))
9869         (gnus-summary-remove-process-mark article)
9870         (unless not-saved
9871           (gnus-summary-set-saved-mark article))))
9872     (gnus-kill-buffer save-buffer)
9873     (gnus-summary-position-point)
9874     (gnus-set-mode-line 'summary)
9875     n))
9876
9877 (defun gnus-summary-pipe-output (&optional arg)
9878   "Pipe the current article to a subprocess.
9879 If N is a positive number, pipe the N next articles.
9880 If N is a negative number, pipe the N previous articles.
9881 If N is nil and any articles have been marked with the process mark,
9882 pipe those articles instead."
9883   (interactive "P")
9884   (require 'gnus-art)
9885   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9886     (gnus-summary-save-article arg t))
9887   (let ((buffer (get-buffer "*Shell Command Output*")))
9888     (if (and buffer
9889              (with-current-buffer buffer (> (point-max) (point-min))))
9890         (gnus-configure-windows 'pipe))))
9891
9892 (defun gnus-summary-save-article-mail (&optional arg)
9893   "Append the current article to an mail file.
9894 If N is a positive number, save the N next articles.
9895 If N is a negative number, save the N previous articles.
9896 If N is nil and any articles have been marked with the process mark,
9897 save those articles instead."
9898   (interactive "P")
9899   (require 'gnus-art)
9900   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9901     (gnus-summary-save-article arg)))
9902
9903 (defun gnus-summary-save-article-rmail (&optional arg)
9904   "Append the current article to an rmail file.
9905 If N is a positive number, save the N next articles.
9906 If N is a negative number, save the N previous articles.
9907 If N is nil and any articles have been marked with the process mark,
9908 save those articles instead."
9909   (interactive "P")
9910   (require 'gnus-art)
9911   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9912     (gnus-summary-save-article arg)))
9913
9914 (defun gnus-summary-save-article-file (&optional arg)
9915   "Append the current article to a file.
9916 If N is a positive number, save the N next articles.
9917 If N is a negative number, save the N previous articles.
9918 If N is nil and any articles have been marked with the process mark,
9919 save those articles instead."
9920   (interactive "P")
9921   (require 'gnus-art)
9922   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9923     (gnus-summary-save-article arg)))
9924
9925 (defun gnus-summary-write-article-file (&optional arg)
9926   "Write the current article to a file, deleting the previous file.
9927 If N is a positive number, save the N next articles.
9928 If N is a negative number, save the N previous articles.
9929 If N is nil and any articles have been marked with the process mark,
9930 save those articles instead."
9931   (interactive "P")
9932   (require 'gnus-art)
9933   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9934     (gnus-summary-save-article arg)))
9935
9936 (defun gnus-summary-save-article-body-file (&optional arg)
9937   "Append the current article body to a file.
9938 If N is a positive number, save the N next articles.
9939 If N is a negative number, save the N previous articles.
9940 If N is nil and any articles have been marked with the process mark,
9941 save those articles instead."
9942   (interactive "P")
9943   (require 'gnus-art)
9944   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9945     (gnus-summary-save-article arg)))
9946
9947 (defun gnus-summary-pipe-message (program)
9948   "Pipe the current article through PROGRAM."
9949   (interactive "sProgram: ")
9950   (gnus-summary-select-article)
9951   (let ((mail-header-separator ""))
9952     (gnus-eval-in-buffer-window gnus-article-buffer
9953       (save-restriction
9954         (widen)
9955         (let ((start (window-start))
9956               buffer-read-only)
9957           (message-pipe-buffer-body program)
9958           (set-window-start (get-buffer-window (current-buffer)) start))))))
9959
9960 (defun gnus-get-split-value (methods)
9961   "Return a value based on the split METHODS."
9962   (let (split-name method result match)
9963     (when methods
9964       (save-excursion
9965         (set-buffer gnus-original-article-buffer)
9966         (save-restriction
9967           (nnheader-narrow-to-headers)
9968           (while (and methods (not split-name))
9969             (goto-char (point-min))
9970             (setq method (pop methods))
9971             (setq match (car method))
9972             (when (cond
9973                    ((stringp match)
9974                     ;; Regular expression.
9975                     (ignore-errors
9976                       (re-search-forward match nil t)))
9977                    ((gnus-functionp match)
9978                     ;; Function.
9979                     (save-restriction
9980                       (widen)
9981                       (setq result (funcall match gnus-newsgroup-name))))
9982                    ((consp match)
9983                     ;; Form.
9984                     (save-restriction
9985                       (widen)
9986                       (setq result (eval match)))))
9987               (setq split-name (cdr method))
9988               (cond ((stringp result)
9989                      (push (expand-file-name
9990                             result gnus-article-save-directory)
9991                            split-name))
9992                     ((consp result)
9993                      (setq split-name (append result split-name)))))))))
9994     (nreverse split-name)))
9995
9996 (defun gnus-valid-move-group-p (group)
9997   (and (boundp group)
9998        (symbol-name group)
9999        (symbol-value group)
10000        (gnus-get-function (gnus-find-method-for-group
10001                            (symbol-name group)) 'request-accept-article t)))
10002
10003 (defun gnus-read-move-group-name (prompt default articles prefix)
10004   "Read a group name."
10005   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10006          (minibuffer-confirm-incomplete nil) ; XEmacs
10007          (prom
10008           (format "%s %s to:"
10009                   prompt
10010                   (if (> (length articles) 1)
10011                       (format "these %d articles" (length articles))
10012                     "this article")))
10013          (to-newsgroup
10014           (cond
10015            ((null split-name)
10016             (gnus-completing-read default prom
10017                                   gnus-active-hashtb
10018                                   'gnus-valid-move-group-p
10019                                   nil prefix
10020                                   'gnus-group-history))
10021            ((= 1 (length split-name))
10022             (gnus-completing-read (car split-name) prom
10023                                   gnus-active-hashtb
10024                                   'gnus-valid-move-group-p
10025                                   nil nil
10026                                   'gnus-group-history))
10027            (t
10028             (gnus-completing-read nil prom
10029                                   (mapcar (lambda (el) (list el))
10030                                           (nreverse split-name))
10031                                   nil nil nil
10032                                   'gnus-group-history))))
10033          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10034     (when to-newsgroup
10035       (if (or (string= to-newsgroup "")
10036               (string= to-newsgroup prefix))
10037           (setq to-newsgroup default))
10038       (unless to-newsgroup
10039         (error "No group name entered"))
10040       (or (gnus-active to-newsgroup)
10041           (gnus-activate-group to-newsgroup nil nil to-method)
10042           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10043                                      to-newsgroup))
10044               (or (and (gnus-request-create-group to-newsgroup to-method)
10045                        (gnus-activate-group
10046                         to-newsgroup nil nil to-method)
10047                        (gnus-subscribe-group to-newsgroup))
10048                   (error "Couldn't create group %s" to-newsgroup)))
10049           (error "No such group: %s" to-newsgroup)))
10050     to-newsgroup))
10051
10052 (defun gnus-summary-save-parts (type dir n &optional reverse)
10053   "Save parts matching TYPE to DIR.
10054 If REVERSE, save parts that do not match TYPE."
10055   (interactive
10056    (list (read-string "Save parts of type: "
10057                       (or (car gnus-summary-save-parts-type-history)
10058                           gnus-summary-save-parts-default-mime)
10059                       'gnus-summary-save-parts-type-history)
10060          (setq gnus-summary-save-parts-last-directory
10061                (read-file-name "Save to directory: "
10062                                gnus-summary-save-parts-last-directory
10063                                nil t))
10064          current-prefix-arg))
10065   (gnus-summary-iterate n
10066     (let ((gnus-display-mime-function nil)
10067           (gnus-inhibit-treatment t))
10068       (gnus-summary-select-article))
10069     (save-excursion
10070       (set-buffer gnus-article-buffer)
10071       (let ((handles (or gnus-article-mime-handles
10072                          (mm-dissect-buffer) (mm-uu-dissect))))
10073         (when handles
10074           (gnus-summary-save-parts-1 type dir handles reverse)
10075           (unless gnus-article-mime-handles ;; Don't destroy this case.
10076             (mm-destroy-parts handles)))))))
10077
10078 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10079   (if (stringp (car handle))
10080       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10081               (cdr handle))
10082     (when (if reverse
10083               (not (string-match type (mm-handle-media-type handle)))
10084             (string-match type (mm-handle-media-type handle)))
10085       (let ((file (expand-file-name
10086                    (file-name-nondirectory
10087                     (or
10088                      (mail-content-type-get
10089                       (mm-handle-disposition handle) 'filename)
10090                      (concat gnus-newsgroup-name
10091                              "." (number-to-string
10092                                   (cdr gnus-article-current)))))
10093                    dir)))
10094         (unless (file-exists-p file)
10095           (mm-save-part-to-file handle file))))))
10096
10097 ;; Summary extract commands
10098
10099 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10100   (let ((buffer-read-only nil)
10101         (article (gnus-summary-article-number))
10102         after-article b e)
10103     (unless (gnus-summary-goto-subject article)
10104       (error "No such article: %d" article))
10105     (gnus-summary-position-point)
10106     ;; If all commands are to be bunched up on one line, we collect
10107     ;; them here.
10108     (unless gnus-view-pseudos-separately
10109       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10110             files action)
10111         (while ps
10112           (setq action (cdr (assq 'action (car ps))))
10113           (setq files (list (cdr (assq 'name (car ps)))))
10114           (while (and ps (cdr ps)
10115                       (string= (or action "1")
10116                                (or (cdr (assq 'action (cadr ps))) "2")))
10117             (push (cdr (assq 'name (cadr ps))) files)
10118             (setcdr ps (cddr ps)))
10119           (when files
10120             (when (not (string-match "%s" action))
10121               (push " " files))
10122             (push " " files)
10123             (when (assq 'execute (car ps))
10124               (setcdr (assq 'execute (car ps))
10125                       (funcall (if (string-match "%s" action)
10126                                    'format 'concat)
10127                                action
10128                                (mapconcat
10129                                 (lambda (f)
10130                                   (if (equal f " ")
10131                                       f
10132                                     (mm-quote-arg f)))
10133                                 files " ")))))
10134           (setq ps (cdr ps)))))
10135     (if (and gnus-view-pseudos (not not-view))
10136         (while pslist
10137           (when (assq 'execute (car pslist))
10138             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10139                                   (eq gnus-view-pseudos 'not-confirm)))
10140           (setq pslist (cdr pslist)))
10141       (save-excursion
10142         (while pslist
10143           (setq after-article (or (cdr (assq 'article (car pslist)))
10144                                   (gnus-summary-article-number)))
10145           (gnus-summary-goto-subject after-article)
10146           (forward-line 1)
10147           (setq b (point))
10148           (insert "    " (file-name-nondirectory
10149                           (cdr (assq 'name (car pslist))))
10150                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10151           (setq e (point))
10152           (forward-line -1)             ; back to `b'
10153           (gnus-add-text-properties
10154            b (1- e) (list 'gnus-number gnus-reffed-article-number
10155                           gnus-mouse-face-prop gnus-mouse-face))
10156           (gnus-data-enter
10157            after-article gnus-reffed-article-number
10158            gnus-unread-mark b (car pslist) 0 (- e b))
10159           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10160           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10161           (setq pslist (cdr pslist)))))))
10162
10163 (defun gnus-pseudos< (p1 p2)
10164   (let ((c1 (cdr (assq 'action p1)))
10165         (c2 (cdr (assq 'action p2))))
10166     (and c1 c2 (string< c1 c2))))
10167
10168 (defun gnus-request-pseudo-article (props)
10169   (cond ((assq 'execute props)
10170          (gnus-execute-command (cdr (assq 'execute props)))))
10171   (let ((gnus-current-article (gnus-summary-article-number)))
10172     (gnus-run-hooks 'gnus-mark-article-hook)))
10173
10174 (defun gnus-execute-command (command &optional automatic)
10175   (save-excursion
10176     (gnus-article-setup-buffer)
10177     (set-buffer gnus-article-buffer)
10178     (setq buffer-read-only nil)
10179     (let ((command (if automatic command
10180                      (read-string "Command: " (cons command 0)))))
10181       (erase-buffer)
10182       (insert "$ " command "\n\n")
10183       (if gnus-view-pseudo-asynchronously
10184           (start-process "gnus-execute" (current-buffer) shell-file-name
10185                          shell-command-switch command)
10186         (call-process shell-file-name nil t nil
10187                       shell-command-switch command)))))
10188
10189 ;; Summary kill commands.
10190
10191 (defun gnus-summary-edit-global-kill (article)
10192   "Edit the \"global\" kill file."
10193   (interactive (list (gnus-summary-article-number)))
10194   (gnus-group-edit-global-kill article))
10195
10196 (defun gnus-summary-edit-local-kill ()
10197   "Edit a local kill file applied to the current newsgroup."
10198   (interactive)
10199   (setq gnus-current-headers (gnus-summary-article-header))
10200   (gnus-group-edit-local-kill
10201    (gnus-summary-article-number) gnus-newsgroup-name))
10202
10203 ;;; Header reading.
10204
10205 (defun gnus-read-header (id &optional header)
10206   "Read the headers of article ID and enter them into the Gnus system."
10207   (let ((group gnus-newsgroup-name)
10208         (gnus-override-method
10209          (or
10210           gnus-override-method
10211           (and (gnus-news-group-p gnus-newsgroup-name)
10212                (car (gnus-refer-article-methods)))))
10213         where)
10214     ;; First we check to see whether the header in question is already
10215     ;; fetched.
10216     (if (stringp id)
10217         ;; This is a Message-ID.
10218         (setq header (or header (gnus-id-to-header id)))
10219       ;; This is an article number.
10220       (setq header (or header (gnus-summary-article-header id))))
10221     (if (and header
10222              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10223         ;; We have found the header.
10224         header
10225       ;; If this is a sparse article, we have to nix out its
10226       ;; previous entry in the thread hashtb.
10227       (when (and header
10228                  (gnus-summary-article-sparse-p (mail-header-number header)))
10229         (let* ((parent (gnus-parent-id (mail-header-references header)))
10230                (thread (and parent (gnus-id-to-thread parent))))
10231           (when thread
10232             (delq (assq header thread) thread))))
10233       ;; We have to really fetch the header to this article.
10234       (save-excursion
10235         (set-buffer nntp-server-buffer)
10236         (when (setq where (gnus-request-head id group))
10237           (nnheader-fold-continuation-lines)
10238           (goto-char (point-max))
10239           (insert ".\n")
10240           (goto-char (point-min))
10241           (insert "211 ")
10242           (princ (cond
10243                   ((numberp id) id)
10244                   ((cdr where) (cdr where))
10245                   (header (mail-header-number header))
10246                   (t gnus-reffed-article-number))
10247                  (current-buffer))
10248           (insert " Article retrieved.\n"))
10249         (if (or (not where)
10250                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10251             ()                          ; Malformed head.
10252           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10253             (when (and (stringp id)
10254                        (not (string= (gnus-group-real-name group)
10255                                      (car where))))
10256               ;; If we fetched by Message-ID and the article came
10257               ;; from a different group, we fudge some bogus article
10258               ;; numbers for this article.
10259               (mail-header-set-number header gnus-reffed-article-number))
10260             (save-excursion
10261               (set-buffer gnus-summary-buffer)
10262               (decf gnus-reffed-article-number)
10263               (gnus-remove-header (mail-header-number header))
10264               (push header gnus-newsgroup-headers)
10265               (setq gnus-current-headers header)
10266               (push (mail-header-number header) gnus-newsgroup-limit)))
10267           header)))))
10268
10269 (defun gnus-remove-header (number)
10270   "Remove header NUMBER from `gnus-newsgroup-headers'."
10271   (if (and gnus-newsgroup-headers
10272            (= number (mail-header-number (car gnus-newsgroup-headers))))
10273       (pop gnus-newsgroup-headers)
10274     (let ((headers gnus-newsgroup-headers))
10275       (while (and (cdr headers)
10276                   (not (= number (mail-header-number (cadr headers)))))
10277         (pop headers))
10278       (when (cdr headers)
10279         (setcdr headers (cddr headers))))))
10280
10281 ;;;
10282 ;;; summary highlights
10283 ;;;
10284
10285 (defun gnus-highlight-selected-summary ()
10286   "Highlight selected article in summary buffer."
10287   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10288   (when gnus-summary-selected-face
10289     (save-excursion
10290       (let* ((beg (progn (beginning-of-line) (point)))
10291              (end (progn (end-of-line) (point)))
10292              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10293              (from (if (get-text-property beg gnus-mouse-face-prop)
10294                        beg
10295                      (or (next-single-property-change
10296                           beg gnus-mouse-face-prop nil end)
10297                          beg)))
10298              (to
10299               (if (= from end)
10300                   (- from 2)
10301                 (or (next-single-property-change
10302                      from gnus-mouse-face-prop nil end)
10303                     end))))
10304         ;; If no mouse-face prop on line we will have to = from = end,
10305         ;; so we highlight the entire line instead.
10306         (when (= (+ to 2) from)
10307           (setq from beg)
10308           (setq to end))
10309         (if gnus-newsgroup-selected-overlay
10310             ;; Move old overlay.
10311             (gnus-move-overlay
10312              gnus-newsgroup-selected-overlay from to (current-buffer))
10313           ;; Create new overlay.
10314           (gnus-overlay-put
10315            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10316            'face gnus-summary-selected-face))))))
10317
10318 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10319 (defun gnus-summary-highlight-line ()
10320   "Highlight current line according to `gnus-summary-highlight'."
10321   (let* ((list gnus-summary-highlight)
10322          (p (point))
10323          (end (progn (end-of-line) (point)))
10324          ;; now find out where the line starts and leave point there.
10325          (beg (progn (beginning-of-line) (point)))
10326          (article (gnus-summary-article-number))
10327          (score (or (cdr (assq (or article gnus-current-article)
10328                                gnus-newsgroup-scored))
10329                     gnus-summary-default-score 0))
10330          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10331          (inhibit-read-only t))
10332     ;; Eval the cars of the lists until we find a match.
10333     (let ((default gnus-summary-default-score)
10334           (default-high gnus-summary-default-high-score)
10335           (default-low gnus-summary-default-low-score))
10336       (while (and list
10337                   (not (eval (caar list))))
10338         (setq list (cdr list))))
10339     (let ((face (cdar list)))
10340       (unless (eq face (get-text-property beg 'face))
10341         (gnus-put-text-property-excluding-characters-with-faces
10342          beg end 'face
10343          (setq face (if (boundp face) (symbol-value face) face)))
10344         (when gnus-summary-highlight-line-function
10345           (funcall gnus-summary-highlight-line-function article face))))
10346     (goto-char p)))
10347
10348 (defun gnus-update-read-articles (group unread &optional compute)
10349   "Update the list of read articles in GROUP."
10350   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10351          (entry (gnus-gethash group gnus-newsrc-hashtb))
10352          (info (nth 2 entry))
10353          (prev 1)
10354          (unread (sort (copy-sequence unread) '<))
10355          read)
10356     (if (or (not info) (not active))
10357         ;; There is no info on this group if it was, in fact,
10358         ;; killed.  Gnus stores no information on killed groups, so
10359         ;; there's nothing to be done.
10360         ;; One could store the information somewhere temporarily,
10361         ;; perhaps...  Hmmm...
10362         ()
10363       ;; Remove any negative articles numbers.
10364       (while (and unread (< (car unread) 0))
10365         (setq unread (cdr unread)))
10366       ;; Remove any expired article numbers
10367       (while (and unread (< (car unread) (car active)))
10368         (setq unread (cdr unread)))
10369       ;; Compute the ranges of read articles by looking at the list of
10370       ;; unread articles.
10371       (while unread
10372         (when (/= (car unread) prev)
10373           (push (if (= prev (1- (car unread))) prev
10374                   (cons prev (1- (car unread))))
10375                 read))
10376         (setq prev (1+ (car unread)))
10377         (setq unread (cdr unread)))
10378       (when (<= prev (cdr active))
10379         (push (cons prev (cdr active)) read))
10380       (setq read (if (> (length read) 1) (nreverse read) read))
10381       (if compute
10382           read
10383         (save-excursion
10384           (let (setmarkundo)
10385             ;; Propagate the read marks to the backend.
10386             (when (gnus-check-backend-function 'request-set-mark group)
10387               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10388                     (add (gnus-remove-from-range read (gnus-info-read info))))
10389                 (when (or add del)
10390                   (unless (gnus-check-group group)
10391                     (error "Can't open server for %s" group))
10392                   (gnus-request-set-mark
10393                    group (delq nil (list (if add (list add 'add '(read)))
10394                                          (if del (list del 'del '(read))))))
10395                   (setq setmarkundo
10396                         `(gnus-request-set-mark
10397                           ,group
10398                           ',(delq nil (list
10399                                        (if del (list del 'add '(read)))
10400                                        (if add (list add 'del '(read))))))))))
10401             (set-buffer gnus-group-buffer)
10402             (gnus-undo-register
10403               `(progn
10404                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10405                  (gnus-info-set-read ',info ',(gnus-info-read info))
10406                  (gnus-get-unread-articles-in-group ',info
10407                                                     (gnus-active ,group))
10408                  (gnus-group-update-group ,group t)
10409                  ,setmarkundo))))
10410         ;; Enter this list into the group info.
10411         (gnus-info-set-read info read)
10412         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10413         (gnus-get-unread-articles-in-group info (gnus-active group))
10414         t))))
10415
10416 (defun gnus-offer-save-summaries ()
10417   "Offer to save all active summary buffers."
10418   (let (buffers)
10419     ;; Go through all buffers and find all summaries.
10420     (dolist (buffer (buffer-list))
10421       (when (and (setq buffer (buffer-name buffer))
10422                  (string-match "Summary" buffer)
10423                  (save-excursion
10424                    (set-buffer buffer)
10425                    ;; We check that this is, indeed, a summary buffer.
10426                    (and (eq major-mode 'gnus-summary-mode)
10427                         ;; Also make sure this isn't bogus.
10428                         gnus-newsgroup-prepared
10429                         ;; Also make sure that this isn't a
10430                         ;; dead summary buffer.
10431                         (not gnus-dead-summary-mode))))
10432         (push buffer buffers)))
10433     ;; Go through all these summary buffers and offer to save them.
10434     (when buffers
10435       (save-excursion
10436         (map-y-or-n-p
10437          "Update summary buffer %s? "
10438          (lambda (buf)
10439            (switch-to-buffer buf)
10440            (gnus-summary-exit))
10441          buffers)))))
10442
10443 (defun gnus-summary-setup-default-charset ()
10444   "Setup newsgroup default charset."
10445   (if (equal gnus-newsgroup-name "nndraft:drafts")
10446       (setq gnus-newsgroup-charset nil)
10447     (let* ((ignored-charsets
10448             (or gnus-newsgroup-ephemeral-ignored-charsets
10449                 (append
10450                  (and gnus-newsgroup-name
10451                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10452                  gnus-newsgroup-ignored-charsets))))
10453       (setq gnus-newsgroup-charset
10454             (or gnus-newsgroup-ephemeral-charset
10455                 (and gnus-newsgroup-name
10456                      (gnus-parameter-charset gnus-newsgroup-name))
10457                 gnus-default-charset))
10458       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10459            ignored-charsets))))
10460
10461 ;;;
10462 ;;; Mime Commands
10463 ;;;
10464
10465 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10466   "Display the current article buffer fully MIME-buttonized.
10467 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10468 treated as multipart/mixed."
10469   (interactive "P")
10470   (require 'gnus-art)
10471   (let ((gnus-unbuttonized-mime-types nil)
10472         (gnus-mime-display-multipart-as-mixed show-all-parts))
10473     (gnus-summary-show-article)))
10474
10475 (defun gnus-summary-repair-multipart (article)
10476   "Add a Content-Type header to a multipart article without one."
10477   (interactive (list (gnus-summary-article-number)))
10478   (gnus-with-article article
10479     (message-narrow-to-head)
10480     (message-remove-header "Mime-Version")
10481     (goto-char (point-max))
10482     (insert "Mime-Version: 1.0\n")
10483     (widen)
10484     (when (search-forward "\n--" nil t)
10485       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10486         (message-narrow-to-head)
10487         (message-remove-header "Content-Type")
10488         (goto-char (point-max))
10489         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10490                         separator))
10491         (widen))))
10492   (let (gnus-mark-article-hook)
10493     (gnus-summary-select-article t t nil article)))
10494
10495 (defun gnus-summary-toggle-display-buttonized ()
10496   "Toggle the buttonizing of the article buffer."
10497   (interactive)
10498   (require 'gnus-art)
10499   (if (setq gnus-inhibit-mime-unbuttonizing
10500             (not gnus-inhibit-mime-unbuttonizing))
10501       (let ((gnus-unbuttonized-mime-types nil))
10502         (gnus-summary-show-article))
10503     (gnus-summary-show-article)))
10504
10505 ;;;
10506 ;;; Generic summary marking commands
10507 ;;;
10508
10509 (defvar gnus-summary-marking-alist
10510   '((read gnus-del-mark "d")
10511     (unread gnus-unread-mark "u")
10512     (ticked gnus-ticked-mark "!")
10513     (dormant gnus-dormant-mark "?")
10514     (expirable gnus-expirable-mark "e"))
10515   "An alist of names/marks/keystrokes.")
10516
10517 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10518 (defvar gnus-summary-mark-map)
10519
10520 (defun gnus-summary-make-all-marking-commands ()
10521   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10522   (dolist (elem gnus-summary-marking-alist)
10523     (apply 'gnus-summary-make-marking-command elem)))
10524
10525 (defun gnus-summary-make-marking-command (name mark keystroke)
10526   (let ((map (make-sparse-keymap)))
10527     (define-key gnus-summary-generic-mark-map keystroke map)
10528     (dolist (lway `((next "next" next nil "n")
10529                     (next-unread "next unread" next t "N")
10530                     (prev "previous" prev nil "p")
10531                     (prev-unread "previous unread" prev t "P")
10532                     (nomove "" nil nil ,keystroke)))
10533       (let ((func (gnus-summary-make-marking-command-1
10534                    mark (car lway) lway name)))
10535         (setq func (eval func))
10536         (define-key map (nth 4 lway) func)))))
10537
10538 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10539   `(defun ,(intern
10540             (format "gnus-summary-put-mark-as-%s%s"
10541                     name (if (eq way 'nomove)
10542                              ""
10543                            (concat "-" (symbol-name way)))))
10544      (n)
10545      ,(format
10546        "Mark the current article as %s%s.
10547 If N, the prefix, then repeat N times.
10548 If N is negative, move in reverse order.
10549 The difference between N and the actual number of articles marked is
10550 returned."
10551        name (cadr lway))
10552      (interactive "p")
10553      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10554
10555 (defun gnus-summary-generic-mark (n mark move unread)
10556   "Mark N articles with MARK."
10557   (unless (eq major-mode 'gnus-summary-mode)
10558     (error "This command can only be used in the summary buffer"))
10559   (gnus-summary-show-thread)
10560   (let ((nummove
10561          (cond
10562           ((eq move 'next) 1)
10563           ((eq move 'prev) -1)
10564           (t 0))))
10565     (if (zerop nummove)
10566         (setq n 1)
10567       (when (< n 0)
10568         (setq n (abs n)
10569               nummove (* -1 nummove))))
10570     (while (and (> n 0)
10571                 (gnus-summary-mark-article nil mark)
10572                 (zerop (gnus-summary-next-subject nummove unread t)))
10573       (setq n (1- n)))
10574     (when (/= 0 n)
10575       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10576     (gnus-summary-recenter)
10577     (gnus-summary-position-point)
10578     (gnus-set-mode-line 'summary)
10579     n))
10580
10581 (defun gnus-summary-insert-articles (articles)
10582   (when (setq articles
10583               (gnus-set-difference articles
10584                                    (mapcar (lambda (h) (mail-header-number h))
10585                                            gnus-newsgroup-headers)))
10586     (setq gnus-newsgroup-headers
10587           (merge 'list
10588                  gnus-newsgroup-headers
10589                  (gnus-fetch-headers articles)
10590                  'gnus-article-sort-by-number))
10591     ;; Suppress duplicates?
10592     (when gnus-suppress-duplicates
10593       (gnus-dup-suppress-articles))
10594
10595     ;; We might want to build some more threads first.
10596     (when (and gnus-fetch-old-headers
10597                (eq gnus-headers-retrieved-by 'nov))
10598       (if (eq gnus-fetch-old-headers 'invisible)
10599           (gnus-build-all-threads)
10600         (gnus-build-old-threads)))
10601     ;; Let the Gnus agent mark articles as read.
10602     (when gnus-agent
10603       (gnus-agent-get-undownloaded-list))
10604     ;; Remove list identifiers from subject
10605     (when gnus-list-identifiers
10606       (gnus-summary-remove-list-identifiers))
10607     ;; First and last article in this newsgroup.
10608     (when gnus-newsgroup-headers
10609       (setq gnus-newsgroup-begin
10610             (mail-header-number (car gnus-newsgroup-headers))
10611             gnus-newsgroup-end
10612             (mail-header-number
10613              (gnus-last-element gnus-newsgroup-headers))))
10614     (when gnus-use-scoring
10615       (gnus-possibly-score-headers))))
10616
10617 (defun gnus-summary-insert-old-articles (&optional all)
10618   "Insert all old articles in this group.
10619 If ALL is non-nil, already read articles become readable.
10620 If ALL is a number, fetch this number of articles."
10621   (interactive "P")
10622   (prog1
10623       (let ((old (mapcar 'car gnus-newsgroup-data))
10624             (i (car gnus-newsgroup-active))
10625             older len)
10626         (while (<= i (cdr gnus-newsgroup-active))
10627           (or (memq i old) (push i older))
10628           (incf i))
10629         (setq len (length older))
10630         (cond
10631          ((null older) nil)
10632          ((numberp all)
10633           (if (< all len)
10634               (setq older (subseq older 0 all))))
10635          (all nil)
10636          (t
10637           (if (and (numberp gnus-large-newsgroup)
10638                    (> len gnus-large-newsgroup))
10639               (let ((input
10640                      (read-string
10641                       (format
10642                        "How many articles from %s (default %d): "
10643                        (gnus-limit-string
10644                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10645                        len))))
10646                 (unless (string-match "^[ \t]*$" input)
10647                   (setq all (string-to-number input))
10648                   (if (< all len)
10649                       (setq older (subseq older 0 all))))))))
10650         (if (not older)
10651             (message "No old news.")
10652           (gnus-summary-insert-articles older)
10653           (gnus-summary-limit (gnus-union older old))))
10654     (gnus-summary-position-point)))
10655
10656 (defun gnus-summary-insert-new-articles ()
10657   "Insert all new articles in this group."
10658   (interactive)
10659   (prog1
10660       (let ((old (mapcar 'car gnus-newsgroup-data))
10661             (old-active gnus-newsgroup-active)
10662             (nnmail-fetched-sources (list t))
10663             i new)
10664         (setq gnus-newsgroup-active
10665               (gnus-activate-group gnus-newsgroup-name 'scan))
10666         (setq i (1+ (cdr old-active)))
10667         (while (<= i (cdr gnus-newsgroup-active))
10668           (push i new)
10669           (incf i))
10670         (if (not new)
10671             (message "No gnus is bad news.")
10672           (setq new (nreverse new))
10673           (gnus-summary-insert-articles new)
10674           (setq gnus-newsgroup-unreads
10675                 (append gnus-newsgroup-unreads new))
10676           (gnus-summary-limit (gnus-union old new))))
10677     (gnus-summary-position-point)))
10678
10679 (gnus-summary-make-all-marking-commands)
10680
10681 (gnus-ems-redefine)
10682
10683 (provide 'gnus-sum)
10684
10685 (run-hooks 'gnus-sum-load-hook)
10686
10687 ;;; gnus-sum.el ends here