2001-08-19 Simon Josefsson <jas@extundo.com>
[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 to."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-no-mark ?               ;Whitespace
458   "*Mark used for articles that have no other secondary mark."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-ancient-mark ?O
463   "*Mark used for ancient articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-sparse-mark ?Q
468   "*Mark used for sparsely reffed articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-canceled-mark ?G
473   "*Mark used for canceled articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-duplicate-mark ?M
478   "*Mark used for duplicate articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-undownloaded-mark ?@
483   "*Mark used for articles that weren't downloaded."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-downloadable-mark ?%
488   "*Mark used for articles that are to be downloaded."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-unsendable-mark ?=
493   "*Mark used for articles that won't be sent."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-score-over-mark ?+
498   "*Score mark used for articles with high scores."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-score-below-mark ?-
503   "*Score mark used for articles with low scores."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-empty-thread-mark ?     ;Whitespace
508   "*There is no thread under the article."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-not-empty-thread-mark ?=
513   "*There is a thread under the article."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-view-pseudo-asynchronously nil
518   "*If non-nil, Gnus will view pseudo-articles asynchronously."
519   :group 'gnus-extract-view
520   :type 'boolean)
521
522 (defcustom gnus-auto-expirable-marks
523   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
524         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
525         gnus-souped-mark gnus-duplicate-mark)
526   "*The list of marks converted into expiration if a group is auto-expirable."
527   :version "21.1"
528   :group 'gnus-summary
529   :type '(repeat character))
530
531 (defcustom gnus-inhibit-user-auto-expire t
532   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
533   :version "21.1"
534   :group 'gnus-summary
535   :type 'boolean)
536
537 (defcustom gnus-view-pseudos nil
538   "*If `automatic', pseudo-articles will be viewed automatically.
539 If `not-confirm', pseudos will be viewed automatically, and the user
540 will not be asked to confirm the command."
541   :group 'gnus-extract-view
542   :type '(choice (const :tag "off" nil)
543                  (const automatic)
544                  (const not-confirm)))
545
546 (defcustom gnus-view-pseudos-separately t
547   "*If non-nil, one pseudo-article will be created for each file to be viewed.
548 If nil, all files that use the same viewing command will be given as a
549 list of parameters to that command."
550   :group 'gnus-extract-view
551   :type 'boolean)
552
553 (defcustom gnus-insert-pseudo-articles t
554   "*If non-nil, insert pseudo-articles when decoding articles."
555   :group 'gnus-extract-view
556   :type 'boolean)
557
558 (defcustom gnus-summary-dummy-line-format
559   "  %(:                          :%) %S\n"
560   "*The format specification for the dummy roots in the summary buffer.
561 It works along the same lines as a normal formatting string,
562 with some simple extensions.
563
564 %S  The subject"
565   :group 'gnus-threading
566   :type 'string)
567
568 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
569   "*The format specification for the summary mode line.
570 It works along the same lines as a normal formatting string,
571 with some simple extensions:
572
573 %G  Group name
574 %p  Unprefixed group name
575 %A  Current article number
576 %z  Current article score
577 %V  Gnus version
578 %U  Number of unread articles in the group
579 %e  Number of unselected articles in the group
580 %Z  A string with unread/unselected article counts
581 %g  Shortish group name
582 %S  Subject of the current article
583 %u  User-defined spec
584 %s  Current score file name
585 %d  Number of dormant articles
586 %r  Number of articles that have been marked as read in this session
587 %E  Number of articles expunged by the score files"
588   :group 'gnus-summary-format
589   :type 'string)
590
591 (defcustom gnus-list-identifiers nil
592   "Regexp that matches list identifiers to be removed from subject.
593 This can also be a list of regexps."
594   :version "21.1"
595   :group 'gnus-summary-format
596   :group 'gnus-article-hiding
597   :type '(choice (const :tag "none" nil)
598                  (regexp :value ".*")
599                  (repeat :value (".*") regexp)))
600
601 (defcustom gnus-summary-mark-below 0
602   "*Mark all articles with a score below this variable as read.
603 This variable is local to each summary buffer and usually set by the
604 score file."
605   :group 'gnus-score-default
606   :type 'integer)
607
608 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
609   "*List of functions used for sorting articles in the summary buffer.
610
611 Each function takes two articles and returns non-nil if the first
612 article should be sorted before the other.  If you use more than one
613 function, the primary sort function should be the last.  You should
614 probably always include `gnus-article-sort-by-number' in the list of
615 sorting functions -- preferably first.  Also note that sorting by date
616 is often much slower than sorting by number, and the sorting order is
617 very similar.  (Sorting by date means sorting by the time the message
618 was sent, sorting by number means sorting by arrival time.)
619
620 Ready-made functions include `gnus-article-sort-by-number',
621 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
622 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
623
624 When threading is turned on, the variable `gnus-thread-sort-functions'
625 controls how articles are sorted."
626   :group 'gnus-summary-sort
627   :type '(repeat (choice (function-item gnus-article-sort-by-number)
628                          (function-item gnus-article-sort-by-author)
629                          (function-item gnus-article-sort-by-subject)
630                          (function-item gnus-article-sort-by-date)
631                          (function-item gnus-article-sort-by-score)
632                          (function :tag "other"))))
633
634 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
635   "*List of functions used for sorting threads in the summary buffer.
636 By default, threads are sorted by article number.
637
638 Each function takes two threads and returns non-nil if the first
639 thread should be sorted before the other.  If you use more than one
640 function, the primary sort function should be the last.  You should
641 probably always include `gnus-thread-sort-by-number' in the list of
642 sorting functions -- preferably first.  Also note that sorting by date
643 is often much slower than sorting by number, and the sorting order is
644 very similar.  (Sorting by date means sorting by the time the message
645 was sent, sorting by number means sorting by arrival time.)
646
647 Ready-made functions include `gnus-thread-sort-by-number',
648 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
649 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
650 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
651
652 When threading is turned off, the variable
653 `gnus-article-sort-functions' controls how articles are sorted."
654   :group 'gnus-summary-sort
655   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
656                          (function-item gnus-thread-sort-by-author)
657                          (function-item gnus-thread-sort-by-subject)
658                          (function-item gnus-thread-sort-by-date)
659                          (function-item gnus-thread-sort-by-score)
660                          (function-item gnus-thread-sort-by-total-score)
661                          (function :tag "other"))))
662
663 (defcustom gnus-thread-score-function '+
664   "*Function used for calculating the total score of a thread.
665
666 The function is called with the scores of the article and each
667 subthread and should then return the score of the thread.
668
669 Some functions you can use are `+', `max', or `min'."
670   :group 'gnus-summary-sort
671   :type 'function)
672
673 (defcustom gnus-summary-expunge-below nil
674   "All articles that have a score less than this variable will be expunged.
675 This variable is local to the summary buffers."
676   :group 'gnus-score-default
677   :type '(choice (const :tag "off" nil)
678                  integer))
679
680 (defcustom gnus-thread-expunge-below nil
681   "All threads that have a total score less than this variable will be expunged.
682 See `gnus-thread-score-function' for en explanation of what a
683 \"thread score\" is.
684
685 This variable is local to the summary buffers."
686   :group 'gnus-threading
687   :group 'gnus-score-default
688   :type '(choice (const :tag "off" nil)
689                  integer))
690
691 (defcustom gnus-summary-mode-hook nil
692   "*A hook for Gnus summary mode.
693 This hook is run before any variables are set in the summary buffer."
694   :options '(turn-on-gnus-mailing-list-mode)
695   :group 'gnus-summary-various
696   :type 'hook)
697
698 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
699 (when (featurep 'xemacs)
700   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
701   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
702   (add-hook 'gnus-summary-mode-hook
703             'gnus-xmas-switch-horizontal-scrollbar-off))
704
705 (defcustom gnus-summary-menu-hook nil
706   "*Hook run after the creation of the summary mode menu."
707   :group 'gnus-summary-visual
708   :type 'hook)
709
710 (defcustom gnus-summary-exit-hook nil
711   "*A hook called on exit from the summary buffer.
712 It will be called with point in the group buffer."
713   :group 'gnus-summary-exit
714   :type 'hook)
715
716 (defcustom gnus-summary-prepare-hook nil
717   "*A hook called after the summary buffer has been generated.
718 If you want to modify the summary buffer, you can use this hook."
719   :group 'gnus-summary-various
720   :type 'hook)
721
722 (defcustom gnus-summary-prepared-hook nil
723   "*A hook called as the last thing after the summary buffer has been generated."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-generate-hook nil
728   "*A hook run just before generating the summary buffer.
729 This hook is commonly used to customize threading variables and the
730 like."
731   :group 'gnus-summary-various
732   :type 'hook)
733
734 (defcustom gnus-select-group-hook nil
735   "*A hook called when a newsgroup is selected.
736
737 If you'd like to simplify subjects like the
738 `gnus-summary-next-same-subject' command does, you can use the
739 following hook:
740
741  (setq gnus-select-group-hook
742       (list
743         (lambda ()
744           (mapcar (lambda (header)
745                      (mail-header-set-subject
746                       header
747                       (gnus-simplify-subject
748                        (mail-header-subject header) 're-only)))
749                   gnus-newsgroup-headers))))"
750   :group 'gnus-group-select
751   :type 'hook)
752
753 (defcustom gnus-select-article-hook nil
754   "*A hook called when an article is selected."
755   :group 'gnus-summary-choose
756   :type 'hook)
757
758 (defcustom gnus-visual-mark-article-hook
759   (list 'gnus-highlight-selected-summary)
760   "*Hook run after selecting an article in the summary buffer.
761 It is meant to be used for highlighting the article in some way.  It
762 is not run if `gnus-visual' is nil."
763   :group 'gnus-summary-visual
764   :type 'hook)
765
766 (defcustom gnus-parse-headers-hook nil
767   "*A hook called before parsing the headers."
768   :group 'gnus-various
769   :type 'hook)
770
771 (defcustom gnus-exit-group-hook nil
772   "*A hook called when exiting summary mode.
773 This hook is not called from the non-updating exit commands like `Q'."
774   :group 'gnus-various
775   :type 'hook)
776
777 (defcustom gnus-summary-update-hook
778   (list 'gnus-summary-highlight-line)
779   "*A hook called when a summary line is changed.
780 The hook will not be called if `gnus-visual' is nil.
781
782 The default function `gnus-summary-highlight-line' will
783 highlight the line according to the `gnus-summary-highlight'
784 variable."
785   :group 'gnus-summary-visual
786   :type 'hook)
787
788 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
789   "*A hook called when an article is selected for the first time.
790 The hook is intended to mark an article as read (or unread)
791 automatically when it is selected."
792   :group 'gnus-summary-choose
793   :type 'hook)
794
795 (defcustom gnus-group-no-more-groups-hook nil
796   "*A hook run when returning to group mode having no more (unread) groups."
797   :group 'gnus-group-select
798   :type 'hook)
799
800 (defcustom gnus-ps-print-hook nil
801   "*A hook run before ps-printing something from Gnus."
802   :group 'gnus-summary
803   :type 'hook)
804
805 (defcustom gnus-summary-display-arrow
806   (and (fboundp 'display-graphic-p)
807        (display-graphic-p))
808   "*If non-nil, display an arrow highlighting the current article."
809   :version "21.1"
810   :group 'gnus-summary
811   :type 'boolean)
812
813 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
814   "Face used for highlighting the current article in the summary buffer."
815   :group 'gnus-summary-visual
816   :type 'face)
817
818 (defcustom gnus-summary-highlight
819   '(((= mark gnus-canceled-mark)
820      . gnus-summary-cancelled-face)
821     ((and (> score default-high)
822           (or (= mark gnus-dormant-mark)
823               (= mark gnus-ticked-mark)))
824      . gnus-summary-high-ticked-face)
825     ((and (< score default-low)
826           (or (= mark gnus-dormant-mark)
827               (= mark gnus-ticked-mark)))
828      . gnus-summary-low-ticked-face)
829     ((or (= mark gnus-dormant-mark)
830          (= mark gnus-ticked-mark))
831      . gnus-summary-normal-ticked-face)
832     ((and (> score default-high) (= mark gnus-ancient-mark))
833      . gnus-summary-high-ancient-face)
834     ((and (< score default-low) (= mark gnus-ancient-mark))
835      . gnus-summary-low-ancient-face)
836     ((= mark gnus-ancient-mark)
837      . gnus-summary-normal-ancient-face)
838     ((and (> score default-high) (= mark gnus-unread-mark))
839      . gnus-summary-high-unread-face)
840     ((and (< score default-low) (= mark gnus-unread-mark))
841      . gnus-summary-low-unread-face)
842     ((= mark gnus-unread-mark)
843      . gnus-summary-normal-unread-face)
844     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
845                                                   gnus-undownloaded-mark)))
846      . gnus-summary-high-unread-face)
847     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
848                                                  gnus-undownloaded-mark)))
849      . gnus-summary-low-unread-face)
850     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
851           (memq article gnus-newsgroup-unreads))
852      . gnus-summary-normal-unread-face)
853     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
854      . gnus-summary-normal-read-face)
855     ((> score default-high)
856      . gnus-summary-high-read-face)
857     ((< score default-low)
858      . gnus-summary-low-read-face)
859     (t
860      . gnus-summary-normal-read-face))
861   "*Controls the highlighting of summary buffer lines.
862
863 A list of (FORM . FACE) pairs.  When deciding how a a particular
864 summary line should be displayed, each form is evaluated.  The content
865 of the face field after the first true form is used.  You can change
866 how those summary lines are displayed, by editing the face field.
867
868 You can use the following variables in the FORM field.
869
870 score:        The article's score
871 default:      The default article score.
872 default-high: The default score for high scored articles.
873 default-low:  The default score for low scored articles.
874 below:        The score below which articles are automatically marked as read.
875 mark:         The articles mark."
876   :group 'gnus-summary-visual
877   :type '(repeat (cons (sexp :tag "Form" nil)
878                        face)))
879
880 (defcustom gnus-alter-header-function nil
881   "Function called to allow alteration of article header structures.
882 The function is called with one parameter, the article header vector,
883 which it may alter in any way.")
884
885 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
886   "Variable that says which function should be used to decode a string with encoded words.")
887
888 (defcustom gnus-extra-headers nil
889   "*Extra headers to parse."
890   :version "21.1"
891   :group 'gnus-summary
892   :type '(repeat symbol))
893
894 (defcustom gnus-ignored-from-addresses
895   (and user-mail-address (regexp-quote user-mail-address))
896   "*Regexp of From headers that may be suppressed in favor of To headers."
897   :version "21.1"
898   :group 'gnus-summary
899   :type 'regexp)
900
901 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
902   "List of charsets that should be ignored.
903 When these charsets are used in the \"charset\" parameter, the
904 default charset will be used instead."
905   :version "21.1"
906   :type '(repeat symbol)
907   :group 'gnus-charset)
908
909 (gnus-define-group-parameter
910  ignored-charsets
911  :type list
912  :function-document
913  "Return the ignored charsets of GROUP."
914  :variable gnus-group-ignored-charsets-alist
915  :variable-default
916  '(("alt\\.chinese\\.text" iso-8859-1))
917  :variable-document
918  "Alist of regexps (to match group names) and charsets that should be ignored.
919 When these charsets are used in the \"charset\" parameter, the
920 default charset will be used instead."
921  :variable-group gnus-charset
922  :variable-type '(repeat (cons (regexp :tag "Group")
923                                (repeat symbol)))
924  :parameter-type '(choice :tag "Ignored charsets"
925                           :value nil
926                           (repeat (symbol)))
927  :parameter-document       "\
928 List of charsets that should be ignored.
929
930 When these charsets are used in the \"charset\" parameter, the
931 default charset will be used instead.")
932
933 (defcustom gnus-group-highlight-words-alist nil
934   "Alist of group regexps and highlight regexps.
935 This variable uses the same syntax as `gnus-emphasis-alist'."
936   :version "21.1"
937   :type '(repeat (cons (regexp :tag "Group")
938                        (repeat (list (regexp :tag "Highlight regexp")
939                                      (number :tag "Group for entire word" 0)
940                                      (number :tag "Group for displayed part" 0)
941                                      (symbol :tag "Face"
942                                              gnus-emphasis-highlight-words)))))
943   :group 'gnus-summary-visual)
944
945 (defcustom gnus-summary-show-article-charset-alist
946   nil
947   "Alist of number and charset.
948 The article will be shown with the charset corresponding to the
949 numbered argument.
950 For example: ((1 . cn-gb-2312) (2 . big5))."
951   :version "21.1"
952   :type '(repeat (cons (number :tag "Argument" 1)
953                        (symbol :tag "Charset")))
954   :group 'gnus-charset)
955
956 (defcustom gnus-preserve-marks t
957   "Whether marks are preserved when moving, copying and respooling messages."
958   :version "21.1"
959   :type 'boolean
960   :group 'gnus-summary-marks)
961
962 (defcustom gnus-alter-articles-to-read-function nil
963   "Function to be called to alter the list of articles to be selected."
964   :type '(choice (const nil) function)
965   :group 'gnus-summary)
966
967 (defcustom gnus-orphan-score nil
968   "*All orphans get this score added.  Set in the score file."
969   :group 'gnus-score-default
970   :type '(choice (const nil)
971                  integer))
972
973 (defcustom gnus-summary-save-parts-default-mime "image/.*"
974   "*A regexp to match MIME parts when saving multiple parts of a message
975 with gnus-summary-save-parts (X m). This regexp will be used by default
976 when prompting the user for which type of files to save."
977   :group 'gnus-summary
978   :type 'regexp)
979
980
981 (defcustom gnus-summary-save-parts-default-mime "image/.*"
982   "*A regexp to match MIME parts when saving multiple parts of a message
983 with gnus-summary-save-parts (X m). This regexp will be used by default
984 when prompting the user for which type of files to save."
985   :group 'gnus-summary
986   :type 'regexp)
987
988 (defcustom gnus-read-all-available-headers nil
989   "Whether Gnus should parse all headers made available to it.
990 This is mostly relevant for slow backends where the user may
991 wish to widen the summary buffer to include all headers
992 that were fetched.  Say, for nnultimate groups."
993   :group 'gnus-summary
994   :type '(choice boolean regexp))
995
996 ;;; Internal variables
997
998 (defvar gnus-summary-display-cache nil)
999 (defvar gnus-article-mime-handles nil)
1000 (defvar gnus-article-decoded-p nil)
1001 (defvar gnus-article-charset nil)
1002 (defvar gnus-article-ignored-charsets nil)
1003 (defvar gnus-scores-exclude-files nil)
1004 (defvar gnus-page-broken nil)
1005 (defvar gnus-inhibit-mime-unbuttonizing nil)
1006
1007 (defvar gnus-original-article nil)
1008 (defvar gnus-article-internal-prepare-hook nil)
1009 (defvar gnus-newsgroup-process-stack nil)
1010
1011 (defvar gnus-thread-indent-array nil)
1012 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1013 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1014   "Function called to sort the articles within a thread after it has been gathered together.")
1015
1016 (defvar gnus-summary-save-parts-type-history nil)
1017 (defvar gnus-summary-save-parts-last-directory nil)
1018
1019 (defvar gnus-summary-save-parts-type-history nil)
1020 (defvar gnus-summary-save-parts-last-directory nil)
1021
1022 ;; Avoid highlighting in kill files.
1023 (defvar gnus-summary-inhibit-highlight nil)
1024 (defvar gnus-newsgroup-selected-overlay nil)
1025 (defvar gnus-inhibit-limiting nil)
1026 (defvar gnus-newsgroup-adaptive-score-file nil)
1027 (defvar gnus-current-score-file nil)
1028 (defvar gnus-current-move-group nil)
1029 (defvar gnus-current-copy-group nil)
1030 (defvar gnus-current-crosspost-group nil)
1031 (defvar gnus-newsgroup-display nil)
1032
1033 (defvar gnus-newsgroup-dependencies nil)
1034 (defvar gnus-newsgroup-adaptive nil)
1035 (defvar gnus-summary-display-article-function nil)
1036 (defvar gnus-summary-highlight-line-function nil
1037   "Function called after highlighting a summary line.")
1038
1039 (defvar gnus-summary-line-format-alist
1040   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1041     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1042     (?s gnus-tmp-subject-or-nil ?s)
1043     (?n gnus-tmp-name ?s)
1044     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1045         ?s)
1046     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1047             gnus-tmp-from) ?s)
1048     (?F gnus-tmp-from ?s)
1049     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1050     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1051     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1052     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1053     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1054     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1055     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1056     (?L gnus-tmp-lines ?s)
1057     (?I gnus-tmp-indentation ?s)
1058     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1059     (?R gnus-tmp-replied ?c)
1060     (?\[ gnus-tmp-opening-bracket ?c)
1061     (?\] gnus-tmp-closing-bracket ?c)
1062     (?\> (make-string gnus-tmp-level ? ) ?s)
1063     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1064     (?i gnus-tmp-score ?d)
1065     (?z gnus-tmp-score-char ?c)
1066     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1067     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1068     (?U gnus-tmp-unread ?c)
1069     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1070     (?t (gnus-summary-number-of-articles-in-thread
1071          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1072         ?d)
1073     (?e (gnus-summary-number-of-articles-in-thread
1074          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1075         ?c)
1076     (?u gnus-tmp-user-defined ?s)
1077     (?P (gnus-pick-line-number) ?d)
1078     (?B gnus-tmp-thread-tree-header-string ?s))
1079   "An alist of format specifications that can appear in summary lines.
1080 These are paired with what variables they correspond with, along with
1081 the type of the variable (string, integer, character, etc).")
1082
1083 (defvar gnus-summary-dummy-line-format-alist
1084   `((?S gnus-tmp-subject ?s)
1085     (?N gnus-tmp-number ?d)
1086     (?u gnus-tmp-user-defined ?s)))
1087
1088 (defvar gnus-summary-mode-line-format-alist
1089   `((?G gnus-tmp-group-name ?s)
1090     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1091     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1092     (?A gnus-tmp-article-number ?d)
1093     (?Z gnus-tmp-unread-and-unselected ?s)
1094     (?V gnus-version ?s)
1095     (?U gnus-tmp-unread-and-unticked ?d)
1096     (?S gnus-tmp-subject ?s)
1097     (?e gnus-tmp-unselected ?d)
1098     (?u gnus-tmp-user-defined ?s)
1099     (?d (length gnus-newsgroup-dormant) ?d)
1100     (?t (length gnus-newsgroup-marked) ?d)
1101     (?r (length gnus-newsgroup-reads) ?d)
1102     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1103     (?E gnus-newsgroup-expunged-tally ?d)
1104     (?s (gnus-current-score-file-nondirectory) ?s)))
1105
1106 (defvar gnus-last-search-regexp nil
1107   "Default regexp for article search command.")
1108
1109 (defvar gnus-last-shell-command nil
1110   "Default shell command on article.")
1111
1112 (defvar gnus-newsgroup-begin nil)
1113 (defvar gnus-newsgroup-end nil)
1114 (defvar gnus-newsgroup-last-rmail nil)
1115 (defvar gnus-newsgroup-last-mail nil)
1116 (defvar gnus-newsgroup-last-folder nil)
1117 (defvar gnus-newsgroup-last-file nil)
1118 (defvar gnus-newsgroup-auto-expire nil)
1119 (defvar gnus-newsgroup-active nil)
1120
1121 (defvar gnus-newsgroup-data nil)
1122 (defvar gnus-newsgroup-data-reverse nil)
1123 (defvar gnus-newsgroup-limit nil)
1124 (defvar gnus-newsgroup-limits nil)
1125
1126 (defvar gnus-newsgroup-unreads nil
1127   "List of unread articles in the current newsgroup.")
1128
1129 (defvar gnus-newsgroup-unselected nil
1130   "List of unselected unread articles in the current newsgroup.")
1131
1132 (defvar gnus-newsgroup-reads nil
1133   "Alist of read articles and article marks in the current newsgroup.")
1134
1135 (defvar gnus-newsgroup-expunged-tally nil)
1136
1137 (defvar gnus-newsgroup-marked nil
1138   "List of ticked articles in the current newsgroup (a subset of unread art).")
1139
1140 (defvar gnus-newsgroup-killed nil
1141   "List of ranges of articles that have been through the scoring process.")
1142
1143 (defvar gnus-newsgroup-cached nil
1144   "List of articles that come from the article cache.")
1145
1146 (defvar gnus-newsgroup-saved nil
1147   "List of articles that have been saved.")
1148
1149 (defvar gnus-newsgroup-kill-headers nil)
1150
1151 (defvar gnus-newsgroup-replied nil
1152   "List of articles that have been replied to in the current newsgroup.")
1153
1154 (defvar gnus-newsgroup-forwarded nil
1155   "List of articles that have been forwarded in the current newsgroup.")
1156
1157 (defvar gnus-newsgroup-recent nil
1158   "List of articles that have are recent in the current newsgroup.")
1159
1160 (defvar gnus-newsgroup-expirable nil
1161   "List of articles in the current newsgroup that can be expired.")
1162
1163 (defvar gnus-newsgroup-processable nil
1164   "List of articles in the current newsgroup that can be processed.")
1165
1166 (defvar gnus-newsgroup-downloadable nil
1167   "List of articles in the current newsgroup that can be processed.")
1168
1169 (defvar gnus-newsgroup-undownloaded nil
1170   "List of articles in the current newsgroup that haven't been downloaded..")
1171
1172 (defvar gnus-newsgroup-unsendable nil
1173   "List of articles in the current newsgroup that won't be sent.")
1174
1175 (defvar gnus-newsgroup-bookmarks nil
1176   "List of articles in the current newsgroup that have bookmarks.")
1177
1178 (defvar gnus-newsgroup-dormant nil
1179   "List of dormant articles in the current newsgroup.")
1180
1181 (defvar gnus-newsgroup-scored nil
1182   "List of scored articles in the current newsgroup.")
1183
1184 (defvar gnus-newsgroup-headers nil
1185   "List of article headers in the current newsgroup.")
1186
1187 (defvar gnus-newsgroup-threads nil)
1188
1189 (defvar gnus-newsgroup-prepared nil
1190   "Whether the current group has been prepared properly.")
1191
1192 (defvar gnus-newsgroup-ancient nil
1193   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-sparse nil)
1196
1197 (defvar gnus-current-article nil)
1198 (defvar gnus-article-current nil)
1199 (defvar gnus-current-headers nil)
1200 (defvar gnus-have-all-headers nil)
1201 (defvar gnus-last-article nil)
1202 (defvar gnus-newsgroup-history nil)
1203 (defvar gnus-newsgroup-charset nil)
1204 (defvar gnus-newsgroup-ephemeral-charset nil)
1205 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1206
1207 (defvar gnus-article-before-search nil)
1208
1209 (defconst gnus-summary-local-variables
1210   '(gnus-newsgroup-name
1211     gnus-newsgroup-begin gnus-newsgroup-end
1212     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1213     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1214     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1215     gnus-newsgroup-unselected gnus-newsgroup-marked
1216     gnus-newsgroup-reads gnus-newsgroup-saved
1217     gnus-newsgroup-replied gnus-newsgroup-forwarded
1218     gnus-newsgroup-recent
1219     gnus-newsgroup-expirable
1220     gnus-newsgroup-processable gnus-newsgroup-killed
1221     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1222     gnus-newsgroup-unsendable
1223     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1224     gnus-newsgroup-headers gnus-newsgroup-threads
1225     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1226     gnus-current-article gnus-current-headers gnus-have-all-headers
1227     gnus-last-article gnus-article-internal-prepare-hook
1228     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1229     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1230     gnus-thread-expunge-below
1231     gnus-score-alist gnus-current-score-file
1232     (gnus-summary-expunge-below . global)
1233     (gnus-summary-mark-below . global)
1234     (gnus-orphan-score . global)
1235     gnus-newsgroup-active gnus-scores-exclude-files
1236     gnus-newsgroup-history gnus-newsgroup-ancient
1237     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1238     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1239     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1240     (gnus-newsgroup-expunged-tally . 0)
1241     gnus-cache-removable-articles gnus-newsgroup-cached
1242     gnus-newsgroup-data gnus-newsgroup-data-reverse
1243     gnus-newsgroup-limit gnus-newsgroup-limits
1244     gnus-newsgroup-charset gnus-newsgroup-display)
1245   "Variables that are buffer-local to the summary buffers.")
1246
1247 (defvar gnus-newsgroup-variables nil
1248   "Variables that have separate values in the newsgroups.")
1249
1250 ;; Byte-compiler warning.
1251 (eval-when-compile (defvar gnus-article-mode-map))
1252
1253 ;; MIME stuff.
1254
1255 (defvar gnus-decode-encoded-word-methods
1256   '(mail-decode-encoded-word-string)
1257   "List of methods used to decode encoded words.
1258
1259 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1260 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1261 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1262 whose names match REGEXP.
1263
1264 For example:
1265 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1266  mail-decode-encoded-word-string
1267  (\"chinese\" . rfc1843-decode-string))")
1268
1269 (defvar gnus-decode-encoded-word-methods-cache nil)
1270
1271 (defun gnus-multi-decode-encoded-word-string (string)
1272   "Apply the functions from `gnus-encoded-word-methods' that match."
1273   (unless (and gnus-decode-encoded-word-methods-cache
1274                (eq gnus-newsgroup-name
1275                    (car gnus-decode-encoded-word-methods-cache)))
1276     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1277     (mapcar (lambda (x)
1278               (if (symbolp x)
1279                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1280                 (if (and gnus-newsgroup-name
1281                          (string-match (car x) gnus-newsgroup-name))
1282                     (nconc gnus-decode-encoded-word-methods-cache
1283                            (list (cdr x))))))
1284             gnus-decode-encoded-word-methods))
1285   (let ((xlist gnus-decode-encoded-word-methods-cache))
1286     (pop xlist)
1287     (while xlist
1288       (setq string (funcall (pop xlist) string))))
1289   string)
1290
1291 ;; Subject simplification.
1292
1293 (defun gnus-simplify-whitespace (str)
1294   "Remove excessive whitespace from STR."
1295   (let ((mystr str))
1296     ;; Multiple spaces.
1297     (while (string-match "[ \t][ \t]+" mystr)
1298       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1299                           " "
1300                           (substring mystr (match-end 0)))))
1301     ;; Leading spaces.
1302     (when (string-match "^[ \t]+" mystr)
1303       (setq mystr (substring mystr (match-end 0))))
1304     ;; Trailing spaces.
1305     (when (string-match "[ \t]+$" mystr)
1306       (setq mystr (substring mystr 0 (match-beginning 0))))
1307     mystr))
1308
1309 (defsubst gnus-simplify-subject-re (subject)
1310   "Remove \"Re:\" from subject lines."
1311   (if (string-match message-subject-re-regexp subject)
1312       (substring subject (match-end 0))
1313     subject))
1314
1315 (defun gnus-simplify-subject (subject &optional re-only)
1316   "Remove `Re:' and words in parentheses.
1317 If RE-ONLY is non-nil, strip leading `Re:'s only."
1318   (let ((case-fold-search t))           ;Ignore case.
1319     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1320     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1321       (setq subject (substring subject (match-end 0))))
1322     ;; Remove uninteresting prefixes.
1323     (when (and (not re-only)
1324                gnus-simplify-ignored-prefixes
1325                (string-match gnus-simplify-ignored-prefixes subject))
1326       (setq subject (substring subject (match-end 0))))
1327     ;; Remove words in parentheses from end.
1328     (unless re-only
1329       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1330         (setq subject (substring subject 0 (match-beginning 0)))))
1331     ;; Return subject string.
1332     subject))
1333
1334 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1335 ;; all whitespace.
1336 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1337   (goto-char (point-min))
1338   (while (re-search-forward regexp nil t)
1339     (replace-match (or newtext ""))))
1340
1341 (defun gnus-simplify-buffer-fuzzy ()
1342   "Simplify string in the buffer fuzzily.
1343 The string in the accessible portion of the current buffer is simplified.
1344 It is assumed to be a single-line subject.
1345 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1346 matter is removed.  Additional things can be deleted by setting
1347 `gnus-simplify-subject-fuzzy-regexp'."
1348   (let ((case-fold-search t)
1349         (modified-tick))
1350     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1351
1352     (while (not (eq modified-tick (buffer-modified-tick)))
1353       (setq modified-tick (buffer-modified-tick))
1354       (cond
1355        ((listp gnus-simplify-subject-fuzzy-regexp)
1356         (mapcar 'gnus-simplify-buffer-fuzzy-step
1357                 gnus-simplify-subject-fuzzy-regexp))
1358        (gnus-simplify-subject-fuzzy-regexp
1359         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1360       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1361       (gnus-simplify-buffer-fuzzy-step
1362        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1363       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1364
1365     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1366     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1367     (gnus-simplify-buffer-fuzzy-step " $")
1368     (gnus-simplify-buffer-fuzzy-step "^ +")))
1369
1370 (defun gnus-simplify-subject-fuzzy (subject)
1371   "Simplify a subject string fuzzily.
1372 See `gnus-simplify-buffer-fuzzy' for details."
1373   (save-excursion
1374     (gnus-set-work-buffer)
1375     (let ((case-fold-search t))
1376       ;; Remove uninteresting prefixes.
1377       (when (and gnus-simplify-ignored-prefixes
1378                  (string-match gnus-simplify-ignored-prefixes subject))
1379         (setq subject (substring subject (match-end 0))))
1380       (insert subject)
1381       (inline (gnus-simplify-buffer-fuzzy))
1382       (buffer-string))))
1383
1384 (defsubst gnus-simplify-subject-fully (subject)
1385   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1386   (cond
1387    (gnus-simplify-subject-functions
1388     (gnus-map-function gnus-simplify-subject-functions subject))
1389    ((null gnus-summary-gather-subject-limit)
1390     (gnus-simplify-subject-re subject))
1391    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1392     (gnus-simplify-subject-fuzzy subject))
1393    ((numberp gnus-summary-gather-subject-limit)
1394     (gnus-limit-string (gnus-simplify-subject-re subject)
1395                        gnus-summary-gather-subject-limit))
1396    (t
1397     subject)))
1398
1399 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1400   "Check whether two subjects are equal.
1401 If optional argument simple-first is t, first argument is already
1402 simplified."
1403   (cond
1404    ((null simple-first)
1405     (equal (gnus-simplify-subject-fully s1)
1406            (gnus-simplify-subject-fully s2)))
1407    (t
1408     (equal s1
1409            (gnus-simplify-subject-fully s2)))))
1410
1411 (defun gnus-summary-bubble-group ()
1412   "Increase the score of the current group.
1413 This is a handy function to add to `gnus-summary-exit-hook' to
1414 increase the score of each group you read."
1415   (gnus-group-add-score gnus-newsgroup-name))
1416
1417 \f
1418 ;;;
1419 ;;; Gnus summary mode
1420 ;;;
1421
1422 (put 'gnus-summary-mode 'mode-class 'special)
1423
1424 (defvar gnus-article-commands-menu)
1425
1426 (when t
1427   ;; Non-orthogonal keys
1428
1429   (gnus-define-keys gnus-summary-mode-map
1430     " " gnus-summary-next-page
1431     "\177" gnus-summary-prev-page
1432     [delete] gnus-summary-prev-page
1433     [backspace] gnus-summary-prev-page
1434     "\r" gnus-summary-scroll-up
1435     "\M-\r" gnus-summary-scroll-down
1436     "n" gnus-summary-next-unread-article
1437     "p" gnus-summary-prev-unread-article
1438     "N" gnus-summary-next-article
1439     "P" gnus-summary-prev-article
1440     "\M-\C-n" gnus-summary-next-same-subject
1441     "\M-\C-p" gnus-summary-prev-same-subject
1442     "\M-n" gnus-summary-next-unread-subject
1443     "\M-p" gnus-summary-prev-unread-subject
1444     "." gnus-summary-first-unread-article
1445     "," gnus-summary-best-unread-article
1446     "\M-s" gnus-summary-search-article-forward
1447     "\M-r" gnus-summary-search-article-backward
1448     "<" gnus-summary-beginning-of-article
1449     ">" gnus-summary-end-of-article
1450     "j" gnus-summary-goto-article
1451     "^" gnus-summary-refer-parent-article
1452     "\M-^" gnus-summary-refer-article
1453     "u" gnus-summary-tick-article-forward
1454     "!" gnus-summary-tick-article-forward
1455     "U" gnus-summary-tick-article-backward
1456     "d" gnus-summary-mark-as-read-forward
1457     "D" gnus-summary-mark-as-read-backward
1458     "E" gnus-summary-mark-as-expirable
1459     "\M-u" gnus-summary-clear-mark-forward
1460     "\M-U" gnus-summary-clear-mark-backward
1461     "k" gnus-summary-kill-same-subject-and-select
1462     "\C-k" gnus-summary-kill-same-subject
1463     "\M-\C-k" gnus-summary-kill-thread
1464     "\M-\C-l" gnus-summary-lower-thread
1465     "e" gnus-summary-edit-article
1466     "#" gnus-summary-mark-as-processable
1467     "\M-#" gnus-summary-unmark-as-processable
1468     "\M-\C-t" gnus-summary-toggle-threads
1469     "\M-\C-s" gnus-summary-show-thread
1470     "\M-\C-h" gnus-summary-hide-thread
1471     "\M-\C-f" gnus-summary-next-thread
1472     "\M-\C-b" gnus-summary-prev-thread
1473     [(meta down)] gnus-summary-next-thread
1474     [(meta up)] gnus-summary-prev-thread
1475     "\M-\C-u" gnus-summary-up-thread
1476     "\M-\C-d" gnus-summary-down-thread
1477     "&" gnus-summary-execute-command
1478     "c" gnus-summary-catchup-and-exit
1479     "\C-w" gnus-summary-mark-region-as-read
1480     "\C-t" gnus-summary-toggle-truncation
1481     "?" gnus-summary-mark-as-dormant
1482     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1483     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1484     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1485     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1486     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1487     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1488     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1489     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1490     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1491     "=" gnus-summary-expand-window
1492     "\C-x\C-s" gnus-summary-reselect-current-group
1493     "\M-g" gnus-summary-rescan-group
1494     "w" gnus-summary-stop-page-breaking
1495     "\C-c\C-r" gnus-summary-caesar-message
1496     "f" gnus-summary-followup
1497     "F" gnus-summary-followup-with-original
1498     "C" gnus-summary-cancel-article
1499     "r" gnus-summary-reply
1500     "R" gnus-summary-reply-with-original
1501     "\C-c\C-f" gnus-summary-mail-forward
1502     "o" gnus-summary-save-article
1503     "\C-o" gnus-summary-save-article-mail
1504     "|" gnus-summary-pipe-output
1505     "\M-k" gnus-summary-edit-local-kill
1506     "\M-K" gnus-summary-edit-global-kill
1507     ;; "V" gnus-version
1508     "\C-c\C-d" gnus-summary-describe-group
1509     "q" gnus-summary-exit
1510     "Q" gnus-summary-exit-no-update
1511     "\C-c\C-i" gnus-info-find-node
1512     gnus-mouse-2 gnus-mouse-pick-article
1513     "m" gnus-summary-mail-other-window
1514     "a" gnus-summary-post-news
1515     "x" gnus-summary-limit-to-unread
1516     "s" gnus-summary-isearch-article
1517     "t" gnus-summary-toggle-header
1518     "g" gnus-summary-show-article
1519     "l" gnus-summary-goto-last-article
1520     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1521     "\C-d" gnus-summary-enter-digest-group
1522     "\M-\C-d" gnus-summary-read-document
1523     "\M-\C-e" gnus-summary-edit-parameters
1524     "\M-\C-a" gnus-summary-customize-parameters
1525     "\C-c\C-b" gnus-bug
1526     "*" gnus-cache-enter-article
1527     "\M-*" gnus-cache-remove-article
1528     "\M-&" gnus-summary-universal-argument
1529     "\C-l" gnus-recenter
1530     "I" gnus-summary-increase-score
1531     "L" gnus-summary-lower-score
1532     "\M-i" gnus-symbolic-argument
1533     "h" gnus-summary-select-article-buffer
1534
1535     "b" gnus-article-view-part
1536     "\M-t" gnus-summary-toggle-display-buttonized
1537
1538     "V" gnus-summary-score-map
1539     "X" gnus-uu-extract-map
1540     "S" gnus-summary-send-map)
1541
1542   ;; Sort of orthogonal keymap
1543   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1544     "t" gnus-summary-tick-article-forward
1545     "!" gnus-summary-tick-article-forward
1546     "d" gnus-summary-mark-as-read-forward
1547     "r" gnus-summary-mark-as-read-forward
1548     "c" gnus-summary-clear-mark-forward
1549     " " gnus-summary-clear-mark-forward
1550     "e" gnus-summary-mark-as-expirable
1551     "x" gnus-summary-mark-as-expirable
1552     "?" gnus-summary-mark-as-dormant
1553     "b" gnus-summary-set-bookmark
1554     "B" gnus-summary-remove-bookmark
1555     "#" gnus-summary-mark-as-processable
1556     "\M-#" gnus-summary-unmark-as-processable
1557     "S" gnus-summary-limit-include-expunged
1558     "C" gnus-summary-catchup
1559     "H" gnus-summary-catchup-to-here
1560     "h" gnus-summary-catchup-from-here
1561     "\C-c" gnus-summary-catchup-all
1562     "k" gnus-summary-kill-same-subject-and-select
1563     "K" gnus-summary-kill-same-subject
1564     "P" gnus-uu-mark-map)
1565
1566   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1567     "c" gnus-summary-clear-above
1568     "u" gnus-summary-tick-above
1569     "m" gnus-summary-mark-above
1570     "k" gnus-summary-kill-below)
1571
1572   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1573     "/" gnus-summary-limit-to-subject
1574     "n" gnus-summary-limit-to-articles
1575     "w" gnus-summary-pop-limit
1576     "s" gnus-summary-limit-to-subject
1577     "a" gnus-summary-limit-to-author
1578     "u" gnus-summary-limit-to-unread
1579     "m" gnus-summary-limit-to-marks
1580     "M" gnus-summary-limit-exclude-marks
1581     "v" gnus-summary-limit-to-score
1582     "*" gnus-summary-limit-include-cached
1583     "D" gnus-summary-limit-include-dormant
1584     "T" gnus-summary-limit-include-thread
1585     "d" gnus-summary-limit-exclude-dormant
1586     "t" gnus-summary-limit-to-age
1587     "x" gnus-summary-limit-to-extra
1588     "E" gnus-summary-limit-include-expunged
1589     "c" gnus-summary-limit-exclude-childless-dormant
1590     "C" gnus-summary-limit-mark-excluded-as-read
1591     "o" gnus-summary-insert-old-articles
1592     "N" gnus-summary-insert-new-articles)
1593
1594   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1595     "n" gnus-summary-next-unread-article
1596     "p" gnus-summary-prev-unread-article
1597     "N" gnus-summary-next-article
1598     "P" gnus-summary-prev-article
1599     "\C-n" gnus-summary-next-same-subject
1600     "\C-p" gnus-summary-prev-same-subject
1601     "\M-n" gnus-summary-next-unread-subject
1602     "\M-p" gnus-summary-prev-unread-subject
1603     "f" gnus-summary-first-unread-article
1604     "b" gnus-summary-best-unread-article
1605     "j" gnus-summary-goto-article
1606     "g" gnus-summary-goto-subject
1607     "l" gnus-summary-goto-last-article
1608     "o" gnus-summary-pop-article)
1609
1610   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1611     "k" gnus-summary-kill-thread
1612     "l" gnus-summary-lower-thread
1613     "i" gnus-summary-raise-thread
1614     "T" gnus-summary-toggle-threads
1615     "t" gnus-summary-rethread-current
1616     "^" gnus-summary-reparent-thread
1617     "s" gnus-summary-show-thread
1618     "S" gnus-summary-show-all-threads
1619     "h" gnus-summary-hide-thread
1620     "H" gnus-summary-hide-all-threads
1621     "n" gnus-summary-next-thread
1622     "p" gnus-summary-prev-thread
1623     "u" gnus-summary-up-thread
1624     "o" gnus-summary-top-thread
1625     "d" gnus-summary-down-thread
1626     "#" gnus-uu-mark-thread
1627     "\M-#" gnus-uu-unmark-thread)
1628
1629   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1630     "g" gnus-summary-prepare
1631     "c" gnus-summary-insert-cached-articles)
1632
1633   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1634     "c" gnus-summary-catchup-and-exit
1635     "C" gnus-summary-catchup-all-and-exit
1636     "E" gnus-summary-exit-no-update
1637     "Q" gnus-summary-exit
1638     "Z" gnus-summary-exit
1639     "n" gnus-summary-catchup-and-goto-next-group
1640     "R" gnus-summary-reselect-current-group
1641     "G" gnus-summary-rescan-group
1642     "N" gnus-summary-next-group
1643     "s" gnus-summary-save-newsrc
1644     "P" gnus-summary-prev-group)
1645
1646   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1647     " " gnus-summary-next-page
1648     "n" gnus-summary-next-page
1649     "\177" gnus-summary-prev-page
1650     [delete] gnus-summary-prev-page
1651     "p" gnus-summary-prev-page
1652     "\r" gnus-summary-scroll-up
1653     "\M-\r" gnus-summary-scroll-down
1654     "<" gnus-summary-beginning-of-article
1655     ">" gnus-summary-end-of-article
1656     "b" gnus-summary-beginning-of-article
1657     "e" gnus-summary-end-of-article
1658     "^" gnus-summary-refer-parent-article
1659     "r" gnus-summary-refer-parent-article
1660     "D" gnus-summary-enter-digest-group
1661     "R" gnus-summary-refer-references
1662     "T" gnus-summary-refer-thread
1663     "g" gnus-summary-show-article
1664     "s" gnus-summary-isearch-article
1665     "P" gnus-summary-print-article
1666     "M" gnus-mailing-list-insinuate
1667     "t" gnus-article-babel)
1668
1669   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1670     "b" gnus-article-add-buttons
1671     "B" gnus-article-add-buttons-to-head
1672     "o" gnus-article-treat-overstrike
1673     "e" gnus-article-emphasize
1674     "w" gnus-article-fill-cited-article
1675     "Q" gnus-article-fill-long-lines
1676     "C" gnus-article-capitalize-sentences
1677     "c" gnus-article-remove-cr
1678     "q" gnus-article-de-quoted-unreadable
1679     "6" gnus-article-de-base64-unreadable
1680     "Z" gnus-article-decode-HZ
1681     "h" gnus-article-wash-html
1682     "s" gnus-summary-force-verify-and-decrypt
1683     "f" gnus-article-display-x-face
1684     "l" gnus-summary-stop-page-breaking
1685     "r" gnus-summary-caesar-message
1686     "t" gnus-summary-toggle-header
1687     "v" gnus-summary-verbose-headers
1688     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1689     "p" gnus-article-verify-x-pgp-sig
1690     "d" gnus-article-treat-dumbquotes)
1691
1692   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1693     "a" gnus-article-hide
1694     "h" gnus-article-hide-headers
1695     "b" gnus-article-hide-boring-headers
1696     "s" gnus-article-hide-signature
1697     "c" gnus-article-hide-citation
1698     "C" gnus-article-hide-citation-in-followups
1699     "l" gnus-article-hide-list-identifiers
1700     "p" gnus-article-hide-pgp
1701     "B" gnus-article-strip-banner
1702     "P" gnus-article-hide-pem
1703     "\C-c" gnus-article-hide-citation-maybe)
1704
1705   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1706     "a" gnus-article-highlight
1707     "h" gnus-article-highlight-headers
1708     "c" gnus-article-highlight-citation
1709     "s" gnus-article-highlight-signature)
1710
1711   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1712     "w" gnus-article-decode-mime-words
1713     "c" gnus-article-decode-charset
1714     "v" gnus-mime-view-all-parts
1715     "b" gnus-article-view-part)
1716
1717   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1718     "z" gnus-article-date-ut
1719     "u" gnus-article-date-ut
1720     "l" gnus-article-date-local
1721     "p" gnus-article-date-english
1722     "e" gnus-article-date-lapsed
1723     "o" gnus-article-date-original
1724     "i" gnus-article-date-iso8601
1725     "s" gnus-article-date-user)
1726
1727   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1728     "t" gnus-article-remove-trailing-blank-lines
1729     "l" gnus-article-strip-leading-blank-lines
1730     "m" gnus-article-strip-multiple-blank-lines
1731     "a" gnus-article-strip-blank-lines
1732     "A" gnus-article-strip-all-blank-lines
1733     "s" gnus-article-strip-leading-space
1734     "e" gnus-article-strip-trailing-space
1735     "w" gnus-article-remove-leading-whitespace)
1736
1737   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1738     "v" gnus-version
1739     "f" gnus-summary-fetch-faq
1740     "d" gnus-summary-describe-group
1741     "h" gnus-summary-describe-briefly
1742     "i" gnus-info-find-node)
1743
1744   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1745     "e" gnus-summary-expire-articles
1746     "\M-\C-e" gnus-summary-expire-articles-now
1747     "\177" gnus-summary-delete-article
1748     [delete] gnus-summary-delete-article
1749     [backspace] gnus-summary-delete-article
1750     "m" gnus-summary-move-article
1751     "r" gnus-summary-respool-article
1752     "w" gnus-summary-edit-article
1753     "c" gnus-summary-copy-article
1754     "B" gnus-summary-crosspost-article
1755     "q" gnus-summary-respool-query
1756     "t" gnus-summary-respool-trace
1757     "i" gnus-summary-import-article
1758     "I" gnus-summary-create-article
1759     "p" gnus-summary-article-posted-p)
1760
1761   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1762     "o" gnus-summary-save-article
1763     "m" gnus-summary-save-article-mail
1764     "F" gnus-summary-write-article-file
1765     "r" gnus-summary-save-article-rmail
1766     "f" gnus-summary-save-article-file
1767     "b" gnus-summary-save-article-body-file
1768     "h" gnus-summary-save-article-folder
1769     "v" gnus-summary-save-article-vm
1770     "p" gnus-summary-pipe-output
1771     "s" gnus-soup-add-article)
1772
1773   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1774     "b" gnus-summary-display-buttonized
1775     "m" gnus-summary-repair-multipart
1776     "v" gnus-article-view-part
1777     "o" gnus-article-save-part
1778     "c" gnus-article-copy-part
1779     "C" gnus-article-view-part-as-charset
1780     "e" gnus-article-externalize-part
1781     "E" gnus-article-encrypt-body
1782     "i" gnus-article-inline-part
1783     "|" gnus-article-pipe-part))
1784
1785 (defvar gnus-article-post-menu nil)
1786
1787 (defun gnus-summary-make-menu-bar ()
1788   (gnus-turn-off-edit-menu 'summary)
1789
1790   (unless (boundp 'gnus-summary-misc-menu)
1791
1792     (easy-menu-define
1793       gnus-summary-kill-menu gnus-summary-mode-map ""
1794       (cons
1795        "Score"
1796        (nconc
1797         (list
1798          ["Customize" gnus-score-customize t])
1799         (gnus-make-score-map 'increase)
1800         (gnus-make-score-map 'lower)
1801         '(("Mark"
1802            ["Kill below" gnus-summary-kill-below t]
1803            ["Mark above" gnus-summary-mark-above t]
1804            ["Tick above" gnus-summary-tick-above t]
1805            ["Clear above" gnus-summary-clear-above t])
1806           ["Current score" gnus-summary-current-score t]
1807           ["Set score" gnus-summary-set-score t]
1808           ["Switch current score file..." gnus-score-change-score-file t]
1809           ["Set mark below..." gnus-score-set-mark-below t]
1810           ["Set expunge below..." gnus-score-set-expunge-below t]
1811           ["Edit current score file" gnus-score-edit-current-scores t]
1812           ["Edit score file" gnus-score-edit-file t]
1813           ["Trace score" gnus-score-find-trace t]
1814           ["Find words" gnus-score-find-favourite-words t]
1815           ["Rescore buffer" gnus-summary-rescore t]
1816           ["Increase score..." gnus-summary-increase-score t]
1817           ["Lower score..." gnus-summary-lower-score t]))))
1818
1819 ;; Define both the Article menu in the summary buffer and the equivalent
1820     ;; Commands menu in the article buffer here for consistency.
1821     (let ((innards
1822            `(("Hide"
1823               ["All" gnus-article-hide t]
1824               ["Headers" gnus-article-hide-headers t]
1825               ["Signature" gnus-article-hide-signature t]
1826               ["Citation" gnus-article-hide-citation t]
1827               ["List identifiers" gnus-article-hide-list-identifiers t]
1828               ["PGP" gnus-article-hide-pgp t]
1829               ["Banner" gnus-article-strip-banner t]
1830               ["Boring headers" gnus-article-hide-boring-headers t])
1831              ("Highlight"
1832               ["All" gnus-article-highlight t]
1833               ["Headers" gnus-article-highlight-headers t]
1834               ["Signature" gnus-article-highlight-signature t]
1835               ["Citation" gnus-article-highlight-citation t])
1836              ("MIME"
1837               ["Words" gnus-article-decode-mime-words t]
1838               ["Charset" gnus-article-decode-charset t]
1839               ["QP" gnus-article-de-quoted-unreadable t]
1840               ["Base64" gnus-article-de-base64-unreadable t]
1841               ["View all" gnus-mime-view-all-parts t]
1842               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1843               ["Encrypt body" gnus-article-encrypt-body t])
1844              ("Date"
1845               ["Local" gnus-article-date-local t]
1846               ["ISO8601" gnus-article-date-iso8601 t]
1847               ["UT" gnus-article-date-ut t]
1848               ["Original" gnus-article-date-original t]
1849               ["Lapsed" gnus-article-date-lapsed t]
1850               ["User-defined" gnus-article-date-user t])
1851              ("Washing"
1852               ("Remove Blanks"
1853                ["Leading" gnus-article-strip-leading-blank-lines t]
1854                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1855                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1856                ["All of the above" gnus-article-strip-blank-lines t]
1857                ["All" gnus-article-strip-all-blank-lines t]
1858                ["Leading space" gnus-article-strip-leading-space t]
1859                ["Trailing space" gnus-article-strip-trailing-space t]
1860                ["Leading space in headers"
1861                 gnus-article-remove-leading-whitespace t])
1862               ["Overstrike" gnus-article-treat-overstrike t]
1863               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1864               ["Emphasis" gnus-article-emphasize t]
1865               ["Word wrap" gnus-article-fill-cited-article t]
1866               ["Fill long lines" gnus-article-fill-long-lines t]
1867               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1868               ["CR" gnus-article-remove-cr t]
1869               ["Show X-Face" gnus-article-display-x-face t]
1870               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1871               ["Base64" gnus-article-de-base64-unreadable t]
1872               ["Rot 13" gnus-summary-caesar-message
1873                ,@(if (featurep 'xemacs) '(t)
1874                    '(:help "\"Caesar rotate\" article by 13"))]
1875               ["Unix pipe" gnus-summary-pipe-message t]
1876               ["Add buttons" gnus-article-add-buttons t]
1877               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1878               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1879               ["Verbose header" gnus-summary-verbose-headers t]
1880               ["Toggle header" gnus-summary-toggle-header t]
1881               ["Html" gnus-article-wash-html t]
1882               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1883               ["HZ" gnus-article-decode-HZ t])
1884              ("Output"
1885               ["Save in default format" gnus-summary-save-article
1886                ,@(if (featurep 'xemacs) '(t)
1887                    '(:help "Save article using default method"))]
1888               ["Save in file" gnus-summary-save-article-file
1889                ,@(if (featurep 'xemacs) '(t)
1890                    '(:help "Save article in file"))]
1891               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1892               ["Save in MH folder" gnus-summary-save-article-folder t]
1893               ["Save in VM folder" gnus-summary-save-article-vm t]
1894               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1895               ["Save body in file" gnus-summary-save-article-body-file t]
1896               ["Pipe through a filter" gnus-summary-pipe-output t]
1897               ["Add to SOUP packet" gnus-soup-add-article t]
1898               ["Print" gnus-summary-print-article t])
1899              ("Backend"
1900               ["Respool article..." gnus-summary-respool-article t]
1901               ["Move article..." gnus-summary-move-article
1902                (gnus-check-backend-function
1903                 'request-move-article gnus-newsgroup-name)]
1904               ["Copy article..." gnus-summary-copy-article t]
1905               ["Crosspost article..." gnus-summary-crosspost-article
1906                (gnus-check-backend-function
1907                 'request-replace-article gnus-newsgroup-name)]
1908               ["Import file..." gnus-summary-import-article t]
1909               ["Create article..." gnus-summary-create-article t]
1910               ["Check if posted" gnus-summary-article-posted-p t]
1911               ["Edit article" gnus-summary-edit-article
1912                (not (gnus-group-read-only-p))]
1913               ["Delete article" gnus-summary-delete-article
1914                (gnus-check-backend-function
1915                 'request-expire-articles gnus-newsgroup-name)]
1916               ["Query respool" gnus-summary-respool-query t]
1917               ["Trace respool" gnus-summary-respool-trace t]
1918               ["Delete expirable articles" gnus-summary-expire-articles-now
1919                (gnus-check-backend-function
1920                 'request-expire-articles gnus-newsgroup-name)])
1921              ("Extract"
1922               ["Uudecode" gnus-uu-decode-uu
1923                ,@(if (featurep 'xemacs) '(t)
1924                    '(:help "Decode uuencoded article(s)"))]
1925               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1926               ["Unshar" gnus-uu-decode-unshar t]
1927               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1928               ["Save" gnus-uu-decode-save t]
1929               ["Binhex" gnus-uu-decode-binhex t]
1930               ["Postscript" gnus-uu-decode-postscript t])
1931              ("Cache"
1932               ["Enter article" gnus-cache-enter-article t]
1933               ["Remove article" gnus-cache-remove-article t])
1934              ["Translate" gnus-article-babel t]
1935              ["Select article buffer" gnus-summary-select-article-buffer t]
1936              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1937              ["Isearch article..." gnus-summary-isearch-article t]
1938              ["Beginning of the article" gnus-summary-beginning-of-article t]
1939              ["End of the article" gnus-summary-end-of-article t]
1940              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1941              ["Fetch referenced articles" gnus-summary-refer-references t]
1942              ["Fetch current thread" gnus-summary-refer-thread t]
1943              ["Fetch article with id..." gnus-summary-refer-article t]
1944              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1945              ["Redisplay" gnus-summary-show-article t]
1946              ["Raw article" gnus-summary-show-raw-article t])))
1947       (easy-menu-define
1948         gnus-summary-article-menu gnus-summary-mode-map ""
1949         (cons "Article" innards))
1950
1951       (if (not (keymapp gnus-summary-article-menu))
1952           (easy-menu-define
1953             gnus-article-commands-menu gnus-article-mode-map ""
1954             (cons "Commands" innards))
1955         ;; in Emacs, don't share menu.
1956         (setq gnus-article-commands-menu
1957               (copy-keymap gnus-summary-article-menu))
1958         (define-key gnus-article-mode-map [menu-bar commands]
1959           (cons "Commands" gnus-article-commands-menu))))
1960
1961     (easy-menu-define
1962       gnus-summary-thread-menu gnus-summary-mode-map ""
1963       '("Threads"
1964         ["Toggle threading" gnus-summary-toggle-threads t]
1965         ["Hide threads" gnus-summary-hide-all-threads t]
1966         ["Show threads" gnus-summary-show-all-threads t]
1967         ["Hide thread" gnus-summary-hide-thread t]
1968         ["Show thread" gnus-summary-show-thread t]
1969         ["Go to next thread" gnus-summary-next-thread t]
1970         ["Go to previous thread" gnus-summary-prev-thread t]
1971         ["Go down thread" gnus-summary-down-thread t]
1972         ["Go up thread" gnus-summary-up-thread t]
1973         ["Top of thread" gnus-summary-top-thread t]
1974         ["Mark thread as read" gnus-summary-kill-thread t]
1975         ["Lower thread score" gnus-summary-lower-thread t]
1976         ["Raise thread score" gnus-summary-raise-thread t]
1977         ["Rethread current" gnus-summary-rethread-current t]))
1978
1979     (easy-menu-define
1980       gnus-summary-post-menu gnus-summary-mode-map ""
1981       `("Post"
1982         ["Post an article" gnus-summary-post-news
1983          ,@(if (featurep 'xemacs) '(t)
1984              '(:help "Post an article"))]
1985         ["Followup" gnus-summary-followup
1986          ,@(if (featurep 'xemacs) '(t)
1987              '(:help "Post followup to this article"))]
1988         ["Followup and yank" gnus-summary-followup-with-original
1989          ,@(if (featurep 'xemacs) '(t)
1990              '(:help "Post followup to this article, quoting its contents"))]
1991         ["Supersede article" gnus-summary-supersede-article t]
1992         ["Cancel article" gnus-summary-cancel-article
1993          ,@(if (featurep 'xemacs) '(t)
1994              '(:help "Cancel an article you posted"))]
1995         ["Reply" gnus-summary-reply t]
1996         ["Reply and yank" gnus-summary-reply-with-original t]
1997         ["Wide reply" gnus-summary-wide-reply t]
1998         ["Wide reply and yank" gnus-summary-wide-reply-with-original
1999          ,@(if (featurep 'xemacs) '(t)
2000              '(:help "Mail a reply, quoting this article"))]
2001         ["Mail forward" gnus-summary-mail-forward t]
2002         ["Post forward" gnus-summary-post-forward t]
2003         ["Digest and mail" gnus-uu-digest-mail-forward t]
2004         ["Digest and post" gnus-uu-digest-post-forward t]
2005         ["Resend message" gnus-summary-resend-message t]
2006         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2007         ["Send a mail" gnus-summary-mail-other-window t]
2008         ["Uuencode and post" gnus-uu-post-news
2009          ,@(if (featurep 'xemacs) '(t)
2010              '(:help "Post a uuencoded article"))]
2011         ["Followup via news" gnus-summary-followup-to-mail t]
2012         ["Followup via news and yank"
2013          gnus-summary-followup-to-mail-with-original t]
2014         ;;("Draft"
2015         ;;["Send" gnus-summary-send-draft t]
2016         ;;["Send bounced" gnus-resend-bounced-mail t])
2017         ))
2018
2019     (cond
2020      ((not (keymapp gnus-summary-post-menu))
2021       (setq gnus-article-post-menu gnus-summary-post-menu))
2022      ((not gnus-article-post-menu)
2023       ;; Don't share post menu.
2024       (setq gnus-article-post-menu
2025             (copy-keymap gnus-summary-post-menu))))
2026     (define-key gnus-article-mode-map [menu-bar post]
2027       (cons "Post" gnus-article-post-menu))
2028
2029     (easy-menu-define
2030       gnus-summary-misc-menu gnus-summary-mode-map ""
2031       `("Gnus"
2032         ("Mark Read"
2033          ["Mark as read" gnus-summary-mark-as-read-forward t]
2034          ["Mark same subject and select"
2035           gnus-summary-kill-same-subject-and-select t]
2036          ["Mark same subject" gnus-summary-kill-same-subject t]
2037          ["Catchup" gnus-summary-catchup
2038           ,@(if (featurep 'xemacs) '(t)
2039               '(:help "Mark unread articles in this group as read"))]
2040          ["Catchup all" gnus-summary-catchup-all t]
2041          ["Catchup to here" gnus-summary-catchup-to-here t]
2042          ["Catchup from here" gnus-summary-catchup-from-here t]
2043          ["Catchup region" gnus-summary-mark-region-as-read t]
2044          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2045         ("Mark Various"
2046          ["Tick" gnus-summary-tick-article-forward t]
2047          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2048          ["Remove marks" gnus-summary-clear-mark-forward t]
2049          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2050          ["Set bookmark" gnus-summary-set-bookmark t]
2051          ["Remove bookmark" gnus-summary-remove-bookmark t])
2052         ("Mark Limit"
2053          ["Marks..." gnus-summary-limit-to-marks t]
2054          ["Subject..." gnus-summary-limit-to-subject t]
2055          ["Author..." gnus-summary-limit-to-author t]
2056          ["Age..." gnus-summary-limit-to-age t]
2057          ["Extra..." gnus-summary-limit-to-extra t]
2058          ["Score" gnus-summary-limit-to-score t]
2059          ["Unread" gnus-summary-limit-to-unread t]
2060          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2061          ["Articles" gnus-summary-limit-to-articles t]
2062          ["Pop limit" gnus-summary-pop-limit t]
2063          ["Show dormant" gnus-summary-limit-include-dormant t]
2064          ["Hide childless dormant"
2065           gnus-summary-limit-exclude-childless-dormant t]
2066          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2067          ["Hide marked" gnus-summary-limit-exclude-marks t]
2068          ["Show expunged" gnus-summary-limit-include-expunged t])
2069         ("Process Mark"
2070          ["Set mark" gnus-summary-mark-as-processable t]
2071          ["Remove mark" gnus-summary-unmark-as-processable t]
2072          ["Remove all marks" gnus-summary-unmark-all-processable t]
2073          ["Mark above" gnus-uu-mark-over t]
2074          ["Mark series" gnus-uu-mark-series t]
2075          ["Mark region" gnus-uu-mark-region t]
2076          ["Unmark region" gnus-uu-unmark-region t]
2077          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2078          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2079          ["Mark all" gnus-uu-mark-all t]
2080          ["Mark buffer" gnus-uu-mark-buffer t]
2081          ["Mark sparse" gnus-uu-mark-sparse t]
2082          ["Mark thread" gnus-uu-mark-thread t]
2083          ["Unmark thread" gnus-uu-unmark-thread t]
2084          ("Process Mark Sets"
2085           ["Kill" gnus-summary-kill-process-mark t]
2086           ["Yank" gnus-summary-yank-process-mark
2087            gnus-newsgroup-process-stack]
2088           ["Save" gnus-summary-save-process-mark t]))
2089         ("Scroll article"
2090          ["Page forward" gnus-summary-next-page
2091           ,@(if (featurep 'xemacs) '(t)
2092               '(:help "Show next page of article"))]
2093          ["Page backward" gnus-summary-prev-page
2094           ,@(if (featurep 'xemacs) '(t)
2095               '(:help "Show previous page of article"))]
2096          ["Line forward" gnus-summary-scroll-up t])
2097         ("Move"
2098          ["Next unread article" gnus-summary-next-unread-article t]
2099          ["Previous unread article" gnus-summary-prev-unread-article t]
2100          ["Next article" gnus-summary-next-article t]
2101          ["Previous article" gnus-summary-prev-article t]
2102          ["Next unread subject" gnus-summary-next-unread-subject t]
2103          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2104          ["Next article same subject" gnus-summary-next-same-subject t]
2105          ["Previous article same subject" gnus-summary-prev-same-subject t]
2106          ["First unread article" gnus-summary-first-unread-article t]
2107          ["Best unread article" gnus-summary-best-unread-article t]
2108          ["Go to subject number..." gnus-summary-goto-subject t]
2109          ["Go to article number..." gnus-summary-goto-article t]
2110          ["Go to the last article" gnus-summary-goto-last-article t]
2111          ["Pop article off history" gnus-summary-pop-article t])
2112         ("Sort"
2113          ["Sort by number" gnus-summary-sort-by-number t]
2114          ["Sort by author" gnus-summary-sort-by-author t]
2115          ["Sort by subject" gnus-summary-sort-by-subject t]
2116          ["Sort by date" gnus-summary-sort-by-date t]
2117          ["Sort by score" gnus-summary-sort-by-score t]
2118          ["Sort by lines" gnus-summary-sort-by-lines t]
2119          ["Sort by characters" gnus-summary-sort-by-chars t]
2120          ["Original sort" gnus-summary-sort-by-original t])
2121         ("Help"
2122          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2123          ["Describe group" gnus-summary-describe-group t]
2124          ["Read manual" gnus-info-find-node t])
2125         ("Modes"
2126          ["Pick and read" gnus-pick-mode t]
2127          ["Binary" gnus-binary-mode t])
2128         ("Regeneration"
2129          ["Regenerate" gnus-summary-prepare t]
2130          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2131          ["Toggle threading" gnus-summary-toggle-threads t])
2132         ["See old articles" gnus-summary-insert-old-articles t]
2133         ["See new articles" gnus-summary-insert-new-articles t]
2134         ["Filter articles..." gnus-summary-execute-command t]
2135         ["Run command on subjects..." gnus-summary-universal-argument t]
2136         ["Search articles forward..." gnus-summary-search-article-forward t]
2137         ["Search articles backward..." gnus-summary-search-article-backward t]
2138         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2139         ["Expand window" gnus-summary-expand-window t]
2140         ["Expire expirable articles" gnus-summary-expire-articles
2141          (gnus-check-backend-function
2142           'request-expire-articles gnus-newsgroup-name)]
2143         ["Edit local kill file" gnus-summary-edit-local-kill t]
2144         ["Edit main kill file" gnus-summary-edit-global-kill t]
2145         ["Edit group parameters" gnus-summary-edit-parameters t]
2146         ["Customize group parameters" gnus-summary-customize-parameters t]
2147         ["Send a bug report" gnus-bug t]
2148         ("Exit"
2149          ["Catchup and exit" gnus-summary-catchup-and-exit
2150           ,@(if (featurep 'xemacs) '(t)
2151               '(:help "Mark unread articles in this group as read, then exit"))]
2152          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2153          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2154          ["Exit group" gnus-summary-exit
2155           ,@(if (featurep 'xemacs) '(t)
2156               '(:help "Exit current group, return to group selection mode"))]
2157          ["Exit group without updating" gnus-summary-exit-no-update t]
2158          ["Exit and goto next group" gnus-summary-next-group t]
2159          ["Exit and goto prev group" gnus-summary-prev-group t]
2160          ["Reselect group" gnus-summary-reselect-current-group t]
2161          ["Rescan group" gnus-summary-rescan-group t]
2162          ["Update dribble" gnus-summary-save-newsrc t])))
2163
2164     (gnus-run-hooks 'gnus-summary-menu-hook)))
2165
2166 (defvar gnus-summary-tool-bar-map nil)
2167
2168 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2169 (defun gnus-summary-make-tool-bar ()
2170   (if (and (fboundp 'tool-bar-add-item-from-menu)
2171            (default-value 'tool-bar-mode)
2172            (not gnus-summary-tool-bar-map))
2173       (setq gnus-summary-tool-bar-map
2174             (let ((tool-bar-map (make-sparse-keymap))
2175                   (load-path (mm-image-load-path)))
2176               (tool-bar-add-item-from-menu
2177                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2178               (tool-bar-add-item-from-menu
2179                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2180               (tool-bar-add-item-from-menu
2181                'gnus-summary-post-news "post" gnus-summary-mode-map)
2182               (tool-bar-add-item-from-menu
2183                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2184               (tool-bar-add-item-from-menu
2185                'gnus-summary-followup "followup" gnus-summary-mode-map)
2186               (tool-bar-add-item-from-menu
2187                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2188               (tool-bar-add-item-from-menu
2189                'gnus-summary-reply "reply" gnus-summary-mode-map)
2190               (tool-bar-add-item-from-menu
2191                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2192               (tool-bar-add-item-from-menu
2193                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2194               (tool-bar-add-item-from-menu
2195                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2196               (tool-bar-add-item-from-menu
2197                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2198               (tool-bar-add-item-from-menu
2199                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2204               (tool-bar-add-item-from-menu
2205                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2206               tool-bar-map)))
2207   (if gnus-summary-tool-bar-map
2208       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2209
2210 (defun gnus-score-set-default (var value)
2211   "A version of set that updates the GNU Emacs menu-bar."
2212   (set var value)
2213   ;; It is the message that forces the active status to be updated.
2214   (message ""))
2215
2216 (defun gnus-make-score-map (type)
2217   "Make a summary score map of type TYPE."
2218   (if t
2219       nil
2220     (let ((headers '(("author" "from" string)
2221                      ("subject" "subject" string)
2222                      ("article body" "body" string)
2223                      ("article head" "head" string)
2224                      ("xref" "xref" string)
2225                      ("extra header" "extra" string)
2226                      ("lines" "lines" number)
2227                      ("followups to author" "followup" string)))
2228           (types '((number ("less than" <)
2229                            ("greater than" >)
2230                            ("equal" =))
2231                    (string ("substring" s)
2232                            ("exact string" e)
2233                            ("fuzzy string" f)
2234                            ("regexp" r))))
2235           (perms '(("temporary" (current-time-string))
2236                    ("permanent" nil)
2237                    ("immediate" now)))
2238           header)
2239       (list
2240        (apply
2241         'nconc
2242         (list
2243          (if (eq type 'lower)
2244              "Lower score"
2245            "Increase score"))
2246         (let (outh)
2247           (while headers
2248             (setq header (car headers))
2249             (setq outh
2250                   (cons
2251                    (apply
2252                     'nconc
2253                     (list (car header))
2254                     (let ((ts (cdr (assoc (nth 2 header) types)))
2255                           outt)
2256                       (while ts
2257                         (setq outt
2258                               (cons
2259                                (apply
2260                                 'nconc
2261                                 (list (caar ts))
2262                                 (let ((ps perms)
2263                                       outp)
2264                                   (while ps
2265                                     (setq outp
2266                                           (cons
2267                                            (vector
2268                                             (caar ps)
2269                                             (list
2270                                              'gnus-summary-score-entry
2271                                              (nth 1 header)
2272                                              (if (or (string= (nth 1 header)
2273                                                               "head")
2274                                                      (string= (nth 1 header)
2275                                                               "body"))
2276                                                  ""
2277                                                (list 'gnus-summary-header
2278                                                      (nth 1 header)))
2279                                              (list 'quote (nth 1 (car ts)))
2280                                              (list 'gnus-score-delta-default
2281                                                    nil)
2282                                              (nth 1 (car ps))
2283                                              t)
2284                                             t)
2285                                            outp))
2286                                     (setq ps (cdr ps)))
2287                                   (list (nreverse outp))))
2288                                outt))
2289                         (setq ts (cdr ts)))
2290                       (list (nreverse outt))))
2291                    outh))
2292             (setq headers (cdr headers)))
2293           (list (nreverse outh))))))))
2294
2295 \f
2296
2297 (defun gnus-summary-mode (&optional group)
2298   "Major mode for reading articles.
2299
2300 All normal editing commands are switched off.
2301 \\<gnus-summary-mode-map>
2302 Each line in this buffer represents one article.  To read an
2303 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2304 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2305 respectively.
2306
2307 You can also post articles and send mail from this buffer.  To
2308 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2309 of an article, type `\\[gnus-summary-reply]'.
2310
2311 There are approx. one gazillion commands you can execute in this
2312 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2313
2314 The following commands are available:
2315
2316 \\{gnus-summary-mode-map}"
2317   (interactive)
2318   (kill-all-local-variables)
2319   (when (gnus-visual-p 'summary-menu 'menu)
2320     (gnus-summary-make-menu-bar)
2321     (gnus-summary-make-tool-bar))
2322   (gnus-summary-make-local-variables)
2323   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2324     (gnus-summary-make-local-variables))
2325   (gnus-make-thread-indent-array)
2326   (gnus-simplify-mode-line)
2327   (setq major-mode 'gnus-summary-mode)
2328   (setq mode-name "Summary")
2329   (make-local-variable 'minor-mode-alist)
2330   (use-local-map gnus-summary-mode-map)
2331   (buffer-disable-undo)
2332   (setq buffer-read-only t)             ;Disable modification
2333   (setq truncate-lines t)
2334   (setq selective-display t)
2335   (setq selective-display-ellipses t)   ;Display `...'
2336   (gnus-summary-set-display-table)
2337   (gnus-set-default-directory)
2338   (setq gnus-newsgroup-name group)
2339   (make-local-variable 'gnus-summary-line-format)
2340   (make-local-variable 'gnus-summary-line-format-spec)
2341   (make-local-variable 'gnus-summary-dummy-line-format)
2342   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2343   (make-local-variable 'gnus-summary-mark-positions)
2344   (make-local-hook 'pre-command-hook)
2345   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2346   (gnus-run-hooks 'gnus-summary-mode-hook)
2347   (turn-on-gnus-mailing-list-mode)
2348   (mm-enable-multibyte-mule4)
2349   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2350   (gnus-update-summary-mark-positions))
2351
2352 (defun gnus-summary-make-local-variables ()
2353   "Make all the local summary buffer variables."
2354   (let (global)
2355     (dolist (local gnus-summary-local-variables)
2356       (if (consp local)
2357           (progn
2358             (if (eq (cdr local) 'global)
2359                 ;; Copy the global value of the variable.
2360                 (setq global (symbol-value (car local)))
2361               ;; Use the value from the list.
2362               (setq global (eval (cdr local))))
2363             (set (make-local-variable (car local)) global))
2364         ;; Simple nil-valued local variable.
2365         (set (make-local-variable local) nil)))))
2366
2367 (defun gnus-summary-clear-local-variables ()
2368   (let ((locals gnus-summary-local-variables))
2369     (while locals
2370       (if (consp (car locals))
2371           (and (vectorp (caar locals))
2372                (set (caar locals) nil))
2373         (and (vectorp (car locals))
2374              (set (car locals) nil)))
2375       (setq locals (cdr locals)))))
2376
2377 ;; Summary data functions.
2378
2379 (defmacro gnus-data-number (data)
2380   `(car ,data))
2381
2382 (defmacro gnus-data-set-number (data number)
2383   `(setcar ,data ,number))
2384
2385 (defmacro gnus-data-mark (data)
2386   `(nth 1 ,data))
2387
2388 (defmacro gnus-data-set-mark (data mark)
2389   `(setcar (nthcdr 1 ,data) ,mark))
2390
2391 (defmacro gnus-data-pos (data)
2392   `(nth 2 ,data))
2393
2394 (defmacro gnus-data-set-pos (data pos)
2395   `(setcar (nthcdr 2 ,data) ,pos))
2396
2397 (defmacro gnus-data-header (data)
2398   `(nth 3 ,data))
2399
2400 (defmacro gnus-data-set-header (data header)
2401   `(setf (nth 3 ,data) ,header))
2402
2403 (defmacro gnus-data-level (data)
2404   `(nth 4 ,data))
2405
2406 (defmacro gnus-data-unread-p (data)
2407   `(= (nth 1 ,data) gnus-unread-mark))
2408
2409 (defmacro gnus-data-read-p (data)
2410   `(/= (nth 1 ,data) gnus-unread-mark))
2411
2412 (defmacro gnus-data-pseudo-p (data)
2413   `(consp (nth 3 ,data)))
2414
2415 (defmacro gnus-data-find (number)
2416   `(assq ,number gnus-newsgroup-data))
2417
2418 (defmacro gnus-data-find-list (number &optional data)
2419   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2420      (memq (assq ,number bdata)
2421            bdata)))
2422
2423 (defmacro gnus-data-make (number mark pos header level)
2424   `(list ,number ,mark ,pos ,header ,level))
2425
2426 (defun gnus-data-enter (after-article number mark pos header level offset)
2427   (let ((data (gnus-data-find-list after-article)))
2428     (unless data
2429       (error "No such article: %d" after-article))
2430     (setcdr data (cons (gnus-data-make number mark pos header level)
2431                        (cdr data)))
2432     (setq gnus-newsgroup-data-reverse nil)
2433     (gnus-data-update-list (cddr data) offset)))
2434
2435 (defun gnus-data-enter-list (after-article list &optional offset)
2436   (when list
2437     (let ((data (and after-article (gnus-data-find-list after-article)))
2438           (ilist list))
2439       (if (not (or data
2440                    after-article))
2441           (let ((odata gnus-newsgroup-data))
2442             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2443             (when offset
2444               (gnus-data-update-list odata offset)))
2445       ;; Find the last element in the list to be spliced into the main
2446         ;; list.
2447         (while (cdr list)
2448           (setq list (cdr list)))
2449         (if (not data)
2450             (progn
2451               (setcdr list gnus-newsgroup-data)
2452               (setq gnus-newsgroup-data ilist)
2453               (when offset
2454                 (gnus-data-update-list (cdr list) offset)))
2455           (setcdr list (cdr data))
2456           (setcdr data ilist)
2457           (when offset
2458             (gnus-data-update-list (cdr list) offset))))
2459       (setq gnus-newsgroup-data-reverse nil))))
2460
2461 (defun gnus-data-remove (article &optional offset)
2462   (let ((data gnus-newsgroup-data))
2463     (if (= (gnus-data-number (car data)) article)
2464         (progn
2465           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2466                 gnus-newsgroup-data-reverse nil)
2467           (when offset
2468             (gnus-data-update-list gnus-newsgroup-data offset)))
2469       (while (cdr data)
2470         (when (= (gnus-data-number (cadr data)) article)
2471           (setcdr data (cddr data))
2472           (when offset
2473             (gnus-data-update-list (cdr data) offset))
2474           (setq data nil
2475                 gnus-newsgroup-data-reverse nil))
2476         (setq data (cdr data))))))
2477
2478 (defmacro gnus-data-list (backward)
2479   `(if ,backward
2480        (or gnus-newsgroup-data-reverse
2481            (setq gnus-newsgroup-data-reverse
2482                  (reverse gnus-newsgroup-data)))
2483      gnus-newsgroup-data))
2484
2485 (defun gnus-data-update-list (data offset)
2486   "Add OFFSET to the POS of all data entries in DATA."
2487   (setq gnus-newsgroup-data-reverse nil)
2488   (while data
2489     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2490     (setq data (cdr data))))
2491
2492 (defun gnus-summary-article-pseudo-p (article)
2493   "Say whether this article is a pseudo article or not."
2494   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2495
2496 (defmacro gnus-summary-article-sparse-p (article)
2497   "Say whether this article is a sparse article or not."
2498   `(memq ,article gnus-newsgroup-sparse))
2499
2500 (defmacro gnus-summary-article-ancient-p (article)
2501   "Say whether this article is a sparse article or not."
2502   `(memq ,article gnus-newsgroup-ancient))
2503
2504 (defun gnus-article-parent-p (number)
2505   "Say whether this article is a parent or not."
2506   (let ((data (gnus-data-find-list number)))
2507     (and (cdr data)              ; There has to be an article after...
2508          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2509             (gnus-data-level (nth 1 data))))))
2510
2511 (defun gnus-article-children (number)
2512   "Return a list of all children to NUMBER."
2513   (let* ((data (gnus-data-find-list number))
2514          (level (gnus-data-level (car data)))
2515          children)
2516     (setq data (cdr data))
2517     (while (and data
2518                 (= (gnus-data-level (car data)) (1+ level)))
2519       (push (gnus-data-number (car data)) children)
2520       (setq data (cdr data)))
2521     children))
2522
2523 (defmacro gnus-summary-skip-intangible ()
2524   "If the current article is intangible, then jump to a different article."
2525   '(let ((to (get-text-property (point) 'gnus-intangible)))
2526      (and to (gnus-summary-goto-subject to))))
2527
2528 (defmacro gnus-summary-article-intangible-p ()
2529   "Say whether this article is intangible or not."
2530   '(get-text-property (point) 'gnus-intangible))
2531
2532 (defun gnus-article-read-p (article)
2533   "Say whether ARTICLE is read or not."
2534   (not (or (memq article gnus-newsgroup-marked)
2535            (memq article gnus-newsgroup-unreads)
2536            (memq article gnus-newsgroup-unselected)
2537            (memq article gnus-newsgroup-dormant))))
2538
2539 ;; Some summary mode macros.
2540
2541 (defmacro gnus-summary-article-number ()
2542   "The article number of the article on the current line.
2543 If there isn's an article number here, then we return the current
2544 article number."
2545   '(progn
2546      (gnus-summary-skip-intangible)
2547      (or (get-text-property (point) 'gnus-number)
2548          (gnus-summary-last-subject))))
2549
2550 (defmacro gnus-summary-article-header (&optional number)
2551   "Return the header of article NUMBER."
2552   `(gnus-data-header (gnus-data-find
2553                       ,(or number '(gnus-summary-article-number)))))
2554
2555 (defmacro gnus-summary-thread-level (&optional number)
2556   "Return the level of thread that starts with article NUMBER."
2557   `(if (and (eq gnus-summary-make-false-root 'dummy)
2558             (get-text-property (point) 'gnus-intangible))
2559        0
2560      (gnus-data-level (gnus-data-find
2561                        ,(or number '(gnus-summary-article-number))))))
2562
2563 (defmacro gnus-summary-article-mark (&optional number)
2564   "Return the mark of article NUMBER."
2565   `(gnus-data-mark (gnus-data-find
2566                     ,(or number '(gnus-summary-article-number)))))
2567
2568 (defmacro gnus-summary-article-pos (&optional number)
2569   "Return the position of the line of article NUMBER."
2570   `(gnus-data-pos (gnus-data-find
2571                    ,(or number '(gnus-summary-article-number)))))
2572
2573 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2574 (defmacro gnus-summary-article-subject (&optional number)
2575   "Return current subject string or nil if nothing."
2576   `(let ((headers
2577           ,(if number
2578                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2579              '(gnus-data-header (assq (gnus-summary-article-number)
2580                                       gnus-newsgroup-data)))))
2581      (and headers
2582           (vectorp headers)
2583           (mail-header-subject headers))))
2584
2585 (defmacro gnus-summary-article-score (&optional number)
2586   "Return current article score."
2587   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2588                   gnus-newsgroup-scored))
2589        gnus-summary-default-score 0))
2590
2591 (defun gnus-summary-article-children (&optional number)
2592   "Return a list of article numbers that are children of article NUMBER."
2593   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2594          (level (gnus-data-level (car data)))
2595          l children)
2596     (while (and (setq data (cdr data))
2597                 (> (setq l (gnus-data-level (car data))) level))
2598       (and (= (1+ level) l)
2599            (push (gnus-data-number (car data))
2600                  children)))
2601     (nreverse children)))
2602
2603 (defun gnus-summary-article-parent (&optional number)
2604   "Return the article number of the parent of article NUMBER."
2605   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2606                                     (gnus-data-list t)))
2607          (level (gnus-data-level (car data))))
2608     (if (zerop level)
2609         ()                              ; This is a root.
2610       ;; We search until we find an article with a level less than
2611       ;; this one.  That function has to be the parent.
2612       (while (and (setq data (cdr data))
2613                   (not (< (gnus-data-level (car data)) level))))
2614       (and data (gnus-data-number (car data))))))
2615
2616 (defun gnus-unread-mark-p (mark)
2617   "Say whether MARK is the unread mark."
2618   (= mark gnus-unread-mark))
2619
2620 (defun gnus-read-mark-p (mark)
2621   "Say whether MARK is one of the marks that mark as read.
2622 This is all marks except unread, ticked, dormant, and expirable."
2623   (not (or (= mark gnus-unread-mark)
2624            (= mark gnus-ticked-mark)
2625            (= mark gnus-dormant-mark)
2626            (= mark gnus-expirable-mark))))
2627
2628 (defmacro gnus-article-mark (number)
2629   "Return the MARK of article NUMBER.
2630 This macro should only be used when computing the mark the \"first\"
2631 time; i.e., when generating the summary lines.  After that,
2632 `gnus-summary-article-mark' should be used to examine the
2633 marks of articles."
2634   `(cond
2635     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2636     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2637     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2638     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2639     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2640     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2641     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2642     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2643            gnus-ancient-mark))))
2644
2645 ;; Saving hidden threads.
2646
2647 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2648 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2649
2650 (defmacro gnus-save-hidden-threads (&rest forms)
2651   "Save hidden threads, eval FORMS, and restore the hidden threads."
2652   (let ((config (make-symbol "config")))
2653     `(let ((,config (gnus-hidden-threads-configuration)))
2654        (unwind-protect
2655            (save-excursion
2656              ,@forms)
2657          (gnus-restore-hidden-threads-configuration ,config)))))
2658
2659 (defun gnus-data-compute-positions ()
2660   "Compute the positions of all articles."
2661   (setq gnus-newsgroup-data-reverse nil)
2662   (let ((data gnus-newsgroup-data))
2663     (save-excursion
2664       (gnus-save-hidden-threads
2665         (gnus-summary-show-all-threads)
2666         (goto-char (point-min))
2667         (while data
2668           (while (get-text-property (point) 'gnus-intangible)
2669             (forward-line 1))
2670           (gnus-data-set-pos (car data) (+ (point) 3))
2671           (setq data (cdr data))
2672           (forward-line 1))))))
2673
2674 (defun gnus-hidden-threads-configuration ()
2675   "Return the current hidden threads configuration."
2676   (save-excursion
2677     (let (config)
2678       (goto-char (point-min))
2679       (while (search-forward "\r" nil t)
2680         (push (1- (point)) config))
2681       config)))
2682
2683 (defun gnus-restore-hidden-threads-configuration (config)
2684   "Restore hidden threads configuration from CONFIG."
2685   (save-excursion
2686     (let (point buffer-read-only)
2687       (while (setq point (pop config))
2688         (when (and (< point (point-max))
2689                    (goto-char point)
2690                    (eq (char-after) ?\n))
2691           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2692
2693 ;; Various summary mode internalish functions.
2694
2695 (defun gnus-mouse-pick-article (e)
2696   (interactive "e")
2697   (mouse-set-point e)
2698   (gnus-summary-next-page nil t))
2699
2700 (defun gnus-summary-set-display-table ()
2701   "Change the display table.
2702 Odd characters have a tendency to mess
2703 up nicely formatted displays - we make all possible glyphs
2704 display only a single character."
2705
2706   ;; We start from the standard display table, if any.
2707   (let ((table (or (copy-sequence standard-display-table)
2708                    (make-display-table)))
2709         (i 32))
2710     ;; Nix out all the control chars...
2711     (while (>= (setq i (1- i)) 0)
2712       (aset table i [??]))
2713    ;; ... but not newline and cr, of course.  (cr is necessary for the
2714     ;; selective display).
2715     (aset table ?\n nil)
2716     (aset table ?\r nil)
2717     ;; We keep TAB as well.
2718     (aset table ?\t nil)
2719     ;; We nix out any glyphs over 126 that are not set already.
2720     (let ((i 256))
2721       (while (>= (setq i (1- i)) 127)
2722         ;; Only modify if the entry is nil.
2723         (unless (aref table i)
2724           (aset table i [??]))))
2725     (setq buffer-display-table table)))
2726
2727 (defun gnus-summary-set-article-display-arrow (pos)
2728   "Update the overlay arrow to point to line at position POS."
2729   (when (and gnus-summary-display-arrow
2730              (boundp 'overlay-arrow-position)
2731              (boundp 'overlay-arrow-string))
2732     (save-excursion
2733       (goto-char pos)
2734       (beginning-of-line)
2735       (unless overlay-arrow-position
2736         (setq overlay-arrow-position (make-marker)))
2737       (setq overlay-arrow-string "=>"
2738             overlay-arrow-position (set-marker overlay-arrow-position
2739                                                (point)
2740                                                (current-buffer))))))
2741
2742 (defun gnus-summary-buffer-name (group)
2743   "Return the summary buffer name of GROUP."
2744   (concat "*Summary " group "*"))
2745
2746 (defun gnus-summary-setup-buffer (group)
2747   "Initialize summary buffer."
2748   (let ((buffer (gnus-summary-buffer-name group)))
2749     (if (get-buffer buffer)
2750         (progn
2751           (set-buffer buffer)
2752           (setq gnus-summary-buffer (current-buffer))
2753           (not gnus-newsgroup-prepared))
2754       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2755       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2756       (gnus-summary-mode group)
2757       (when gnus-carpal
2758         (gnus-carpal-setup-buffer 'summary))
2759       (unless gnus-single-article-buffer
2760         (make-local-variable 'gnus-article-buffer)
2761         (make-local-variable 'gnus-article-current)
2762         (make-local-variable 'gnus-original-article-buffer))
2763       (setq gnus-newsgroup-name group)
2764       ;; Set any local variables in the group parameters.
2765       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2766       t)))
2767
2768 (defun gnus-set-global-variables ()
2769   "Set the global equivalents of the buffer-local variables.
2770 They are set to the latest values they had.  These reflect the summary
2771 buffer that was in action when the last article was fetched."
2772   (when (eq major-mode 'gnus-summary-mode)
2773     (setq gnus-summary-buffer (current-buffer))
2774     (let ((name gnus-newsgroup-name)
2775           (marked gnus-newsgroup-marked)
2776           (unread gnus-newsgroup-unreads)
2777           (headers gnus-current-headers)
2778           (data gnus-newsgroup-data)
2779           (summary gnus-summary-buffer)
2780           (article-buffer gnus-article-buffer)
2781           (original gnus-original-article-buffer)
2782           (gac gnus-article-current)
2783           (reffed gnus-reffed-article-number)
2784           (score-file gnus-current-score-file)
2785           (default-charset gnus-newsgroup-charset)
2786           vlist)
2787       (let ((locals gnus-newsgroup-variables))
2788         (while locals
2789           (if (consp (car locals))
2790               (push (eval (caar locals)) vlist)
2791             (push (eval (car locals)) vlist))
2792           (setq locals (cdr locals)))
2793         (setq vlist (nreverse vlist)))
2794       (save-excursion
2795         (set-buffer gnus-group-buffer)
2796         (setq gnus-newsgroup-name name
2797               gnus-newsgroup-marked marked
2798               gnus-newsgroup-unreads unread
2799               gnus-current-headers headers
2800               gnus-newsgroup-data data
2801               gnus-article-current gac
2802               gnus-summary-buffer summary
2803               gnus-article-buffer article-buffer
2804               gnus-original-article-buffer original
2805               gnus-reffed-article-number reffed
2806               gnus-current-score-file score-file
2807               gnus-newsgroup-charset default-charset)
2808         (let ((locals gnus-newsgroup-variables))
2809           (while locals
2810             (if (consp (car locals))
2811                 (set (caar locals) (pop vlist))
2812               (set (car locals) (pop vlist)))
2813             (setq locals (cdr locals))))
2814         ;; The article buffer also has local variables.
2815         (when (gnus-buffer-live-p gnus-article-buffer)
2816           (set-buffer gnus-article-buffer)
2817           (setq gnus-summary-buffer summary))))))
2818
2819 (defun gnus-summary-article-unread-p (article)
2820   "Say whether ARTICLE is unread or not."
2821   (memq article gnus-newsgroup-unreads))
2822
2823 (defun gnus-summary-first-article-p (&optional article)
2824   "Return whether ARTICLE is the first article in the buffer."
2825   (if (not (setq article (or article (gnus-summary-article-number))))
2826       nil
2827     (eq article (caar gnus-newsgroup-data))))
2828
2829 (defun gnus-summary-last-article-p (&optional article)
2830   "Return whether ARTICLE is the last article in the buffer."
2831   (if (not (setq article (or article (gnus-summary-article-number))))
2832       ;; All non-existent numbers are the last article.  :-)
2833       t
2834     (not (cdr (gnus-data-find-list article)))))
2835
2836 (defun gnus-make-thread-indent-array ()
2837   (let ((n 200))
2838     (unless (and gnus-thread-indent-array
2839                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2840       (setq gnus-thread-indent-array (make-vector 201 "")
2841             gnus-thread-indent-array-level gnus-thread-indent-level)
2842       (while (>= n 0)
2843         (aset gnus-thread-indent-array n
2844               (make-string (* n gnus-thread-indent-level) ? ))
2845         (setq n (1- n))))))
2846
2847 (defun gnus-update-summary-mark-positions ()
2848   "Compute where the summary marks are to go."
2849   (save-excursion
2850     (when (gnus-buffer-exists-p gnus-summary-buffer)
2851       (set-buffer gnus-summary-buffer))
2852     (let ((gnus-replied-mark 129)
2853           (gnus-score-below-mark 130)
2854           (gnus-score-over-mark 130)
2855           (gnus-download-mark 131)
2856           (spec gnus-summary-line-format-spec)
2857           gnus-visual pos)
2858       (save-excursion
2859         (gnus-set-work-buffer)
2860         (let ((gnus-summary-line-format-spec spec)
2861               (gnus-newsgroup-downloadable '((0 . t))))
2862           (gnus-summary-insert-line
2863            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2864            0 nil 128 t nil "" nil 1)
2865           (goto-char (point-min))
2866           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2867                                              (- (point) 2)))))
2868           (goto-char (point-min))
2869           (push (cons 'replied (and (search-forward "\201" nil t)
2870                                     (- (point) 2)))
2871                 pos)
2872           (goto-char (point-min))
2873           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2874                 pos)
2875           (goto-char (point-min))
2876           (push (cons 'download
2877                       (and (search-forward "\203" nil t) (- (point) 2)))
2878                 pos)))
2879       (setq gnus-summary-mark-positions pos))))
2880
2881 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2882   "Insert a dummy root in the summary buffer."
2883   (beginning-of-line)
2884   (gnus-add-text-properties
2885    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2886    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2887
2888 (defun gnus-summary-from-or-to-or-newsgroups (header)
2889   (let ((to (cdr (assq 'To (mail-header-extra header))))
2890         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2891         (mail-parse-charset gnus-newsgroup-charset)
2892         (mail-parse-ignored-charsets
2893          (save-excursion (set-buffer gnus-summary-buffer)
2894                          gnus-newsgroup-ignored-charsets)))
2895     (cond
2896      ((and to
2897            gnus-ignored-from-addresses
2898            (string-match gnus-ignored-from-addresses
2899                          (mail-header-from header)))
2900       (concat "-> "
2901               (or (car (funcall gnus-extract-address-components
2902                                 (funcall
2903                                  gnus-decode-encoded-word-function to)))
2904                   (funcall gnus-decode-encoded-word-function to))))
2905      ((and newsgroups
2906            gnus-ignored-from-addresses
2907            (string-match gnus-ignored-from-addresses
2908                          (mail-header-from header)))
2909       (concat "=> " newsgroups))
2910      (t
2911       (or (car (funcall gnus-extract-address-components
2912                         (mail-header-from header)))
2913           (mail-header-from header))))))
2914
2915 (defun gnus-summary-insert-line (gnus-tmp-header
2916                                  gnus-tmp-level gnus-tmp-current
2917                                  gnus-tmp-unread gnus-tmp-replied
2918                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2919                                  &optional gnus-tmp-dummy gnus-tmp-score
2920                                  gnus-tmp-process)
2921   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2922          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2923          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2924          (gnus-tmp-score-char
2925           (if (or (null gnus-summary-default-score)
2926                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2927                       gnus-summary-zcore-fuzz))
2928               ?                         ;Whitespace
2929             (if (< gnus-tmp-score gnus-summary-default-score)
2930                 gnus-score-below-mark gnus-score-over-mark)))
2931          (gnus-tmp-replied
2932           (cond (gnus-tmp-process gnus-process-mark)
2933                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2934                  gnus-cached-mark)
2935                 (gnus-tmp-replied gnus-replied-mark)
2936                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2937                  gnus-saved-mark)
2938                 (t gnus-no-mark)))
2939          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2940          (gnus-tmp-name
2941           (cond
2942            ((string-match "<[^>]+> *$" gnus-tmp-from)
2943             (let ((beg (match-beginning 0)))
2944               (or (and (string-match "^\".+\"" gnus-tmp-from)
2945                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2946                   (substring gnus-tmp-from 0 beg))))
2947            ((string-match "(.+)" gnus-tmp-from)
2948             (substring gnus-tmp-from
2949                        (1+ (match-beginning 0)) (1- (match-end 0))))
2950            (t gnus-tmp-from)))
2951          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2952          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2953          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2954          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2955          (buffer-read-only nil))
2956     (when (string= gnus-tmp-name "")
2957       (setq gnus-tmp-name gnus-tmp-from))
2958     (unless (numberp gnus-tmp-lines)
2959       (setq gnus-tmp-lines -1))
2960     (when (= gnus-tmp-lines -1)
2961       (setq gnus-tmp-lines "?"))
2962     (gnus-put-text-property
2963      (point)
2964      (progn (eval gnus-summary-line-format-spec) (point))
2965      'gnus-number gnus-tmp-number)
2966     (when (gnus-visual-p 'summary-highlight 'highlight)
2967       (forward-line -1)
2968       (gnus-run-hooks 'gnus-summary-update-hook)
2969       (forward-line 1))))
2970
2971 (defun gnus-summary-update-line (&optional dont-update)
2972   "Update summary line after change."
2973   (when (and gnus-summary-default-score
2974              (not gnus-summary-inhibit-highlight))
2975     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2976            (article (gnus-summary-article-number))
2977            (score (gnus-summary-article-score article)))
2978       (unless dont-update
2979         (if (and gnus-summary-mark-below
2980                  (< (gnus-summary-article-score)
2981                     gnus-summary-mark-below))
2982             ;; This article has a low score, so we mark it as read.
2983             (when (memq article gnus-newsgroup-unreads)
2984               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2985           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2986             ;; This article was previously marked as read on account
2987             ;; of a low score, but now it has risen, so we mark it as
2988             ;; unread.
2989             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2990         (gnus-summary-update-mark
2991          (if (or (null gnus-summary-default-score)
2992                  (<= (abs (- score gnus-summary-default-score))
2993                      gnus-summary-zcore-fuzz))
2994              ?                          ;Whitespace
2995            (if (< score gnus-summary-default-score)
2996                gnus-score-below-mark gnus-score-over-mark))
2997          'score))
2998       ;; Do visual highlighting.
2999       (when (gnus-visual-p 'summary-highlight 'highlight)
3000         (gnus-run-hooks 'gnus-summary-update-hook)))))
3001
3002 (defvar gnus-tmp-new-adopts nil)
3003
3004 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3005   "Return the number of articles in THREAD.
3006 This may be 0 in some cases -- if none of the articles in
3007 the thread are to be displayed."
3008   (let* ((number
3009          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3010           (cond
3011            ((not (listp thread))
3012             1)
3013            ((and (consp thread) (cdr thread))
3014             (apply
3015              '+ 1 (mapcar
3016                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3017            ((null thread)
3018             1)
3019            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3020             1)
3021            (t 0))))
3022     (when (and level (zerop level) gnus-tmp-new-adopts)
3023       (incf number
3024             (apply '+ (mapcar
3025                        'gnus-summary-number-of-articles-in-thread
3026                        gnus-tmp-new-adopts))))
3027     (if char
3028         (if (> number 1) gnus-not-empty-thread-mark
3029           gnus-empty-thread-mark)
3030       number)))
3031
3032 (defun gnus-summary-set-local-parameters (group)
3033   "Go through the local params of GROUP and set all variable specs in that list."
3034   (let ((params (gnus-group-find-parameter group))
3035         (vars '(quit-config))           ; Ignore quit-config.
3036         elem)
3037     (while params
3038       (setq elem (car params)
3039             params (cdr params))
3040       (and (consp elem)                 ; Has to be a cons.
3041            (consp (cdr elem))           ; The cdr has to be a list.
3042            (symbolp (car elem))         ; Has to be a symbol in there.
3043            (not (memq (car elem) vars))
3044            (ignore-errors               ; So we set it.
3045              (push (car elem) vars)
3046              (make-local-variable (car elem))
3047              (set (car elem) (eval (nth 1 elem))))))))
3048
3049 (defun gnus-summary-read-group (group &optional show-all no-article
3050                                       kill-buffer no-display backward
3051                                       select-articles)
3052   "Start reading news in newsgroup GROUP.
3053 If SHOW-ALL is non-nil, already read articles are also listed.
3054 If NO-ARTICLE is non-nil, no article is selected initially.
3055 If NO-DISPLAY, don't generate a summary buffer."
3056   (let (result)
3057     (while (and group
3058                 (null (setq result
3059                             (let ((gnus-auto-select-next nil))
3060                               (or (gnus-summary-read-group-1
3061                                    group show-all no-article
3062                                    kill-buffer no-display
3063                                    select-articles)
3064                                   (setq show-all nil
3065                                         select-articles nil)))))
3066                 (eq gnus-auto-select-next 'quietly))
3067       (set-buffer gnus-group-buffer)
3068       ;; The entry function called above goes to the next
3069       ;; group automatically, so we go two groups back
3070       ;; if we are searching for the previous group.
3071       (when backward
3072         (gnus-group-prev-unread-group 2))
3073       (if (not (equal group (gnus-group-group-name)))
3074           (setq group (gnus-group-group-name))
3075         (setq group nil)))
3076     result))
3077
3078 (defun gnus-summary-read-group-1 (group show-all no-article
3079                                         kill-buffer no-display
3080                                         &optional select-articles)
3081   ;; Killed foreign groups can't be entered.
3082   ;;  (when (and (not (gnus-group-native-p group))
3083   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3084   ;;    (error "Dead non-native groups can't be entered"))
3085   (gnus-message 5 "Retrieving newsgroup: %s..."
3086                 (gnus-group-decoded-name group))
3087   (let* ((new-group (gnus-summary-setup-buffer group))
3088          (quit-config (gnus-group-quit-config group))
3089          (did-select (and new-group (gnus-select-newsgroup
3090                                      group show-all select-articles))))
3091     (cond
3092      ;; This summary buffer exists already, so we just select it.
3093      ((not new-group)
3094       (gnus-set-global-variables)
3095       (when kill-buffer
3096         (gnus-kill-or-deaden-summary kill-buffer))
3097       (gnus-configure-windows 'summary 'force)
3098       (gnus-set-mode-line 'summary)
3099       (gnus-summary-position-point)
3100       (message "")
3101       t)
3102      ;; We couldn't select this group.
3103      ((null did-select)
3104       (when (and (eq major-mode 'gnus-summary-mode)
3105                  (not (equal (current-buffer) kill-buffer)))
3106         (kill-buffer (current-buffer))
3107         (if (not quit-config)
3108             (progn
3109               ;; Update the info -- marks might need to be removed,
3110               ;; for instance.
3111               (gnus-summary-update-info)
3112               (set-buffer gnus-group-buffer)
3113               (gnus-group-jump-to-group group)
3114               (gnus-group-next-unread-group 1))
3115           (gnus-handle-ephemeral-exit quit-config)))
3116       (let ((grpinfo (gnus-get-info group)))
3117         (if (null (gnus-info-read grpinfo))
3118             (gnus-message 3 "Group %s contains no messages"
3119                           (gnus-group-decoded-name group))
3120           (gnus-message 3 "Can't select group")))
3121       nil)
3122      ;; The user did a `C-g' while prompting for number of articles,
3123      ;; so we exit this group.
3124      ((eq did-select 'quit)
3125       (and (eq major-mode 'gnus-summary-mode)
3126            (not (equal (current-buffer) kill-buffer))
3127            (kill-buffer (current-buffer)))
3128       (when kill-buffer
3129         (gnus-kill-or-deaden-summary kill-buffer))
3130       (if (not quit-config)
3131           (progn
3132             (set-buffer gnus-group-buffer)
3133             (gnus-group-jump-to-group group)
3134             (gnus-group-next-unread-group 1)
3135             (gnus-configure-windows 'group 'force))
3136         (gnus-handle-ephemeral-exit quit-config))
3137       ;; Finally signal the quit.
3138       (signal 'quit nil))
3139      ;; The group was successfully selected.
3140      (t
3141       (gnus-set-global-variables)
3142       ;; Save the active value in effect when the group was entered.
3143       (setq gnus-newsgroup-active
3144             (gnus-copy-sequence
3145              (gnus-active gnus-newsgroup-name)))
3146       ;; You can change the summary buffer in some way with this hook.
3147       (gnus-run-hooks 'gnus-select-group-hook)
3148       (gnus-update-format-specifications
3149        nil 'summary 'summary-mode 'summary-dummy)
3150       (gnus-update-summary-mark-positions)
3151       ;; Do score processing.
3152       (when gnus-use-scoring
3153         (gnus-possibly-score-headers))
3154       ;; Check whether to fill in the gaps in the threads.
3155       (when gnus-build-sparse-threads
3156         (gnus-build-sparse-threads))
3157       ;; Find the initial limit.
3158       (if gnus-show-threads
3159           (if show-all
3160               (let ((gnus-newsgroup-dormant nil))
3161                 (gnus-summary-initial-limit show-all))
3162             (gnus-summary-initial-limit show-all))
3163         ;; When untreaded, all articles are always shown.
3164         (setq gnus-newsgroup-limit
3165               (mapcar
3166                (lambda (header) (mail-header-number header))
3167                gnus-newsgroup-headers)))
3168       ;; Generate the summary buffer.
3169       (unless no-display
3170         (gnus-summary-prepare))
3171       (when gnus-use-trees
3172         (gnus-tree-open group)
3173         (setq gnus-summary-highlight-line-function
3174               'gnus-tree-highlight-article))
3175       ;; If the summary buffer is empty, but there are some low-scored
3176       ;; articles or some excluded dormants, we include these in the
3177       ;; buffer.
3178       (when (and (zerop (buffer-size))
3179                  (not no-display))
3180         (cond (gnus-newsgroup-dormant
3181                (gnus-summary-limit-include-dormant))
3182               ((and gnus-newsgroup-scored show-all)
3183                (gnus-summary-limit-include-expunged t))))
3184       ;; Function `gnus-apply-kill-file' must be called in this hook.
3185       (gnus-run-hooks 'gnus-apply-kill-hook)
3186       (if (and (zerop (buffer-size))
3187                (not no-display))
3188           (progn
3189             ;; This newsgroup is empty.
3190             (gnus-summary-catchup-and-exit nil t)
3191             (gnus-message 6 "No unread news")
3192             (when kill-buffer
3193               (gnus-kill-or-deaden-summary kill-buffer))
3194             ;; Return nil from this function.
3195             nil)
3196         ;; Hide conversation thread subtrees.  We cannot do this in
3197         ;; gnus-summary-prepare-hook since kill processing may not
3198         ;; work with hidden articles.
3199         (and gnus-show-threads
3200              gnus-thread-hide-subtree
3201              (gnus-summary-hide-all-threads))
3202         (when kill-buffer
3203           (gnus-kill-or-deaden-summary kill-buffer))
3204         ;; Show first unread article if requested.
3205         (if (and (not no-article)
3206                  (not no-display)
3207                  gnus-newsgroup-unreads
3208                  gnus-auto-select-first)
3209             (progn
3210               (gnus-configure-windows 'summary)
3211               (cond
3212                ((eq gnus-auto-select-first 'best)
3213                 (gnus-summary-best-unread-article))
3214                ((eq gnus-auto-select-first t)
3215                 (gnus-summary-first-unread-article))
3216                ((gnus-functionp gnus-auto-select-first)
3217                 (funcall gnus-auto-select-first))))
3218           ;; Don't select any articles, just move point to the first
3219           ;; article in the group.
3220           (goto-char (point-min))
3221           (gnus-summary-position-point)
3222           (gnus-configure-windows 'summary 'force)
3223           (gnus-set-mode-line 'summary))
3224         (when (get-buffer-window gnus-group-buffer t)
3225           ;; Gotta use windows, because recenter does weird stuff if
3226           ;; the current buffer ain't the displayed window.
3227           (let ((owin (selected-window)))
3228             (select-window (get-buffer-window gnus-group-buffer t))
3229             (when (gnus-group-goto-group group)
3230               (recenter))
3231             (select-window owin)))
3232         ;; Mark this buffer as "prepared".
3233         (setq gnus-newsgroup-prepared t)
3234         (gnus-run-hooks 'gnus-summary-prepared-hook)
3235         t)))))
3236
3237 (defun gnus-summary-prepare ()
3238   "Generate the summary buffer."
3239   (interactive)
3240   (let ((buffer-read-only nil))
3241     (erase-buffer)
3242     (setq gnus-newsgroup-data nil
3243           gnus-newsgroup-data-reverse nil)
3244     (gnus-run-hooks 'gnus-summary-generate-hook)
3245     ;; Generate the buffer, either with threads or without.
3246     (when gnus-newsgroup-headers
3247       (gnus-summary-prepare-threads
3248        (if gnus-show-threads
3249            (gnus-sort-gathered-threads
3250             (funcall gnus-summary-thread-gathering-function
3251                      (gnus-sort-threads
3252                       (gnus-cut-threads (gnus-make-threads)))))
3253          ;; Unthreaded display.
3254          (gnus-sort-articles gnus-newsgroup-headers))))
3255     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3256     ;; Call hooks for modifying summary buffer.
3257     (goto-char (point-min))
3258     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3259
3260 (defsubst gnus-general-simplify-subject (subject)
3261   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3262   (setq subject
3263         (cond
3264          ;; Truncate the subject.
3265          (gnus-simplify-subject-functions
3266           (gnus-map-function gnus-simplify-subject-functions subject))
3267          ((numberp gnus-summary-gather-subject-limit)
3268           (setq subject (gnus-simplify-subject-re subject))
3269           (if (> (length subject) gnus-summary-gather-subject-limit)
3270               (substring subject 0 gnus-summary-gather-subject-limit)
3271             subject))
3272          ;; Fuzzily simplify it.
3273          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3274           (gnus-simplify-subject-fuzzy subject))
3275          ;; Just remove the leading "Re:".
3276          (t
3277           (gnus-simplify-subject-re subject))))
3278
3279   (if (and gnus-summary-gather-exclude-subject
3280            (string-match gnus-summary-gather-exclude-subject subject))
3281       nil                         ; This article shouldn't be gathered
3282     subject))
3283
3284 (defun gnus-summary-simplify-subject-query ()
3285   "Query where the respool algorithm would put this article."
3286   (interactive)
3287   (gnus-summary-select-article)
3288   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3289
3290 (defun gnus-gather-threads-by-subject (threads)
3291   "Gather threads by looking at Subject headers."
3292   (if (not gnus-summary-make-false-root)
3293       threads
3294     (let ((hashtb (gnus-make-hashtable 1024))
3295           (prev threads)
3296           (result threads)
3297           subject hthread whole-subject)
3298       (while threads
3299         (setq subject (gnus-general-simplify-subject
3300                        (setq whole-subject (mail-header-subject
3301                                             (caar threads)))))
3302         (when subject
3303           (if (setq hthread (gnus-gethash subject hashtb))
3304               (progn
3305                 ;; We enter a dummy root into the thread, if we
3306                 ;; haven't done that already.
3307                 (unless (stringp (caar hthread))
3308                   (setcar hthread (list whole-subject (car hthread))))
3309                 ;; We add this new gathered thread to this gathered
3310                 ;; thread.
3311                 (setcdr (car hthread)
3312                         (nconc (cdar hthread) (list (car threads))))
3313                 ;; Remove it from the list of threads.
3314                 (setcdr prev (cdr threads))
3315                 (setq threads prev))
3316             ;; Enter this thread into the hash table.
3317             (gnus-sethash subject threads hashtb)))
3318         (setq prev threads)
3319         (setq threads (cdr threads)))
3320       result)))
3321
3322 (defun gnus-gather-threads-by-references (threads)
3323   "Gather threads by looking at References headers."
3324   (let ((idhashtb (gnus-make-hashtable 1024))
3325         (thhashtb (gnus-make-hashtable 1024))
3326         (prev threads)
3327         (result threads)
3328         ids references id gthread gid entered ref)
3329     (while threads
3330       (when (setq references (mail-header-references (caar threads)))
3331         (setq id (mail-header-id (caar threads))
3332               ids (gnus-split-references references)
3333               entered nil)
3334         (while (setq ref (pop ids))
3335           (setq ids (delete ref ids))
3336           (if (not (setq gid (gnus-gethash ref idhashtb)))
3337               (progn
3338                 (gnus-sethash ref id idhashtb)
3339                 (gnus-sethash id threads thhashtb))
3340             (setq gthread (gnus-gethash gid thhashtb))
3341             (unless entered
3342               ;; We enter a dummy root into the thread, if we
3343               ;; haven't done that already.
3344               (unless (stringp (caar gthread))
3345                 (setcar gthread (list (mail-header-subject (caar gthread))
3346                                       (car gthread))))
3347               ;; We add this new gathered thread to this gathered
3348               ;; thread.
3349               (setcdr (car gthread)
3350                       (nconc (cdar gthread) (list (car threads)))))
3351             ;; Add it into the thread hash table.
3352             (gnus-sethash id gthread thhashtb)
3353             (setq entered t)
3354             ;; Remove it from the list of threads.
3355             (setcdr prev (cdr threads))
3356             (setq threads prev))))
3357       (setq prev threads)
3358       (setq threads (cdr threads)))
3359     result))
3360
3361 (defun gnus-sort-gathered-threads (threads)
3362   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3363   (let ((result threads))
3364     (while threads
3365       (when (stringp (caar threads))
3366         (setcdr (car threads)
3367                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3368       (setq threads (cdr threads)))
3369     result))
3370
3371 (defun gnus-thread-loop-p (root thread)
3372   "Say whether ROOT is in THREAD."
3373   (let ((stack (list thread))
3374         (infloop 0)
3375         th)
3376     (while (setq thread (pop stack))
3377       (setq th (cdr thread))
3378       (while (and th
3379                   (not (eq (caar th) root)))
3380         (pop th))
3381       (if th
3382           ;; We have found a loop.
3383           (let (ref-dep)
3384             (setcdr thread (delq (car th) (cdr thread)))
3385             (if (boundp (setq ref-dep (intern "none"
3386                                               gnus-newsgroup-dependencies)))
3387                 (setcdr (symbol-value ref-dep)
3388                         (nconc (cdr (symbol-value ref-dep))
3389                                (list (car th))))
3390               (set ref-dep (list nil (car th))))
3391             (setq infloop 1
3392                   stack nil))
3393         ;; Push all the subthreads onto the stack.
3394         (push (cdr thread) stack)))
3395     infloop))
3396
3397 (defun gnus-make-threads ()
3398   "Go through the dependency hashtb and find the roots.  Return all threads."
3399   (let (threads)
3400     (while (catch 'infloop
3401              (mapatoms
3402               (lambda (refs)
3403                 ;; Deal with self-referencing References loops.
3404                 (when (and (car (symbol-value refs))
3405                            (not (zerop
3406                                  (apply
3407                                   '+
3408                                   (mapcar
3409                                    (lambda (thread)
3410                                      (gnus-thread-loop-p
3411                                       (car (symbol-value refs)) thread))
3412                                    (cdr (symbol-value refs)))))))
3413                   (setq threads nil)
3414                   (throw 'infloop t))
3415                 (unless (car (symbol-value refs))
3416              ;; These threads do not refer back to any other articles,
3417                   ;; so they're roots.
3418                   (setq threads (append (cdr (symbol-value refs)) threads))))
3419               gnus-newsgroup-dependencies)))
3420     threads))
3421
3422 ;; Build the thread tree.
3423 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3424   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3425
3426 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3427 if it was already present.
3428
3429 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3430 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3431 Message-IDs will be renamed be renamed to a unique Message-ID before
3432 being entered.
3433
3434 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3435   (let* ((id (mail-header-id header))
3436          (id-dep (and id (intern id dependencies)))
3437          ref ref-dep ref-header)
3438     ;; Enter this `header' in the `dependencies' table.
3439     (cond
3440      ((not id-dep)
3441       (setq header nil))
3442      ;; The first two cases do the normal part: enter a new `header'
3443      ;; in the `dependencies' table.
3444      ((not (boundp id-dep))
3445       (set id-dep (list header)))
3446      ((null (car (symbol-value id-dep)))
3447       (setcar (symbol-value id-dep) header))
3448
3449      ;; From here the `header' was already present in the
3450      ;; `dependencies' table.
3451      (force-new
3452       ;; Overrides an existing entry;
3453       ;; just set the header part of the entry.
3454       (setcar (symbol-value id-dep) header))
3455
3456      ;; Renames the existing `header' to a unique Message-ID.
3457      ((not gnus-summary-ignore-duplicates)
3458       ;; An article with this Message-ID has already been seen.
3459       ;; We rename the Message-ID.
3460       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3461            (list header))
3462       (mail-header-set-id header id))
3463
3464      ;; The last case ignores an existing entry, except it adds any
3465      ;; additional Xrefs (in case the two articles came from different
3466      ;; servers.
3467      ;; Also sets `header' to `nil' meaning that the `dependencies'
3468      ;; table was *not* modified.
3469      (t
3470       (mail-header-set-xref
3471        (car (symbol-value id-dep))
3472        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3473                    "")
3474                (or (mail-header-xref header) "")))
3475       (setq header nil)))
3476
3477     (when header
3478       ;; First check if that we are not creating a References loop.
3479       (setq ref (gnus-parent-id (mail-header-references header)))
3480       (while (and ref
3481                   (setq ref-dep (intern-soft ref dependencies))
3482                   (boundp ref-dep)
3483                   (setq ref-header (car (symbol-value ref-dep))))
3484         (if (string= id ref)
3485             ;; Yuk!  This is a reference loop.  Make the article be a
3486             ;; root article.
3487             (progn
3488               (mail-header-set-references (car (symbol-value id-dep)) "none")
3489               (setq ref nil))
3490           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3491       (setq ref (gnus-parent-id (mail-header-references header)))
3492       (setq ref-dep (intern (or ref "none") dependencies))
3493       (if (boundp ref-dep)
3494           (setcdr (symbol-value ref-dep)
3495                   (nconc (cdr (symbol-value ref-dep))
3496                          (list (symbol-value id-dep))))
3497         (set ref-dep (list nil (symbol-value id-dep)))))
3498     header))
3499
3500 (defun gnus-build-sparse-threads ()
3501   (let ((headers gnus-newsgroup-headers)
3502         (mail-parse-charset gnus-newsgroup-charset)
3503         (gnus-summary-ignore-duplicates t)
3504         header references generation relations
3505         subject child end new-child date)
3506     ;; First we create an alist of generations/relations, where
3507     ;; generations is how much we trust the relation, and the relation
3508     ;; is parent/child.
3509     (gnus-message 7 "Making sparse threads...")
3510     (save-excursion
3511       (nnheader-set-temp-buffer " *gnus sparse threads*")
3512       (while (setq header (pop headers))
3513         (when (and (setq references (mail-header-references header))
3514                    (not (string= references "")))
3515           (insert references)
3516           (setq child (mail-header-id header)
3517                 subject (mail-header-subject header)
3518                 date (mail-header-date header)
3519                 generation 0)
3520           (while (search-backward ">" nil t)
3521             (setq end (1+ (point)))
3522             (when (search-backward "<" nil t)
3523               (setq new-child (buffer-substring (point) end))
3524               (push (list (incf generation)
3525                           child (setq child new-child)
3526                           subject date)
3527                     relations)))
3528           (when child
3529             (push (list (1+ generation) child nil subject) relations))
3530           (erase-buffer)))
3531       (kill-buffer (current-buffer)))
3532     ;; Sort over trustworthiness.
3533     (mapcar
3534      (lambda (relation)
3535        (when (gnus-dependencies-add-header
3536               (make-full-mail-header
3537                gnus-reffed-article-number
3538                (nth 3 relation) "" (or (nth 4 relation) "")
3539                (nth 1 relation)
3540                (or (nth 2 relation) "") 0 0 "")
3541               gnus-newsgroup-dependencies nil)
3542          (push gnus-reffed-article-number gnus-newsgroup-limit)
3543          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3544          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3545                gnus-newsgroup-reads)
3546          (decf gnus-reffed-article-number)))
3547      (sort relations 'car-less-than-car))
3548     (gnus-message 7 "Making sparse threads...done")))
3549
3550 (defun gnus-build-old-threads ()
3551   ;; Look at all the articles that refer back to old articles, and
3552   ;; fetch the headers for the articles that aren't there.  This will
3553   ;; build complete threads - if the roots haven't been expired by the
3554   ;; server, that is.
3555   (let ((mail-parse-charset gnus-newsgroup-charset)
3556         id heads)
3557     (mapatoms
3558      (lambda (refs)
3559        (when (not (car (symbol-value refs)))
3560          (setq heads (cdr (symbol-value refs)))
3561          (while heads
3562            (if (memq (mail-header-number (caar heads))
3563                      gnus-newsgroup-dormant)
3564                (setq heads (cdr heads))
3565              (setq id (symbol-name refs))
3566              (while (and (setq id (gnus-build-get-header id))
3567                          (not (car (gnus-id-to-thread id)))))
3568              (setq heads nil)))))
3569      gnus-newsgroup-dependencies)))
3570
3571 ;; This function has to be called with point after the article number
3572 ;; on the beginning of the line.
3573 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3574   (let ((eol (gnus-point-at-eol))
3575         (buffer (current-buffer))
3576         header)
3577
3578     ;; overview: [num subject from date id refs chars lines misc]
3579     (unwind-protect
3580         (progn
3581           (narrow-to-region (point) eol)
3582           (unless (eobp)
3583             (forward-char))
3584
3585           (setq header
3586                 (make-full-mail-header
3587                  number                 ; number
3588                  (funcall gnus-decode-encoded-word-function
3589                           (nnheader-nov-field)) ; subject
3590                  (funcall gnus-decode-encoded-word-function
3591                           (nnheader-nov-field)) ; from
3592                  (nnheader-nov-field)   ; date
3593                  (nnheader-nov-read-message-id) ; id
3594                  (nnheader-nov-field)   ; refs
3595                  (nnheader-nov-read-integer) ; chars
3596                  (nnheader-nov-read-integer) ; lines
3597                  (unless (eobp)
3598                    (if (looking-at "Xref: ")
3599                        (goto-char (match-end 0)))
3600                    (nnheader-nov-field)) ; Xref
3601                  (nnheader-nov-parse-extra)))) ; extra
3602
3603       (widen))
3604
3605     (when gnus-alter-header-function
3606       (funcall gnus-alter-header-function header))
3607     (gnus-dependencies-add-header header dependencies force-new)))
3608
3609 (defun gnus-build-get-header (id)
3610   "Look through the buffer of NOV lines and find the header to ID.
3611 Enter this line into the dependencies hash table, and return
3612 the id of the parent article (if any)."
3613   (let ((deps gnus-newsgroup-dependencies)
3614         found header)
3615     (prog1
3616         (save-excursion
3617           (set-buffer nntp-server-buffer)
3618           (let ((case-fold-search nil))
3619             (goto-char (point-min))
3620             (while (and (not found)
3621                         (search-forward id nil t))
3622               (beginning-of-line)
3623               (setq found (looking-at
3624                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3625                                    (regexp-quote id))))
3626               (or found (beginning-of-line 2)))
3627             (when found
3628               (beginning-of-line)
3629               (and
3630                (setq header (gnus-nov-parse-line
3631                              (read (current-buffer)) deps))
3632                (gnus-parent-id (mail-header-references header))))))
3633       (when header
3634         (let ((number (mail-header-number header)))
3635           (push number gnus-newsgroup-limit)
3636           (push header gnus-newsgroup-headers)
3637           (if (memq number gnus-newsgroup-unselected)
3638               (progn
3639                 (push number gnus-newsgroup-unreads)
3640                 (setq gnus-newsgroup-unselected
3641                       (delq number gnus-newsgroup-unselected)))
3642             (push number gnus-newsgroup-ancient)))))))
3643
3644 (defun gnus-build-all-threads ()
3645   "Read all the headers."
3646   (let ((gnus-summary-ignore-duplicates t)
3647         (mail-parse-charset gnus-newsgroup-charset)
3648         (dependencies gnus-newsgroup-dependencies)
3649         header article)
3650     (save-excursion
3651       (set-buffer nntp-server-buffer)
3652       (let ((case-fold-search nil))
3653         (goto-char (point-min))
3654         (while (not (eobp))
3655           (ignore-errors
3656             (setq article (read (current-buffer))
3657                   header (gnus-nov-parse-line article dependencies)))
3658           (when header
3659             (save-excursion
3660               (set-buffer gnus-summary-buffer)
3661               (push header gnus-newsgroup-headers)
3662               (if (memq (setq article (mail-header-number header))
3663                         gnus-newsgroup-unselected)
3664                   (progn
3665                     (push article gnus-newsgroup-unreads)
3666                     (setq gnus-newsgroup-unselected
3667                           (delq article gnus-newsgroup-unselected)))
3668                 (push article gnus-newsgroup-ancient)))
3669             (forward-line 1)))))))
3670
3671 (defun gnus-summary-update-article-line (article header)
3672   "Update the line for ARTICLE using HEADERS."
3673   (let* ((id (mail-header-id header))
3674          (thread (gnus-id-to-thread id)))
3675     (unless thread
3676       (error "Article in no thread"))
3677     ;; Update the thread.
3678     (setcar thread header)
3679     (gnus-summary-goto-subject article)
3680     (let* ((datal (gnus-data-find-list article))
3681            (data (car datal))
3682            (length (when (cdr datal)
3683                      (- (gnus-data-pos data)
3684                         (gnus-data-pos (cadr datal)))))
3685            (buffer-read-only nil)
3686            (level (gnus-summary-thread-level)))
3687       (gnus-delete-line)
3688       (gnus-summary-insert-line
3689        header level nil (gnus-article-mark article)
3690        (memq article gnus-newsgroup-replied)
3691        (memq article gnus-newsgroup-expirable)
3692        ;; Only insert the Subject string when it's different
3693        ;; from the previous Subject string.
3694        (if (and
3695             gnus-show-threads
3696             (gnus-subject-equal
3697              (condition-case ()
3698                  (mail-header-subject
3699                   (gnus-data-header
3700                    (cadr
3701                     (gnus-data-find-list
3702                      article
3703                      (gnus-data-list t)))))
3704                ;; Error on the side of excessive subjects.
3705                (error ""))
3706              (mail-header-subject header)))
3707            ""
3708          (mail-header-subject header))
3709        nil (cdr (assq article gnus-newsgroup-scored))
3710        (memq article gnus-newsgroup-processable))
3711       (when length
3712         (gnus-data-update-list
3713          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3714
3715 (defun gnus-summary-update-article (article &optional iheader)
3716   "Update ARTICLE in the summary buffer."
3717   (set-buffer gnus-summary-buffer)
3718   (let* ((header (gnus-summary-article-header article))
3719          (id (mail-header-id header))
3720          (data (gnus-data-find article))
3721          (thread (gnus-id-to-thread id))
3722          (references (mail-header-references header))
3723          (parent
3724           (gnus-id-to-thread
3725            (or (gnus-parent-id
3726                 (when (and references
3727                            (not (equal "" references)))
3728                   references))
3729                "none")))
3730          (buffer-read-only nil)
3731          (old (car thread)))
3732     (when thread
3733       (unless iheader
3734         (setcar thread nil)
3735         (when parent
3736           (delq thread parent)))
3737       (if (gnus-summary-insert-subject id header)
3738        ;; Set the (possibly) new article number in the data structure.
3739           (gnus-data-set-number data (gnus-id-to-article id))
3740         (setcar thread old)
3741         nil))))
3742
3743 (defun gnus-rebuild-thread (id &optional line)
3744   "Rebuild the thread containing ID.
3745 If LINE, insert the rebuilt thread starting on line LINE."
3746   (let ((buffer-read-only nil)
3747         old-pos current thread data)
3748     (if (not gnus-show-threads)
3749         (setq thread (list (car (gnus-id-to-thread id))))
3750       ;; Get the thread this article is part of.
3751       (setq thread (gnus-remove-thread id)))
3752     (setq old-pos (gnus-point-at-bol))
3753     (setq current (save-excursion
3754                     (and (re-search-backward "[\r\n]" nil t)
3755                          (gnus-summary-article-number))))
3756     ;; If this is a gathered thread, we have to go some re-gathering.
3757     (when (stringp (car thread))
3758       (let ((subject (car thread))
3759             roots thr)
3760         (setq thread (cdr thread))
3761         (while thread
3762           (unless (memq (setq thr (gnus-id-to-thread
3763                                    (gnus-root-id
3764                                     (mail-header-id (caar thread)))))
3765                         roots)
3766             (push thr roots))
3767           (setq thread (cdr thread)))
3768         ;; We now have all (unique) roots.
3769         (if (= (length roots) 1)
3770             ;; All the loose roots are now one solid root.
3771             (setq thread (car roots))
3772           (setq thread (cons subject (gnus-sort-threads roots))))))
3773     (let (threads)
3774       ;; We then insert this thread into the summary buffer.
3775       (when line
3776         (goto-char (point-min))
3777         (forward-line (1- line)))
3778       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3779         (if gnus-show-threads
3780             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3781           (gnus-summary-prepare-unthreaded thread))
3782         (setq data (nreverse gnus-newsgroup-data))
3783         (setq threads gnus-newsgroup-threads))
3784       ;; We splice the new data into the data structure.
3785       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3786       ;;!!! then we want to insert at the beginning of the buffer.
3787       ;;!!! That happens to be true with Gnus now, but that may
3788       ;;!!! change in the future.  Perhaps.
3789       (gnus-data-enter-list
3790        (if line nil current) data (- (point) old-pos))
3791       (setq gnus-newsgroup-threads
3792             (nconc threads gnus-newsgroup-threads))
3793       (gnus-data-compute-positions))))
3794
3795 (defun gnus-number-to-header (number)
3796   "Return the header for article NUMBER."
3797   (let ((headers gnus-newsgroup-headers))
3798     (while (and headers
3799                 (not (= number (mail-header-number (car headers)))))
3800       (pop headers))
3801     (when headers
3802       (car headers))))
3803
3804 (defun gnus-parent-headers (in-headers &optional generation)
3805   "Return the headers of the GENERATIONeth parent of HEADERS."
3806   (unless generation
3807     (setq generation 1))
3808   (let ((parent t)
3809         (headers in-headers)
3810         references)
3811     (while (and parent
3812                 (not (zerop generation))
3813                 (setq references (mail-header-references headers)))
3814       (setq headers (if (and references
3815                              (setq parent (gnus-parent-id references)))
3816                         (car (gnus-id-to-thread parent))
3817                       nil))
3818       (decf generation))
3819     (and (not (eq headers in-headers))
3820          headers)))
3821
3822 (defun gnus-id-to-thread (id)
3823   "Return the (sub-)thread where ID appears."
3824   (gnus-gethash id gnus-newsgroup-dependencies))
3825
3826 (defun gnus-id-to-article (id)
3827   "Return the article number of ID."
3828   (let ((thread (gnus-id-to-thread id)))
3829     (when (and thread
3830                (car thread))
3831       (mail-header-number (car thread)))))
3832
3833 (defun gnus-id-to-header (id)
3834   "Return the article headers of ID."
3835   (car (gnus-id-to-thread id)))
3836
3837 (defun gnus-article-displayed-root-p (article)
3838   "Say whether ARTICLE is a root(ish) article."
3839   (let ((level (gnus-summary-thread-level article))
3840         (refs (mail-header-references  (gnus-summary-article-header article)))
3841         particle)
3842     (cond
3843      ((null level) nil)
3844      ((zerop level) t)
3845      ((null refs) t)
3846      ((null (gnus-parent-id refs)) t)
3847      ((and (= 1 level)
3848            (null (setq particle (gnus-id-to-article
3849                                  (gnus-parent-id refs))))
3850            (null (gnus-summary-thread-level particle)))))))
3851
3852 (defun gnus-root-id (id)
3853   "Return the id of the root of the thread where ID appears."
3854   (let (last-id prev)
3855     (while (and id (setq prev (car (gnus-id-to-thread id))))
3856       (setq last-id id
3857             id (gnus-parent-id (mail-header-references prev))))
3858     last-id))
3859
3860 (defun gnus-articles-in-thread (thread)
3861   "Return the list of articles in THREAD."
3862   (cons (mail-header-number (car thread))
3863         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3864
3865 (defun gnus-remove-thread (id &optional dont-remove)
3866   "Remove the thread that has ID in it."
3867   (let (headers thread last-id)
3868     ;; First go up in this thread until we find the root.
3869     (setq last-id (gnus-root-id id)
3870           headers (message-flatten-list (gnus-id-to-thread last-id)))
3871     ;; We have now found the real root of this thread.  It might have
3872     ;; been gathered into some loose thread, so we have to search
3873     ;; through the threads to find the thread we wanted.
3874     (let ((threads gnus-newsgroup-threads)
3875           sub)
3876       (while threads
3877         (setq sub (car threads))
3878         (if (stringp (car sub))
3879             ;; This is a gathered thread, so we look at the roots
3880             ;; below it to find whether this article is in this
3881             ;; gathered root.
3882             (progn
3883               (setq sub (cdr sub))
3884               (while sub
3885                 (when (member (caar sub) headers)
3886                   (setq thread (car threads)
3887                         threads nil
3888                         sub nil))
3889                 (setq sub (cdr sub))))
3890           ;; It's an ordinary thread, so we check it.
3891           (when (eq (car sub) (car headers))
3892             (setq thread sub
3893                   threads nil)))
3894         (setq threads (cdr threads)))
3895       ;; If this article is in no thread, then it's a root.
3896       (if thread
3897           (unless dont-remove
3898             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3899         (setq thread (gnus-id-to-thread last-id)))
3900       (when thread
3901         (prog1
3902             thread                      ; We return this thread.
3903           (unless dont-remove
3904             (if (stringp (car thread))
3905                 (progn
3906                   ;; If we use dummy roots, then we have to remove the
3907                   ;; dummy root as well.
3908                   (when (eq gnus-summary-make-false-root 'dummy)
3909                     ;; We go to the dummy root by going to
3910                     ;; the first sub-"thread", and then one line up.
3911                     (gnus-summary-goto-article
3912                      (mail-header-number (caadr thread)))
3913                     (forward-line -1)
3914                     (gnus-delete-line)
3915                     (gnus-data-compute-positions))
3916                   (setq thread (cdr thread))
3917                   (while thread
3918                     (gnus-remove-thread-1 (car thread))
3919                     (setq thread (cdr thread))))
3920               (gnus-remove-thread-1 thread))))))))
3921
3922 (defun gnus-remove-thread-1 (thread)
3923   "Remove the thread THREAD recursively."
3924   (let ((number (mail-header-number (pop thread)))
3925         d)
3926     (setq thread (reverse thread))
3927     (while thread
3928       (gnus-remove-thread-1 (pop thread)))
3929     (when (setq d (gnus-data-find number))
3930       (goto-char (gnus-data-pos d))
3931       (gnus-summary-show-thread)
3932       (gnus-data-remove
3933        number
3934        (- (gnus-point-at-bol)
3935           (prog1
3936               (1+ (gnus-point-at-eol))
3937             (gnus-delete-line)))))))
3938
3939 (defun gnus-sort-threads-1 (threads func)
3940   (sort (mapcar (lambda (thread)
3941                   (cons (car thread)
3942                         (and (cdr thread)
3943                              (gnus-sort-threads-1 (cdr thread) func))))
3944                 threads) func))
3945
3946 (defun gnus-sort-threads (threads)
3947   "Sort THREADS."
3948   (if (not gnus-thread-sort-functions)
3949       threads
3950     (gnus-message 8 "Sorting threads...")
3951     (prog1
3952         (gnus-sort-threads-1
3953          threads
3954          (gnus-make-sort-function gnus-thread-sort-functions))
3955       (gnus-message 8 "Sorting threads...done"))))
3956
3957 (defun gnus-sort-articles (articles)
3958   "Sort ARTICLES."
3959   (when gnus-article-sort-functions
3960     (gnus-message 7 "Sorting articles...")
3961     (prog1
3962         (setq gnus-newsgroup-headers
3963               (sort articles (gnus-make-sort-function
3964                               gnus-article-sort-functions)))
3965       (gnus-message 7 "Sorting articles...done"))))
3966
3967 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3968 (defmacro gnus-thread-header (thread)
3969   "Return header of first article in THREAD.
3970 Note that THREAD must never, ever be anything else than a variable -
3971 using some other form will lead to serious barfage."
3972   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3973   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3974   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3975         (vector thread) 2))
3976
3977 (defsubst gnus-article-sort-by-number (h1 h2)
3978   "Sort articles by article number."
3979   (< (mail-header-number h1)
3980      (mail-header-number h2)))
3981
3982 (defun gnus-thread-sort-by-number (h1 h2)
3983   "Sort threads by root article number."
3984   (gnus-article-sort-by-number
3985    (gnus-thread-header h1) (gnus-thread-header h2)))
3986
3987 (defsubst gnus-article-sort-by-lines (h1 h2)
3988   "Sort articles by article Lines header."
3989   (< (mail-header-lines h1)
3990      (mail-header-lines h2)))
3991
3992 (defun gnus-thread-sort-by-lines (h1 h2)
3993   "Sort threads by root article Lines header."
3994   (gnus-article-sort-by-lines
3995    (gnus-thread-header h1) (gnus-thread-header h2)))
3996
3997 (defsubst gnus-article-sort-by-chars (h1 h2)
3998   "Sort articles by octet length."
3999   (< (mail-header-chars h1)
4000      (mail-header-chars h2)))
4001
4002 (defun gnus-thread-sort-by-chars (h1 h2)
4003   "Sort threads by root article octet length."
4004   (gnus-article-sort-by-chars
4005    (gnus-thread-header h1) (gnus-thread-header h2)))
4006
4007 (defsubst gnus-article-sort-by-author (h1 h2)
4008   "Sort articles by root author."
4009   (string-lessp
4010    (let ((extract (funcall
4011                    gnus-extract-address-components
4012                    (mail-header-from h1))))
4013      (or (car extract) (cadr extract) ""))
4014    (let ((extract (funcall
4015                    gnus-extract-address-components
4016                    (mail-header-from h2))))
4017      (or (car extract) (cadr extract) ""))))
4018
4019 (defun gnus-thread-sort-by-author (h1 h2)
4020   "Sort threads by root author."
4021   (gnus-article-sort-by-author
4022    (gnus-thread-header h1)  (gnus-thread-header h2)))
4023
4024 (defsubst gnus-article-sort-by-subject (h1 h2)
4025   "Sort articles by root subject."
4026   (string-lessp
4027    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4028    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4029
4030 (defun gnus-thread-sort-by-subject (h1 h2)
4031   "Sort threads by root subject."
4032   (gnus-article-sort-by-subject
4033    (gnus-thread-header h1) (gnus-thread-header h2)))
4034
4035 (defsubst gnus-article-sort-by-date (h1 h2)
4036   "Sort articles by root article date."
4037   (time-less-p
4038    (gnus-date-get-time (mail-header-date h1))
4039    (gnus-date-get-time (mail-header-date h2))))
4040
4041 (defun gnus-thread-sort-by-date (h1 h2)
4042   "Sort threads by root article date."
4043   (gnus-article-sort-by-date
4044    (gnus-thread-header h1) (gnus-thread-header h2)))
4045
4046 (defsubst gnus-article-sort-by-score (h1 h2)
4047   "Sort articles by root article score.
4048 Unscored articles will be counted as having a score of zero."
4049   (> (or (cdr (assq (mail-header-number h1)
4050                     gnus-newsgroup-scored))
4051          gnus-summary-default-score 0)
4052      (or (cdr (assq (mail-header-number h2)
4053                     gnus-newsgroup-scored))
4054          gnus-summary-default-score 0)))
4055
4056 (defun gnus-thread-sort-by-score (h1 h2)
4057   "Sort threads by root article score."
4058   (gnus-article-sort-by-score
4059    (gnus-thread-header h1) (gnus-thread-header h2)))
4060
4061 (defun gnus-thread-sort-by-total-score (h1 h2)
4062   "Sort threads by the sum of all scores in the thread.
4063 Unscored articles will be counted as having a score of zero."
4064   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4065
4066 (defun gnus-thread-total-score (thread)
4067   ;; This function find the total score of THREAD.
4068   (cond ((null thread)
4069          0)
4070         ((consp thread)
4071          (if (stringp (car thread))
4072              (apply gnus-thread-score-function 0
4073                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4074            (gnus-thread-total-score-1 thread)))
4075         (t
4076          (gnus-thread-total-score-1 (list thread)))))
4077
4078 (defun gnus-thread-total-score-1 (root)
4079   ;; This function find the total score of the thread below ROOT.
4080   (setq root (car root))
4081   (apply gnus-thread-score-function
4082          (or (append
4083               (mapcar 'gnus-thread-total-score
4084                       (cdr (gnus-id-to-thread (mail-header-id root))))
4085               (when (> (mail-header-number root) 0)
4086                 (list (or (cdr (assq (mail-header-number root)
4087                                      gnus-newsgroup-scored))
4088                           gnus-summary-default-score 0))))
4089              (list gnus-summary-default-score)
4090              '(0))))
4091
4092 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4093 (defvar gnus-tmp-prev-subject nil)
4094 (defvar gnus-tmp-false-parent nil)
4095 (defvar gnus-tmp-root-expunged nil)
4096 (defvar gnus-tmp-dummy-line nil)
4097
4098 (eval-when-compile (defvar gnus-tmp-header))
4099 (defun gnus-extra-header (type &optional header)
4100   "Return the extra header of TYPE."
4101   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4102       ""))
4103
4104 (defvar gnus-tmp-thread-tree-header-string "")
4105
4106 (defvar gnus-sum-thread-tree-root "> "
4107   "With %B spec, used for the root of a thread.
4108 If nil, use subject instead.")
4109 (defvar gnus-sum-thread-tree-single-indent ""
4110   "With %B spec, used for a thread with just one message.
4111 If nil, use subject instead.")
4112 (defvar gnus-sum-thread-tree-vertical "| "
4113   "With %B spec, used for drawing a vertical line.")
4114 (defvar gnus-sum-thread-tree-indent "  "
4115   "With %B spec, used for indenting.")
4116 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4117   "With %B spec, used for a leaf with brothers.")
4118 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4119   "With %B spec, used for a leaf without brothers.")
4120
4121 (defun gnus-summary-prepare-threads (threads)
4122   "Prepare summary buffer from THREADS and indentation LEVEL.
4123 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4124 or a straight list of headers."
4125   (gnus-message 7 "Generating summary...")
4126
4127   (setq gnus-newsgroup-threads threads)
4128   (beginning-of-line)
4129
4130   (let ((gnus-tmp-level 0)
4131         (default-score (or gnus-summary-default-score 0))
4132         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4133         thread number subject stack state gnus-tmp-gathered beg-match
4134         new-roots gnus-tmp-new-adopts thread-end
4135         gnus-tmp-header gnus-tmp-unread
4136         gnus-tmp-replied gnus-tmp-subject-or-nil
4137         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4138         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4139         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4140         tree-stack)
4141
4142     (setq gnus-tmp-prev-subject nil)
4143
4144     (if (vectorp (car threads))
4145         ;; If this is a straight (sic) list of headers, then a
4146         ;; threaded summary display isn't required, so we just create
4147         ;; an unthreaded one.
4148         (gnus-summary-prepare-unthreaded threads)
4149
4150       ;; Do the threaded display.
4151
4152       (while (or threads stack gnus-tmp-new-adopts new-roots)
4153
4154         (if (and (= gnus-tmp-level 0)
4155                  (or (not stack)
4156                      (= (caar stack) 0))
4157                  (not gnus-tmp-false-parent)
4158                  (or gnus-tmp-new-adopts new-roots))
4159             (if gnus-tmp-new-adopts
4160                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4161                       thread (list (car gnus-tmp-new-adopts))
4162                       gnus-tmp-header (caar thread)
4163                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4164               (when new-roots
4165                 (setq thread (list (car new-roots))
4166                       gnus-tmp-header (caar thread)
4167                       new-roots (cdr new-roots))))
4168
4169           (if threads
4170               ;; If there are some threads, we do them before the
4171               ;; threads on the stack.
4172               (setq thread threads
4173                     gnus-tmp-header (caar thread))
4174             ;; There were no current threads, so we pop something off
4175             ;; the stack.
4176             (setq state (car stack)
4177                   gnus-tmp-level (car state)
4178                   tree-stack (cadr state)
4179                   thread (caddr state)
4180                   stack (cdr stack)
4181                   gnus-tmp-header (caar thread))))
4182
4183         (setq gnus-tmp-false-parent nil)
4184         (setq gnus-tmp-root-expunged nil)
4185         (setq thread-end nil)
4186
4187         (if (stringp gnus-tmp-header)
4188             ;; The header is a dummy root.
4189             (cond
4190              ((eq gnus-summary-make-false-root 'adopt)
4191               ;; We let the first article adopt the rest.
4192               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4193                                                (cddar thread)))
4194               (setq gnus-tmp-gathered
4195                     (nconc (mapcar
4196                             (lambda (h) (mail-header-number (car h)))
4197                             (cddar thread))
4198                            gnus-tmp-gathered))
4199               (setq thread (cons (list (caar thread)
4200                                        (cadar thread))
4201                                  (cdr thread)))
4202               (setq gnus-tmp-level -1
4203                     gnus-tmp-false-parent t))
4204              ((eq gnus-summary-make-false-root 'empty)
4205               ;; We print adopted articles with empty subject fields.
4206               (setq gnus-tmp-gathered
4207                     (nconc (mapcar
4208                             (lambda (h) (mail-header-number (car h)))
4209                             (cddar thread))
4210                            gnus-tmp-gathered))
4211               (setq gnus-tmp-level -1))
4212              ((eq gnus-summary-make-false-root 'dummy)
4213               ;; We remember that we probably want to output a dummy
4214               ;; root.
4215               (setq gnus-tmp-dummy-line gnus-tmp-header)
4216               (setq gnus-tmp-prev-subject gnus-tmp-header))
4217              (t
4218               ;; We do not make a root for the gathered
4219               ;; sub-threads at all.
4220               (setq gnus-tmp-level -1)))
4221
4222           (setq number (mail-header-number gnus-tmp-header)
4223                 subject (mail-header-subject gnus-tmp-header))
4224
4225           (cond
4226            ;; If the thread has changed subject, we might want to make
4227            ;; this subthread into a root.
4228            ((and (null gnus-thread-ignore-subject)
4229                  (not (zerop gnus-tmp-level))
4230                  gnus-tmp-prev-subject
4231                  (not (inline
4232                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4233             (setq new-roots (nconc new-roots (list (car thread)))
4234                   thread-end t
4235                   gnus-tmp-header nil))
4236            ;; If the article lies outside the current limit,
4237            ;; then we do not display it.
4238            ((not (memq number gnus-newsgroup-limit))
4239             (setq gnus-tmp-gathered
4240                   (nconc (mapcar
4241                           (lambda (h) (mail-header-number (car h)))
4242                           (cdar thread))
4243                          gnus-tmp-gathered))
4244             (setq gnus-tmp-new-adopts (if (cdar thread)
4245                                           (append gnus-tmp-new-adopts
4246                                                   (cdar thread))
4247                                         gnus-tmp-new-adopts)
4248                   thread-end t
4249                   gnus-tmp-header nil)
4250             (when (zerop gnus-tmp-level)
4251               (setq gnus-tmp-root-expunged t)))
4252            ;; Perhaps this article is to be marked as read?
4253            ((and gnus-summary-mark-below
4254                  (< (or (cdr (assq number gnus-newsgroup-scored))
4255                         default-score)
4256                     gnus-summary-mark-below)
4257                  ;; Don't touch sparse articles.
4258                  (not (gnus-summary-article-sparse-p number))
4259                  (not (gnus-summary-article-ancient-p number)))
4260             (setq gnus-newsgroup-unreads
4261                   (delq number gnus-newsgroup-unreads))
4262             (if gnus-newsgroup-auto-expire
4263                 (push number gnus-newsgroup-expirable)
4264               (push (cons number gnus-low-score-mark)
4265                     gnus-newsgroup-reads))))
4266
4267           (when gnus-tmp-header
4268             ;; We may have an old dummy line to output before this
4269             ;; article.
4270             (when (and gnus-tmp-dummy-line
4271                        (gnus-subject-equal
4272                         gnus-tmp-dummy-line
4273                         (mail-header-subject gnus-tmp-header)))
4274               (gnus-summary-insert-dummy-line
4275                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4276               (setq gnus-tmp-dummy-line nil))
4277
4278             ;; Compute the mark.
4279             (setq gnus-tmp-unread (gnus-article-mark number))
4280
4281             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4282                                   gnus-tmp-header gnus-tmp-level)
4283                   gnus-newsgroup-data)
4284
4285             ;; Actually insert the line.
4286             (setq
4287              gnus-tmp-subject-or-nil
4288              (cond
4289               ((and gnus-thread-ignore-subject
4290                     gnus-tmp-prev-subject
4291                     (not (inline (gnus-subject-equal
4292                                   gnus-tmp-prev-subject subject))))
4293                subject)
4294               ((zerop gnus-tmp-level)
4295                (if (and (eq gnus-summary-make-false-root 'empty)
4296                         (memq number gnus-tmp-gathered)
4297                         gnus-tmp-prev-subject
4298                         (inline (gnus-subject-equal
4299                                  gnus-tmp-prev-subject subject)))
4300                    gnus-summary-same-subject
4301                  subject))
4302               (t gnus-summary-same-subject)))
4303             (if (and (eq gnus-summary-make-false-root 'adopt)
4304                      (= gnus-tmp-level 1)
4305                      (memq number gnus-tmp-gathered))
4306                 (setq gnus-tmp-opening-bracket ?\<
4307                       gnus-tmp-closing-bracket ?\>)
4308               (setq gnus-tmp-opening-bracket ?\[
4309                     gnus-tmp-closing-bracket ?\]))
4310             (setq
4311              gnus-tmp-indentation
4312              (aref gnus-thread-indent-array gnus-tmp-level)
4313              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4314              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4315                                 gnus-summary-default-score 0)
4316              gnus-tmp-score-char
4317              (if (or (null gnus-summary-default-score)
4318                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4319                          gnus-summary-zcore-fuzz))
4320                  ?                      ;Whitespace
4321                (if (< gnus-tmp-score gnus-summary-default-score)
4322                    gnus-score-below-mark gnus-score-over-mark))
4323              gnus-tmp-replied
4324              (cond ((memq number gnus-newsgroup-processable)
4325                     gnus-process-mark)
4326                    ((memq number gnus-newsgroup-cached)
4327                     gnus-cached-mark)
4328                    ((memq number gnus-newsgroup-replied)
4329                     gnus-replied-mark)
4330                    ((memq number gnus-newsgroup-forwarded)
4331                     gnus-forwarded-mark)
4332                    ((memq number gnus-newsgroup-saved)
4333                     gnus-saved-mark)
4334                    ((memq number gnus-newsgroup-recent)
4335                     gnus-recent-mark)
4336                    (t gnus-no-mark))
4337              gnus-tmp-from (mail-header-from gnus-tmp-header)
4338              gnus-tmp-name
4339              (cond
4340               ((string-match "<[^>]+> *$" gnus-tmp-from)
4341                (setq beg-match (match-beginning 0))
4342                (or (and (string-match "^\".+\"" gnus-tmp-from)
4343                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4344                    (substring gnus-tmp-from 0 beg-match)))
4345               ((string-match "(.+)" gnus-tmp-from)
4346                (substring gnus-tmp-from
4347                           (1+ (match-beginning 0)) (1- (match-end 0))))
4348               (t gnus-tmp-from))
4349              gnus-tmp-thread-tree-header-string
4350              (cond 
4351               ((not gnus-show-threads) "")
4352               ((zerop gnus-tmp-level)
4353                (if (cdar thread) 
4354                    (or gnus-sum-thread-tree-root subject)
4355                  (or gnus-sum-thread-tree-single-indent subject)))
4356               (t
4357                (concat (apply 'concat
4358                               (mapcar (lambda (item) 
4359                                         (if (= item 1) 
4360                                             gnus-sum-thread-tree-vertical
4361                                           gnus-sum-thread-tree-indent))
4362                                       (cdr (reverse tree-stack))))
4363                        (if (nth 1 thread) 
4364                            gnus-sum-thread-tree-leaf-with-other
4365                          gnus-sum-thread-tree-single-leaf)))))
4366             (when (string= gnus-tmp-name "")
4367               (setq gnus-tmp-name gnus-tmp-from))
4368             (unless (numberp gnus-tmp-lines)
4369               (setq gnus-tmp-lines -1))
4370             (when (= gnus-tmp-lines -1)
4371               (setq gnus-tmp-lines "?"))
4372             (gnus-put-text-property
4373              (point)
4374              (progn (eval gnus-summary-line-format-spec) (point))
4375              'gnus-number number)
4376             (when gnus-visual-p
4377               (forward-line -1)
4378               (gnus-run-hooks 'gnus-summary-update-hook)
4379               (forward-line 1))
4380
4381             (setq gnus-tmp-prev-subject subject)))
4382
4383         (when (nth 1 thread)
4384           (push (list (max 0 gnus-tmp-level)
4385                       (copy-list tree-stack)
4386                       (nthcdr 1 thread))
4387                 stack))
4388         (push (if (nth 1 thread) 1 0) tree-stack)
4389         (incf gnus-tmp-level)
4390         (setq threads (if thread-end nil (cdar thread)))
4391         (unless threads
4392           (setq gnus-tmp-level 0)))))
4393   (gnus-message 7 "Generating summary...done"))
4394
4395 (defun gnus-summary-prepare-unthreaded (headers)
4396   "Generate an unthreaded summary buffer based on HEADERS."
4397   (let (header number mark)
4398
4399     (beginning-of-line)
4400
4401     (while headers
4402       ;; We may have to root out some bad articles...
4403       (when (memq (setq number (mail-header-number
4404                                 (setq header (pop headers))))
4405                   gnus-newsgroup-limit)
4406         ;; Mark article as read when it has a low score.
4407         (when (and gnus-summary-mark-below
4408                    (< (or (cdr (assq number gnus-newsgroup-scored))
4409                           gnus-summary-default-score 0)
4410                       gnus-summary-mark-below)
4411                    (not (gnus-summary-article-ancient-p number)))
4412           (setq gnus-newsgroup-unreads
4413                 (delq number gnus-newsgroup-unreads))
4414           (if gnus-newsgroup-auto-expire
4415               (push number gnus-newsgroup-expirable)
4416             (push (cons number gnus-low-score-mark)
4417                   gnus-newsgroup-reads)))
4418
4419         (setq mark (gnus-article-mark number))
4420         (push (gnus-data-make number mark (1+ (point)) header 0)
4421               gnus-newsgroup-data)
4422         (gnus-summary-insert-line
4423          header 0 number
4424          mark (memq number gnus-newsgroup-replied)
4425          (memq number gnus-newsgroup-expirable)
4426          (mail-header-subject header) nil
4427          (cdr (assq number gnus-newsgroup-scored))
4428          (memq number gnus-newsgroup-processable))))))
4429
4430 (defun gnus-summary-remove-list-identifiers ()
4431   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4432   (let ((regexp (if (consp gnus-list-identifiers)
4433                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4434                   gnus-list-identifiers))
4435         changed subject)
4436     (when regexp
4437       (dolist (header gnus-newsgroup-headers)
4438         (setq subject (mail-header-subject header)
4439               changed nil)
4440         (while (string-match
4441                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4442                 subject)
4443           (setq subject
4444                 (concat (substring subject 0 (match-beginning 2))
4445                         (substring subject (match-end 0)))
4446                 changed t))
4447         (when (and changed
4448                    (string-match
4449                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4450           (setq subject
4451                 (concat (substring subject 0 (match-beginning 1))
4452                         (substring subject (match-end 1)))))
4453         (when changed
4454           (mail-header-set-subject header subject))))))
4455
4456 (defun gnus-fetch-headers (articles)
4457   "Fetch headers of ARTICLES."
4458   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4459     (gnus-message 5 "Fetching headers for %s..." name)
4460     (prog1
4461         (if (eq 'nov
4462                 (setq gnus-headers-retrieved-by
4463                       (gnus-retrieve-headers
4464                        articles gnus-newsgroup-name
4465                        ;; We might want to fetch old headers, but
4466                        ;; not if there is only 1 article.
4467                        (and (or (and
4468                                  (not (eq gnus-fetch-old-headers 'some))
4469                                  (not (numberp gnus-fetch-old-headers)))
4470                                 (> (length articles) 1))
4471                             gnus-fetch-old-headers))))
4472             (gnus-get-newsgroup-headers-xover
4473              articles nil nil gnus-newsgroup-name t)
4474           (gnus-get-newsgroup-headers))
4475       (gnus-message 5 "Fetching headers for %s...done" name))))
4476
4477 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4478   "Select newsgroup GROUP.
4479 If READ-ALL is non-nil, all articles in the group are selected.
4480 If SELECT-ARTICLES, only select those articles from GROUP."
4481   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4482          ;;!!! Dirty hack; should be removed.
4483          (gnus-summary-ignore-duplicates
4484           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4485               t
4486             gnus-summary-ignore-duplicates))
4487          (info (nth 2 entry))
4488          articles fetched-articles cached)
4489
4490     (unless (gnus-check-server
4491              (setq gnus-current-select-method
4492                    (gnus-find-method-for-group group)))
4493       (error "Couldn't open server"))
4494
4495     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4496         (gnus-activate-group group)     ; Or we can activate it...
4497         (progn                          ; Or we bug out.
4498           (when (equal major-mode 'gnus-summary-mode)
4499             (kill-buffer (current-buffer)))
4500           (error "Couldn't activate group %s: %s"
4501                  group (gnus-status-message group))))
4502
4503     (unless (gnus-request-group group t)
4504       (when (equal major-mode 'gnus-summary-mode)
4505         (kill-buffer (current-buffer)))
4506       (error "Couldn't request group %s: %s"
4507              group (gnus-status-message group)))
4508
4509     (setq gnus-newsgroup-name group
4510           gnus-newsgroup-unselected nil
4511           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4512     
4513     (setq gnus-newsgroup-display (gnus-group-find-parameter group 'display))
4514     (setq gnus-newsgroup-display
4515           (cond
4516            ((eq gnus-newsgroup-display 'all)
4517             (setq gnus-newsgroup-display 'identity))
4518            ((arrayp gnus-newsgroup-display)
4519             (gnus-summary-display-make-predicate
4520              (mapcar 'identity gnus-newsgroup-display)))
4521            (t
4522             nil)))
4523       
4524     (gnus-summary-setup-default-charset)
4525
4526     ;; Adjust and set lists of article marks.
4527     (when info
4528       (gnus-adjust-marked-articles info))
4529
4530     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4531     (when (gnus-virtual-group-p group)
4532       (setq cached gnus-newsgroup-cached))
4533
4534     (setq gnus-newsgroup-unreads
4535           (gnus-set-difference
4536            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4537            gnus-newsgroup-dormant))
4538
4539     (setq gnus-newsgroup-processable nil)
4540
4541     (gnus-update-read-articles group gnus-newsgroup-unreads)
4542
4543     (if (setq articles select-articles)
4544         (setq gnus-newsgroup-unselected
4545               (gnus-sorted-intersection
4546                gnus-newsgroup-unreads
4547                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4548       (setq articles (gnus-articles-to-read group read-all)))
4549
4550     (cond
4551      ((null articles)
4552       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4553       'quit)
4554      ((eq articles 0) nil)
4555      (t
4556       ;; Init the dependencies hash table.
4557       (setq gnus-newsgroup-dependencies
4558             (gnus-make-hashtable (length articles)))
4559       (gnus-set-global-variables)
4560       ;; Retrieve the headers and read them in.
4561       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4562
4563       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4564       (when cached
4565         (setq gnus-newsgroup-cached cached))
4566
4567       ;; Suppress duplicates?
4568       (when gnus-suppress-duplicates
4569         (gnus-dup-suppress-articles))
4570
4571       ;; Set the initial limit.
4572       (setq gnus-newsgroup-limit (copy-sequence articles))
4573       ;; Remove canceled articles from the list of unread articles.
4574       (setq gnus-newsgroup-unreads
4575             (gnus-set-sorted-intersection
4576              gnus-newsgroup-unreads
4577              (setq fetched-articles
4578                    (mapcar (lambda (headers) (mail-header-number headers))
4579                            gnus-newsgroup-headers))))
4580       ;; Removed marked articles that do not exist.
4581       (gnus-update-missing-marks
4582        (gnus-sorted-complement fetched-articles articles))
4583       ;; We might want to build some more threads first.
4584       (when (and gnus-fetch-old-headers
4585                  (eq gnus-headers-retrieved-by 'nov))
4586         (if (eq gnus-fetch-old-headers 'invisible)
4587             (gnus-build-all-threads)
4588           (gnus-build-old-threads)))
4589       ;; Let the Gnus agent mark articles as read.
4590       (when gnus-agent
4591         (gnus-agent-get-undownloaded-list))
4592       ;; Remove list identifiers from subject
4593       (when gnus-list-identifiers
4594         (gnus-summary-remove-list-identifiers))
4595       ;; Check whether auto-expire is to be done in this group.
4596       (setq gnus-newsgroup-auto-expire
4597             (gnus-group-auto-expirable-p group))
4598       ;; Set up the article buffer now, if necessary.
4599       (unless gnus-single-article-buffer
4600         (gnus-article-setup-buffer))
4601       ;; First and last article in this newsgroup.
4602       (when gnus-newsgroup-headers
4603         (setq gnus-newsgroup-begin
4604               (mail-header-number (car gnus-newsgroup-headers))
4605               gnus-newsgroup-end
4606               (mail-header-number
4607                (gnus-last-element gnus-newsgroup-headers))))
4608       ;; GROUP is successfully selected.
4609       (or gnus-newsgroup-headers t)))))
4610
4611 (defun gnus-summary-display-make-predicate (display)
4612   (require 'gnus-agent)
4613   (when (= (length display) 1)
4614     (setq display (car display)))
4615   (unless gnus-summary-display-cache
4616     (dolist (elem gnus-article-mark-lists)
4617         (push (cons (cdr elem)
4618                     (gnus-byte-compile
4619                      `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4620               gnus-summary-display-cache)))
4621   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4622     (gnus-get-predicate display)))
4623
4624 ;; Uses the dynamically bound `number' variable.
4625 (defvar number)
4626 (defun gnus-article-marked-p (type &optional article)
4627   (let ((article (or article number)))
4628     (cond
4629      ((eq type 'tick)
4630       (memq article gnus-newsgroup-marked))
4631      ((eq type 'unsend)
4632       (memq article gnus-newsgroup-unsendable))
4633      ((eq type 'undownload)
4634       (memq article gnus-newsgroup-undownloaded))
4635      ((eq type 'download)
4636       (memq article gnus-newsgroup-downloadable))
4637      ((eq type 'unread)
4638       (memq article gnus-newsgroup-unreads))
4639      ((eq type 'read)
4640       (memq article gnus-newsgroup-reads))
4641      ((eq type 'dormant)
4642       (memq article gnus-newsgroup-dormant) )
4643      ((eq type 'expire)
4644       (memq article gnus-newsgroup-expirable))
4645      ((eq type 'reply)
4646       (memq article gnus-newsgroup-replied))
4647      ((eq type 'killed)
4648       (memq article gnus-newsgroup-killed))
4649      ((eq type 'bookmark)
4650       (assq article gnus-newsgroup-bookmarks))
4651      ((eq type 'score)
4652       (assq article gnus-newsgroup-scored))
4653      ((eq type 'save)
4654       (memq article gnus-newsgroup-saved))
4655      ((eq type 'cache)
4656       (memq article gnus-newsgroup-cached))
4657      ((eq type 'forward)
4658       (memq article gnus-newsgroup-forwarded))
4659      ((eq type 'recent)
4660       (memq article gnus-newsgroup-recent))
4661      (t t))))
4662
4663 (defun gnus-articles-to-read (group &optional read-all)
4664   "Find out what articles the user wants to read."
4665   (let* ((articles
4666           ;; Select all articles if `read-all' is non-nil, or if there
4667           ;; are no unread articles.
4668           (if (or read-all
4669                   (and (zerop (length gnus-newsgroup-marked))
4670                        (zerop (length gnus-newsgroup-unreads)))
4671                   gnus-newsgroup-display)
4672               ;; We want to select the headers for all the articles in
4673               ;; the group, so we select either all the active
4674               ;; articles in the group, or (if that's nil), the
4675               ;; articles in the cache.
4676               (or
4677                (gnus-uncompress-range (gnus-active group))
4678                (gnus-cache-articles-in-group group))
4679             ;; Select only the "normal" subset of articles.
4680             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4681                           (copy-sequence gnus-newsgroup-unreads))
4682                   '<)))
4683          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4684          (scored (length scored-list))
4685          (number (length articles))
4686          (marked (+ (length gnus-newsgroup-marked)
4687                     (length gnus-newsgroup-dormant)))
4688          (select
4689           (cond
4690            ((numberp read-all)
4691             read-all)
4692            (t
4693             (condition-case ()
4694                 (cond
4695                  ((and (or (<= scored marked) (= scored number))
4696                        (numberp gnus-large-newsgroup)
4697                        (> number gnus-large-newsgroup))
4698                   (let ((input
4699                          (read-string
4700                           (format
4701                            "How many articles from %s (default %d): "
4702                            (gnus-limit-string
4703                             (gnus-group-decoded-name gnus-newsgroup-name)
4704                             35)
4705                            number))))
4706                     (if (string-match "^[ \t]*$" input) number input)))
4707                  ((and (> scored marked) (< scored number)
4708                        (> (- scored number) 20))
4709                   (let ((input
4710                          (read-string
4711                           (format "%s %s (%d scored, %d total): "
4712                                   "How many articles from"
4713                                   (gnus-group-decoded-name group)
4714                                   scored number))))
4715                     (if (string-match "^[ \t]*$" input)
4716                         number input)))
4717                  (t number))
4718               (quit
4719                (message "Quit getting the articles to read")
4720                nil))))))
4721     (setq select (if (stringp select) (string-to-number select) select))
4722     (if (or (null select) (zerop select))
4723         select
4724       (if (and (not (zerop scored)) (<= (abs select) scored))
4725           (progn
4726             (setq articles (sort scored-list '<))
4727             (setq number (length articles)))
4728         (setq articles (copy-sequence articles)))
4729
4730       (when (< (abs select) number)
4731         (if (< select 0)
4732             ;; Select the N oldest articles.
4733             (setcdr (nthcdr (1- (abs select)) articles) nil)
4734           ;; Select the N most recent articles.
4735           (setq articles (nthcdr (- number select) articles))))
4736       (setq gnus-newsgroup-unselected
4737             (gnus-sorted-intersection
4738              gnus-newsgroup-unreads
4739              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4740       (when gnus-alter-articles-to-read-function
4741         (setq gnus-newsgroup-unreads
4742               (sort
4743                (funcall gnus-alter-articles-to-read-function
4744                         gnus-newsgroup-name gnus-newsgroup-unreads)
4745                '<)))
4746       articles)))
4747
4748 (defun gnus-killed-articles (killed articles)
4749   (let (out)
4750     (while articles
4751       (when (inline (gnus-member-of-range (car articles) killed))
4752         (push (car articles) out))
4753       (setq articles (cdr articles)))
4754     out))
4755
4756 (defun gnus-uncompress-marks (marks)
4757   "Uncompress the mark ranges in MARKS."
4758   (let ((uncompressed '(score bookmark))
4759         out)
4760     (while marks
4761       (if (memq (caar marks) uncompressed)
4762           (push (car marks) out)
4763         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4764       (setq marks (cdr marks)))
4765     out))
4766
4767 (defun gnus-adjust-marked-articles (info)
4768   "Set all article lists and remove all marks that are no longer valid."
4769   (let* ((marked-lists (gnus-info-marks info))
4770          (active (gnus-active (gnus-info-group info)))
4771          (min (car active))
4772          (max (cdr active))
4773          (types gnus-article-mark-lists)
4774          (uncompressed '(score bookmark killed))
4775          marks var articles article mark)
4776
4777     (while marked-lists
4778       (setq marks (pop marked-lists))
4779       (set (setq var (intern (format "gnus-newsgroup-%s"
4780                                      (car (rassq (setq mark (car marks))
4781                                                  types)))))
4782            (if (memq (car marks) uncompressed) (cdr marks)
4783              (gnus-uncompress-range (cdr marks))))
4784
4785       (setq articles (symbol-value var))
4786
4787       ;; All articles have to be subsets of the active articles.
4788       (cond
4789        ;; Adjust "simple" lists.
4790        ((memq mark '(tick dormant expire reply save))
4791         (while articles
4792           (when (or (< (setq article (pop articles)) min) (> article max))
4793             (set var (delq article (symbol-value var))))))
4794        ;; Adjust assocs.
4795        ((memq mark uncompressed)
4796         (when (not (listp (cdr (symbol-value var))))
4797           (set var (list (symbol-value var))))
4798         (when (not (listp (cdr articles)))
4799           (setq articles (list articles)))
4800         (while articles
4801           (when (or (not (consp (setq article (pop articles))))
4802                     (< (car article) min)
4803                     (> (car article) max))
4804             (set var (delq article (symbol-value var))))))))))
4805
4806 (defun gnus-update-missing-marks (missing)
4807   "Go through the list of MISSING articles and remove them from the mark lists."
4808   (when missing
4809     (let ((types gnus-article-mark-lists)
4810           var m)
4811       ;; Go through all types.
4812       (while types
4813         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4814         (when (symbol-value var)
4815          ;; This list has articles.  So we delete all missing articles
4816           ;; from it.
4817           (setq m missing)
4818           (while m
4819             (set var (delq (pop m) (symbol-value var)))))))))
4820
4821 (defun gnus-update-marks ()
4822   "Enter the various lists of marked articles into the newsgroup info list."
4823   (let ((types gnus-article-mark-lists)
4824         (info (gnus-get-info gnus-newsgroup-name))
4825         (uncompressed '(score bookmark killed))
4826         type list newmarked symbol delta-marks)
4827     (when info
4828       ;; Add all marks lists to the list of marks lists.
4829       (while (setq type (pop types))
4830         (setq list (symbol-value
4831                     (setq symbol
4832                           (intern (format "gnus-newsgroup-%s"
4833                                           (car type))))))
4834
4835         (when list
4836           ;; Get rid of the entries of the articles that have the
4837           ;; default score.
4838           (when (and (eq (cdr type) 'score)
4839                      gnus-save-score
4840                      list)
4841             (let* ((arts list)
4842                    (prev (cons nil list))
4843                    (all prev))
4844               (while arts
4845                 (if (or (not (consp (car arts)))
4846                         (= (cdar arts) gnus-summary-default-score))
4847                     (setcdr prev (cdr arts))
4848                   (setq prev arts))
4849                 (setq arts (cdr arts)))
4850               (setq list (cdr all)))))
4851
4852         (unless (memq (cdr type) uncompressed)
4853           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4854
4855         (when (gnus-check-backend-function
4856                'request-set-mark gnus-newsgroup-name)
4857           ;; propagate flags to server, with the following exceptions:
4858           ;; uncompressed:s are not proper flags (they are cons cells)
4859           ;; cache is a internal gnus flag
4860           ;; download are local to one gnus installation (well)
4861           ;; unsend are for nndraft groups only
4862           ;; xxx: generality of this?  this suits nnimap anyway
4863           (unless (memq (cdr type) (append '(cache download unsend)
4864                                            uncompressed))
4865             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4866                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4867                    (add (gnus-remove-from-range
4868                          (gnus-copy-sequence list) old)))
4869               (when add
4870                 (push (list add 'add (list (cdr type))) delta-marks))
4871               (when del
4872                 (push (list del 'del (list (cdr type))) delta-marks)))))
4873
4874         (when list
4875           (push (cons (cdr type) list) newmarked)))
4876
4877       (when delta-marks
4878         (unless (gnus-check-group gnus-newsgroup-name)
4879           (error "Can't open server for %s" gnus-newsgroup-name))
4880         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4881
4882       ;; Enter these new marks into the info of the group.
4883       (if (nthcdr 3 info)
4884           (setcar (nthcdr 3 info) newmarked)
4885         ;; Add the marks lists to the end of the info.
4886         (when newmarked
4887           (setcdr (nthcdr 2 info) (list newmarked))))
4888
4889       ;; Cut off the end of the info if there's nothing else there.
4890       (let ((i 5))
4891         (while (and (> i 2)
4892                     (not (nth i info)))
4893           (when (nthcdr (decf i) info)
4894             (setcdr (nthcdr i info) nil)))))))
4895
4896 (defun gnus-set-mode-line (where)
4897   "Set the mode line of the article or summary buffers.
4898 If WHERE is `summary', the summary mode line format will be used."
4899   ;; Is this mode line one we keep updated?
4900   (when (and (memq where gnus-updated-mode-lines)
4901              (symbol-value
4902               (intern (format "gnus-%s-mode-line-format-spec" where))))
4903     (let (mode-string)
4904       (save-excursion
4905         ;; We evaluate this in the summary buffer since these
4906         ;; variables are buffer-local to that buffer.
4907         (set-buffer gnus-summary-buffer)
4908        ;; We bind all these variables that are used in the `eval' form
4909         ;; below.
4910         (let* ((mformat (symbol-value
4911                          (intern
4912                           (format "gnus-%s-mode-line-format-spec" where))))
4913                (gnus-tmp-group-name (gnus-group-decoded-name
4914                                      gnus-newsgroup-name))
4915                (gnus-tmp-article-number (or gnus-current-article 0))
4916                (gnus-tmp-unread gnus-newsgroup-unreads)
4917                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4918                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4919                (gnus-tmp-unread-and-unselected
4920                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4921                             (zerop gnus-tmp-unselected))
4922                        "")
4923                       ((zerop gnus-tmp-unselected)
4924                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4925                       (t (format "{%d(+%d) more}"
4926                                  gnus-tmp-unread-and-unticked
4927                                  gnus-tmp-unselected))))
4928                (gnus-tmp-subject
4929                 (if (and gnus-current-headers
4930                          (vectorp gnus-current-headers))
4931                     (gnus-mode-string-quote
4932                      (mail-header-subject gnus-current-headers))
4933                   ""))
4934                bufname-length max-len
4935                gnus-tmp-header) ;; passed as argument to any user-format-funcs
4936           (setq mode-string (eval mformat))
4937           (setq bufname-length (if (string-match "%b" mode-string)
4938                                    (- (length
4939                                        (buffer-name
4940                                         (if (eq where 'summary)
4941                                             nil
4942                                           (get-buffer gnus-article-buffer))))
4943                                       2)
4944                                  0))
4945           (setq max-len (max 4 (if gnus-mode-non-string-length
4946                                    (- (window-width)
4947                                       gnus-mode-non-string-length
4948                                       bufname-length)
4949                                  (length mode-string))))
4950           ;; We might have to chop a bit of the string off...
4951           (when (> (length mode-string) max-len)
4952             (setq mode-string
4953                   (concat (truncate-string-to-width mode-string (- max-len 3))
4954                           "...")))
4955           ;; Pad the mode string a bit.
4956           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4957       ;; Update the mode line.
4958       (setq mode-line-buffer-identification
4959             (gnus-mode-line-buffer-identification (list mode-string)))
4960       (set-buffer-modified-p t))))
4961
4962 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4963   "Go through the HEADERS list and add all Xrefs to a hash table.
4964 The resulting hash table is returned, or nil if no Xrefs were found."
4965   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4966          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4967          (xref-hashtb (gnus-make-hashtable))
4968          start group entry number xrefs header)
4969     (while headers
4970       (setq header (pop headers))
4971       (when (and (setq xrefs (mail-header-xref header))
4972                  (not (memq (setq number (mail-header-number header))
4973                             unreads)))
4974         (setq start 0)
4975         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4976           (setq start (match-end 0))
4977           (setq group (if prefix
4978                           (concat prefix (substring xrefs (match-beginning 1)
4979                                                     (match-end 1)))
4980                         (substring xrefs (match-beginning 1) (match-end 1))))
4981           (setq number
4982                 (string-to-int (substring xrefs (match-beginning 2)
4983                                           (match-end 2))))
4984           (if (setq entry (gnus-gethash group xref-hashtb))
4985               (setcdr entry (cons number (cdr entry)))
4986             (gnus-sethash group (cons number nil) xref-hashtb)))))
4987     (and start xref-hashtb)))
4988
4989 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4990   "Look through all the headers and mark the Xrefs as read."
4991   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4992         name entry info xref-hashtb idlist method nth4)
4993     (save-excursion
4994       (set-buffer gnus-group-buffer)
4995       (when (setq xref-hashtb
4996                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4997         (mapatoms
4998          (lambda (group)
4999            (unless (string= from-newsgroup (setq name (symbol-name group)))
5000              (setq idlist (symbol-value group))
5001              ;; Dead groups are not updated.
5002              (and (prog1
5003                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5004                             info (nth 2 entry))
5005                     (when (stringp (setq nth4 (gnus-info-method info)))
5006                       (setq nth4 (gnus-server-to-method nth4))))
5007                   ;; Only do the xrefs if the group has the same
5008                   ;; select method as the group we have just read.
5009                   (or (gnus-methods-equal-p
5010                        nth4 (gnus-find-method-for-group from-newsgroup))
5011                       virtual
5012                       (equal nth4 (setq method (gnus-find-method-for-group
5013                                                 from-newsgroup)))
5014                       (and (equal (car nth4) (car method))
5015                            (equal (nth 1 nth4) (nth 1 method))))
5016                   gnus-use-cross-reference
5017                   (or (not (eq gnus-use-cross-reference t))
5018                       virtual
5019                       ;; Only do cross-references on subscribed
5020                       ;; groups, if that is what is wanted.
5021                       (<= (gnus-info-level info) gnus-level-subscribed))
5022                   (gnus-group-make-articles-read name idlist))))
5023          xref-hashtb)))))
5024
5025 (defun gnus-compute-read-articles (group articles)
5026   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5027          (info (nth 2 entry))
5028          (active (gnus-active group))
5029          ninfo)
5030     (when entry
5031       ;; First peel off all invalid article numbers.
5032       (when active
5033         (let ((ids articles)
5034               id first)
5035           (while (setq id (pop ids))
5036             (when (and first (> id (cdr active)))
5037               ;; We'll end up in this situation in one particular
5038               ;; obscure situation.  If you re-scan a group and get
5039               ;; a new article that is cross-posted to a different
5040               ;; group that has not been re-scanned, you might get
5041               ;; crossposted article that has a higher number than
5042               ;; Gnus believes possible.  So we re-activate this
5043               ;; group as well.  This might mean doing the
5044               ;; crossposting thingy will *increase* the number
5045               ;; of articles in some groups.  Tsk, tsk.
5046               (setq active (or (gnus-activate-group group) active)))
5047             (when (or (> id (cdr active))
5048                       (< id (car active)))
5049               (setq articles (delq id articles))))))
5050       ;; If the read list is nil, we init it.
5051       (if (and active
5052                (null (gnus-info-read info))
5053                (> (car active) 1))
5054           (setq ninfo (cons 1 (1- (car active))))
5055         (setq ninfo (gnus-info-read info)))
5056       ;; Then we add the read articles to the range.
5057       (gnus-add-to-range
5058        ninfo (setq articles (sort articles '<))))))
5059
5060 (defun gnus-group-make-articles-read (group articles)
5061   "Update the info of GROUP to say that ARTICLES are read."
5062   (let* ((num 0)
5063          (entry (gnus-gethash group gnus-newsrc-hashtb))
5064          (info (nth 2 entry))
5065          (active (gnus-active group))
5066          range)
5067     (when entry
5068       (setq range (gnus-compute-read-articles group articles))
5069       (save-excursion
5070         (set-buffer gnus-group-buffer)
5071         (gnus-undo-register
5072           `(progn
5073              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5074              (gnus-info-set-read ',info ',(gnus-info-read info))
5075              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5076              (gnus-group-update-group ,group t))))
5077       ;; Add the read articles to the range.
5078       (gnus-info-set-read info range)
5079       (gnus-request-set-mark group (list (list range 'add '(read))))
5080       ;; Then we have to re-compute how many unread
5081       ;; articles there are in this group.
5082       (when active
5083         (cond
5084          ((not range)
5085           (setq num (- (1+ (cdr active)) (car active))))
5086          ((not (listp (cdr range)))
5087           (setq num (- (cdr active) (- (1+ (cdr range))
5088                                        (car range)))))
5089          (t
5090           (while range
5091             (if (numberp (car range))
5092                 (setq num (1+ num))
5093               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5094             (setq range (cdr range)))
5095           (setq num (- (cdr active) num))))
5096         ;; Update the number of unread articles.
5097         (setcar entry num)
5098         ;; Update the group buffer.
5099         (gnus-group-update-group group t)))))
5100
5101 (defvar gnus-newsgroup-none-id 0)
5102
5103 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5104   (let ((cur nntp-server-buffer)
5105         (dependencies
5106          (or dependencies
5107              (save-excursion (set-buffer gnus-summary-buffer)
5108                              gnus-newsgroup-dependencies)))
5109         headers id end ref
5110         (mail-parse-charset gnus-newsgroup-charset)
5111         (mail-parse-ignored-charsets
5112          (save-excursion (condition-case nil
5113                              (set-buffer gnus-summary-buffer)
5114                            (error))
5115                          gnus-newsgroup-ignored-charsets)))
5116     (save-excursion
5117       (set-buffer nntp-server-buffer)
5118       ;; Translate all TAB characters into SPACE characters.
5119       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5120       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5121       (gnus-run-hooks 'gnus-parse-headers-hook)
5122       (let ((case-fold-search t)
5123             in-reply-to header p lines chars)
5124         (goto-char (point-min))
5125         ;; Search to the beginning of the next header.  Error messages
5126         ;; do not begin with 2 or 3.
5127         (while (re-search-forward "^[23][0-9]+ " nil t)
5128           (setq id nil
5129                 ref nil)
5130           ;; This implementation of this function, with nine
5131           ;; search-forwards instead of the one re-search-forward and
5132           ;; a case (which basically was the old function) is actually
5133           ;; about twice as fast, even though it looks messier.  You
5134           ;; can't have everything, I guess.  Speed and elegance
5135           ;; doesn't always go hand in hand.
5136           (setq
5137            header
5138            (vector
5139             ;; Number.
5140             (prog1
5141                 (read cur)
5142               (end-of-line)
5143               (setq p (point))
5144               (narrow-to-region (point)
5145                                 (or (and (search-forward "\n.\n" nil t)
5146                                          (- (point) 2))
5147                                     (point))))
5148             ;; Subject.
5149             (progn
5150               (goto-char p)
5151               (if (search-forward "\nsubject:" nil t)
5152                   (funcall gnus-decode-encoded-word-function
5153                            (nnheader-header-value))
5154                 "(none)"))
5155             ;; From.
5156             (progn
5157               (goto-char p)
5158               (if (search-forward "\nfrom:" nil t)
5159                   (funcall gnus-decode-encoded-word-function
5160                            (nnheader-header-value))
5161                 "(nobody)"))
5162             ;; Date.
5163             (progn
5164               (goto-char p)
5165               (if (search-forward "\ndate:" nil t)
5166                   (nnheader-header-value) ""))
5167             ;; Message-ID.
5168             (progn
5169               (goto-char p)
5170               (setq id (if (re-search-forward
5171                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5172                       ;; We do it this way to make sure the Message-ID
5173                            ;; is (somewhat) syntactically valid.
5174                            (buffer-substring (match-beginning 1)
5175                                              (match-end 1))
5176                        ;; If there was no message-id, we just fake one
5177                          ;; to make subsequent routines simpler.
5178                          (nnheader-generate-fake-message-id))))
5179             ;; References.
5180             (progn
5181               (goto-char p)
5182               (if (search-forward "\nreferences:" nil t)
5183                   (progn
5184                     (setq end (point))
5185                     (prog1
5186                         (nnheader-header-value)
5187                       (setq ref
5188                             (buffer-substring
5189                              (progn
5190                                (end-of-line)
5191                                (search-backward ">" end t)
5192                                (1+ (point)))
5193                              (progn
5194                                (search-backward "<" end t)
5195                                (point))))))
5196             ;; Get the references from the in-reply-to header if there
5197                 ;; were no references and the in-reply-to header looks
5198                 ;; promising.
5199                 (if (and (search-forward "\nin-reply-to:" nil t)
5200                          (setq in-reply-to (nnheader-header-value))
5201                          (string-match "<[^>]+>" in-reply-to))
5202                     (let (ref2)
5203                       (setq ref (substring in-reply-to (match-beginning 0)
5204                                            (match-end 0)))
5205                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5206                         (setq ref2 (substring in-reply-to (match-beginning 0)
5207                                               (match-end 0)))
5208                         (when (> (length ref2) (length ref))
5209                           (setq ref ref2)))
5210                       ref)
5211                   (setq ref nil))))
5212             ;; Chars.
5213             (progn
5214               (goto-char p)
5215               (if (search-forward "\nchars: " nil t)
5216                   (if (numberp (setq chars (ignore-errors (read cur))))
5217                       chars -1)
5218                 -1))
5219             ;; Lines.
5220             (progn
5221               (goto-char p)
5222               (if (search-forward "\nlines: " nil t)
5223                   (if (numberp (setq lines (ignore-errors (read cur))))
5224                       lines -1)
5225                 -1))
5226             ;; Xref.
5227             (progn
5228               (goto-char p)
5229               (and (search-forward "\nxref:" nil t)
5230                    (nnheader-header-value)))
5231             ;; Extra.
5232             (when gnus-extra-headers
5233               (let ((extra gnus-extra-headers)
5234                     out)
5235                 (while extra
5236                   (goto-char p)
5237                   (when (search-forward
5238                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5239                     (push (cons (car extra) (nnheader-header-value))
5240                           out))
5241                   (pop extra))
5242                 out))))
5243           (when (equal id ref)
5244             (setq ref nil))
5245
5246           (when gnus-alter-header-function
5247             (funcall gnus-alter-header-function header)
5248             (setq id (mail-header-id header)
5249                   ref (gnus-parent-id (mail-header-references header))))
5250
5251           (when (setq header
5252                       (gnus-dependencies-add-header
5253                        header dependencies force-new))
5254             (push header headers))
5255           (goto-char (point-max))
5256           (widen))
5257         (nreverse headers)))))
5258
5259 ;; Goes through the xover lines and returns a list of vectors
5260 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5261                                                   force-new dependencies
5262                                                   group also-fetch-heads)
5263   "Parse the news overview data in the server buffer.
5264 Return a list of headers that match SEQUENCE (see
5265 `nntp-retrieve-headers')."
5266   ;; Get the Xref when the users reads the articles since most/some
5267   ;; NNTP servers do not include Xrefs when using XOVER.
5268   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5269   (let ((mail-parse-charset gnus-newsgroup-charset)
5270         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5271         (cur nntp-server-buffer)
5272         (dependencies (or dependencies gnus-newsgroup-dependencies))
5273         (allp (cond
5274                ((eq gnus-read-all-available-headers t)
5275                 t)
5276                ((stringp gnus-read-all-available-headers)
5277                 (string-match gnus-read-all-available-headers group))
5278                (t
5279                 nil)))
5280         number headers header)
5281     (save-excursion
5282       (set-buffer nntp-server-buffer)
5283       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5284       ;; Allow the user to mangle the headers before parsing them.
5285       (gnus-run-hooks 'gnus-parse-headers-hook)
5286       (goto-char (point-min))
5287       (while (not (eobp))
5288         (condition-case ()
5289             (while (and (or sequence allp)
5290                         (not (eobp)))
5291               (setq number (read cur))
5292               (when (not allp)
5293                 (while (and sequence
5294                             (< (car sequence) number))
5295                   (setq sequence (cdr sequence))))
5296               (when (and (or allp
5297                              (and sequence
5298                                   (eq number (car sequence))))
5299                          (progn
5300                            (setq sequence (cdr sequence))
5301                            (setq header (inline
5302                                           (gnus-nov-parse-line
5303                                            number dependencies force-new)))))
5304                 (push header headers))
5305               (forward-line 1))
5306           (error
5307            (gnus-error 4 "Strange nov line (%d)"
5308                        (count-lines (point-min) (point)))))
5309         (forward-line 1))
5310       ;; A common bug in inn is that if you have posted an article and
5311       ;; then retrieves the active file, it will answer correctly --
5312       ;; the new article is included.  However, a NOV entry for the
5313       ;; article may not have been generated yet, so this may fail.
5314       ;; We work around this problem by retrieving the last few
5315       ;; headers using HEAD.
5316       (if (or (not also-fetch-heads)
5317               (not sequence))
5318           ;; We (probably) got all the headers.
5319           (nreverse headers)
5320         (let ((gnus-nov-is-evil t))
5321           (nconc
5322            (nreverse headers)
5323            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5324              (gnus-get-newsgroup-headers))))))))
5325
5326 (defun gnus-article-get-xrefs ()
5327   "Fill in the Xref value in `gnus-current-headers', if necessary.
5328 This is meant to be called in `gnus-article-internal-prepare-hook'."
5329   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5330                                  gnus-current-headers)))
5331     (or (not gnus-use-cross-reference)
5332         (not headers)
5333         (and (mail-header-xref headers)
5334              (not (string= (mail-header-xref headers) "")))
5335         (let ((case-fold-search t)
5336               xref)
5337           (save-restriction
5338             (nnheader-narrow-to-headers)
5339             (goto-char (point-min))
5340             (when (or (and (not (eobp))
5341                            (eq (downcase (char-after)) ?x)
5342                            (looking-at "Xref:"))
5343                       (search-forward "\nXref:" nil t))
5344               (goto-char (1+ (match-end 0)))
5345               (setq xref (buffer-substring (point)
5346                                            (progn (end-of-line) (point))))
5347               (mail-header-set-xref headers xref)))))))
5348
5349 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5350   "Find article ID and insert the summary line for that article.
5351 OLD-HEADER can either be a header or a line number to insert
5352 the subject line on."
5353   (let* ((line (and (numberp old-header) old-header))
5354          (old-header (and (vectorp old-header) old-header))
5355          (header (cond ((and old-header use-old-header)
5356                         old-header)
5357                        ((and (numberp id)
5358                              (gnus-number-to-header id))
5359                         (gnus-number-to-header id))
5360                        (t
5361                         (gnus-read-header id))))
5362          (number (and (numberp id) id))
5363          d)
5364     (when header
5365       ;; Rebuild the thread that this article is part of and go to the
5366       ;; article we have fetched.
5367       (when (and (not gnus-show-threads)
5368                  old-header)
5369         (when (and number
5370                    (setq d (gnus-data-find (mail-header-number old-header))))
5371           (goto-char (gnus-data-pos d))
5372           (gnus-data-remove
5373            number
5374            (- (gnus-point-at-bol)
5375               (prog1
5376                   (1+ (gnus-point-at-eol))
5377                 (gnus-delete-line))))))
5378       (when old-header
5379         (mail-header-set-number header (mail-header-number old-header)))
5380       (setq gnus-newsgroup-sparse
5381             (delq (setq number (mail-header-number header))
5382                   gnus-newsgroup-sparse))
5383       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5384       (push number gnus-newsgroup-limit)
5385       (gnus-rebuild-thread (mail-header-id header) line)
5386       (gnus-summary-goto-subject number nil t))
5387     (when (and (numberp number)
5388                (> number 0))
5389       ;; We have to update the boundaries even if we can't fetch the
5390       ;; article if ID is a number -- so that the next `P' or `N'
5391       ;; command will fetch the previous (or next) article even
5392       ;; if the one we tried to fetch this time has been canceled.
5393       (when (> number gnus-newsgroup-end)
5394         (setq gnus-newsgroup-end number))
5395       (when (< number gnus-newsgroup-begin)
5396         (setq gnus-newsgroup-begin number))
5397       (setq gnus-newsgroup-unselected
5398             (delq number gnus-newsgroup-unselected)))
5399     ;; Report back a success?
5400     (and header (mail-header-number header))))
5401
5402 ;;; Process/prefix in the summary buffer
5403
5404 (defun gnus-summary-work-articles (n)
5405   "Return a list of articles to be worked upon.
5406 The prefix argument, the list of process marked articles, and the
5407 current article will be taken into consideration."
5408   (save-excursion
5409     (set-buffer gnus-summary-buffer)
5410     (cond
5411      (n
5412       ;; A numerical prefix has been given.
5413       (setq n (prefix-numeric-value n))
5414       (let ((backward (< n 0))
5415             (n (abs (prefix-numeric-value n)))
5416             articles article)
5417         (save-excursion
5418           (while
5419               (and (> n 0)
5420                    (push (setq article (gnus-summary-article-number))
5421                          articles)
5422                    (if backward
5423                        (gnus-summary-find-prev nil article)
5424                      (gnus-summary-find-next nil article)))
5425             (decf n)))
5426         (nreverse articles)))
5427      ((and (gnus-region-active-p) (mark))
5428       (message "region active")
5429       ;; Work on the region between point and mark.
5430       (let ((max (max (point) (mark)))
5431             articles article)
5432         (save-excursion
5433           (goto-char (min (min (point) (mark))))
5434           (while
5435               (and
5436                (push (setq article (gnus-summary-article-number)) articles)
5437                (gnus-summary-find-next nil article)
5438                (< (point) max)))
5439           (nreverse articles))))
5440      (gnus-newsgroup-processable
5441       ;; There are process-marked articles present.
5442       ;; Save current state.
5443       (gnus-summary-save-process-mark)
5444       ;; Return the list.
5445       (reverse gnus-newsgroup-processable))
5446      (t
5447       ;; Just return the current article.
5448       (list (gnus-summary-article-number))))))
5449
5450 (defmacro gnus-summary-iterate (arg &rest forms)
5451   "Iterate over the process/prefixed articles and do FORMS.
5452 ARG is the interactive prefix given to the command.  FORMS will be
5453 executed with point over the summary line of the articles."
5454   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5455     `(let ((,articles (gnus-summary-work-articles ,arg)))
5456        (while ,articles
5457          (gnus-summary-goto-subject (car ,articles))
5458          ,@forms
5459          (pop ,articles)))))
5460
5461 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5462 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5463
5464 (defun gnus-summary-save-process-mark ()
5465   "Push the current set of process marked articles on the stack."
5466   (interactive)
5467   (push (copy-sequence gnus-newsgroup-processable)
5468         gnus-newsgroup-process-stack))
5469
5470 (defun gnus-summary-kill-process-mark ()
5471   "Push the current set of process marked articles on the stack and unmark."
5472   (interactive)
5473   (gnus-summary-save-process-mark)
5474   (gnus-summary-unmark-all-processable))
5475
5476 (defun gnus-summary-yank-process-mark ()
5477   "Pop the last process mark state off the stack and restore it."
5478   (interactive)
5479   (unless gnus-newsgroup-process-stack
5480     (error "Empty mark stack"))
5481   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5482
5483 (defun gnus-summary-process-mark-set (set)
5484   "Make SET into the current process marked articles."
5485   (gnus-summary-unmark-all-processable)
5486   (while set
5487     (gnus-summary-set-process-mark (pop set))))
5488
5489 ;;; Searching and stuff
5490
5491 (defun gnus-summary-search-group (&optional backward use-level)
5492   "Search for next unread newsgroup.
5493 If optional argument BACKWARD is non-nil, search backward instead."
5494   (save-excursion
5495     (set-buffer gnus-group-buffer)
5496     (when (gnus-group-search-forward
5497            backward nil (if use-level (gnus-group-group-level) nil))
5498       (gnus-group-group-name))))
5499
5500 (defun gnus-summary-best-group (&optional exclude-group)
5501   "Find the name of the best unread group.
5502 If EXCLUDE-GROUP, do not go to this group."
5503   (save-excursion
5504     (set-buffer gnus-group-buffer)
5505     (save-excursion
5506       (gnus-group-best-unread-group exclude-group))))
5507
5508 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5509   (if backward (gnus-summary-find-prev)
5510     (let* ((dummy (gnus-summary-article-intangible-p))
5511            (article (or article (gnus-summary-article-number)))
5512            (arts (gnus-data-find-list article))
5513            result)
5514       (when (and (not dummy)
5515                  (or (not gnus-summary-check-current)
5516                      (not unread)
5517                      (not (gnus-data-unread-p (car arts)))))
5518         (setq arts (cdr arts)))
5519       (when (setq result
5520                   (if unread
5521                       (progn
5522                         (while arts
5523                           (when (or (and undownloaded
5524                                          (eq gnus-undownloaded-mark
5525                                              (gnus-data-mark (car arts))))
5526                                     (gnus-data-unread-p (car arts)))
5527                             (setq result (car arts)
5528                                   arts nil))
5529                           (setq arts (cdr arts)))
5530                         result)
5531                     (car arts)))
5532         (goto-char (gnus-data-pos result))
5533         (gnus-data-number result)))))
5534
5535 (defun gnus-summary-find-prev (&optional unread article)
5536   (let* ((eobp (eobp))
5537          (article (or article (gnus-summary-article-number)))
5538          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5539          result)
5540     (when (and (not eobp)
5541                (or (not gnus-summary-check-current)
5542                    (not unread)
5543                    (not (gnus-data-unread-p (car arts)))))
5544       (setq arts (cdr arts)))
5545     (when (setq result
5546                 (if unread
5547                     (progn
5548                       (while arts
5549                         (when (gnus-data-unread-p (car arts))
5550                           (setq result (car arts)
5551                                 arts nil))
5552                         (setq arts (cdr arts)))
5553                       result)
5554                   (car arts)))
5555       (goto-char (gnus-data-pos result))
5556       (gnus-data-number result))))
5557
5558 (defun gnus-summary-find-subject (subject &optional unread backward article)
5559   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5560          (article (or article (gnus-summary-article-number)))
5561          (articles (gnus-data-list backward))
5562          (arts (gnus-data-find-list article articles))
5563          result)
5564     (when (or (not gnus-summary-check-current)
5565               (not unread)
5566               (not (gnus-data-unread-p (car arts))))
5567       (setq arts (cdr arts)))
5568     (while arts
5569       (and (or (not unread)
5570                (gnus-data-unread-p (car arts)))
5571            (vectorp (gnus-data-header (car arts)))
5572            (gnus-subject-equal
5573             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5574            (setq result (car arts)
5575                  arts nil))
5576       (setq arts (cdr arts)))
5577     (and result
5578          (goto-char (gnus-data-pos result))
5579          (gnus-data-number result))))
5580
5581 (defun gnus-summary-search-forward (&optional unread subject backward)
5582   "Search forward for an article.
5583 If UNREAD, look for unread articles.  If SUBJECT, look for
5584 articles with that subject.  If BACKWARD, search backward instead."
5585   (cond (subject (gnus-summary-find-subject subject unread backward))
5586         (backward (gnus-summary-find-prev unread))
5587         (t (gnus-summary-find-next unread))))
5588
5589 (defun gnus-recenter (&optional n)
5590   "Center point in window and redisplay frame.
5591 Also do horizontal recentering."
5592   (interactive "P")
5593   (when (and gnus-auto-center-summary
5594              (not (eq gnus-auto-center-summary 'vertical)))
5595     (gnus-horizontal-recenter))
5596   (recenter n))
5597
5598 (defun gnus-summary-recenter ()
5599   "Center point in the summary window.
5600 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5601 displayed, no centering will be performed."
5602   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5603 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5604   (interactive)
5605   (let* ((top (cond ((< (window-height) 4) 0)
5606                     ((< (window-height) 7) 1)
5607                     (t (if (numberp gnus-auto-center-summary)
5608                            gnus-auto-center-summary
5609                          2))))
5610          (height (1- (window-height)))
5611          (bottom (save-excursion (goto-char (point-max))
5612                                  (forward-line (- height))
5613                                  (point)))
5614          (window (get-buffer-window (current-buffer))))
5615     ;; The user has to want it.
5616     (when gnus-auto-center-summary
5617       (when (get-buffer-window gnus-article-buffer)
5618         ;; Only do recentering when the article buffer is displayed,
5619       ;; Set the window start to either `bottom', which is the biggest
5620         ;; possible valid number, or the second line from the top,
5621         ;; whichever is the least.
5622         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5623           (if (> bottom top-pos)
5624               ;; Keep the second line from the top visible
5625               (set-window-start window top-pos t)
5626             ;; Try to keep the bottom line visible; if it's partially
5627             ;; obscured, either scroll one more line to make it fully
5628             ;; visible, or revert to using TOP-POS.
5629             (save-excursion
5630               (goto-char (point-max))
5631               (forward-line -1)
5632               (let ((last-line-start (point)))
5633                 (goto-char bottom)
5634                 (set-window-start window (point) t)
5635                 (when (not (pos-visible-in-window-p last-line-start window))
5636                   (forward-line 1)
5637                   (set-window-start window (min (point) top-pos) t)))))))
5638       ;; Do horizontal recentering while we're at it.
5639       (when (and (get-buffer-window (current-buffer) t)
5640                  (not (eq gnus-auto-center-summary 'vertical)))
5641         (let ((selected (selected-window)))
5642           (select-window (get-buffer-window (current-buffer) t))
5643           (gnus-summary-position-point)
5644           (gnus-horizontal-recenter)
5645           (select-window selected))))))
5646
5647 (defun gnus-summary-jump-to-group (newsgroup)
5648   "Move point to NEWSGROUP in group mode buffer."
5649   ;; Keep update point of group mode buffer if visible.
5650   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5651       (save-window-excursion
5652         ;; Take care of tree window mode.
5653         (when (get-buffer-window gnus-group-buffer)
5654           (pop-to-buffer gnus-group-buffer))
5655         (gnus-group-jump-to-group newsgroup))
5656     (save-excursion
5657       ;; Take care of tree window mode.
5658       (if (get-buffer-window gnus-group-buffer)
5659           (pop-to-buffer gnus-group-buffer)
5660         (set-buffer gnus-group-buffer))
5661       (gnus-group-jump-to-group newsgroup))))
5662
5663 ;; This function returns a list of article numbers based on the
5664 ;; difference between the ranges of read articles in this group and
5665 ;; the range of active articles.
5666 (defun gnus-list-of-unread-articles (group)
5667   (let* ((read (gnus-info-read (gnus-get-info group)))
5668          (active (or (gnus-active group) (gnus-activate-group group)))
5669          (last (cdr active))
5670          first nlast unread)
5671     ;; If none are read, then all are unread.
5672     (if (not read)
5673         (setq first (car active))
5674       ;; If the range of read articles is a single range, then the
5675       ;; first unread article is the article after the last read
5676       ;; article.  Sounds logical, doesn't it?
5677       (if (and (not (listp (cdr read)))
5678                (or (< (car read) (car active))
5679                    (progn (setq read (list read))
5680                           nil)))
5681           (setq first (max (car active) (1+ (cdr read))))
5682         ;; `read' is a list of ranges.
5683         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5684                                   (caar read)))
5685                   1)
5686           (setq first (car active)))
5687         (while read
5688           (when first
5689             (while (< first nlast)
5690               (push first unread)
5691               (setq first (1+ first))))
5692           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5693           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5694           (setq read (cdr read)))))
5695     ;; And add the last unread articles.
5696     (while (<= first last)
5697       (push first unread)
5698       (setq first (1+ first)))
5699     ;; Return the list of unread articles.
5700     (delq 0 (nreverse unread))))
5701
5702 (defun gnus-list-of-read-articles (group)
5703   "Return a list of unread, unticked and non-dormant articles."
5704   (let* ((info (gnus-get-info group))
5705          (marked (gnus-info-marks info))
5706          (active (gnus-active group)))
5707     (and info active
5708          (gnus-set-difference
5709           (gnus-sorted-complement
5710            (gnus-uncompress-range active)
5711            (gnus-list-of-unread-articles group))
5712           (append
5713            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5714            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5715
5716 ;; Various summary commands
5717
5718 (defun gnus-summary-select-article-buffer ()
5719   "Reconfigure windows to show article buffer."
5720   (interactive)
5721   (if (not (gnus-buffer-live-p gnus-article-buffer))
5722       (error "There is no article buffer for this summary buffer")
5723     (gnus-configure-windows 'article)
5724     (select-window (get-buffer-window gnus-article-buffer))))
5725
5726 (defun gnus-summary-universal-argument (arg)
5727   "Perform any operation on all articles that are process/prefixed."
5728   (interactive "P")
5729   (let ((articles (gnus-summary-work-articles arg))
5730         func article)
5731     (if (eq
5732          (setq
5733           func
5734           (key-binding
5735            (read-key-sequence
5736             (substitute-command-keys
5737              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5738          'undefined)
5739         (gnus-error 1 "Undefined key")
5740       (save-excursion
5741         (while articles
5742           (gnus-summary-goto-subject (setq article (pop articles)))
5743           (let (gnus-newsgroup-processable)
5744             (command-execute func))
5745           (gnus-summary-remove-process-mark article)))))
5746   (gnus-summary-position-point))
5747
5748 (defun gnus-summary-toggle-truncation (&optional arg)
5749   "Toggle truncation of summary lines.
5750 With arg, turn line truncation on iff arg is positive."
5751   (interactive "P")
5752   (setq truncate-lines
5753         (if (null arg) (not truncate-lines)
5754           (> (prefix-numeric-value arg) 0)))
5755   (redraw-display))
5756
5757 (defun gnus-summary-reselect-current-group (&optional all rescan)
5758   "Exit and then reselect the current newsgroup.
5759 The prefix argument ALL means to select all articles."
5760   (interactive "P")
5761   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5762     (error "Ephemeral groups can't be reselected"))
5763   (let ((current-subject (gnus-summary-article-number))
5764         (group gnus-newsgroup-name))
5765     (setq gnus-newsgroup-begin nil)
5766     (gnus-summary-exit)
5767     ;; We have to adjust the point of group mode buffer because
5768     ;; point was moved to the next unread newsgroup by exiting.
5769     (gnus-summary-jump-to-group group)
5770     (when rescan
5771       (save-excursion
5772         (gnus-group-get-new-news-this-group 1)))
5773     (gnus-group-read-group all t)
5774     (gnus-summary-goto-subject current-subject nil t)))
5775
5776 (defun gnus-summary-rescan-group (&optional all)
5777   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5778   (interactive "P")
5779   (gnus-summary-reselect-current-group all t))
5780
5781 (defun gnus-summary-update-info (&optional non-destructive)
5782   (save-excursion
5783     (let ((group gnus-newsgroup-name))
5784       (when group
5785         (when gnus-newsgroup-kill-headers
5786           (setq gnus-newsgroup-killed
5787                 (gnus-compress-sequence
5788                  (nconc
5789                   (gnus-set-sorted-intersection
5790                    (gnus-uncompress-range gnus-newsgroup-killed)
5791                    (setq gnus-newsgroup-unselected
5792                          (sort gnus-newsgroup-unselected '<)))
5793                   (setq gnus-newsgroup-unreads
5794                         (sort gnus-newsgroup-unreads '<)))
5795                  t)))
5796         (unless (listp (cdr gnus-newsgroup-killed))
5797           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5798         (let ((headers gnus-newsgroup-headers))
5799           ;; Set the new ranges of read articles.
5800           (save-excursion
5801             (set-buffer gnus-group-buffer)
5802             (gnus-undo-force-boundary))
5803           (gnus-update-read-articles
5804            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5805           ;; Set the current article marks.
5806           (let ((gnus-newsgroup-scored
5807                  (if (and (not gnus-save-score)
5808                           (not non-destructive))
5809                      nil
5810                    gnus-newsgroup-scored)))
5811             (save-excursion
5812               (gnus-update-marks)))
5813           ;; Do the cross-ref thing.
5814           (when gnus-use-cross-reference
5815             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5816           ;; Do not switch windows but change the buffer to work.
5817           (set-buffer gnus-group-buffer)
5818           (unless (gnus-ephemeral-group-p group)
5819             (gnus-group-update-group group)))))))
5820
5821 (defun gnus-summary-save-newsrc (&optional force)
5822   "Save the current number of read/marked articles in the dribble buffer.
5823 The dribble buffer will then be saved.
5824 If FORCE (the prefix), also save the .newsrc file(s)."
5825   (interactive "P")
5826   (gnus-summary-update-info t)
5827   (if force
5828       (gnus-save-newsrc-file)
5829     (gnus-dribble-save)))
5830
5831 (defun gnus-summary-exit (&optional temporary)
5832   "Exit reading current newsgroup, and then return to group selection mode.
5833 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5834   (interactive)
5835   (gnus-set-global-variables)
5836   (when (gnus-buffer-live-p gnus-article-buffer)
5837     (save-excursion
5838       (set-buffer gnus-article-buffer)
5839       (mm-destroy-parts gnus-article-mime-handles)
5840       ;; Set it to nil for safety reason.
5841       (setq gnus-article-mime-handle-alist nil)
5842       (setq gnus-article-mime-handles nil)))
5843   (gnus-kill-save-kill-buffer)
5844   (gnus-async-halt-prefetch)
5845   (let* ((group gnus-newsgroup-name)
5846          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5847          (mode major-mode)
5848          (group-point nil)
5849          (buf (current-buffer)))
5850     (unless quit-config
5851       ;; Do adaptive scoring, and possibly save score files.
5852       (when gnus-newsgroup-adaptive
5853         (gnus-score-adaptive))
5854       (when gnus-use-scoring
5855         (gnus-score-save)))
5856     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5857     ;; If we have several article buffers, we kill them at exit.
5858     (unless gnus-single-article-buffer
5859       (gnus-kill-buffer gnus-original-article-buffer)
5860       (setq gnus-article-current nil))
5861     (when gnus-use-cache
5862       (gnus-cache-possibly-remove-articles)
5863       (gnus-cache-save-buffers))
5864     (gnus-async-prefetch-remove-group group)
5865     (when gnus-suppress-duplicates
5866       (gnus-dup-enter-articles))
5867     (when gnus-use-trees
5868       (gnus-tree-close group))
5869     (when gnus-use-cache
5870       (gnus-cache-write-active))
5871     ;; Remove entries for this group.
5872     (nnmail-purge-split-history (gnus-group-real-name group))
5873     ;; Make all changes in this group permanent.
5874     (unless quit-config
5875       (gnus-run-hooks 'gnus-exit-group-hook)
5876       (gnus-summary-update-info))
5877     (gnus-close-group group)
5878     ;; Make sure where we were, and go to next newsgroup.
5879     (set-buffer gnus-group-buffer)
5880     (unless quit-config
5881       (gnus-group-jump-to-group group))
5882     (gnus-run-hooks 'gnus-summary-exit-hook)
5883     (unless (or quit-config
5884                 ;; If this group has disappeared from the summary
5885                 ;; buffer, don't skip forwards.
5886                 (not (string= group (gnus-group-group-name))))
5887       (gnus-group-next-unread-group 1))
5888     (setq group-point (point))
5889     (if temporary
5890         nil                             ;Nothing to do.
5891       ;; If we have several article buffers, we kill them at exit.
5892       (unless gnus-single-article-buffer
5893         (gnus-kill-buffer gnus-article-buffer)
5894         (gnus-kill-buffer gnus-original-article-buffer)
5895         (setq gnus-article-current nil))
5896       (set-buffer buf)
5897       (if (not gnus-kill-summary-on-exit)
5898           (progn
5899             (gnus-deaden-summary)
5900             (setq mode nil))
5901        ;; We set all buffer-local variables to nil.  It is unclear why
5902         ;; this is needed, but if we don't, buffer-local variables are
5903         ;; not garbage-collected, it seems.  This would the lead to en
5904         ;; ever-growing Emacs.
5905         (gnus-summary-clear-local-variables)
5906         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5907           (gnus-summary-clear-local-variables))
5908         (when (get-buffer gnus-article-buffer)
5909           (bury-buffer gnus-article-buffer))
5910         ;; We clear the global counterparts of the buffer-local
5911         ;; variables as well, just to be on the safe side.
5912         (set-buffer gnus-group-buffer)
5913         (gnus-summary-clear-local-variables)
5914         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5915           (gnus-summary-clear-local-variables)))
5916       (setq gnus-current-select-method gnus-select-method)
5917       (pop-to-buffer gnus-group-buffer)
5918       (if (not quit-config)
5919           (progn
5920             (goto-char group-point)
5921             (gnus-configure-windows 'group 'force))
5922         (gnus-handle-ephemeral-exit quit-config))
5923       ;; Return to group mode buffer.
5924       (when (eq mode 'gnus-summary-mode)
5925         (gnus-kill-buffer buf))
5926       ;; Clear the current group name.
5927       (unless quit-config
5928         (setq gnus-newsgroup-name nil)))))
5929
5930 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5931 (defun gnus-summary-exit-no-update (&optional no-questions)
5932   "Quit reading current newsgroup without updating read article info."
5933   (interactive)
5934   (let* ((group gnus-newsgroup-name)
5935          (quit-config (gnus-group-quit-config group)))
5936     (when (or no-questions
5937               gnus-expert-user
5938               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5939       (gnus-async-halt-prefetch)
5940       (mapcar 'funcall
5941               (delq 'gnus-summary-expire-articles
5942                     (copy-sequence gnus-summary-prepare-exit-hook)))
5943       (when (gnus-buffer-live-p gnus-article-buffer)
5944         (save-excursion
5945           (set-buffer gnus-article-buffer)
5946           (mm-destroy-parts gnus-article-mime-handles)
5947           ;; Set it to nil for safety reason.
5948           (setq gnus-article-mime-handle-alist nil)
5949           (setq gnus-article-mime-handles nil)))
5950       ;; If we have several article buffers, we kill them at exit.
5951       (unless gnus-single-article-buffer
5952         (gnus-kill-buffer gnus-article-buffer)
5953         (gnus-kill-buffer gnus-original-article-buffer)
5954         (setq gnus-article-current nil))
5955       (if (not gnus-kill-summary-on-exit)
5956           (gnus-deaden-summary)
5957         (gnus-close-group group)
5958         (gnus-summary-clear-local-variables)
5959         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5960           (gnus-summary-clear-local-variables))
5961         (set-buffer gnus-group-buffer)
5962         (gnus-summary-clear-local-variables)
5963         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5964           (gnus-summary-clear-local-variables))
5965         (when (get-buffer gnus-summary-buffer)
5966           (kill-buffer gnus-summary-buffer)))
5967       (unless gnus-single-article-buffer
5968         (setq gnus-article-current nil))
5969       (when gnus-use-trees
5970         (gnus-tree-close group))
5971       (gnus-async-prefetch-remove-group group)
5972       (when (get-buffer gnus-article-buffer)
5973         (bury-buffer gnus-article-buffer))
5974       ;; Return to the group buffer.
5975       (gnus-configure-windows 'group 'force)
5976       ;; Clear the current group name.
5977       (setq gnus-newsgroup-name nil)
5978       (when (equal (gnus-group-group-name) group)
5979         (gnus-group-next-unread-group 1))
5980       (when quit-config
5981         (gnus-handle-ephemeral-exit quit-config)))))
5982
5983 (defun gnus-handle-ephemeral-exit (quit-config)
5984   "Handle movement when leaving an ephemeral group.
5985 The state which existed when entering the ephemeral is reset."
5986   (if (not (buffer-name (car quit-config)))
5987       (gnus-configure-windows 'group 'force)
5988     (set-buffer (car quit-config))
5989     (cond ((eq major-mode 'gnus-summary-mode)
5990            (gnus-set-global-variables))
5991           ((eq major-mode 'gnus-article-mode)
5992            (save-excursion
5993              ;; The `gnus-summary-buffer' variable may point
5994              ;; to the old summary buffer when using a single
5995              ;; article buffer.
5996              (unless (gnus-buffer-live-p gnus-summary-buffer)
5997                (set-buffer gnus-group-buffer))
5998              (set-buffer gnus-summary-buffer)
5999              (gnus-set-global-variables))))
6000     (if (or (eq (cdr quit-config) 'article)
6001             (eq (cdr quit-config) 'pick))
6002         (progn
6003           ;; The current article may be from the ephemeral group
6004           ;; thus it is best that we reload this article
6005           (gnus-summary-show-article)
6006           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6007               (gnus-configure-windows 'pick 'force)
6008             (gnus-configure-windows (cdr quit-config) 'force)))
6009       (gnus-configure-windows (cdr quit-config) 'force))
6010     (when (eq major-mode 'gnus-summary-mode)
6011       (gnus-summary-next-subject 1 nil t)
6012       (gnus-summary-recenter)
6013       (gnus-summary-position-point))))
6014
6015 ;;; Dead summaries.
6016
6017 (defvar gnus-dead-summary-mode-map nil)
6018
6019 (unless gnus-dead-summary-mode-map
6020   (setq gnus-dead-summary-mode-map (make-keymap))
6021   (suppress-keymap gnus-dead-summary-mode-map)
6022   (substitute-key-definition
6023    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6024   (let ((keys '("\C-d" "\r" "\177" [delete])))
6025     (while keys
6026       (define-key gnus-dead-summary-mode-map
6027         (pop keys) 'gnus-summary-wake-up-the-dead))))
6028
6029 (defvar gnus-dead-summary-mode nil
6030   "Minor mode for Gnus summary buffers.")
6031
6032 (defun gnus-dead-summary-mode (&optional arg)
6033   "Minor mode for Gnus summary buffers."
6034   (interactive "P")
6035   (when (eq major-mode 'gnus-summary-mode)
6036     (make-local-variable 'gnus-dead-summary-mode)
6037     (setq gnus-dead-summary-mode
6038           (if (null arg) (not gnus-dead-summary-mode)
6039             (> (prefix-numeric-value arg) 0)))
6040     (when gnus-dead-summary-mode
6041       (gnus-add-minor-mode
6042        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6043
6044 (defun gnus-deaden-summary ()
6045   "Make the current summary buffer into a dead summary buffer."
6046   ;; Kill any previous dead summary buffer.
6047   (when (and gnus-dead-summary
6048              (buffer-name gnus-dead-summary))
6049     (save-excursion
6050       (set-buffer gnus-dead-summary)
6051       (when gnus-dead-summary-mode
6052         (kill-buffer (current-buffer)))))
6053   ;; Make this the current dead summary.
6054   (setq gnus-dead-summary (current-buffer))
6055   (gnus-dead-summary-mode 1)
6056   (let ((name (buffer-name)))
6057     (when (string-match "Summary" name)
6058       (rename-buffer
6059        (concat (substring name 0 (match-beginning 0)) "Dead "
6060                (substring name (match-beginning 0)))
6061        t)
6062       (bury-buffer))))
6063
6064 (defun gnus-kill-or-deaden-summary (buffer)
6065   "Kill or deaden the summary BUFFER."
6066   (save-excursion
6067     (when (and (buffer-name buffer)
6068                (not gnus-single-article-buffer))
6069       (save-excursion
6070         (set-buffer buffer)
6071         (gnus-kill-buffer gnus-article-buffer)
6072         (gnus-kill-buffer gnus-original-article-buffer)))
6073     (cond (gnus-kill-summary-on-exit
6074            (when (and gnus-use-trees
6075                       (gnus-buffer-exists-p buffer))
6076              (save-excursion
6077                (set-buffer buffer)
6078                (gnus-tree-close gnus-newsgroup-name)))
6079            (gnus-kill-buffer buffer))
6080           ((gnus-buffer-exists-p buffer)
6081            (save-excursion
6082              (set-buffer buffer)
6083              (gnus-deaden-summary))))))
6084
6085 (defun gnus-summary-wake-up-the-dead (&rest args)
6086   "Wake up the dead summary buffer."
6087   (interactive)
6088   (gnus-dead-summary-mode -1)
6089   (let ((name (buffer-name)))
6090     (when (string-match "Dead " name)
6091       (rename-buffer
6092        (concat (substring name 0 (match-beginning 0))
6093                (substring name (match-end 0)))
6094        t)))
6095   (gnus-message 3 "This dead summary is now alive again"))
6096
6097 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6098 (defun gnus-summary-fetch-faq (&optional faq-dir)
6099   "Fetch the FAQ for the current group.
6100 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6101 in."
6102   (interactive
6103    (list
6104     (when current-prefix-arg
6105       (completing-read
6106        "Faq dir: " (and (listp gnus-group-faq-directory)
6107                         (mapcar (lambda (file) (list file))
6108                                 gnus-group-faq-directory))))))
6109   (let (gnus-faq-buffer)
6110     (when (setq gnus-faq-buffer
6111                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6112       (gnus-configure-windows 'summary-faq))))
6113
6114 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6115 (defun gnus-summary-describe-group (&optional force)
6116   "Describe the current newsgroup."
6117   (interactive "P")
6118   (gnus-group-describe-group force gnus-newsgroup-name))
6119
6120 (defun gnus-summary-describe-briefly ()
6121   "Describe summary mode commands briefly."
6122   (interactive)
6123   (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")))
6124
6125 ;; Walking around group mode buffer from summary mode.
6126
6127 (defun gnus-summary-next-group (&optional no-article target-group backward)
6128   "Exit current newsgroup and then select next unread newsgroup.
6129 If prefix argument NO-ARTICLE is non-nil, no article is selected
6130 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6131 previous group instead."
6132   (interactive "P")
6133   ;; Stop pre-fetching.
6134   (gnus-async-halt-prefetch)
6135   (let ((current-group gnus-newsgroup-name)
6136         (current-buffer (current-buffer))
6137         entered)
6138    ;; First we semi-exit this group to update Xrefs and all variables.
6139     ;; We can't do a real exit, because the window conf must remain
6140     ;; the same in case the user is prompted for info, and we don't
6141     ;; want the window conf to change before that...
6142     (gnus-summary-exit t)
6143     (while (not entered)
6144       ;; Then we find what group we are supposed to enter.
6145       (set-buffer gnus-group-buffer)
6146       (gnus-group-jump-to-group current-group)
6147       (setq target-group
6148             (or target-group
6149                 (if (eq gnus-keep-same-level 'best)
6150                     (gnus-summary-best-group gnus-newsgroup-name)
6151                   (gnus-summary-search-group backward gnus-keep-same-level))))
6152       (if (not target-group)
6153           ;; There are no further groups, so we return to the group
6154           ;; buffer.
6155           (progn
6156             (gnus-message 5 "Returning to the group buffer")
6157             (setq entered t)
6158             (when (gnus-buffer-live-p current-buffer)
6159               (set-buffer current-buffer)
6160               (gnus-summary-exit))
6161             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6162         ;; We try to enter the target group.
6163         (gnus-group-jump-to-group target-group)
6164         (let ((unreads (gnus-group-group-unread)))
6165           (if (and (or (eq t unreads)
6166                        (and unreads (not (zerop unreads))))
6167                    (gnus-summary-read-group
6168                     target-group nil no-article
6169                     (and (buffer-name current-buffer) current-buffer)
6170                     nil backward))
6171               (setq entered t)
6172             (setq current-group target-group
6173                   target-group nil)))))))
6174
6175 (defun gnus-summary-prev-group (&optional no-article)
6176   "Exit current newsgroup and then select previous unread newsgroup.
6177 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6178   (interactive "P")
6179   (gnus-summary-next-group no-article nil t))
6180
6181 ;; Walking around summary lines.
6182
6183 (defun gnus-summary-first-subject (&optional unread undownloaded)
6184   "Go to the first unread subject.
6185 If UNREAD is non-nil, go to the first unread article.
6186 Returns the article selected or nil if there are no unread articles."
6187   (interactive "P")
6188   (prog1
6189       (cond
6190        ;; Empty summary.
6191        ((null gnus-newsgroup-data)
6192         (gnus-message 3 "No articles in the group")
6193         nil)
6194        ;; Pick the first article.
6195        ((not unread)
6196         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6197         (gnus-data-number (car gnus-newsgroup-data)))
6198        ;; No unread articles.
6199        ((null gnus-newsgroup-unreads)
6200         (gnus-message 3 "No more unread articles")
6201         nil)
6202        ;; Find the first unread article.
6203        (t
6204         (let ((data gnus-newsgroup-data))
6205           (while (and data
6206                       (and (not (and undownloaded
6207                                      (eq gnus-undownloaded-mark
6208                                          (gnus-data-mark (car data)))))
6209                            (not (gnus-data-unread-p (car data)))))
6210             (setq data (cdr data)))
6211           (when data
6212             (goto-char (gnus-data-pos (car data)))
6213             (gnus-data-number (car data))))))
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-next-subject (n &optional unread dont-display)
6217   "Go to next N'th summary line.
6218 If N is negative, go to the previous N'th subject line.
6219 If UNREAD is non-nil, only unread articles are selected.
6220 The difference between N and the actual number of steps taken is
6221 returned."
6222   (interactive "p")
6223   (let ((backward (< n 0))
6224         (n (abs n)))
6225     (while (and (> n 0)
6226                 (if backward
6227                     (gnus-summary-find-prev unread)
6228                   (gnus-summary-find-next unread)))
6229       (unless (zerop (setq n (1- n)))
6230         (gnus-summary-show-thread)))
6231     (when (/= 0 n)
6232       (gnus-message 7 "No more%s articles"
6233                     (if unread " unread" "")))
6234     (unless dont-display
6235       (gnus-summary-recenter)
6236       (gnus-summary-position-point))
6237     n))
6238
6239 (defun gnus-summary-next-unread-subject (n)
6240   "Go to next N'th unread summary line."
6241   (interactive "p")
6242   (gnus-summary-next-subject n t))
6243
6244 (defun gnus-summary-prev-subject (n &optional unread)
6245   "Go to previous N'th summary line.
6246 If optional argument UNREAD is non-nil, only unread article is selected."
6247   (interactive "p")
6248   (gnus-summary-next-subject (- n) unread))
6249
6250 (defun gnus-summary-prev-unread-subject (n)
6251   "Go to previous N'th unread summary line."
6252   (interactive "p")
6253   (gnus-summary-next-subject (- n) t))
6254
6255 (defun gnus-summary-goto-subject (article &optional force silent)
6256   "Go the subject line of ARTICLE.
6257 If FORCE, also allow jumping to articles not currently shown."
6258   (interactive "nArticle number: ")
6259   (let ((b (point))
6260         (data (gnus-data-find article)))
6261     ;; We read in the article if we have to.
6262     (and (not data)
6263          force
6264          (gnus-summary-insert-subject
6265           article
6266           (if (or (numberp force) (vectorp force)) force)
6267           t)
6268          (setq data (gnus-data-find article)))
6269     (goto-char b)
6270     (if (not data)
6271         (progn
6272           (unless silent
6273             (gnus-message 3 "Can't find article %d" article))
6274           nil)
6275       (let ((pt (gnus-data-pos data)))
6276         (goto-char pt)
6277         (gnus-summary-set-article-display-arrow pt))
6278       (gnus-summary-position-point)
6279       article)))
6280
6281 ;; Walking around summary lines with displaying articles.
6282
6283 (defun gnus-summary-expand-window (&optional arg)
6284   "Make the summary buffer take up the entire Emacs frame.
6285 Given a prefix, will force an `article' buffer configuration."
6286   (interactive "P")
6287   (if arg
6288       (gnus-configure-windows 'article 'force)
6289     (gnus-configure-windows 'summary 'force)))
6290
6291 (defun gnus-summary-display-article (article &optional all-header)
6292   "Display ARTICLE in article buffer."
6293   (when (gnus-buffer-live-p gnus-article-buffer)
6294     (with-current-buffer gnus-article-buffer
6295       (mm-enable-multibyte-mule4)))
6296   (gnus-set-global-variables)
6297   (when (gnus-buffer-live-p gnus-article-buffer)
6298     (with-current-buffer gnus-article-buffer
6299       (setq gnus-article-charset gnus-newsgroup-charset)
6300       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6301       (mm-enable-multibyte-mule4)))
6302   (if (null article)
6303       nil
6304     (prog1
6305         (if gnus-summary-display-article-function
6306             (funcall gnus-summary-display-article-function article all-header)
6307           (gnus-article-prepare article all-header))
6308       (gnus-run-hooks 'gnus-select-article-hook)
6309       (when (and gnus-current-article
6310                  (not (zerop gnus-current-article)))
6311         (gnus-summary-goto-subject gnus-current-article))
6312       (gnus-summary-recenter)
6313       (when (and gnus-use-trees gnus-show-threads)
6314         (gnus-possibly-generate-tree article)
6315         (gnus-highlight-selected-tree article))
6316       ;; Successfully display article.
6317       (gnus-article-set-window-start
6318        (cdr (assq article gnus-newsgroup-bookmarks))))))
6319
6320 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6321   "Select the current article.
6322 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6323 non-nil, the article will be re-fetched even if it already present in
6324 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6325 be displayed."
6326   ;; Make sure we are in the summary buffer to work around bbdb bug.
6327   (unless (eq major-mode 'gnus-summary-mode)
6328     (set-buffer gnus-summary-buffer))
6329   (let ((article (or article (gnus-summary-article-number)))
6330         (all-headers (not (not all-headers))) ;Must be T or NIL.
6331         gnus-summary-display-article-function)
6332     (and (not pseudo)
6333          (gnus-summary-article-pseudo-p article)
6334          (error "This is a pseudo-article"))
6335     (save-excursion
6336       (set-buffer gnus-summary-buffer)
6337       (if (or (and gnus-single-article-buffer
6338                    (or (null gnus-current-article)
6339                        (null gnus-article-current)
6340                        (null (get-buffer gnus-article-buffer))
6341                        (not (eq article (cdr gnus-article-current)))
6342                        (not (equal (car gnus-article-current)
6343                                    gnus-newsgroup-name))))
6344               (and (not gnus-single-article-buffer)
6345                    (or (null gnus-current-article)
6346                        (not (eq gnus-current-article article))))
6347               force)
6348        ;; The requested article is different from the current article.
6349           (progn
6350             (gnus-summary-display-article article all-headers)
6351             (when (gnus-buffer-live-p gnus-article-buffer)
6352               (with-current-buffer gnus-article-buffer
6353                 (if (not gnus-article-decoded-p) ;; a local variable
6354                     (mm-disable-multibyte-mule4))))
6355             (when (or all-headers gnus-show-all-headers)
6356               (gnus-article-show-all-headers))
6357             (gnus-article-set-window-start
6358              (cdr (assq article gnus-newsgroup-bookmarks)))
6359             article)
6360         (when (or all-headers gnus-show-all-headers)
6361           (gnus-article-show-all-headers))
6362         'old))))
6363
6364 (defun gnus-summary-force-verify-and-decrypt ()
6365   (interactive)
6366   (let ((mm-verify-option 'known)
6367         (mm-decrypt-option 'known))
6368     (gnus-summary-select-article nil 'force)))
6369
6370 (defun gnus-summary-set-current-mark (&optional current-mark)
6371   "Obsolete function."
6372   nil)
6373
6374 (defun gnus-summary-next-article (&optional unread subject backward push)
6375   "Select the next article.
6376 If UNREAD, only unread articles are selected.
6377 If SUBJECT, only articles with SUBJECT are selected.
6378 If BACKWARD, the previous article is selected instead of the next."
6379   (interactive "P")
6380   (cond
6381    ;; Is there such an article?
6382    ((and (gnus-summary-search-forward unread subject backward)
6383          (or (gnus-summary-display-article (gnus-summary-article-number))
6384              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6385     (gnus-summary-position-point))
6386    ;; If not, we try the first unread, if that is wanted.
6387    ((and subject
6388          gnus-auto-select-same
6389          (gnus-summary-first-unread-article))
6390     (gnus-summary-position-point)
6391     (gnus-message 6 "Wrapped"))
6392    ;; Try to get next/previous article not displayed in this group.
6393    ((and gnus-auto-extend-newsgroup
6394          (not unread) (not subject))
6395     (gnus-summary-goto-article
6396      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6397      nil (count-lines (point-min) (point))))
6398    ;; Go to next/previous group.
6399    (t
6400     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6401       (gnus-summary-jump-to-group gnus-newsgroup-name))
6402     (let ((cmd last-command-char)
6403           (point
6404            (save-excursion
6405              (set-buffer gnus-group-buffer)
6406              (point)))
6407           (group
6408            (if (eq gnus-keep-same-level 'best)
6409                (gnus-summary-best-group gnus-newsgroup-name)
6410              (gnus-summary-search-group backward gnus-keep-same-level))))
6411       ;; For some reason, the group window gets selected.  We change
6412       ;; it back.
6413       (select-window (get-buffer-window (current-buffer)))
6414       ;; Select next unread newsgroup automagically.
6415       (cond
6416        ((or (not gnus-auto-select-next)
6417             (not cmd))
6418         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6419        ((or (eq gnus-auto-select-next 'quietly)
6420             (and (eq gnus-auto-select-next 'slightly-quietly)
6421                  push)
6422             (and (eq gnus-auto-select-next 'almost-quietly)
6423                  (gnus-summary-last-article-p)))
6424         ;; Select quietly.
6425         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6426             (gnus-summary-exit)
6427           (gnus-message 7 "No more%s articles (%s)..."
6428                         (if unread " unread" "")
6429                         (if group (concat "selecting " group)
6430                           "exiting"))
6431           (gnus-summary-next-group nil group backward)))
6432        (t
6433         (when (gnus-key-press-event-p last-input-event)
6434           (gnus-summary-walk-group-buffer
6435            gnus-newsgroup-name cmd unread backward point))))))))
6436
6437 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6438   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6439                       (?\C-p (gnus-group-prev-unread-group 1))))
6440         (cursor-in-echo-area t)
6441         keve key group ended)
6442     (save-excursion
6443       (set-buffer gnus-group-buffer)
6444       (goto-char start)
6445       (setq group
6446             (if (eq gnus-keep-same-level 'best)
6447                 (gnus-summary-best-group gnus-newsgroup-name)
6448               (gnus-summary-search-group backward gnus-keep-same-level))))
6449     (while (not ended)
6450       (gnus-message
6451        5 "No more%s articles%s" (if unread " unread" "")
6452        (if (and group
6453                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6454            (format " (Type %s for %s [%s])"
6455                    (single-key-description cmd) group
6456                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6457          (format " (Type %s to exit %s)"
6458                  (single-key-description cmd)
6459                  gnus-newsgroup-name)))
6460       ;; Confirm auto selection.
6461       (setq key (car (setq keve (gnus-read-event-char))))
6462       (setq ended t)
6463       (cond
6464        ((assq key keystrokes)
6465         (let ((obuf (current-buffer)))
6466           (switch-to-buffer gnus-group-buffer)
6467           (when group
6468             (gnus-group-jump-to-group group))
6469           (eval (cadr (assq key keystrokes)))
6470           (setq group (gnus-group-group-name))
6471           (switch-to-buffer obuf))
6472         (setq ended nil))
6473        ((equal key cmd)
6474         (if (or (not group)
6475                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6476             (gnus-summary-exit)
6477           (gnus-summary-next-group nil group backward)))
6478        (t
6479         (push (cdr keve) unread-command-events))))))
6480
6481 (defun gnus-summary-next-unread-article ()
6482   "Select unread article after current one."
6483   (interactive)
6484   (gnus-summary-next-article
6485    (or (not (eq gnus-summary-goto-unread 'never))
6486        (gnus-summary-last-article-p (gnus-summary-article-number)))
6487    (and gnus-auto-select-same
6488         (gnus-summary-article-subject))))
6489
6490 (defun gnus-summary-prev-article (&optional unread subject)
6491   "Select the article after the current one.
6492 If UNREAD is non-nil, only unread articles are selected."
6493   (interactive "P")
6494   (gnus-summary-next-article unread subject t))
6495
6496 (defun gnus-summary-prev-unread-article ()
6497   "Select unread article before current one."
6498   (interactive)
6499   (gnus-summary-prev-article
6500    (or (not (eq gnus-summary-goto-unread 'never))
6501        (gnus-summary-first-article-p (gnus-summary-article-number)))
6502    (and gnus-auto-select-same
6503         (gnus-summary-article-subject))))
6504
6505 (defun gnus-summary-next-page (&optional lines circular)
6506   "Show next page of the selected article.
6507 If at the end of the current article, select the next article.
6508 LINES says how many lines should be scrolled up.
6509
6510 If CIRCULAR is non-nil, go to the start of the article instead of
6511 selecting the next article when reaching the end of the current
6512 article."
6513   (interactive "P")
6514   (setq gnus-summary-buffer (current-buffer))
6515   (gnus-set-global-variables)
6516   (let ((article (gnus-summary-article-number))
6517         (article-window (get-buffer-window gnus-article-buffer t))
6518         endp)
6519     ;; If the buffer is empty, we have no article.
6520     (unless article
6521       (error "No article to select"))
6522     (gnus-configure-windows 'article)
6523     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6524         (if (and (eq gnus-summary-goto-unread 'never)
6525                  (not (gnus-summary-last-article-p article)))
6526             (gnus-summary-next-article)
6527           (gnus-summary-next-unread-article))
6528       (if (or (null gnus-current-article)
6529               (null gnus-article-current)
6530               (/= article (cdr gnus-article-current))
6531               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6532           ;; Selected subject is different from current article's.
6533           (gnus-summary-display-article article)
6534         (when article-window
6535           (gnus-eval-in-buffer-window gnus-article-buffer
6536             (setq endp (gnus-article-next-page lines)))
6537           (when endp
6538             (cond (circular
6539                    (gnus-summary-beginning-of-article))
6540                   (lines
6541                    (gnus-message 3 "End of message"))
6542                   ((null lines)
6543                    (if (and (eq gnus-summary-goto-unread 'never)
6544                             (not (gnus-summary-last-article-p article)))
6545                        (gnus-summary-next-article)
6546                      (gnus-summary-next-unread-article))))))))
6547     (gnus-summary-recenter)
6548     (gnus-summary-position-point)))
6549
6550 (defun gnus-summary-prev-page (&optional lines move)
6551   "Show previous page of selected article.
6552 Argument LINES specifies lines to be scrolled down.
6553 If MOVE, move to the previous unread article if point is at
6554 the beginning of the buffer."
6555   (interactive "P")
6556   (let ((article (gnus-summary-article-number))
6557         (article-window (get-buffer-window gnus-article-buffer t))
6558         endp)
6559     (gnus-configure-windows 'article)
6560     (if (or (null gnus-current-article)
6561             (null gnus-article-current)
6562             (/= article (cdr gnus-article-current))
6563             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6564         ;; Selected subject is different from current article's.
6565         (gnus-summary-display-article article)
6566       (gnus-summary-recenter)
6567       (when article-window
6568         (gnus-eval-in-buffer-window gnus-article-buffer
6569           (setq endp (gnus-article-prev-page lines)))
6570         (when (and move endp)
6571           (cond (lines
6572                  (gnus-message 3 "Beginning of message"))
6573                 ((null lines)
6574                  (if (and (eq gnus-summary-goto-unread 'never)
6575                           (not (gnus-summary-first-article-p article)))
6576                      (gnus-summary-prev-article)
6577                    (gnus-summary-prev-unread-article))))))))
6578   (gnus-summary-position-point))
6579
6580 (defun gnus-summary-prev-page-or-article (&optional lines)
6581   "Show previous page of selected article.
6582 Argument LINES specifies lines to be scrolled down.
6583 If at the beginning of the article, go to the next article."
6584   (interactive "P")
6585   (gnus-summary-prev-page lines t))
6586
6587 (defun gnus-summary-scroll-up (lines)
6588   "Scroll up (or down) one line current article.
6589 Argument LINES specifies lines to be scrolled up (or down if negative)."
6590   (interactive "p")
6591   (gnus-configure-windows 'article)
6592   (gnus-summary-show-thread)
6593   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6594     (gnus-eval-in-buffer-window gnus-article-buffer
6595       (cond ((> lines 0)
6596              (when (gnus-article-next-page lines)
6597                (gnus-message 3 "End of message")))
6598             ((< lines 0)
6599              (gnus-article-prev-page (- lines))))))
6600   (gnus-summary-recenter)
6601   (gnus-summary-position-point))
6602
6603 (defun gnus-summary-scroll-down (lines)
6604   "Scroll down (or up) one line current article.
6605 Argument LINES specifies lines to be scrolled down (or up if negative)."
6606   (interactive "p")
6607   (gnus-summary-scroll-up (- lines)))
6608
6609 (defun gnus-summary-next-same-subject ()
6610   "Select next article which has the same subject as current one."
6611   (interactive)
6612   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6613
6614 (defun gnus-summary-prev-same-subject ()
6615   "Select previous article which has the same subject as current one."
6616   (interactive)
6617   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6618
6619 (defun gnus-summary-next-unread-same-subject ()
6620   "Select next unread article which has the same subject as current one."
6621   (interactive)
6622   (gnus-summary-next-article t (gnus-summary-article-subject)))
6623
6624 (defun gnus-summary-prev-unread-same-subject ()
6625   "Select previous unread article which has the same subject as current one."
6626   (interactive)
6627   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6628
6629 (defun gnus-summary-first-unread-article ()
6630   "Select the first unread article.
6631 Return nil if there are no unread articles."
6632   (interactive)
6633   (prog1
6634       (when (gnus-summary-first-subject t)
6635         (gnus-summary-show-thread)
6636         (gnus-summary-first-subject t)
6637         (gnus-summary-display-article (gnus-summary-article-number)))
6638     (gnus-summary-position-point)))
6639
6640 (defun gnus-summary-first-unread-subject ()
6641   "Place the point on the subject line of the first unread article.
6642 Return nil if there are no unread articles."
6643   (interactive)
6644   (prog1
6645       (when (gnus-summary-first-subject t)
6646         (gnus-summary-show-thread)
6647         (gnus-summary-first-subject t))
6648     (gnus-summary-position-point)))
6649
6650 (defun gnus-summary-first-article ()
6651   "Select the first article.
6652 Return nil if there are no articles."
6653   (interactive)
6654   (prog1
6655       (when (gnus-summary-first-subject)
6656         (gnus-summary-show-thread)
6657         (gnus-summary-first-subject)
6658         (gnus-summary-display-article (gnus-summary-article-number)))
6659     (gnus-summary-position-point)))
6660
6661 (defun gnus-summary-best-unread-article ()
6662   "Select the unread article with the highest score."
6663   (interactive)
6664   (let ((best -1000000)
6665         (data gnus-newsgroup-data)
6666         article score)
6667     (while data
6668       (and (gnus-data-unread-p (car data))
6669            (> (setq score
6670                     (gnus-summary-article-score (gnus-data-number (car data))))
6671               best)
6672            (setq best score
6673                  article (gnus-data-number (car data))))
6674       (setq data (cdr data)))
6675     (prog1
6676         (if article
6677             (gnus-summary-goto-article article)
6678           (error "No unread articles"))
6679       (gnus-summary-position-point))))
6680
6681 (defun gnus-summary-last-subject ()
6682   "Go to the last displayed subject line in the group."
6683   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6684     (when article
6685       (gnus-summary-goto-subject article))))
6686
6687 (defun gnus-summary-goto-article (article &optional all-headers force)
6688   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6689 If ALL-HEADERS is non-nil, no header lines are hidden.
6690 If FORCE, go to the article even if it isn't displayed.  If FORCE
6691 is a number, it is the line the article is to be displayed on."
6692   (interactive
6693    (list
6694     (completing-read
6695      "Article number or Message-ID: "
6696      (mapcar (lambda (number) (list (int-to-string number)))
6697              gnus-newsgroup-limit))
6698     current-prefix-arg
6699     t))
6700   (prog1
6701       (if (and (stringp article)
6702                (string-match "@" article))
6703           (gnus-summary-refer-article article)
6704         (when (stringp article)
6705           (setq article (string-to-number article)))
6706         (if (gnus-summary-goto-subject article force)
6707             (gnus-summary-display-article article all-headers)
6708           (gnus-message 4 "Couldn't go to article %s" article) nil))
6709     (gnus-summary-position-point)))
6710
6711 (defun gnus-summary-goto-last-article ()
6712   "Go to the previously read article."
6713   (interactive)
6714   (prog1
6715       (when gnus-last-article
6716         (gnus-summary-goto-article gnus-last-article nil t))
6717     (gnus-summary-position-point)))
6718
6719 (defun gnus-summary-pop-article (number)
6720   "Pop one article off the history and go to the previous.
6721 NUMBER articles will be popped off."
6722   (interactive "p")
6723   (let (to)
6724     (setq gnus-newsgroup-history
6725           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6726     (if to
6727         (gnus-summary-goto-article (car to) nil t)
6728       (error "Article history empty")))
6729   (gnus-summary-position-point))
6730
6731 ;; Summary commands and functions for limiting the summary buffer.
6732
6733 (defun gnus-summary-limit-to-articles (n)
6734   "Limit the summary buffer to the next N articles.
6735 If not given a prefix, use the process marked articles instead."
6736   (interactive "P")
6737   (prog1
6738       (let ((articles (gnus-summary-work-articles n)))
6739         (setq gnus-newsgroup-processable nil)
6740         (gnus-summary-limit articles))
6741     (gnus-summary-position-point)))
6742
6743 (defun gnus-summary-pop-limit (&optional total)
6744   "Restore the previous limit.
6745 If given a prefix, remove all limits."
6746   (interactive "P")
6747   (when total
6748     (setq gnus-newsgroup-limits
6749           (list (mapcar (lambda (h) (mail-header-number h))
6750                         gnus-newsgroup-headers))))
6751   (unless gnus-newsgroup-limits
6752     (error "No limit to pop"))
6753   (prog1
6754       (gnus-summary-limit nil 'pop)
6755     (gnus-summary-position-point)))
6756
6757 (defun gnus-summary-limit-to-subject (subject &optional header)
6758   "Limit the summary buffer to articles that have subjects that match a regexp."
6759   (interactive "sLimit to subject (regexp): ")
6760   (unless header
6761     (setq header "subject"))
6762   (when (not (equal "" subject))
6763     (prog1
6764         (let ((articles (gnus-summary-find-matching
6765                          (or header "subject") subject 'all)))
6766           (unless articles
6767             (error "Found no matches for \"%s\"" subject))
6768           (gnus-summary-limit articles))
6769       (gnus-summary-position-point))))
6770
6771 (defun gnus-summary-limit-to-author (from)
6772   "Limit the summary buffer to articles that have authors that match a regexp."
6773   (interactive "sLimit to author (regexp): ")
6774   (gnus-summary-limit-to-subject from "from"))
6775
6776 (defun gnus-summary-limit-to-age (age &optional younger-p)
6777   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6778 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6779 articles that are younger than AGE days."
6780   (interactive
6781    (let ((younger current-prefix-arg)
6782          (days-got nil)
6783          days)
6784      (while (not days-got)
6785        (setq days (if younger
6786                       (read-string "Limit to articles within (in days): ")
6787                     (read-string "Limit to articles older than (in days): ")))
6788        (when (> (length days) 0)
6789          (setq days (read days)))
6790        (if (numberp days)
6791            (setq days-got t)
6792          (message "Please enter a number.")
6793          (sleep-for 1)))
6794      (list days younger)))
6795   (prog1
6796       (let ((data gnus-newsgroup-data)
6797             (cutoff (days-to-time age))
6798             articles d date is-younger)
6799         (while (setq d (pop data))
6800           (when (and (vectorp (gnus-data-header d))
6801                      (setq date (mail-header-date (gnus-data-header d))))
6802             (setq is-younger (time-less-p
6803                               (time-since (condition-case ()
6804                                               (date-to-time date)
6805                                             (error '(0 0))))
6806                               cutoff))
6807             (when (if younger-p
6808                       is-younger
6809                     (not is-younger))
6810               (push (gnus-data-number d) articles))))
6811         (gnus-summary-limit (nreverse articles)))
6812     (gnus-summary-position-point)))
6813
6814 (defun gnus-summary-limit-to-extra (header regexp)
6815   "Limit the summary buffer to articles that match an 'extra' header."
6816   (interactive
6817    (let ((header
6818           (intern
6819            (gnus-completing-read
6820             (symbol-name (car gnus-extra-headers))
6821             "Limit extra header:"
6822             (mapcar (lambda (x)
6823                       (cons (symbol-name x) x))
6824                     gnus-extra-headers)
6825             nil
6826             t))))
6827      (list header
6828            (read-string (format "Limit to header %s (regexp): " header)))))
6829   (when (not (equal "" regexp))
6830     (prog1
6831         (let ((articles (gnus-summary-find-matching
6832                          (cons 'extra header) regexp 'all)))
6833           (unless articles
6834             (error "Found no matches for \"%s\"" regexp))
6835           (gnus-summary-limit articles))
6836       (gnus-summary-position-point))))
6837
6838 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6839 (make-obsolete
6840  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6841
6842 (defun gnus-summary-limit-to-unread (&optional all)
6843   "Limit the summary buffer to articles that are not marked as read.
6844 If ALL is non-nil, limit strictly to unread articles."
6845   (interactive "P")
6846   (if all
6847       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6848     (gnus-summary-limit-to-marks
6849      ;; Concat all the marks that say that an article is read and have
6850      ;; those removed.
6851      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6852            gnus-killed-mark gnus-kill-file-mark
6853            gnus-low-score-mark gnus-expirable-mark
6854            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6855            gnus-duplicate-mark gnus-souped-mark)
6856      'reverse)))
6857
6858 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6859 (make-obsolete 'gnus-summary-delete-marked-with
6860                'gnus-summary-limit-exlude-marks)
6861
6862 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6863   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6864 If REVERSE, limit the summary buffer to articles that are marked
6865 with MARKS.  MARKS can either be a string of marks or a list of marks.
6866 Returns how many articles were removed."
6867   (interactive "sMarks: ")
6868   (gnus-summary-limit-to-marks marks t))
6869
6870 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6871   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6872 If REVERSE (the prefix), limit the summary buffer to articles that are
6873 not marked with MARKS.  MARKS can either be a string of marks or a
6874 list of marks.
6875 Returns how many articles were removed."
6876   (interactive "sMarks: \nP")
6877   (prog1
6878       (let ((data gnus-newsgroup-data)
6879             (marks (if (listp marks) marks
6880                      (append marks nil))) ; Transform to list.
6881             articles)
6882         (while data
6883           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6884                   (memq (gnus-data-mark (car data)) marks))
6885             (push (gnus-data-number (car data)) articles))
6886           (setq data (cdr data)))
6887         (gnus-summary-limit articles))
6888     (gnus-summary-position-point)))
6889
6890 (defun gnus-summary-limit-to-score (&optional score)
6891   "Limit to articles with score at or above SCORE."
6892   (interactive "P")
6893   (setq score (if score
6894                   (prefix-numeric-value score)
6895                 (or gnus-summary-default-score 0)))
6896   (let ((data gnus-newsgroup-data)
6897         articles)
6898     (while data
6899       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6900                 score)
6901         (push (gnus-data-number (car data)) articles))
6902       (setq data (cdr data)))
6903     (prog1
6904         (gnus-summary-limit articles)
6905       (gnus-summary-position-point))))
6906
6907 (defun gnus-summary-limit-include-thread (id)
6908   "Display all the hidden articles that is in the thread with ID in it.
6909 When called interactively, ID is the Message-ID of the current
6910 article."
6911   (interactive (list (mail-header-id (gnus-summary-article-header))))
6912   (let ((articles (gnus-articles-in-thread
6913                    (gnus-id-to-thread (gnus-root-id id)))))
6914     (prog1
6915         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6916       (gnus-summary-limit-include-matching-articles
6917        "subject"
6918        (regexp-quote (gnus-simplify-subject-re
6919                       (mail-header-subject (gnus-id-to-header id)))))
6920       (gnus-summary-position-point))))
6921
6922 (defun gnus-summary-limit-include-matching-articles (header regexp)
6923   "Display all the hidden articles that have HEADERs that match REGEXP."
6924   (interactive (list (read-string "Match on header: ")
6925                      (read-string "Regexp: ")))
6926   (let ((articles (gnus-find-matching-articles header regexp)))
6927     (prog1
6928         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6929       (gnus-summary-position-point))))
6930
6931 (defun gnus-summary-limit-include-dormant ()
6932   "Display all the hidden articles that are marked as dormant.
6933 Note that this command only works on a subset of the articles currently
6934 fetched for this group."
6935   (interactive)
6936   (unless gnus-newsgroup-dormant
6937     (error "There are no dormant articles in this group"))
6938   (prog1
6939       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6940     (gnus-summary-position-point)))
6941
6942 (defun gnus-summary-limit-exclude-dormant ()
6943   "Hide all dormant articles."
6944   (interactive)
6945   (prog1
6946       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6947     (gnus-summary-position-point)))
6948
6949 (defun gnus-summary-limit-exclude-childless-dormant ()
6950   "Hide all dormant articles that have no children."
6951   (interactive)
6952   (let ((data (gnus-data-list t))
6953         articles d children)
6954     ;; Find all articles that are either not dormant or have
6955     ;; children.
6956     (while (setq d (pop data))
6957       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6958                 (and (setq children
6959                            (gnus-article-children (gnus-data-number d)))
6960                      (let (found)
6961                        (while children
6962                          (when (memq (car children) articles)
6963                            (setq children nil
6964                                  found t))
6965                          (pop children))
6966                        found)))
6967         (push (gnus-data-number d) articles)))
6968     ;; Do the limiting.
6969     (prog1
6970         (gnus-summary-limit articles)
6971       (gnus-summary-position-point))))
6972
6973 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6974   "Mark all unread excluded articles as read.
6975 If ALL, mark even excluded ticked and dormants as read."
6976   (interactive "P")
6977   (let ((articles (gnus-sorted-complement
6978                    (sort
6979                     (mapcar (lambda (h) (mail-header-number h))
6980                             gnus-newsgroup-headers)
6981                     '<)
6982                    (sort gnus-newsgroup-limit '<)))
6983         article)
6984     (setq gnus-newsgroup-unreads
6985           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6986     (if all
6987         (setq gnus-newsgroup-dormant nil
6988               gnus-newsgroup-marked nil
6989               gnus-newsgroup-reads
6990               (nconc
6991                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6992                gnus-newsgroup-reads))
6993       (while (setq article (pop articles))
6994         (unless (or (memq article gnus-newsgroup-dormant)
6995                     (memq article gnus-newsgroup-marked))
6996           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6997
6998 (defun gnus-summary-limit (articles &optional pop)
6999   (if pop
7000       ;; We pop the previous limit off the stack and use that.
7001       (setq articles (car gnus-newsgroup-limits)
7002             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7003     ;; We use the new limit, so we push the old limit on the stack.
7004     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7005   ;; Set the limit.
7006   (setq gnus-newsgroup-limit articles)
7007   (let ((total (length gnus-newsgroup-data))
7008         (data (gnus-data-find-list (gnus-summary-article-number)))
7009         (gnus-summary-mark-below nil)   ; Inhibit this.
7010         found)
7011     ;; This will do all the work of generating the new summary buffer
7012     ;; according to the new limit.
7013     (gnus-summary-prepare)
7014     ;; Hide any threads, possibly.
7015     (and gnus-show-threads
7016          gnus-thread-hide-subtree
7017          (gnus-summary-hide-all-threads))
7018     ;; Try to return to the article you were at, or one in the
7019     ;; neighborhood.
7020     (when data
7021       ;; We try to find some article after the current one.
7022       (while data
7023         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7024           (setq data nil
7025                 found t))
7026         (setq data (cdr data))))
7027     (unless found
7028       ;; If there is no data, that means that we were after the last
7029       ;; article.  The same goes when we can't find any articles
7030       ;; after the current one.
7031       (goto-char (point-max))
7032       (gnus-summary-find-prev))
7033     (gnus-set-mode-line 'summary)
7034     ;; We return how many articles were removed from the summary
7035     ;; buffer as a result of the new limit.
7036     (- total (length gnus-newsgroup-data))))
7037
7038 (defsubst gnus-invisible-cut-children (threads)
7039   (let ((num 0))
7040     (while threads
7041       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7042         (incf num))
7043       (pop threads))
7044     (< num 2)))
7045
7046 (defsubst gnus-cut-thread (thread)
7047   "Go forwards in the thread until we find an article that we want to display."
7048   (when (or (eq gnus-fetch-old-headers 'some)
7049             (eq gnus-fetch-old-headers 'invisible)
7050             (numberp gnus-fetch-old-headers)
7051             (eq gnus-build-sparse-threads 'some)
7052             (eq gnus-build-sparse-threads 'more))
7053     ;; Deal with old-fetched headers and sparse threads.
7054     (while (and
7055             thread
7056             (or
7057              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7058              (gnus-summary-article-ancient-p
7059               (mail-header-number (car thread))))
7060             (if (or (<= (length (cdr thread)) 1)
7061                     (eq gnus-fetch-old-headers 'invisible))
7062                 (setq gnus-newsgroup-limit
7063                       (delq (mail-header-number (car thread))
7064                             gnus-newsgroup-limit)
7065                       thread (cadr thread))
7066               (when (gnus-invisible-cut-children (cdr thread))
7067                 (let ((th (cdr thread)))
7068                   (while th
7069                     (if (memq (mail-header-number (caar th))
7070                               gnus-newsgroup-limit)
7071                         (setq thread (car th)
7072                               th nil)
7073                       (setq th (cdr th))))))))))
7074   thread)
7075
7076 (defun gnus-cut-threads (threads)
7077   "Cut off all uninteresting articles from the beginning of threads."
7078   (when (or (eq gnus-fetch-old-headers 'some)
7079             (eq gnus-fetch-old-headers 'invisible)
7080             (numberp gnus-fetch-old-headers)
7081             (eq gnus-build-sparse-threads 'some)
7082             (eq gnus-build-sparse-threads 'more))
7083     (let ((th threads))
7084       (while th
7085         (setcar th (gnus-cut-thread (car th)))
7086         (setq th (cdr th)))))
7087   ;; Remove nixed out threads.
7088   (delq nil threads))
7089
7090 (defun gnus-summary-initial-limit (&optional show-if-empty)
7091   "Figure out what the initial limit is supposed to be on group entry.
7092 This entails weeding out unwanted dormants, low-scored articles,
7093 fetch-old-headers verbiage, and so on."
7094   ;; Most groups have nothing to remove.
7095   (if (or gnus-inhibit-limiting
7096           (and (null gnus-newsgroup-dormant)
7097                (eq gnus-newsgroup-display 'identity)
7098                (not (eq gnus-fetch-old-headers 'some))
7099                (not (numberp gnus-fetch-old-headers))
7100                (not (eq gnus-fetch-old-headers 'invisible))
7101                (null gnus-summary-expunge-below)
7102                (not (eq gnus-build-sparse-threads 'some))
7103                (not (eq gnus-build-sparse-threads 'more))
7104                (null gnus-thread-expunge-below)
7105                (not gnus-use-nocem)))
7106       ()                                ; Do nothing.
7107     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7108     (setq gnus-newsgroup-limit nil)
7109     (mapatoms
7110      (lambda (node)
7111        (unless (car (symbol-value node))
7112          ;; These threads have no parents -- they are roots.
7113          (let ((nodes (cdr (symbol-value node)))
7114                thread)
7115            (while nodes
7116              (if (and gnus-thread-expunge-below
7117                       (< (gnus-thread-total-score (car nodes))
7118                          gnus-thread-expunge-below))
7119                  (gnus-expunge-thread (pop nodes))
7120                (setq thread (pop nodes))
7121                (gnus-summary-limit-children thread))))))
7122      gnus-newsgroup-dependencies)
7123     ;; If this limitation resulted in an empty group, we might
7124     ;; pop the previous limit and use it instead.
7125     (when (and (not gnus-newsgroup-limit)
7126                show-if-empty)
7127       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7128     gnus-newsgroup-limit))
7129
7130 (defun gnus-summary-limit-children (thread)
7131   "Return 1 if this subthread is visible and 0 if it is not."
7132   ;; First we get the number of visible children to this thread.  This
7133   ;; is done by recursing down the thread using this function, so this
7134   ;; will really go down to a leaf article first, before slowly
7135   ;; working its way up towards the root.
7136   (when thread
7137     (let ((children
7138            (if (cdr thread)
7139                (apply '+ (mapcar 'gnus-summary-limit-children
7140                                  (cdr thread)))
7141              0))
7142           (number (mail-header-number (car thread)))
7143           score)
7144       (if (and
7145            (not (memq number gnus-newsgroup-marked))
7146            (or
7147             ;; If this article is dormant and has absolutely no visible
7148             ;; children, then this article isn't visible.
7149             (and (memq number gnus-newsgroup-dormant)
7150                  (zerop children))
7151             ;; If this is "fetch-old-headered" and there is no
7152             ;; visible children, then we don't want this article.
7153             (and (or (eq gnus-fetch-old-headers 'some)
7154                      (numberp gnus-fetch-old-headers))
7155                  (gnus-summary-article-ancient-p number)
7156                  (zerop children))
7157             ;; If this is "fetch-old-headered" and `invisible', then
7158             ;; we don't want this article.
7159             (and (eq gnus-fetch-old-headers 'invisible)
7160                  (gnus-summary-article-ancient-p number))
7161             ;; If this is a sparsely inserted article with no children,
7162             ;; we don't want it.
7163             (and (eq gnus-build-sparse-threads 'some)
7164                  (gnus-summary-article-sparse-p number)
7165                  (zerop children))
7166             ;; If we use expunging, and this article is really
7167             ;; low-scored, then we don't want this article.
7168             (when (and gnus-summary-expunge-below
7169                        (< (setq score
7170                                 (or (cdr (assq number gnus-newsgroup-scored))
7171                                     gnus-summary-default-score))
7172                           gnus-summary-expunge-below))
7173               ;; We increase the expunge-tally here, but that has
7174               ;; nothing to do with the limits, really.
7175               (incf gnus-newsgroup-expunged-tally)
7176               ;; We also mark as read here, if that's wanted.
7177               (when (and gnus-summary-mark-below
7178                          (< score gnus-summary-mark-below))
7179                 (setq gnus-newsgroup-unreads
7180                       (delq number gnus-newsgroup-unreads))
7181                 (if gnus-newsgroup-auto-expire
7182                     (push number gnus-newsgroup-expirable)
7183                   (push (cons number gnus-low-score-mark)
7184                         gnus-newsgroup-reads)))
7185               t)
7186             ;; Do the `display' group parameter.
7187             (and gnus-newsgroup-display
7188                  (not (funcall gnus-newsgroup-display)))
7189             ;; Check NoCeM things.
7190             (if (and gnus-use-nocem
7191                      (gnus-nocem-unwanted-article-p
7192                       (mail-header-id (car thread))))
7193                 (progn
7194                   (setq gnus-newsgroup-unreads
7195                         (delq number gnus-newsgroup-unreads))
7196                   t))))
7197           ;; Nope, invisible article.
7198           0
7199         ;; Ok, this article is to be visible, so we add it to the limit
7200         ;; and return 1.
7201         (push number gnus-newsgroup-limit)
7202         1))))
7203
7204 (defun gnus-expunge-thread (thread)
7205   "Mark all articles in THREAD as read."
7206   (let* ((number (mail-header-number (car thread))))
7207     (incf gnus-newsgroup-expunged-tally)
7208     ;; We also mark as read here, if that's wanted.
7209     (setq gnus-newsgroup-unreads
7210           (delq number gnus-newsgroup-unreads))
7211     (if gnus-newsgroup-auto-expire
7212         (push number gnus-newsgroup-expirable)
7213       (push (cons number gnus-low-score-mark)
7214             gnus-newsgroup-reads)))
7215   ;; Go recursively through all subthreads.
7216   (mapcar 'gnus-expunge-thread (cdr thread)))
7217
7218 ;; Summary article oriented commands
7219
7220 (defun gnus-summary-refer-parent-article (n)
7221   "Refer parent article N times.
7222 If N is negative, go to ancestor -N instead.
7223 The difference between N and the number of articles fetched is returned."
7224   (interactive "p")
7225   (let ((skip 1)
7226         error header ref)
7227     (when (not (natnump n))
7228       (setq skip (abs n)
7229             n 1))
7230     (while (and (> n 0)
7231                 (not error))
7232       (setq header (gnus-summary-article-header))
7233       (if (and (eq (mail-header-number header)
7234                    (cdr gnus-article-current))
7235                (equal gnus-newsgroup-name
7236                       (car gnus-article-current)))
7237           ;; If we try to find the parent of the currently
7238           ;; displayed article, then we take a look at the actual
7239           ;; References header, since this is slightly more
7240           ;; reliable than the References field we got from the
7241           ;; server.
7242           (save-excursion
7243             (set-buffer gnus-original-article-buffer)
7244             (nnheader-narrow-to-headers)
7245             (unless (setq ref (message-fetch-field "references"))
7246               (setq ref (message-fetch-field "in-reply-to")))
7247             (widen))
7248         (setq ref
7249               ;; It's not the current article, so we take a bet on
7250               ;; the value we got from the server.
7251               (mail-header-references header)))
7252       (if (and ref
7253                (not (equal ref "")))
7254           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7255             (gnus-message 1 "Couldn't find parent"))
7256         (gnus-message 1 "No references in article %d"
7257                       (gnus-summary-article-number))
7258         (setq error t))
7259       (decf n))
7260     (gnus-summary-position-point)
7261     n))
7262
7263 (defun gnus-summary-refer-references ()
7264   "Fetch all articles mentioned in the References header.
7265 Return the number of articles fetched."
7266   (interactive)
7267   (let ((ref (mail-header-references (gnus-summary-article-header)))
7268         (current (gnus-summary-article-number))
7269         (n 0))
7270     (if (or (not ref)
7271             (equal ref ""))
7272         (error "No References in the current article")
7273       ;; For each Message-ID in the References header...
7274       (while (string-match "<[^>]*>" ref)
7275         (incf n)
7276         ;; ... fetch that article.
7277         (gnus-summary-refer-article
7278          (prog1 (match-string 0 ref)
7279            (setq ref (substring ref (match-end 0))))))
7280       (gnus-summary-goto-subject current)
7281       (gnus-summary-position-point)
7282       n)))
7283
7284 (defun gnus-summary-refer-thread (&optional limit)
7285   "Fetch all articles in the current thread.
7286 If LIMIT (the numerical prefix), fetch that many old headers instead
7287 of what's specified by the `gnus-refer-thread-limit' variable."
7288   (interactive "P")
7289   (let ((id (mail-header-id (gnus-summary-article-header)))
7290         (limit (if limit (prefix-numeric-value limit)
7291                  gnus-refer-thread-limit)))
7292     ;; We want to fetch LIMIT *old* headers, but we also have to
7293     ;; re-fetch all the headers in the current buffer, because many of
7294     ;; them may be undisplayed.  So we adjust LIMIT.
7295     (when (numberp limit)
7296       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7297     (unless (eq gnus-fetch-old-headers 'invisible)
7298       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7299       ;; Retrieve the headers and read them in.
7300       (if (eq (gnus-retrieve-headers
7301                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7302               'nov)
7303           (gnus-build-all-threads)
7304         (error "Can't fetch thread from backends that don't support NOV"))
7305       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7306     (gnus-summary-limit-include-thread id)))
7307
7308 (defun gnus-summary-refer-article (message-id)
7309   "Fetch an article specified by MESSAGE-ID."
7310   (interactive "sMessage-ID: ")
7311   (when (and (stringp message-id)
7312              (not (zerop (length message-id))))
7313     ;; Construct the correct Message-ID if necessary.
7314     ;; Suggested by tale@pawl.rpi.edu.
7315     (unless (string-match "^<" message-id)
7316       (setq message-id (concat "<" message-id)))
7317     (unless (string-match ">$" message-id)
7318       (setq message-id (concat message-id ">")))
7319     (let* ((header (gnus-id-to-header message-id))
7320            (sparse (and header
7321                         (gnus-summary-article-sparse-p
7322                          (mail-header-number header))
7323                         (memq (mail-header-number header)
7324                               gnus-newsgroup-limit)))
7325            number)
7326       (cond
7327        ;; If the article is present in the buffer we just go to it.
7328        ((and header
7329              (or (not (gnus-summary-article-sparse-p
7330                        (mail-header-number header)))
7331                  sparse))
7332         (prog1
7333             (gnus-summary-goto-article
7334              (mail-header-number header) nil t)
7335           (when sparse
7336             (gnus-summary-update-article (mail-header-number header)))))
7337        (t
7338         ;; We fetch the article.
7339         (catch 'found
7340           (dolist (gnus-override-method (gnus-refer-article-methods))
7341             (gnus-check-server gnus-override-method)
7342             ;; Fetch the header, and display the article.
7343             (when (setq number (gnus-summary-insert-subject message-id))
7344               (gnus-summary-select-article nil nil nil number)
7345               (throw 'found t)))
7346           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7347
7348 (defun gnus-refer-article-methods ()
7349   "Return a list of referrable methods."
7350   (cond
7351    ;; No method, so we default to current and native.
7352    ((null gnus-refer-article-method)
7353     (list gnus-current-select-method gnus-select-method))
7354    ;; Current.
7355    ((eq 'current gnus-refer-article-method)
7356     (list gnus-current-select-method))
7357    ;; List of select methods.
7358    ((not (and (symbolp (car gnus-refer-article-method))
7359               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7360     (let (out)
7361       (dolist (method gnus-refer-article-method)
7362         (push (if (eq 'current method)
7363                   gnus-current-select-method
7364                 method)
7365               out))
7366       (nreverse out)))
7367    ;; One single select method.
7368    (t
7369     (list gnus-refer-article-method))))
7370
7371 (defun gnus-summary-edit-parameters ()
7372   "Edit the group parameters of the current group."
7373   (interactive)
7374   (gnus-group-edit-group gnus-newsgroup-name 'params))
7375
7376 (defun gnus-summary-customize-parameters ()
7377   "Customize the group parameters of the current group."
7378   (interactive)
7379   (gnus-group-customize gnus-newsgroup-name))
7380
7381 (defun gnus-summary-enter-digest-group (&optional force)
7382   "Enter an nndoc group based on the current article.
7383 If FORCE, force a digest interpretation.  If not, try
7384 to guess what the document format is."
7385   (interactive "P")
7386   (let ((conf gnus-current-window-configuration))
7387     (save-excursion
7388       (gnus-summary-select-article))
7389     (setq gnus-current-window-configuration conf)
7390     (let* ((name (format "%s-%d"
7391                          (gnus-group-prefixed-name
7392                           gnus-newsgroup-name (list 'nndoc ""))
7393                          (save-excursion
7394                            (set-buffer gnus-summary-buffer)
7395                            gnus-current-article)))
7396            (ogroup gnus-newsgroup-name)
7397            (params (append (gnus-info-params (gnus-get-info ogroup))
7398                            (list (cons 'to-group ogroup))
7399                            (list (cons 'save-article-group ogroup))))
7400            (case-fold-search t)
7401            (buf (current-buffer))
7402            dig to-address)
7403       (save-excursion
7404         (set-buffer gnus-original-article-buffer)
7405         ;; Have the digest group inherit the main mail address of
7406         ;; the parent article.
7407         (when (setq to-address (or (message-fetch-field "reply-to")
7408                                    (message-fetch-field "from")))
7409           (setq params (append
7410                         (list (cons 'to-address
7411                                     (funcall gnus-decode-encoded-word-function
7412                                              to-address))))))
7413         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7414         (insert-buffer-substring gnus-original-article-buffer)
7415         ;; Remove lines that may lead nndoc to misinterpret the
7416         ;; document type.
7417         (narrow-to-region
7418          (goto-char (point-min))
7419          (or (search-forward "\n\n" nil t) (point)))
7420         (goto-char (point-min))
7421         (delete-matching-lines "^Path:\\|^From ")
7422         (widen))
7423       (unwind-protect
7424           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7425                     (gnus-newsgroup-ephemeral-ignored-charsets
7426                      gnus-newsgroup-ignored-charsets))
7427                 (gnus-group-read-ephemeral-group
7428                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7429                               (nndoc-article-type
7430                                ,(if force 'mbox 'guess))) t))
7431             ;; Make all postings to this group go to the parent group.
7432               (nconc (gnus-info-params (gnus-get-info name))
7433                      params)
7434             ;; Couldn't select this doc group.
7435             (switch-to-buffer buf)
7436             (gnus-set-global-variables)
7437             (gnus-configure-windows 'summary)
7438             (gnus-message 3 "Article couldn't be entered?"))
7439         (kill-buffer dig)))))
7440
7441 (defun gnus-summary-read-document (n)
7442   "Open a new group based on the current article(s).
7443 This will allow you to read digests and other similar
7444 documents as newsgroups.
7445 Obeys the standard process/prefix convention."
7446   (interactive "P")
7447   (let* ((articles (gnus-summary-work-articles n))
7448          (ogroup gnus-newsgroup-name)
7449          (params (append (gnus-info-params (gnus-get-info ogroup))
7450                          (list (cons 'to-group ogroup))))
7451          article group egroup groups vgroup)
7452     (while (setq article (pop articles))
7453       (setq group (format "%s-%d" gnus-newsgroup-name article))
7454       (gnus-summary-remove-process-mark article)
7455       (when (gnus-summary-display-article article)
7456         (save-excursion
7457           (with-temp-buffer
7458             (insert-buffer-substring gnus-original-article-buffer)
7459             ;; Remove some headers that may lead nndoc to make
7460             ;; the wrong guess.
7461             (message-narrow-to-head)
7462             (goto-char (point-min))
7463             (delete-matching-lines "^\\(Path\\):\\|^From ")
7464             (widen)
7465             (if (setq egroup
7466                       (gnus-group-read-ephemeral-group
7467                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7468                                      (nndoc-article-type guess))
7469                        t nil t))
7470                 (progn
7471             ;; Make all postings to this group go to the parent group.
7472                   (nconc (gnus-info-params (gnus-get-info egroup))
7473                          params)
7474                   (push egroup groups))
7475               ;; Couldn't select this doc group.
7476               (gnus-error 3 "Article couldn't be entered"))))))
7477     ;; Now we have selected all the documents.
7478     (cond
7479      ((not groups)
7480       (error "None of the articles could be interpreted as documents"))
7481      ((gnus-group-read-ephemeral-group
7482        (setq vgroup (format
7483                      "nnvirtual:%s-%s" gnus-newsgroup-name
7484                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7485        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7486        t
7487        (cons (current-buffer) 'summary)))
7488      (t
7489       (error "Couldn't select virtual nndoc group")))))
7490
7491 (defun gnus-summary-isearch-article (&optional regexp-p)
7492   "Do incremental search forward on the current article.
7493 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7494   (interactive "P")
7495   (gnus-summary-select-article)
7496   (gnus-configure-windows 'article)
7497   (gnus-eval-in-buffer-window gnus-article-buffer
7498     (save-restriction
7499       (widen)
7500       (isearch-forward regexp-p))))
7501
7502 (defun gnus-summary-search-article-forward (regexp &optional backward)
7503   "Search for an article containing REGEXP forward.
7504 If BACKWARD, search backward instead."
7505   (interactive
7506    (list (read-string
7507           (format "Search article %s (regexp%s): "
7508                   (if current-prefix-arg "backward" "forward")
7509                   (if gnus-last-search-regexp
7510                       (concat ", default " gnus-last-search-regexp)
7511                     "")))
7512          current-prefix-arg))
7513   (if (string-equal regexp "")
7514       (setq regexp (or gnus-last-search-regexp ""))
7515     (setq gnus-last-search-regexp regexp)
7516     (setq gnus-article-before-search gnus-current-article))
7517   ;; Intentionally set gnus-last-article.
7518   (setq gnus-last-article gnus-article-before-search)
7519   (let ((gnus-last-article gnus-last-article))
7520     (if (gnus-summary-search-article regexp backward)
7521         (gnus-summary-show-thread)
7522       (error "Search failed: \"%s\"" regexp))))
7523
7524 (defun gnus-summary-search-article-backward (regexp)
7525   "Search for an article containing REGEXP backward."
7526   (interactive
7527    (list (read-string
7528           (format "Search article backward (regexp%s): "
7529                   (if gnus-last-search-regexp
7530                       (concat ", default " gnus-last-search-regexp)
7531                     "")))))
7532   (gnus-summary-search-article-forward regexp 'backward))
7533
7534 (defun gnus-summary-search-article (regexp &optional backward)
7535   "Search for an article containing REGEXP.
7536 Optional argument BACKWARD means do search for backward.
7537 `gnus-select-article-hook' is not called during the search."
7538   ;; We have to require this here to make sure that the following
7539   ;; dynamic binding isn't shadowed by autoloading.
7540   (require 'gnus-async)
7541   (require 'gnus-art)
7542   (let ((gnus-select-article-hook nil)  ;Disable hook.
7543         (gnus-article-prepare-hook nil)
7544         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7545         (gnus-use-article-prefetch nil)
7546         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7547         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7548         (sum (current-buffer))
7549         (gnus-display-mime-function nil)
7550         (found nil)
7551         point)
7552     (gnus-save-hidden-threads
7553       (gnus-summary-select-article)
7554       (set-buffer gnus-article-buffer)
7555       (goto-char (window-point (get-buffer-window (current-buffer))))
7556       (when backward
7557         (forward-line -1))
7558       (while (not found)
7559         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7560         (if (if backward
7561                 (re-search-backward regexp nil t)
7562               (re-search-forward regexp nil t))
7563             ;; We found the regexp.
7564             (progn
7565               (setq found 'found)
7566               (beginning-of-line)
7567               (set-window-start
7568                (get-buffer-window (current-buffer))
7569                (point))
7570               (forward-line 1)
7571               (set-window-point
7572                (get-buffer-window (current-buffer))
7573                (point))
7574               (set-buffer sum)
7575               (setq point (point)))
7576           ;; We didn't find it, so we go to the next article.
7577           (set-buffer sum)
7578           (setq found 'not)
7579           (while (eq found 'not)
7580             (if (not (if backward (gnus-summary-find-prev)
7581                        (gnus-summary-find-next)))
7582                 ;; No more articles.
7583                 (setq found t)
7584               ;; Select the next article and adjust point.
7585               (unless (gnus-summary-article-sparse-p
7586                        (gnus-summary-article-number))
7587                 (setq found nil)
7588                 (gnus-summary-select-article)
7589                 (set-buffer gnus-article-buffer)
7590                 (widen)
7591                 (goto-char (if backward (point-max) (point-min))))))))
7592       (gnus-message 7 ""))
7593     ;; Return whether we found the regexp.
7594     (when (eq found 'found)
7595       (goto-char point)
7596       (gnus-summary-show-thread)
7597       (gnus-summary-goto-subject gnus-current-article)
7598       (gnus-summary-position-point)
7599       t)))
7600
7601 (defun gnus-find-matching-articles (header regexp)
7602   "Return a list of all articles that match REGEXP on HEADER.
7603 This search includes all articles in the current group that Gnus has
7604 fetched headers for, whether they are displayed or not."
7605   (let ((articles nil)
7606         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7607         (case-fold-search t))
7608     (dolist (header gnus-newsgroup-headers)
7609       (when (string-match regexp (funcall func header))
7610         (push (mail-header-number header) articles)))
7611     (nreverse articles)))
7612
7613 (defun gnus-summary-find-matching (header regexp &optional backward unread
7614                                           not-case-fold)
7615   "Return a list of all articles that match REGEXP on HEADER.
7616 The search stars on the current article and goes forwards unless
7617 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7618 If UNREAD is non-nil, only unread articles will
7619 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7620 in the comparisons."
7621   (let ((case-fold-search (not not-case-fold))
7622         articles d func)
7623     (if (consp header)
7624         (if (eq (car header) 'extra)
7625             (setq func
7626                   `(lambda (h)
7627                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7628                          "")))
7629           (error "%s is an invalid header" header))
7630       (unless (fboundp (intern (concat "mail-header-" header)))
7631         (error "%s is not a valid header" header))
7632       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7633     (dolist (d (if (eq backward 'all)
7634                    gnus-newsgroup-data
7635                  (gnus-data-find-list
7636                   (gnus-summary-article-number)
7637                   (gnus-data-list backward))))
7638       (when (and (or (not unread)       ; We want all articles...
7639                      (gnus-data-unread-p d)) ; Or just unreads.
7640                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7641                  (string-match regexp
7642                                (funcall func (gnus-data-header d)))) ; Match.
7643         (push (gnus-data-number d) articles))) ; Success!
7644     (nreverse articles)))
7645
7646 (defun gnus-summary-execute-command (header regexp command &optional backward)
7647   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7648 If HEADER is an empty string (or nil), the match is done on the entire
7649 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7650   (interactive
7651    (list (let ((completion-ignore-case t))
7652            (completing-read
7653             "Header name: "
7654             (mapcar (lambda (header) (list (format "%s" header)))
7655                     (append
7656                      '("Number" "Subject" "From" "Lines" "Date"
7657                        "Message-ID" "Xref" "References" "Body")
7658                      gnus-extra-headers))
7659             nil 'require-match))
7660          (read-string "Regexp: ")
7661          (read-key-sequence "Command: ")
7662          current-prefix-arg))
7663   (when (equal header "Body")
7664     (setq header ""))
7665   ;; Hidden thread subtrees must be searched as well.
7666   (gnus-summary-show-all-threads)
7667   ;; We don't want to change current point nor window configuration.
7668   (save-excursion
7669     (save-window-excursion
7670       (gnus-message 6 "Executing %s..." (key-description command))
7671 ;; We'd like to execute COMMAND interactively so as to give arguments.
7672       (gnus-execute header regexp
7673                     `(call-interactively ',(key-binding command))
7674                     backward)
7675       (gnus-message 6 "Executing %s...done" (key-description command)))))
7676
7677 (defun gnus-summary-beginning-of-article ()
7678   "Scroll the article back to the beginning."
7679   (interactive)
7680   (gnus-summary-select-article)
7681   (gnus-configure-windows 'article)
7682   (gnus-eval-in-buffer-window gnus-article-buffer
7683     (widen)
7684     (goto-char (point-min))
7685     (when gnus-page-broken
7686       (gnus-narrow-to-page))))
7687
7688 (defun gnus-summary-end-of-article ()
7689   "Scroll to the end of the article."
7690   (interactive)
7691   (gnus-summary-select-article)
7692   (gnus-configure-windows 'article)
7693   (gnus-eval-in-buffer-window gnus-article-buffer
7694     (widen)
7695     (goto-char (point-max))
7696     (recenter -3)
7697     (when gnus-page-broken
7698       (gnus-narrow-to-page))))
7699
7700 (defun gnus-summary-print-article (&optional filename n)
7701   "Generate and print a PostScript image of the N next (mail) articles.
7702
7703 If N is negative, print the N previous articles.  If N is nil and articles
7704 have been marked with the process mark, print these instead.
7705
7706 If the optional first argument FILENAME is nil, send the image to the
7707 printer.  If FILENAME is a string, save the PostScript image in a file with
7708 that name.  If FILENAME is a number, prompt the user for the name of the file
7709 to save in."
7710   (interactive (list (ps-print-preprint current-prefix-arg)))
7711   (dolist (article (gnus-summary-work-articles n))
7712     (gnus-summary-select-article nil nil 'pseudo article)
7713     (gnus-eval-in-buffer-window gnus-article-buffer
7714       (let ((buffer (generate-new-buffer " *print*")))
7715         (unwind-protect
7716             (progn
7717               (copy-to-buffer buffer (point-min) (point-max))
7718               (set-buffer buffer)
7719               (gnus-article-delete-invisible-text)
7720               (when (gnus-visual-p 'article-highlight 'highlight)
7721                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7722                 ;; highlight.
7723                 (let ((gnus-article-buffer buffer))
7724                   (gnus-article-highlight-citation t)
7725                   (gnus-article-highlight-signature)))
7726               (let ((ps-left-header
7727                      (list
7728                       (concat "("
7729                               (mail-header-subject gnus-current-headers) ")")
7730                       (concat "("
7731                               (mail-header-from gnus-current-headers) ")")))
7732                     (ps-right-header
7733                      (list
7734                       "/pagenumberstring load"
7735                       (concat "("
7736                               (mail-header-date gnus-current-headers) ")"))))
7737                 (gnus-run-hooks 'gnus-ps-print-hook)
7738                 (save-excursion
7739                   (if window-system
7740                       (ps-spool-buffer-with-faces)
7741                     (ps-spool-buffer)))))
7742           (kill-buffer buffer))))
7743     (gnus-summary-remove-process-mark article))
7744   (ps-despool filename))
7745
7746 (defun gnus-summary-show-article (&optional arg)
7747   "Force re-fetching of the current article.
7748 If ARG (the prefix) is a number, show the article with the charset
7749 defined in `gnus-summary-show-article-charset-alist', or the charset
7750 inputed.
7751 If ARG (the prefix) is non-nil and not a number, show the raw article
7752 without any article massaging functions being run."
7753   (interactive "P")
7754   (cond
7755    ((numberp arg)
7756     (let ((gnus-newsgroup-charset
7757            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7758                (mm-read-coding-system "Charset: ")))
7759           (gnus-newsgroup-ignored-charsets 'gnus-all))
7760       (gnus-summary-select-article nil 'force)
7761       (let ((deps gnus-newsgroup-dependencies)
7762             head header)
7763         (save-excursion
7764           (set-buffer gnus-original-article-buffer)
7765           (save-restriction
7766             (message-narrow-to-head)
7767             (setq head (buffer-string)))
7768           (with-temp-buffer
7769             (insert (format "211 %d Article retrieved.\n"
7770                             (cdr gnus-article-current)))
7771             (insert head)
7772             (insert ".\n")
7773             (let ((nntp-server-buffer (current-buffer)))
7774               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7775         (gnus-data-set-header
7776          (gnus-data-find (cdr gnus-article-current))
7777          header)
7778         (gnus-summary-update-article-line
7779          (cdr gnus-article-current) header)
7780         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7781             (gnus-summary-update-secondary-mark
7782              (cdr gnus-article-current))))))
7783    ((not arg)
7784     ;; Select the article the normal way.
7785     (gnus-summary-select-article nil 'force))
7786    (t
7787     ;; We have to require this here to make sure that the following
7788     ;; dynamic binding isn't shadowed by autoloading.
7789     (require 'gnus-async)
7790     (require 'gnus-art)
7791     ;; Bind the article treatment functions to nil.
7792     (let ((gnus-have-all-headers t)
7793           gnus-article-prepare-hook
7794           gnus-article-decode-hook
7795           gnus-display-mime-function
7796           gnus-break-pages)
7797       ;; Destroy any MIME parts.
7798       (when (gnus-buffer-live-p gnus-article-buffer)
7799         (save-excursion
7800           (set-buffer gnus-article-buffer)
7801           (mm-destroy-parts gnus-article-mime-handles)
7802           ;; Set it to nil for safety reason.
7803           (setq gnus-article-mime-handle-alist nil)
7804           (setq gnus-article-mime-handles nil)))
7805       (gnus-summary-select-article nil 'force))))
7806   (gnus-summary-goto-subject gnus-current-article)
7807   (gnus-summary-position-point))
7808
7809 (defun gnus-summary-show-raw-article ()
7810   "Show the raw article without any article massaging functions being run."
7811   (interactive)
7812   (gnus-summary-show-article t))
7813
7814 (defun gnus-summary-verbose-headers (&optional arg)
7815   "Toggle permanent full header display.
7816 If ARG is a positive number, turn header display on.
7817 If ARG is a negative number, turn header display off."
7818   (interactive "P")
7819   (setq gnus-show-all-headers
7820         (cond ((or (not (numberp arg))
7821                    (zerop arg))
7822                (not gnus-show-all-headers))
7823               ((natnump arg)
7824                t)))
7825   (gnus-summary-show-article))
7826
7827 (defun gnus-summary-toggle-header (&optional arg)
7828   "Show the headers if they are hidden, or hide them if they are shown.
7829 If ARG is a positive number, show the entire header.
7830 If ARG is a negative number, hide the unwanted header lines."
7831   (interactive "P")
7832   (save-excursion
7833     (set-buffer gnus-article-buffer)
7834     (save-restriction
7835       (let* ((buffer-read-only nil)
7836              (inhibit-point-motion-hooks t)
7837              hidden e)
7838         (setq hidden
7839               (if (numberp arg)
7840                   (>= arg 0)
7841                 (save-restriction
7842                   (article-narrow-to-head)
7843                   (gnus-article-hidden-text-p 'headers))))
7844         (goto-char (point-min))
7845         (when (search-forward "\n\n" nil t)
7846           (delete-region (point-min) (1- (point))))
7847         (goto-char (point-min))
7848         (save-excursion
7849           (set-buffer gnus-original-article-buffer)
7850           (goto-char (point-min))
7851           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7852         (insert-buffer-substring gnus-original-article-buffer 1 e)
7853         (save-restriction
7854           (narrow-to-region (point-min) (point))
7855           (article-decode-encoded-words)
7856           (if  hidden
7857               (let ((gnus-treat-hide-headers nil)
7858                     (gnus-treat-hide-boring-headers nil))
7859                 (setq gnus-article-wash-types
7860                       (delq 'headers gnus-article-wash-types))
7861                 (gnus-treat-article 'head))
7862             (gnus-treat-article 'head)))
7863         (gnus-set-mode-line 'article)))))
7864
7865 (defun gnus-summary-show-all-headers ()
7866   "Make all header lines visible."
7867   (interactive)
7868   (gnus-summary-toggle-header 1))
7869
7870 (defun gnus-summary-caesar-message (&optional arg)
7871   "Caesar rotate the current article by 13.
7872 The numerical prefix specifies how many places to rotate each letter
7873 forward."
7874   (interactive "P")
7875   (gnus-summary-select-article)
7876   (let ((mail-header-separator ""))
7877     (gnus-eval-in-buffer-window gnus-article-buffer
7878       (save-restriction
7879         (widen)
7880         (let ((start (window-start))
7881               buffer-read-only)
7882           (message-caesar-buffer-body arg)
7883           (set-window-start (get-buffer-window (current-buffer)) start))))))
7884
7885 (defun gnus-summary-stop-page-breaking ()
7886   "Stop page breaking in the current article."
7887   (interactive)
7888   (gnus-summary-select-article)
7889   (gnus-eval-in-buffer-window gnus-article-buffer
7890     (widen)
7891     (when (gnus-visual-p 'page-marker)
7892       (let ((buffer-read-only nil))
7893         (gnus-remove-text-with-property 'gnus-prev)
7894         (gnus-remove-text-with-property 'gnus-next))
7895       (setq gnus-page-broken nil))))
7896
7897 (defun gnus-summary-move-article (&optional n to-newsgroup
7898                                             select-method action)
7899   "Move the current article to a different newsgroup.
7900 If N is a positive number, move the N next articles.
7901 If N is a negative number, move the N previous articles.
7902 If N is nil and any articles have been marked with the process mark,
7903 move those articles instead.
7904 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7905 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7906 re-spool using this method.
7907
7908 For this function to work, both the current newsgroup and the
7909 newsgroup that you want to move to have to support the `request-move'
7910 and `request-accept' functions.
7911
7912 ACTION can be either `move' (the default), `crosspost' or `copy'."
7913   (interactive "P")
7914   (unless action
7915     (setq action 'move))
7916   ;; Disable marking as read.
7917   (let (gnus-mark-article-hook)
7918     (save-window-excursion
7919       (gnus-summary-select-article)))
7920   ;; Check whether the source group supports the required functions.
7921   (cond ((and (eq action 'move)
7922               (not (gnus-check-backend-function
7923                     'request-move-article gnus-newsgroup-name)))
7924          (error "The current group does not support article moving"))
7925         ((and (eq action 'crosspost)
7926               (not (gnus-check-backend-function
7927                     'request-replace-article gnus-newsgroup-name)))
7928          (error "The current group does not support article editing")))
7929   (let ((articles (gnus-summary-work-articles n))
7930         (prefix (if (gnus-check-backend-function
7931                      'request-move-article gnus-newsgroup-name)
7932                     (gnus-group-real-prefix gnus-newsgroup-name)
7933                   ""))
7934         (names '((move "Move" "Moving")
7935                  (copy "Copy" "Copying")
7936                  (crosspost "Crosspost" "Crossposting")))
7937         (copy-buf (save-excursion
7938                     (nnheader-set-temp-buffer " *copy article*")))
7939         art-group to-method new-xref article to-groups)
7940     (unless (assq action names)
7941       (error "Unknown action %s" action))
7942     ;; Read the newsgroup name.
7943     (when (and (not to-newsgroup)
7944                (not select-method))
7945       (setq to-newsgroup
7946             (gnus-read-move-group-name
7947              (cadr (assq action names))
7948              (symbol-value (intern (format "gnus-current-%s-group" action)))
7949              articles prefix))
7950       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7951     (setq to-method (or select-method
7952                         (gnus-server-to-method
7953                          (gnus-group-method to-newsgroup))))
7954     ;; Check the method we are to move this article to...
7955     (unless (gnus-check-backend-function
7956              'request-accept-article (car to-method))
7957       (error "%s does not support article copying" (car to-method)))
7958     (unless (gnus-check-server to-method)
7959       (error "Can't open server %s" (car to-method)))
7960     (gnus-message 6 "%s to %s: %s..."
7961                   (caddr (assq action names))
7962                   (or (car select-method) to-newsgroup) articles)
7963     (while articles
7964       (setq article (pop articles))
7965       (setq
7966        art-group
7967        (cond
7968         ;; Move the article.
7969         ((eq action 'move)
7970          ;; Remove this article from future suppression.
7971          (gnus-dup-unsuppress-article article)
7972          (gnus-request-move-article
7973           article                       ; Article to move
7974           gnus-newsgroup-name           ; From newsgroup
7975           (nth 1 (gnus-find-method-for-group
7976                   gnus-newsgroup-name)) ; Server
7977           (list 'gnus-request-accept-article
7978                 to-newsgroup (list 'quote select-method)
7979                 (not articles) t)       ; Accept form
7980           (not articles)))              ; Only save nov last time
7981         ;; Copy the article.
7982         ((eq action 'copy)
7983          (save-excursion
7984            (set-buffer copy-buf)
7985            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7986              (gnus-request-accept-article
7987               to-newsgroup select-method (not articles) t))))
7988         ;; Crosspost the article.
7989         ((eq action 'crosspost)
7990          (let ((xref (message-tokenize-header
7991                       (mail-header-xref (gnus-summary-article-header article))
7992                       " ")))
7993            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7994                                   ":" article))
7995            (unless xref
7996              (setq xref (list (system-name))))
7997            (setq new-xref
7998                  (concat
7999                   (mapconcat 'identity
8000                              (delete "Xref:" (delete new-xref xref))
8001                              " ")
8002                   " " new-xref))
8003            (save-excursion
8004              (set-buffer copy-buf)
8005              ;; First put the article in the destination group.
8006              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8007              (when (consp (setq art-group
8008                                 (gnus-request-accept-article
8009                                  to-newsgroup select-method (not articles))))
8010                (setq new-xref (concat new-xref " " (car art-group)
8011                                       ":" (cdr art-group)))
8012                ;; Now we have the new Xrefs header, so we insert
8013                ;; it and replace the new article.
8014                (nnheader-replace-header "Xref" new-xref)
8015                (gnus-request-replace-article
8016                 (cdr art-group) to-newsgroup (current-buffer))
8017                art-group))))))
8018       (cond
8019        ((not art-group)
8020         (gnus-message 1 "Couldn't %s article %s: %s"
8021                       (cadr (assq action names)) article
8022                       (nnheader-get-report (car to-method))))
8023        ((eq art-group 'junk)
8024         (when (eq action 'move)
8025           (gnus-summary-mark-article article gnus-canceled-mark)
8026           (gnus-message 4 "Deleted article %s" article)))
8027        (t
8028         (let* ((pto-group (gnus-group-prefixed-name
8029                            (car art-group) to-method))
8030                (entry
8031                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8032                (info (nth 2 entry))
8033                (to-group (gnus-info-group info))
8034                to-marks)
8035           ;; Update the group that has been moved to.
8036           (when (and info
8037                      (memq action '(move copy)))
8038             (unless (member to-group to-groups)
8039               (push to-group to-groups))
8040
8041             (unless (memq article gnus-newsgroup-unreads)
8042               (push 'read to-marks)
8043               (gnus-info-set-read
8044                info (gnus-add-to-range (gnus-info-read info)
8045                                        (list (cdr art-group)))))
8046
8047             ;; See whether the article is to be put in the cache.
8048             (let ((marks gnus-article-mark-lists)
8049                   (to-article (cdr art-group)))
8050
8051               ;; Enter the article into the cache in the new group,
8052               ;; if that is required.
8053               (when gnus-use-cache
8054                 (gnus-cache-possibly-enter-article
8055                  to-group to-article
8056                  (memq article gnus-newsgroup-marked)
8057                  (memq article gnus-newsgroup-dormant)
8058                  (memq article gnus-newsgroup-unreads)))
8059
8060               (when gnus-preserve-marks
8061                 ;; Copy any marks over to the new group.
8062                 (when (and (equal to-group gnus-newsgroup-name)
8063                            (not (memq article gnus-newsgroup-unreads)))
8064                   ;; Mark this article as read in this group.
8065                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8066                   (setcdr (gnus-active to-group) to-article)
8067                   (setcdr gnus-newsgroup-active to-article))
8068
8069                 (while marks
8070                   (when (memq article (symbol-value
8071                                        (intern (format "gnus-newsgroup-%s"
8072                                                        (caar marks)))))
8073                     (push (cdar marks) to-marks)
8074                     ;; If the other group is the same as this group,
8075                     ;; then we have to add the mark to the list.
8076                     (when (equal to-group gnus-newsgroup-name)
8077                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8078                            (cons to-article
8079                                  (symbol-value
8080                                   (intern (format "gnus-newsgroup-%s"
8081                                                   (caar marks)))))))
8082                     ;; Copy the marks to other group.
8083                     (gnus-add-marked-articles
8084                      to-group (cdar marks) (list to-article) info))
8085                   (setq marks (cdr marks)))
8086
8087                 (gnus-request-set-mark to-group (list (list (list to-article)
8088                                                             'add
8089                                                             to-marks))))
8090
8091               (gnus-dribble-enter
8092                (concat "(gnus-group-set-info '"
8093                        (gnus-prin1-to-string (gnus-get-info to-group))
8094                        ")"))))
8095
8096           ;; Update the Xref header in this article to point to
8097           ;; the new crossposted article we have just created.
8098           (when (eq action 'crosspost)
8099             (save-excursion
8100               (set-buffer copy-buf)
8101               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8102               (nnheader-replace-header "Xref" new-xref)
8103               (gnus-request-replace-article
8104                article gnus-newsgroup-name (current-buffer)))))
8105
8106         ;;;!!!Why is this necessary?
8107         (set-buffer gnus-summary-buffer)
8108
8109         (gnus-summary-goto-subject article)
8110         (when (eq action 'move)
8111           (gnus-summary-mark-article article gnus-canceled-mark))))
8112       (gnus-summary-remove-process-mark article))
8113     ;; Re-activate all groups that have been moved to.
8114     (while to-groups
8115       (save-excursion
8116         (set-buffer gnus-group-buffer)
8117         (when (gnus-group-goto-group (car to-groups) t)
8118           (gnus-group-get-new-news-this-group 1 t))
8119         (pop to-groups)))
8120
8121     (gnus-kill-buffer copy-buf)
8122     (gnus-summary-position-point)
8123     (gnus-set-mode-line 'summary)))
8124
8125 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8126   "Move the current article to a different newsgroup.
8127 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8128 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8129 re-spool using this method."
8130   (interactive "P")
8131   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8132
8133 (defun gnus-summary-crosspost-article (&optional n)
8134   "Crosspost the current article to some other group."
8135   (interactive "P")
8136   (gnus-summary-move-article n nil nil 'crosspost))
8137
8138 (defcustom gnus-summary-respool-default-method nil
8139   "Default method for respooling an article.
8140 If nil, use to the current newsgroup method."
8141   :type '(choice (gnus-select-method :value (nnml ""))
8142                  (const nil))
8143   :group 'gnus-summary-mail)
8144
8145 (defun gnus-summary-respool-article (&optional n method)
8146   "Respool the current article.
8147 The article will be squeezed through the mail spooling process again,
8148 which means that it will be put in some mail newsgroup or other
8149 depending on `nnmail-split-methods'.
8150 If N is a positive number, respool the N next articles.
8151 If N is a negative number, respool the N previous articles.
8152 If N is nil and any articles have been marked with the process mark,
8153 respool those articles instead.
8154
8155 Respooling can be done both from mail groups and \"real\" newsgroups.
8156 In the former case, the articles in question will be moved from the
8157 current group into whatever groups they are destined to.  In the
8158 latter case, they will be copied into the relevant groups."
8159   (interactive
8160    (list current-prefix-arg
8161          (let* ((methods (gnus-methods-using 'respool))
8162                 (methname
8163                  (symbol-name (or gnus-summary-respool-default-method
8164                                   (car (gnus-find-method-for-group
8165                                         gnus-newsgroup-name)))))
8166                 (method
8167                  (gnus-completing-read
8168                   methname "What backend do you want to use when respooling?"
8169                   methods nil t nil 'gnus-mail-method-history))
8170                 ms)
8171            (cond
8172             ((zerop (length (setq ms (gnus-servers-using-backend
8173                                       (intern method)))))
8174              (list (intern method) ""))
8175             ((= 1 (length ms))
8176              (car ms))
8177             (t
8178              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8179                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8180                            ms-alist))))))))
8181   (unless method
8182     (error "No method given for respooling"))
8183   (if (assoc (symbol-name
8184               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8185              (gnus-methods-using 'respool))
8186       (gnus-summary-move-article n nil method)
8187     (gnus-summary-copy-article n nil method)))
8188
8189 (defun gnus-summary-import-article (file &optional edit)
8190   "Import an arbitrary file into a mail newsgroup."
8191   (interactive "fImport file: \nP")
8192   (let ((group gnus-newsgroup-name)
8193         (now (current-time))
8194         atts lines group-art)
8195     (unless (gnus-check-backend-function 'request-accept-article group)
8196       (error "%s does not support article importing" group))
8197     (or (file-readable-p file)
8198         (not (file-regular-p file))
8199         (error "Can't read %s" file))
8200     (save-excursion
8201       (set-buffer (gnus-get-buffer-create " *import file*"))
8202       (erase-buffer)
8203       (nnheader-insert-file-contents file)
8204       (goto-char (point-min))
8205       (if (nnheader-article-p)
8206           (save-restriction
8207             (goto-char (point-min))
8208             (search-forward "\n\n" nil t)
8209             (narrow-to-region (point-min) (1- (point)))
8210             (goto-char (point-min))
8211             (unless (re-search-forward "^date:" nil t)
8212               (goto-char (point-max))
8213               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8214        ;; This doesn't look like an article, so we fudge some headers.
8215         (setq atts (file-attributes file)
8216               lines (count-lines (point-min) (point-max)))
8217         (insert "From: " (read-string "From: ") "\n"
8218                 "Subject: " (read-string "Subject: ") "\n"
8219                 "Date: " (message-make-date (nth 5 atts)) "\n"
8220                 "Message-ID: " (message-make-message-id) "\n"
8221                 "Lines: " (int-to-string lines) "\n"
8222                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8223       (setq group-art (gnus-request-accept-article group nil t))
8224       (kill-buffer (current-buffer)))
8225     (setq gnus-newsgroup-active (gnus-activate-group group))
8226     (forward-line 1)
8227     (gnus-summary-goto-article (cdr group-art) nil t)
8228     (when edit
8229       (gnus-summary-edit-article))))
8230
8231 (defun gnus-summary-create-article ()
8232   "Create an article in a mail newsgroup."
8233   (interactive)
8234   (let ((group gnus-newsgroup-name)
8235         (now (current-time))
8236         group-art)
8237     (unless (gnus-check-backend-function 'request-accept-article group)
8238       (error "%s does not support article importing" group))
8239     (save-excursion
8240       (set-buffer (gnus-get-buffer-create " *import file*"))
8241       (erase-buffer)
8242       (goto-char (point-min))
8243       ;; This doesn't look like an article, so we fudge some headers.
8244       (insert "From: " (read-string "From: ") "\n"
8245               "Subject: " (read-string "Subject: ") "\n"
8246               "Date: " (message-make-date now) "\n"
8247               "Message-ID: " (message-make-message-id) "\n")
8248       (setq group-art (gnus-request-accept-article group nil t))
8249       (kill-buffer (current-buffer)))
8250     (setq gnus-newsgroup-active (gnus-activate-group group))
8251     (forward-line 1)
8252     (gnus-summary-goto-article (cdr group-art) nil t)
8253     (gnus-summary-edit-article)))
8254
8255 (defun gnus-summary-article-posted-p ()
8256   "Say whether the current (mail) article is available from news as well.
8257 This will be the case if the article has both been mailed and posted."
8258   (interactive)
8259   (let ((id (mail-header-references (gnus-summary-article-header)))
8260         (gnus-override-method (car (gnus-refer-article-methods))))
8261     (if (gnus-request-head id "")
8262         (gnus-message 2 "The current message was found on %s"
8263                       gnus-override-method)
8264       (gnus-message 2 "The current message couldn't be found on %s"
8265                     gnus-override-method)
8266       nil)))
8267
8268 (defun gnus-summary-expire-articles (&optional now)
8269   "Expire all articles that are marked as expirable in the current group."
8270   (interactive)
8271   (when (gnus-check-backend-function
8272          'request-expire-articles gnus-newsgroup-name)
8273     ;; This backend supports expiry.
8274     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8275            (expirable (if total
8276                           (progn
8277                             ;; We need to update the info for
8278                         ;; this group for `gnus-list-of-read-articles'
8279                             ;; to give us the right answer.
8280                             (gnus-run-hooks 'gnus-exit-group-hook)
8281                             (gnus-summary-update-info)
8282                             (gnus-list-of-read-articles gnus-newsgroup-name))
8283                         (setq gnus-newsgroup-expirable
8284                               (sort gnus-newsgroup-expirable '<))))
8285            (expiry-wait (if now 'immediate
8286                           (gnus-group-find-parameter
8287                            gnus-newsgroup-name 'expiry-wait)))
8288            (nnmail-expiry-target
8289             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8290                 nnmail-expiry-target))
8291            es)
8292       (when expirable
8293         ;; There are expirable articles in this group, so we run them
8294         ;; through the expiry process.
8295         (gnus-message 6 "Expiring articles...")
8296         (unless (gnus-check-group gnus-newsgroup-name)
8297           (error "Can't open server for %s" gnus-newsgroup-name))
8298         ;; The list of articles that weren't expired is returned.
8299         (save-excursion
8300           (if expiry-wait
8301               (let ((nnmail-expiry-wait-function nil)
8302                     (nnmail-expiry-wait expiry-wait))
8303                 (setq es (gnus-request-expire-articles
8304                           expirable gnus-newsgroup-name)))
8305             (setq es (gnus-request-expire-articles
8306                       expirable gnus-newsgroup-name)))
8307           (unless total
8308             (setq gnus-newsgroup-expirable es))
8309           ;; We go through the old list of expirable, and mark all
8310           ;; really expired articles as nonexistent.
8311           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8312             (let ((gnus-use-cache nil))
8313               (while expirable
8314                 (unless (memq (car expirable) es)
8315                   (when (gnus-data-find (car expirable))
8316                     (gnus-summary-mark-article
8317                      (car expirable) gnus-canceled-mark)))
8318                 (setq expirable (cdr expirable))))))
8319         (gnus-message 6 "Expiring articles...done")))))
8320
8321 (defun gnus-summary-expire-articles-now ()
8322   "Expunge all expirable articles in the current group.
8323 This means that *all* articles that are marked as expirable will be
8324 deleted forever, right now."
8325   (interactive)
8326   (or gnus-expert-user
8327       (gnus-yes-or-no-p
8328        "Are you really, really, really sure you want to delete all these messages? ")
8329       (error "Phew!"))
8330   (gnus-summary-expire-articles t))
8331
8332 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8333 (defun gnus-summary-delete-article (&optional n)
8334   "Delete the N next (mail) articles.
8335 This command actually deletes articles.  This is not a marking
8336 command.  The article will disappear forever from your life, never to
8337 return.
8338 If N is negative, delete backwards.
8339 If N is nil and articles have been marked with the process mark,
8340 delete these instead."
8341   (interactive "P")
8342   (unless (gnus-check-backend-function 'request-expire-articles
8343                                        gnus-newsgroup-name)
8344     (error "The current newsgroup does not support article deletion"))
8345   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8346     (error "Couldn't open server"))
8347   ;; Compute the list of articles to delete.
8348   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8349         not-deleted)
8350     (if (and gnus-novice-user
8351              (not (gnus-yes-or-no-p
8352                    (format "Do you really want to delete %s forever? "
8353                            (if (> (length articles) 1)
8354                                (format "these %s articles" (length articles))
8355                              "this article")))))
8356         ()
8357       ;; Delete the articles.
8358       (setq not-deleted (gnus-request-expire-articles
8359                          articles gnus-newsgroup-name 'force))
8360       (while articles
8361         (gnus-summary-remove-process-mark (car articles))
8362         ;; The backend might not have been able to delete the article
8363         ;; after all.
8364         (unless (memq (car articles) not-deleted)
8365           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8366         (setq articles (cdr articles)))
8367       (when not-deleted
8368         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8369     (gnus-summary-position-point)
8370     (gnus-set-mode-line 'summary)
8371     not-deleted))
8372
8373 (defun gnus-summary-edit-article (&optional arg)
8374   "Edit the current article.
8375 This will have permanent effect only in mail groups.
8376 If ARG is nil, edit the decoded articles.
8377 If ARG is 1, edit the raw articles.
8378 If ARG is 2, edit the raw articles even in read-only groups.
8379 If ARG is 3, edit the articles with the current handles.
8380 Otherwise, allow editing of articles even in read-only
8381 groups."
8382   (interactive "P")
8383   (let (force raw current-handles)
8384     (cond
8385      ((null arg))
8386      ((eq arg 1) (setq raw t))
8387      ((eq arg 2) (setq raw t
8388                        force t))
8389      ((eq arg 3) (setq current-handles
8390                        (and (gnus-buffer-live-p gnus-article-buffer)
8391                             (with-current-buffer gnus-article-buffer
8392                               (prog1
8393                                   gnus-article-mime-handles
8394                                 (setq gnus-article-mime-handles nil))))))
8395      (t (setq force t)))
8396     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8397         (error "Can't edit the raw article in group nndraft:drafts"))
8398     (save-excursion
8399       (set-buffer gnus-summary-buffer)
8400       (let ((mail-parse-charset gnus-newsgroup-charset)
8401             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8402         (gnus-set-global-variables)
8403         (when (and (not force)
8404                    (gnus-group-read-only-p))
8405           (error "The current newsgroup does not support article editing"))
8406         (gnus-summary-show-article t)
8407         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8408           (with-current-buffer gnus-article-buffer
8409             (mm-enable-multibyte-mule4)))
8410         (if (equal gnus-newsgroup-name "nndraft:drafts")
8411             (setq raw t))
8412         (gnus-article-edit-article
8413          (if raw 'ignore
8414            `(lambda ()
8415               (let ((mbl mml-buffer-list))
8416                 (setq mml-buffer-list nil)
8417                 (mime-to-mml ,'current-handles)
8418                 (let ((mbl1 mml-buffer-list))
8419                   (setq mml-buffer-list mbl)
8420                   (set (make-local-variable 'mml-buffer-list) mbl1))
8421                 (make-local-hook 'kill-buffer-hook)
8422                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8423          `(lambda (no-highlight)
8424             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8425                   (message-options message-options)
8426                   (message-options-set-recipient)
8427                   (mail-parse-ignored-charsets
8428                    ',gnus-newsgroup-ignored-charsets))
8429               ,(if (not raw) '(progn
8430                                 (mml-to-mime)
8431                                 (mml-destroy-buffers)
8432                                 (remove-hook 'kill-buffer-hook
8433                                              'mml-destroy-buffers t)
8434                                 (kill-local-variable 'mml-buffer-list)))
8435               (gnus-summary-edit-article-done
8436                ,(or (mail-header-references gnus-current-headers) "")
8437                ,(gnus-group-read-only-p)
8438                ,gnus-summary-buffer no-highlight))))))))
8439
8440 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8441
8442 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8443                                                  no-highlight)
8444   "Make edits to the current article permanent."
8445   (interactive)
8446   (save-excursion
8447    ;; The buffer restriction contains the entire article if it exists.
8448     (when (article-goto-body)
8449       (let ((lines (count-lines (point) (point-max)))
8450             (length (- (point-max) (point)))
8451             (case-fold-search t)
8452             (body (copy-marker (point))))
8453         (goto-char (point-min))
8454         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8455           (delete-region (match-beginning 1) (match-end 1))
8456           (insert (number-to-string length)))
8457         (goto-char (point-min))
8458         (when (re-search-forward
8459                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8460           (delete-region (match-beginning 1) (match-end 1))
8461           (insert (number-to-string length)))
8462         (goto-char (point-min))
8463         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8464           (delete-region (match-beginning 1) (match-end 1))
8465           (insert (number-to-string lines))))))
8466   ;; Replace the article.
8467   (let ((buf (current-buffer)))
8468     (with-temp-buffer
8469       (insert-buffer-substring buf)
8470
8471       (if (and (not read-only)
8472                (not (gnus-request-replace-article
8473                      (cdr gnus-article-current) (car gnus-article-current)
8474                      (current-buffer) t)))
8475           (error "Couldn't replace article")
8476         ;; Update the summary buffer.
8477         (if (and references
8478                  (equal (message-tokenize-header references " ")
8479                         (message-tokenize-header
8480                          (or (message-fetch-field "references") "") " ")))
8481             ;; We only have to update this line.
8482             (save-excursion
8483               (save-restriction
8484                 (message-narrow-to-head)
8485                 (let ((head (buffer-string))
8486                       header)
8487                   (with-temp-buffer
8488                     (insert (format "211 %d Article retrieved.\n"
8489                                     (cdr gnus-article-current)))
8490                     (insert head)
8491                     (insert ".\n")
8492                     (let ((nntp-server-buffer (current-buffer)))
8493                       (setq header (car (gnus-get-newsgroup-headers
8494                                          (save-excursion
8495                                            (set-buffer gnus-summary-buffer)
8496                                            gnus-newsgroup-dependencies)
8497                                          t))))
8498                     (save-excursion
8499                       (set-buffer gnus-summary-buffer)
8500                       (gnus-data-set-header
8501                        (gnus-data-find (cdr gnus-article-current))
8502                        header)
8503                       (gnus-summary-update-article-line
8504                        (cdr gnus-article-current) header)
8505                       (if (gnus-summary-goto-subject
8506                            (cdr gnus-article-current) nil t)
8507                           (gnus-summary-update-secondary-mark
8508                            (cdr gnus-article-current))))))))
8509           ;; Update threads.
8510           (set-buffer (or buffer gnus-summary-buffer))
8511           (gnus-summary-update-article (cdr gnus-article-current))
8512           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8513               (gnus-summary-update-secondary-mark
8514                (cdr gnus-article-current))))
8515         ;; Prettify the article buffer again.
8516         (unless no-highlight
8517           (save-excursion
8518             (set-buffer gnus-article-buffer)
8519             ;;;!!! Fix this -- article should be rehighlighted.
8520             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8521             (set-buffer gnus-original-article-buffer)
8522             (gnus-request-article
8523              (cdr gnus-article-current)
8524              (car gnus-article-current) (current-buffer))))
8525         ;; Prettify the summary buffer line.
8526         (when (gnus-visual-p 'summary-highlight 'highlight)
8527           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8528
8529 (defun gnus-summary-edit-wash (key)
8530   "Perform editing command KEY in the article buffer."
8531   (interactive
8532    (list
8533     (progn
8534       (message "%s" (concat (this-command-keys) "- "))
8535       (read-char))))
8536   (message "")
8537   (gnus-summary-edit-article)
8538   (execute-kbd-macro (concat (this-command-keys) key))
8539   (gnus-article-edit-done))
8540
8541 ;;; Respooling
8542
8543 (defun gnus-summary-respool-query (&optional silent trace)
8544   "Query where the respool algorithm would put this article."
8545   (interactive)
8546   (let (gnus-mark-article-hook)
8547     (gnus-summary-select-article)
8548     (save-excursion
8549       (set-buffer gnus-original-article-buffer)
8550       (save-restriction
8551         (message-narrow-to-head)
8552         (let ((groups (nnmail-article-group 'identity trace)))
8553           (unless silent
8554             (if groups
8555                 (message "This message would go to %s"
8556                          (mapconcat 'car groups ", "))
8557               (message "This message would go to no groups"))
8558             groups))))))
8559
8560 (defun gnus-summary-respool-trace ()
8561   "Trace where the respool algorithm would put this article.
8562 Display a buffer showing all fancy splitting patterns which matched."
8563   (interactive)
8564   (gnus-summary-respool-query nil t))
8565
8566 ;; Summary marking commands.
8567
8568 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8569   "Mark articles which has the same subject as read, and then select the next.
8570 If UNMARK is positive, remove any kind of mark.
8571 If UNMARK is negative, tick articles."
8572   (interactive "P")
8573   (when unmark
8574     (setq unmark (prefix-numeric-value unmark)))
8575   (let ((count
8576          (gnus-summary-mark-same-subject
8577           (gnus-summary-article-subject) unmark)))
8578     ;; Select next unread article.  If auto-select-same mode, should
8579     ;; select the first unread article.
8580     (gnus-summary-next-article t (and gnus-auto-select-same
8581                                       (gnus-summary-article-subject)))
8582     (gnus-message 7 "%d article%s marked as %s"
8583                   count (if (= count 1) " is" "s are")
8584                   (if unmark "unread" "read"))))
8585
8586 (defun gnus-summary-kill-same-subject (&optional unmark)
8587   "Mark articles which has the same subject as read.
8588 If UNMARK is positive, remove any kind of mark.
8589 If UNMARK is negative, tick articles."
8590   (interactive "P")
8591   (when unmark
8592     (setq unmark (prefix-numeric-value unmark)))
8593   (let ((count
8594          (gnus-summary-mark-same-subject
8595           (gnus-summary-article-subject) unmark)))
8596     ;; If marked as read, go to next unread subject.
8597     (when (null unmark)
8598       ;; Go to next unread subject.
8599       (gnus-summary-next-subject 1 t))
8600     (gnus-message 7 "%d articles are marked as %s"
8601                   count (if unmark "unread" "read"))))
8602
8603 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8604   "Mark articles with same SUBJECT as read, and return marked number.
8605 If optional argument UNMARK is positive, remove any kinds of marks.
8606 If optional argument UNMARK is negative, mark articles as unread instead."
8607   (let ((count 1))
8608     (save-excursion
8609       (cond
8610        ((null unmark)                   ; Mark as read.
8611         (while (and
8612                 (progn
8613                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8614                   (gnus-summary-show-thread) t)
8615                 (gnus-summary-find-subject subject))
8616           (setq count (1+ count))))
8617        ((> unmark 0)                    ; Tick.
8618         (while (and
8619                 (progn
8620                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8621                   (gnus-summary-show-thread) t)
8622                 (gnus-summary-find-subject subject))
8623           (setq count (1+ count))))
8624        (t                               ; Mark as unread.
8625         (while (and
8626                 (progn
8627                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8628                   (gnus-summary-show-thread) t)
8629                 (gnus-summary-find-subject subject))
8630           (setq count (1+ count)))))
8631       (gnus-set-mode-line 'summary)
8632       ;; Return the number of marked articles.
8633       count)))
8634
8635 (defun gnus-summary-mark-as-processable (n &optional unmark)
8636   "Set the process mark on the next N articles.
8637 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8638 the process mark instead.  The difference between N and the actual
8639 number of articles marked is returned."
8640   (interactive "P")
8641   (if (and (null n) (gnus-region-active-p))
8642       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8643     (setq n (prefix-numeric-value n))
8644     (let ((backward (< n 0))
8645           (n (abs n)))
8646       (while (and
8647               (> n 0)
8648               (if unmark
8649                   (gnus-summary-remove-process-mark
8650                    (gnus-summary-article-number))
8651                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8652               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8653         (setq n (1- n)))
8654       (when (/= 0 n)
8655         (gnus-message 7 "No more articles"))
8656       (gnus-summary-recenter)
8657       (gnus-summary-position-point)
8658       n)))
8659
8660 (defun gnus-summary-unmark-as-processable (n)
8661   "Remove the process mark from the next N articles.
8662 If N is negative, unmark backward instead.  The difference between N and
8663 the actual number of articles unmarked is returned."
8664   (interactive "P")
8665   (gnus-summary-mark-as-processable n t))
8666
8667 (defun gnus-summary-unmark-all-processable ()
8668   "Remove the process mark from all articles."
8669   (interactive)
8670   (save-excursion
8671     (while gnus-newsgroup-processable
8672       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8673   (gnus-summary-position-point))
8674
8675 (defun gnus-summary-add-mark (article type)
8676   "Mark ARTICLE with a mark of TYPE."
8677   (let ((vtype (car (assq type gnus-article-mark-lists)))
8678         var)
8679     (if (not vtype)
8680         (error "No such mark type: %s" type)
8681       (setq var (intern (format "gnus-newsgroup-%s" type)))
8682       (set var (cons article (symbol-value var)))
8683       (if (memq type '(processable cached replied forwarded recent saved))
8684           (gnus-summary-update-secondary-mark article)
8685         ;;; !!! This is bogus.  We should find out what primary
8686         ;;; !!! mark we want to set.
8687         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8688
8689 (defun gnus-summary-mark-as-expirable (n)
8690   "Mark N articles forward as expirable.
8691 If N is negative, mark backward instead.  The difference between N and
8692 the actual number of articles marked is returned."
8693   (interactive "p")
8694   (gnus-summary-mark-forward n gnus-expirable-mark))
8695
8696 (defun gnus-summary-mark-article-as-replied (article)
8697   "Mark ARTICLE as replied to and update the summary line.
8698 ARTICLE can also be a list of articles."
8699   (interactive (list (gnus-summary-article-number)))
8700   (let ((articles (if (listp article) article (list article))))
8701     (dolist (article articles)
8702       (push article gnus-newsgroup-replied)
8703       (let ((buffer-read-only nil))
8704         (when (gnus-summary-goto-subject article nil t)
8705           (gnus-summary-update-secondary-mark article))))))
8706
8707 (defun gnus-summary-mark-article-as-forwarded (article)
8708   "Mark ARTICLE as forwarded and update the summary line.
8709 ARTICLE can also be a list of articles."
8710   (let ((articles (if (listp article) article (list article))))
8711     (dolist (article articles)
8712       (push article gnus-newsgroup-forwarded)
8713       (let ((buffer-read-only nil))
8714         (when (gnus-summary-goto-subject article nil t)
8715           (gnus-summary-update-secondary-mark article))))))
8716
8717 (defun gnus-summary-set-bookmark (article)
8718   "Set a bookmark in current article."
8719   (interactive (list (gnus-summary-article-number)))
8720   (when (or (not (get-buffer gnus-article-buffer))
8721             (not gnus-current-article)
8722             (not gnus-article-current)
8723             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8724     (error "No current article selected"))
8725   ;; Remove old bookmark, if one exists.
8726   (let ((old (assq article gnus-newsgroup-bookmarks)))
8727     (when old
8728       (setq gnus-newsgroup-bookmarks
8729             (delq old gnus-newsgroup-bookmarks))))
8730   ;; Set the new bookmark, which is on the form
8731   ;; (article-number . line-number-in-body).
8732   (push
8733    (cons article
8734          (save-excursion
8735            (set-buffer gnus-article-buffer)
8736            (count-lines
8737             (min (point)
8738                  (save-excursion
8739                    (goto-char (point-min))
8740                    (search-forward "\n\n" nil t)
8741                    (point)))
8742             (point))))
8743    gnus-newsgroup-bookmarks)
8744   (gnus-message 6 "A bookmark has been added to the current article."))
8745
8746 (defun gnus-summary-remove-bookmark (article)
8747   "Remove the bookmark from the current article."
8748   (interactive (list (gnus-summary-article-number)))
8749   ;; Remove old bookmark, if one exists.
8750   (let ((old (assq article gnus-newsgroup-bookmarks)))
8751     (if old
8752         (progn
8753           (setq gnus-newsgroup-bookmarks
8754                 (delq old gnus-newsgroup-bookmarks))
8755           (gnus-message 6 "Removed bookmark."))
8756       (gnus-message 6 "No bookmark in current article."))))
8757
8758 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8759 (defun gnus-summary-mark-as-dormant (n)
8760   "Mark N articles forward as dormant.
8761 If N is negative, mark backward instead.  The difference between N and
8762 the actual number of articles marked is returned."
8763   (interactive "p")
8764   (gnus-summary-mark-forward n gnus-dormant-mark))
8765
8766 (defun gnus-summary-set-process-mark (article)
8767   "Set the process mark on ARTICLE and update the summary line."
8768   (setq gnus-newsgroup-processable
8769         (cons article
8770               (delq article gnus-newsgroup-processable)))
8771   (when (gnus-summary-goto-subject article)
8772     (gnus-summary-show-thread)
8773     (gnus-summary-goto-subject article)
8774     (gnus-summary-update-secondary-mark article)))
8775
8776 (defun gnus-summary-remove-process-mark (article)
8777   "Remove the process mark from ARTICLE and update the summary line."
8778   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8779   (when (gnus-summary-goto-subject article)
8780     (gnus-summary-show-thread)
8781     (gnus-summary-goto-subject article)
8782     (gnus-summary-update-secondary-mark article)))
8783
8784 (defun gnus-summary-set-saved-mark (article)
8785   "Set the process mark on ARTICLE and update the summary line."
8786   (push article gnus-newsgroup-saved)
8787   (when (gnus-summary-goto-subject article)
8788     (gnus-summary-update-secondary-mark article)))
8789
8790 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8791   "Mark N articles as read forwards.
8792 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8793 The difference between N and the actual number of articles marked is
8794 returned.
8795 Iff NO-EXPIRE, auto-expiry will be inhibited."
8796   (interactive "p")
8797   (gnus-summary-show-thread)
8798   (let ((backward (< n 0))
8799         (gnus-summary-goto-unread
8800          (and gnus-summary-goto-unread
8801               (not (eq gnus-summary-goto-unread 'never))
8802               (not (memq mark (list gnus-unread-mark
8803                                     gnus-ticked-mark gnus-dormant-mark)))))
8804         (n (abs n))
8805         (mark (or mark gnus-del-mark)))
8806     (while (and (> n 0)
8807                 (gnus-summary-mark-article nil mark no-expire)
8808                 (zerop (gnus-summary-next-subject
8809                         (if backward -1 1)
8810                         (and gnus-summary-goto-unread
8811                              (not (eq gnus-summary-goto-unread 'never)))
8812                         t)))
8813       (setq n (1- n)))
8814     (when (/= 0 n)
8815       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8816     (gnus-summary-recenter)
8817     (gnus-summary-position-point)
8818     (gnus-set-mode-line 'summary)
8819     n))
8820
8821 (defun gnus-summary-mark-article-as-read (mark)
8822   "Mark the current article quickly as read with MARK."
8823   (let ((article (gnus-summary-article-number)))
8824     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8825     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8826     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8827     (push (cons article mark) gnus-newsgroup-reads)
8828     ;; Possibly remove from cache, if that is used.
8829     (when gnus-use-cache
8830       (gnus-cache-enter-remove-article article))
8831     ;; Allow the backend to change the mark.
8832     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8833     ;; Check for auto-expiry.
8834     (when (and gnus-newsgroup-auto-expire
8835                (memq mark gnus-auto-expirable-marks))
8836       (setq mark gnus-expirable-mark)
8837       ;; Let the backend know about the mark change.
8838       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8839       (push article gnus-newsgroup-expirable))
8840     ;; Set the mark in the buffer.
8841     (gnus-summary-update-mark mark 'unread)
8842     t))
8843
8844 (defun gnus-summary-mark-article-as-unread (mark)
8845   "Mark the current article quickly as unread with MARK."
8846   (let* ((article (gnus-summary-article-number))
8847          (old-mark (gnus-summary-article-mark article)))
8848     ;; Allow the backend to change the mark.
8849     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8850     (if (eq mark old-mark)
8851         t
8852       (if (<= article 0)
8853           (progn
8854             (gnus-error 1 "Can't mark negative article numbers")
8855             nil)
8856         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8857         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8858         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8859         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8860         (cond ((= mark gnus-ticked-mark)
8861                (push article gnus-newsgroup-marked))
8862               ((= mark gnus-dormant-mark)
8863                (push article gnus-newsgroup-dormant))
8864               (t
8865                (push article gnus-newsgroup-unreads)))
8866         (gnus-pull article gnus-newsgroup-reads)
8867
8868         ;; See whether the article is to be put in the cache.
8869         (and gnus-use-cache
8870              (vectorp (gnus-summary-article-header article))
8871              (save-excursion
8872                (gnus-cache-possibly-enter-article
8873                 gnus-newsgroup-name article
8874                 (= mark gnus-ticked-mark)
8875                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8876
8877         ;; Fix the mark.
8878         (gnus-summary-update-mark mark 'unread)
8879         t))))
8880
8881 (defun gnus-summary-mark-article (&optional article mark no-expire)
8882   "Mark ARTICLE with MARK.  MARK can be any character.
8883 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8884 `??' (dormant) and `?E' (expirable).
8885 If MARK is nil, then the default character `?r' is used.
8886 If ARTICLE is nil, then the article on the current line will be
8887 marked.
8888 Iff NO-EXPIRE, auto-expiry will be inhibited."
8889   ;; The mark might be a string.
8890   (when (stringp mark)
8891     (setq mark (aref mark 0)))
8892   ;; If no mark is given, then we check auto-expiring.
8893   (when (null mark)
8894     (setq mark gnus-del-mark))
8895   (when (and (not no-expire)
8896              gnus-newsgroup-auto-expire
8897              (memq mark gnus-auto-expirable-marks))
8898     (setq mark gnus-expirable-mark))
8899   (let ((article (or article (gnus-summary-article-number)))
8900         (old-mark (gnus-summary-article-mark article)))
8901     ;; Allow the backend to change the mark.
8902     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8903     (if (eq mark old-mark)
8904         t
8905       (unless article
8906         (error "No article on current line"))
8907       (if (not (if (or (= mark gnus-unread-mark)
8908                        (= mark gnus-ticked-mark)
8909                        (= mark gnus-dormant-mark))
8910                    (gnus-mark-article-as-unread article mark)
8911                  (gnus-mark-article-as-read article mark)))
8912           t
8913         ;; See whether the article is to be put in the cache.
8914         (and gnus-use-cache
8915              (not (= mark gnus-canceled-mark))
8916              (vectorp (gnus-summary-article-header article))
8917              (save-excursion
8918                (gnus-cache-possibly-enter-article
8919                 gnus-newsgroup-name article
8920                 (= mark gnus-ticked-mark)
8921                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8922
8923         (when (gnus-summary-goto-subject article nil t)
8924           (let ((buffer-read-only nil))
8925             (gnus-summary-show-thread)
8926             ;; Fix the mark.
8927             (gnus-summary-update-mark mark 'unread)
8928             t))))))
8929
8930 (defun gnus-summary-update-secondary-mark (article)
8931   "Update the secondary (read, process, cache) mark."
8932   (gnus-summary-update-mark
8933    (cond ((memq article gnus-newsgroup-processable)
8934           gnus-process-mark)
8935          ((memq article gnus-newsgroup-cached)
8936           gnus-cached-mark)
8937          ((memq article gnus-newsgroup-replied)
8938           gnus-replied-mark)
8939          ((memq article gnus-newsgroup-forwarded)
8940           gnus-forwarded-mark)
8941          ((memq article gnus-newsgroup-saved)
8942           gnus-saved-mark)
8943          ((memq article gnus-newsgroup-recent)
8944           gnus-recent-mark)
8945          (t gnus-no-mark))
8946    'replied)
8947   (when (gnus-visual-p 'summary-highlight 'highlight)
8948     (gnus-run-hooks 'gnus-summary-update-hook))
8949   t)
8950
8951 (defun gnus-summary-update-mark (mark type)
8952   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8953         (buffer-read-only nil))
8954     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8955     (when forward
8956       (when (looking-at "\r")
8957         (incf forward))
8958       (when (<= (+ forward (point)) (point-max))
8959         ;; Go to the right position on the line.
8960         (goto-char (+ forward (point)))
8961         ;; Replace the old mark with the new mark.
8962         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8963         ;; Optionally update the marks by some user rule.
8964         (when (eq type 'unread)
8965           (gnus-data-set-mark
8966            (gnus-data-find (gnus-summary-article-number)) mark)
8967           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8968
8969 (defun gnus-mark-article-as-read (article &optional mark)
8970   "Enter ARTICLE in the pertinent lists and remove it from others."
8971   ;; Make the article expirable.
8972   (let ((mark (or mark gnus-del-mark)))
8973     (if (= mark gnus-expirable-mark)
8974         (push article gnus-newsgroup-expirable)
8975       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8976     ;; Remove from unread and marked lists.
8977     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8978     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8979     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8980     (push (cons article mark) gnus-newsgroup-reads)
8981     ;; Possibly remove from cache, if that is used.
8982     (when gnus-use-cache
8983       (gnus-cache-enter-remove-article article))
8984     t))
8985
8986 (defun gnus-mark-article-as-unread (article &optional mark)
8987   "Enter ARTICLE in the pertinent lists and remove it from others."
8988   (let ((mark (or mark gnus-ticked-mark)))
8989     (if (<= article 0)
8990         (progn
8991           (gnus-error 1 "Can't mark negative article numbers")
8992           nil)
8993       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8994             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8995             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8996             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8997
8998       ;; Unsuppress duplicates?
8999       (when gnus-suppress-duplicates
9000         (gnus-dup-unsuppress-article article))
9001
9002       (cond ((= mark gnus-ticked-mark)
9003              (push article gnus-newsgroup-marked))
9004             ((= mark gnus-dormant-mark)
9005              (push article gnus-newsgroup-dormant))
9006             (t
9007              (push article gnus-newsgroup-unreads)))
9008       (gnus-pull article gnus-newsgroup-reads)
9009       t)))
9010
9011 (defalias 'gnus-summary-mark-as-unread-forward
9012   'gnus-summary-tick-article-forward)
9013 (make-obsolete 'gnus-summary-mark-as-unread-forward
9014                'gnus-summary-tick-article-forward)
9015 (defun gnus-summary-tick-article-forward (n)
9016   "Tick N articles forwards.
9017 If N is negative, tick backwards instead.
9018 The difference between N and the number of articles ticked is returned."
9019   (interactive "p")
9020   (gnus-summary-mark-forward n gnus-ticked-mark))
9021
9022 (defalias 'gnus-summary-mark-as-unread-backward
9023   'gnus-summary-tick-article-backward)
9024 (make-obsolete 'gnus-summary-mark-as-unread-backward
9025                'gnus-summary-tick-article-backward)
9026 (defun gnus-summary-tick-article-backward (n)
9027   "Tick N articles backwards.
9028 The difference between N and the number of articles ticked is returned."
9029   (interactive "p")
9030   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9031
9032 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9033 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9034 (defun gnus-summary-tick-article (&optional article clear-mark)
9035   "Mark current article as unread.
9036 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9037 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9038   (interactive)
9039   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9040                                        gnus-ticked-mark)))
9041
9042 (defun gnus-summary-mark-as-read-forward (n)
9043   "Mark N articles as read forwards.
9044 If N is negative, mark backwards instead.
9045 The difference between N and the actual number of articles marked is
9046 returned."
9047   (interactive "p")
9048   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9049
9050 (defun gnus-summary-mark-as-read-backward (n)
9051   "Mark the N articles as read backwards.
9052 The difference between N and the actual number of articles marked is
9053 returned."
9054   (interactive "p")
9055   (gnus-summary-mark-forward
9056    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9057
9058 (defun gnus-summary-mark-as-read (&optional article mark)
9059   "Mark current article as read.
9060 ARTICLE specifies the article to be marked as read.
9061 MARK specifies a string to be inserted at the beginning of the line."
9062   (gnus-summary-mark-article article mark))
9063
9064 (defun gnus-summary-clear-mark-forward (n)
9065   "Clear marks from N articles forward.
9066 If N is negative, clear backward instead.
9067 The difference between N and the number of marks cleared is returned."
9068   (interactive "p")
9069   (gnus-summary-mark-forward n gnus-unread-mark))
9070
9071 (defun gnus-summary-clear-mark-backward (n)
9072   "Clear marks from N articles backward.
9073 The difference between N and the number of marks cleared is returned."
9074   (interactive "p")
9075   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9076
9077 (defun gnus-summary-mark-unread-as-read ()
9078   "Intended to be used by `gnus-summary-mark-article-hook'."
9079   (when (memq gnus-current-article gnus-newsgroup-unreads)
9080     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9081
9082 (defun gnus-summary-mark-read-and-unread-as-read ()
9083   "Intended to be used by `gnus-summary-mark-article-hook'."
9084   (let ((mark (gnus-summary-article-mark)))
9085     (when (or (gnus-unread-mark-p mark)
9086               (gnus-read-mark-p mark))
9087       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9088
9089 (defun gnus-summary-mark-unread-as-ticked ()
9090   "Intended to be used by `gnus-summary-mark-article-hook'."
9091   (when (memq gnus-current-article gnus-newsgroup-unreads)
9092     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9093
9094 (defun gnus-summary-mark-region-as-read (point mark all)
9095   "Mark all unread articles between point and mark as read.
9096 If given a prefix, mark all articles between point and mark as read,
9097 even ticked and dormant ones."
9098   (interactive "r\nP")
9099   (save-excursion
9100     (let (article)
9101       (goto-char point)
9102       (beginning-of-line)
9103       (while (and
9104               (< (point) mark)
9105               (progn
9106                 (when (or all
9107                           (memq (setq article (gnus-summary-article-number))
9108                                 gnus-newsgroup-unreads))
9109                   (gnus-summary-mark-article article gnus-del-mark))
9110                 t)
9111               (gnus-summary-find-next))))))
9112
9113 (defun gnus-summary-mark-below (score mark)
9114   "Mark articles with score less than SCORE with MARK."
9115   (interactive "P\ncMark: ")
9116   (setq score (if score
9117                   (prefix-numeric-value score)
9118                 (or gnus-summary-default-score 0)))
9119   (save-excursion
9120     (set-buffer gnus-summary-buffer)
9121     (goto-char (point-min))
9122     (while
9123         (progn
9124           (and (< (gnus-summary-article-score) score)
9125                (gnus-summary-mark-article nil mark))
9126           (gnus-summary-find-next)))))
9127
9128 (defun gnus-summary-kill-below (&optional score)
9129   "Mark articles with score below SCORE as read."
9130   (interactive "P")
9131   (gnus-summary-mark-below score gnus-killed-mark))
9132
9133 (defun gnus-summary-clear-above (&optional score)
9134   "Clear all marks from articles with score above SCORE."
9135   (interactive "P")
9136   (gnus-summary-mark-above score gnus-unread-mark))
9137
9138 (defun gnus-summary-tick-above (&optional score)
9139   "Tick all articles with score above SCORE."
9140   (interactive "P")
9141   (gnus-summary-mark-above score gnus-ticked-mark))
9142
9143 (defun gnus-summary-mark-above (score mark)
9144   "Mark articles with score over SCORE with MARK."
9145   (interactive "P\ncMark: ")
9146   (setq score (if score
9147                   (prefix-numeric-value score)
9148                 (or gnus-summary-default-score 0)))
9149   (save-excursion
9150     (set-buffer gnus-summary-buffer)
9151     (goto-char (point-min))
9152     (while (and (progn
9153                   (when (> (gnus-summary-article-score) score)
9154                     (gnus-summary-mark-article nil mark))
9155                   t)
9156                 (gnus-summary-find-next)))))
9157
9158 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9159 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9160 (defun gnus-summary-limit-include-expunged (&optional no-error)
9161   "Display all the hidden articles that were expunged for low scores."
9162   (interactive)
9163   (let ((buffer-read-only nil))
9164     (let ((scored gnus-newsgroup-scored)
9165           headers h)
9166       (while scored
9167         (unless (gnus-summary-article-header (caar scored))
9168           (and (setq h (gnus-number-to-header (caar scored)))
9169                (< (cdar scored) gnus-summary-expunge-below)
9170                (push h headers)))
9171         (setq scored (cdr scored)))
9172       (if (not headers)
9173           (when (not no-error)
9174             (error "No expunged articles hidden"))
9175         (goto-char (point-min))
9176         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9177         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9178         (mapcar (lambda (x) (push (mail-header-number x)
9179                                   gnus-newsgroup-limit))
9180                 headers)
9181         (gnus-summary-prepare-unthreaded (nreverse headers))
9182         (goto-char (point-min))
9183         (gnus-summary-position-point)
9184         t))))
9185
9186 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9187   "Mark all unread articles in this newsgroup as read.
9188 If prefix argument ALL is non-nil, ticked and dormant articles will
9189 also be marked as read.
9190 If QUIETLY is non-nil, no questions will be asked.
9191 If TO-HERE is non-nil, it should be a point in the buffer.  All
9192 articles before (after, if REVERSE is set) this point will be marked as read.
9193 Note that this function will only catch up the unread article
9194 in the current summary buffer limitation.
9195 The number of articles marked as read is returned."
9196   (interactive "P")
9197   (prog1
9198       (save-excursion
9199         (when (or quietly
9200                   (not gnus-interactive-catchup) ;Without confirmation?
9201                   gnus-expert-user
9202                   (gnus-y-or-n-p
9203                    (if all
9204                        "Mark absolutely all articles as read? "
9205                      "Mark all unread articles as read? ")))
9206           (if (and not-mark
9207                    (not gnus-newsgroup-adaptive)
9208                    (not gnus-newsgroup-auto-expire)
9209                    (not gnus-suppress-duplicates)
9210                    (or (not gnus-use-cache)
9211                        (eq gnus-use-cache 'passive)))
9212               (progn
9213                 (when all
9214                   (setq gnus-newsgroup-marked nil
9215                         gnus-newsgroup-dormant nil))
9216                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9217             ;; We actually mark all articles as canceled, which we
9218             ;; have to do when using auto-expiry or adaptive scoring.
9219             (gnus-summary-show-all-threads)
9220             (if (and to-here reverse)
9221                 (progn
9222                   (goto-char to-here)
9223                   (while (and
9224                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9225                           (gnus-summary-find-next (not all) nil nil t))))
9226               (when (gnus-summary-first-subject (not all) t)
9227                 (while (and
9228                         (if to-here (< (point) to-here) t)
9229                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9230                         (gnus-summary-find-next (not all) nil nil t)))))
9231             (gnus-set-mode-line 'summary))
9232           t))
9233     (gnus-summary-position-point)))
9234
9235 (defun gnus-summary-catchup-to-here (&optional all)
9236   "Mark all unticked articles before the current one as read.
9237 If ALL is non-nil, also mark ticked and dormant articles as read."
9238   (interactive "P")
9239   (save-excursion
9240     (gnus-save-hidden-threads
9241       (let ((beg (point)))
9242         ;; We check that there are unread articles.
9243         (when (or all (gnus-summary-find-prev))
9244           (gnus-summary-catchup all t beg)))))
9245   (gnus-summary-position-point))
9246
9247 (defun gnus-summary-catchup-from-here (&optional all)
9248   "Mark all unticked articles after the current one as read.
9249 If ALL is non-nil, also mark ticked and dormant articles as read."
9250   (interactive "P")
9251   (save-excursion
9252     (gnus-save-hidden-threads
9253       (let ((beg (point)))
9254         ;; We check that there are unread articles.
9255         (when (or all (gnus-summary-find-next))
9256           (gnus-summary-catchup all t beg nil t)))))
9257
9258   (gnus-summary-position-point))
9259 (defun gnus-summary-catchup-all (&optional quietly)
9260   "Mark all articles in this newsgroup as read."
9261   (interactive "P")
9262   (gnus-summary-catchup t quietly))
9263
9264 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9265   "Mark all unread articles in this group as read, then exit.
9266 If prefix argument ALL is non-nil, all articles are marked as read.
9267 If QUIETLY is non-nil, no questions will be asked."
9268   (interactive "P")
9269   (when (gnus-summary-catchup all quietly nil 'fast)
9270     ;; Select next newsgroup or exit.
9271     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9272              (eq gnus-auto-select-next 'quietly))
9273         (gnus-summary-next-group nil)
9274       (gnus-summary-exit))))
9275
9276 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9277   "Mark all articles in this newsgroup as read, and then exit."
9278   (interactive "P")
9279   (gnus-summary-catchup-and-exit t quietly))
9280
9281 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9282   "Mark all articles in this group as read and select the next group.
9283 If given a prefix, mark all articles, unread as well as ticked, as
9284 read."
9285   (interactive "P")
9286   (save-excursion
9287     (gnus-summary-catchup all))
9288   (gnus-summary-next-group))
9289
9290 ;;;
9291 ;;; with article
9292 ;;;
9293
9294 (defmacro gnus-with-article (article &rest forms)
9295   "Select ARTICLE and perform FORMS in the original article buffer.
9296 Then replace the article with the result."
9297   `(progn
9298      ;; We don't want the article to be marked as read.
9299      (let (gnus-mark-article-hook)
9300        (gnus-summary-select-article t t nil ,article))
9301      (set-buffer gnus-original-article-buffer)
9302      ,@forms
9303      (if (not (gnus-check-backend-function
9304                'request-replace-article (car gnus-article-current)))
9305          (gnus-message 5 "Read-only group; not replacing")
9306        (unless (gnus-request-replace-article
9307                 ,article (car gnus-article-current)
9308                 (current-buffer) t)
9309          (error "Couldn't replace article")))
9310      ;; The cache and backlog have to be flushed somewhat.
9311      (when gnus-keep-backlog
9312        (gnus-backlog-remove-article
9313         (car gnus-article-current) (cdr gnus-article-current)))
9314      (when gnus-use-cache
9315        (gnus-cache-update-article
9316         (car gnus-article-current) (cdr gnus-article-current)))))
9317
9318 (put 'gnus-with-article 'lisp-indent-function 1)
9319 (put 'gnus-with-article 'edebug-form-spec '(form body))
9320
9321 ;; Thread-based commands.
9322
9323 (defun gnus-summary-articles-in-thread (&optional article)
9324   "Return a list of all articles in the current thread.
9325 If ARTICLE is non-nil, return all articles in the thread that starts
9326 with that article."
9327   (let* ((article (or article (gnus-summary-article-number)))
9328          (data (gnus-data-find-list article))
9329          (top-level (gnus-data-level (car data)))
9330          (top-subject
9331           (cond ((null gnus-thread-operation-ignore-subject)
9332                  (gnus-simplify-subject-re
9333                   (mail-header-subject (gnus-data-header (car data)))))
9334                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9335                  (gnus-simplify-subject-fuzzy
9336                   (mail-header-subject (gnus-data-header (car data)))))
9337                 (t nil)))
9338          (end-point (save-excursion
9339                       (if (gnus-summary-go-to-next-thread)
9340                           (point) (point-max))))
9341          articles)
9342     (while (and data
9343                 (< (gnus-data-pos (car data)) end-point))
9344       (when (or (not top-subject)
9345                 (string= top-subject
9346                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9347                              (gnus-simplify-subject-fuzzy
9348                               (mail-header-subject
9349                                (gnus-data-header (car data))))
9350                            (gnus-simplify-subject-re
9351                             (mail-header-subject
9352                              (gnus-data-header (car data)))))))
9353         (push (gnus-data-number (car data)) articles))
9354       (unless (and (setq data (cdr data))
9355                    (> (gnus-data-level (car data)) top-level))
9356         (setq data nil)))
9357     ;; Return the list of articles.
9358     (nreverse articles)))
9359
9360 (defun gnus-summary-rethread-current ()
9361   "Rethread the thread the current article is part of."
9362   (interactive)
9363   (let* ((gnus-show-threads t)
9364          (article (gnus-summary-article-number))
9365          (id (mail-header-id (gnus-summary-article-header)))
9366          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9367     (unless id
9368       (error "No article on the current line"))
9369     (gnus-rebuild-thread id)
9370     (gnus-summary-goto-subject article)))
9371
9372 (defun gnus-summary-reparent-thread ()
9373   "Make the current article child of the marked (or previous) article.
9374
9375 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9376 is non-nil or the Subject: of both articles are the same."
9377   (interactive)
9378   (unless (not (gnus-group-read-only-p))
9379     (error "The current newsgroup does not support article editing"))
9380   (unless (<= (length gnus-newsgroup-processable) 1)
9381     (error "No more than one article may be marked"))
9382   (save-window-excursion
9383     (let ((gnus-article-buffer " *reparent*")
9384           (current-article (gnus-summary-article-number))
9385           ;; First grab the marked article, otherwise one line up.
9386           (parent-article (if (not (null gnus-newsgroup-processable))
9387                               (car gnus-newsgroup-processable)
9388                             (save-excursion
9389                               (if (eq (forward-line -1) 0)
9390                                   (gnus-summary-article-number)
9391                                 (error "Beginning of summary buffer"))))))
9392       (unless (not (eq current-article parent-article))
9393         (error "An article may not be self-referential"))
9394       (let ((message-id (mail-header-id
9395                          (gnus-summary-article-header parent-article))))
9396         (unless (and message-id (not (equal message-id "")))
9397           (error "No message-id in desired parent"))
9398         (gnus-with-article current-article
9399           (save-restriction
9400             (goto-char (point-min))
9401             (message-narrow-to-head)
9402             (if (re-search-forward "^References: " nil t)
9403                 (progn
9404                   (re-search-forward "^[^ \t]" nil t)
9405                   (forward-line -1)
9406                   (end-of-line)
9407                   (insert " " message-id))
9408               (insert "References: " message-id "\n"))))
9409         (set-buffer gnus-summary-buffer)
9410         (gnus-summary-unmark-all-processable)
9411         (gnus-summary-update-article current-article)
9412         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9413             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9414         (gnus-summary-rethread-current)
9415         (gnus-message 3 "Article %d is now the child of article %d"
9416                       current-article parent-article)))))
9417
9418 (defun gnus-summary-toggle-threads (&optional arg)
9419   "Toggle showing conversation threads.
9420 If ARG is positive number, turn showing conversation threads on."
9421   (interactive "P")
9422   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9423     (setq gnus-show-threads
9424           (if (null arg) (not gnus-show-threads)
9425             (> (prefix-numeric-value arg) 0)))
9426     (gnus-summary-prepare)
9427     (gnus-summary-goto-subject current)
9428     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9429     (gnus-summary-position-point)))
9430
9431 (defun gnus-summary-show-all-threads ()
9432   "Show all threads."
9433   (interactive)
9434   (save-excursion
9435     (let ((buffer-read-only nil))
9436       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9437   (gnus-summary-position-point))
9438
9439 (defun gnus-summary-show-thread ()
9440   "Show thread subtrees.
9441 Returns nil if no thread was there to be shown."
9442   (interactive)
9443   (let ((buffer-read-only nil)
9444         (orig (point))
9445         ;; first goto end then to beg, to have point at beg after let
9446         (end (progn (end-of-line) (point)))
9447         (beg (progn (beginning-of-line) (point))))
9448     (prog1
9449         ;; Any hidden lines here?
9450         (search-forward "\r" end t)
9451       (subst-char-in-region beg end ?\^M ?\n t)
9452       (goto-char orig)
9453       (gnus-summary-position-point))))
9454
9455 (defun gnus-summary-hide-all-threads ()
9456   "Hide all thread subtrees."
9457   (interactive)
9458   (save-excursion
9459     (goto-char (point-min))
9460     (gnus-summary-hide-thread)
9461     (while (zerop (gnus-summary-next-thread 1 t))
9462       (gnus-summary-hide-thread)))
9463   (gnus-summary-position-point))
9464
9465 (defun gnus-summary-hide-thread ()
9466   "Hide thread subtrees.
9467 Returns nil if no threads were there to be hidden."
9468   (interactive)
9469   (let ((buffer-read-only nil)
9470         (start (point))
9471         (article (gnus-summary-article-number)))
9472     (goto-char start)
9473     ;; Go forward until either the buffer ends or the subthread
9474     ;; ends.
9475     (when (and (not (eobp))
9476                (or (zerop (gnus-summary-next-thread 1 t))
9477                    (goto-char (point-max))))
9478       (prog1
9479           (if (and (> (point) start)
9480                    (search-backward "\n" start t))
9481               (progn
9482                 (subst-char-in-region start (point) ?\n ?\^M)
9483                 (gnus-summary-goto-subject article))
9484             (goto-char start)
9485             nil)))))
9486
9487 (defun gnus-summary-go-to-next-thread (&optional previous)
9488   "Go to the same level (or less) next thread.
9489 If PREVIOUS is non-nil, go to previous thread instead.
9490 Return the article number moved to, or nil if moving was impossible."
9491   (let ((level (gnus-summary-thread-level))
9492         (way (if previous -1 1))
9493         (beg (point)))
9494     (forward-line way)
9495     (while (and (not (eobp))
9496                 (< level (gnus-summary-thread-level)))
9497       (forward-line way))
9498     (if (eobp)
9499         (progn
9500           (goto-char beg)
9501           nil)
9502       (setq beg (point))
9503       (prog1
9504           (gnus-summary-article-number)
9505         (goto-char beg)))))
9506
9507 (defun gnus-summary-next-thread (n &optional silent)
9508   "Go to the same level next N'th thread.
9509 If N is negative, search backward instead.
9510 Returns the difference between N and the number of skips actually
9511 done.
9512
9513 If SILENT, don't output messages."
9514   (interactive "p")
9515   (let ((backward (< n 0))
9516         (n (abs n)))
9517     (while (and (> n 0)
9518                 (gnus-summary-go-to-next-thread backward))
9519       (decf n))
9520     (unless silent
9521       (gnus-summary-position-point))
9522     (when (and (not silent) (/= 0 n))
9523       (gnus-message 7 "No more threads"))
9524     n))
9525
9526 (defun gnus-summary-prev-thread (n)
9527   "Go to the same level previous N'th thread.
9528 Returns the difference between N and the number of skips actually
9529 done."
9530   (interactive "p")
9531   (gnus-summary-next-thread (- n)))
9532
9533 (defun gnus-summary-go-down-thread ()
9534   "Go down one level in the current thread."
9535   (let ((children (gnus-summary-article-children)))
9536     (when children
9537       (gnus-summary-goto-subject (car children)))))
9538
9539 (defun gnus-summary-go-up-thread ()
9540   "Go up one level in the current thread."
9541   (let ((parent (gnus-summary-article-parent)))
9542     (when parent
9543       (gnus-summary-goto-subject parent))))
9544
9545 (defun gnus-summary-down-thread (n)
9546   "Go down thread N steps.
9547 If N is negative, go up instead.
9548 Returns the difference between N and how many steps down that were
9549 taken."
9550   (interactive "p")
9551   (let ((up (< n 0))
9552         (n (abs n)))
9553     (while (and (> n 0)
9554                 (if up (gnus-summary-go-up-thread)
9555                   (gnus-summary-go-down-thread)))
9556       (setq n (1- n)))
9557     (gnus-summary-position-point)
9558     (when (/= 0 n)
9559       (gnus-message 7 "Can't go further"))
9560     n))
9561
9562 (defun gnus-summary-up-thread (n)
9563   "Go up thread N steps.
9564 If N is negative, go down instead.
9565 Returns the difference between N and how many steps down that were
9566 taken."
9567   (interactive "p")
9568   (gnus-summary-down-thread (- n)))
9569
9570 (defun gnus-summary-top-thread ()
9571   "Go to the top of the thread."
9572   (interactive)
9573   (while (gnus-summary-go-up-thread))
9574   (gnus-summary-article-number))
9575
9576 (defun gnus-summary-kill-thread (&optional unmark)
9577   "Mark articles under current thread as read.
9578 If the prefix argument is positive, remove any kinds of marks.
9579 If the prefix argument is negative, tick articles instead."
9580   (interactive "P")
9581   (when unmark
9582     (setq unmark (prefix-numeric-value unmark)))
9583   (let ((articles (gnus-summary-articles-in-thread)))
9584     (save-excursion
9585       ;; Expand the thread.
9586       (gnus-summary-show-thread)
9587       ;; Mark all the articles.
9588       (while articles
9589         (gnus-summary-goto-subject (car articles))
9590         (cond ((null unmark)
9591                (gnus-summary-mark-article-as-read gnus-killed-mark))
9592               ((> unmark 0)
9593                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9594               (t
9595                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9596         (setq articles (cdr articles))))
9597     ;; Hide killed subtrees.
9598     (and (null unmark)
9599          gnus-thread-hide-killed
9600          (gnus-summary-hide-thread))
9601     ;; If marked as read, go to next unread subject.
9602     (when (null unmark)
9603       ;; Go to next unread subject.
9604       (gnus-summary-next-subject 1 t)))
9605   (gnus-set-mode-line 'summary))
9606
9607 ;; Summary sorting commands
9608
9609 (defun gnus-summary-sort-by-number (&optional reverse)
9610   "Sort the summary buffer by article number.
9611 Argument REVERSE means reverse order."
9612   (interactive "P")
9613   (gnus-summary-sort 'number reverse))
9614
9615 (defun gnus-summary-sort-by-author (&optional reverse)
9616   "Sort the summary buffer by author name alphabetically.
9617 If `case-fold-search' is non-nil, case of letters is ignored.
9618 Argument REVERSE means reverse order."
9619   (interactive "P")
9620   (gnus-summary-sort 'author reverse))
9621
9622 (defun gnus-summary-sort-by-subject (&optional reverse)
9623   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9624 If `case-fold-search' is non-nil, case of letters is ignored.
9625 Argument REVERSE means reverse order."
9626   (interactive "P")
9627   (gnus-summary-sort 'subject reverse))
9628
9629 (defun gnus-summary-sort-by-date (&optional reverse)
9630   "Sort the summary buffer by date.
9631 Argument REVERSE means reverse order."
9632   (interactive "P")
9633   (gnus-summary-sort 'date reverse))
9634
9635 (defun gnus-summary-sort-by-score (&optional reverse)
9636   "Sort the summary buffer by score.
9637 Argument REVERSE means reverse order."
9638   (interactive "P")
9639   (gnus-summary-sort 'score reverse))
9640
9641 (defun gnus-summary-sort-by-lines (&optional reverse)
9642   "Sort the summary buffer by the number of lines.
9643 Argument REVERSE means reverse order."
9644   (interactive "P")
9645   (gnus-summary-sort 'lines reverse))
9646
9647 (defun gnus-summary-sort-by-chars (&optional reverse)
9648   "Sort the summary buffer by article length.
9649 Argument REVERSE means reverse order."
9650   (interactive "P")
9651   (gnus-summary-sort 'chars reverse))
9652
9653 (defun gnus-summary-sort-by-original (&optional reverse)
9654   "Sort the summary buffer using the default sorting method.
9655 Argument REVERSE means reverse order."
9656   (interactive "P")
9657   (let* ((buffer-read-only)
9658          (gnus-summary-prepare-hook nil))
9659     ;; We do the sorting by regenerating the threads.
9660     (gnus-summary-prepare)
9661     ;; Hide subthreads if needed.
9662     (when (and gnus-show-threads gnus-thread-hide-subtree)
9663       (gnus-summary-hide-all-threads))))
9664
9665 (defun gnus-summary-sort (predicate reverse)
9666   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9667   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9668          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9669          (gnus-thread-sort-functions
9670           (if (not reverse)
9671               thread
9672             `(lambda (t1 t2)
9673                (,thread t2 t1))))
9674          (gnus-sort-gathered-threads-function
9675           gnus-thread-sort-functions)
9676          (gnus-article-sort-functions
9677           (if (not reverse)
9678               article
9679             `(lambda (t1 t2)
9680                (,article t2 t1))))
9681          (buffer-read-only)
9682          (gnus-summary-prepare-hook nil))
9683     ;; We do the sorting by regenerating the threads.
9684     (gnus-summary-prepare)
9685     ;; Hide subthreads if needed.
9686     (when (and gnus-show-threads gnus-thread-hide-subtree)
9687       (gnus-summary-hide-all-threads))))
9688
9689 ;; Summary saving commands.
9690
9691 (defun gnus-summary-save-article (&optional n not-saved)
9692   "Save the current article using the default saver function.
9693 If N is a positive number, save the N next articles.
9694 If N is a negative number, save the N previous articles.
9695 If N is nil and any articles have been marked with the process mark,
9696 save those articles instead.
9697 The variable `gnus-default-article-saver' specifies the saver function."
9698   (interactive "P")
9699   (let* ((articles (gnus-summary-work-articles n))
9700          (save-buffer (save-excursion
9701                         (nnheader-set-temp-buffer " *Gnus Save*")))
9702          (num (length articles))
9703          header file)
9704     (dolist (article articles)
9705       (setq header (gnus-summary-article-header article))
9706       (if (not (vectorp header))
9707           ;; This is a pseudo-article.
9708           (if (assq 'name header)
9709               (gnus-copy-file (cdr (assq 'name header)))
9710             (gnus-message 1 "Article %d is unsaveable" article))
9711         ;; This is a real article.
9712         (save-window-excursion
9713           (gnus-summary-select-article t nil nil article))
9714         (save-excursion
9715           (set-buffer save-buffer)
9716           (erase-buffer)
9717           (insert-buffer-substring gnus-original-article-buffer))
9718         (setq file (gnus-article-save save-buffer file num))
9719         (gnus-summary-remove-process-mark article)
9720         (unless not-saved
9721           (gnus-summary-set-saved-mark article))))
9722     (gnus-kill-buffer save-buffer)
9723     (gnus-summary-position-point)
9724     (gnus-set-mode-line 'summary)
9725     n))
9726
9727 (defun gnus-summary-pipe-output (&optional arg)
9728   "Pipe the current article to a subprocess.
9729 If N is a positive number, pipe the N next articles.
9730 If N is a negative number, pipe the N previous articles.
9731 If N is nil and any articles have been marked with the process mark,
9732 pipe those articles instead."
9733   (interactive "P")
9734   (require 'gnus-art)
9735   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9736     (gnus-summary-save-article arg t))
9737   (let ((buffer (get-buffer "*Shell Command Output*")))
9738     (if (and buffer
9739              (with-current-buffer buffer (> (point-max) (point-min))))
9740         (gnus-configure-windows 'pipe))))
9741
9742 (defun gnus-summary-save-article-mail (&optional arg)
9743   "Append the current article to an mail file.
9744 If N is a positive number, save the N next articles.
9745 If N is a negative number, save the N previous articles.
9746 If N is nil and any articles have been marked with the process mark,
9747 save those articles instead."
9748   (interactive "P")
9749   (require 'gnus-art)
9750   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9751     (gnus-summary-save-article arg)))
9752
9753 (defun gnus-summary-save-article-rmail (&optional arg)
9754   "Append the current article to an rmail file.
9755 If N is a positive number, save the N next articles.
9756 If N is a negative number, save the N previous articles.
9757 If N is nil and any articles have been marked with the process mark,
9758 save those articles instead."
9759   (interactive "P")
9760   (require 'gnus-art)
9761   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9762     (gnus-summary-save-article arg)))
9763
9764 (defun gnus-summary-save-article-file (&optional arg)
9765   "Append the current article to a file.
9766 If N is a positive number, save the N next articles.
9767 If N is a negative number, save the N previous articles.
9768 If N is nil and any articles have been marked with the process mark,
9769 save those articles instead."
9770   (interactive "P")
9771   (require 'gnus-art)
9772   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9773     (gnus-summary-save-article arg)))
9774
9775 (defun gnus-summary-write-article-file (&optional arg)
9776   "Write the current article to a file, deleting the previous file.
9777 If N is a positive number, save the N next articles.
9778 If N is a negative number, save the N previous articles.
9779 If N is nil and any articles have been marked with the process mark,
9780 save those articles instead."
9781   (interactive "P")
9782   (require 'gnus-art)
9783   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9784     (gnus-summary-save-article arg)))
9785
9786 (defun gnus-summary-save-article-body-file (&optional arg)
9787   "Append the current article body to a file.
9788 If N is a positive number, save the N next articles.
9789 If N is a negative number, save the N previous articles.
9790 If N is nil and any articles have been marked with the process mark,
9791 save those articles instead."
9792   (interactive "P")
9793   (require 'gnus-art)
9794   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9795     (gnus-summary-save-article arg)))
9796
9797 (defun gnus-summary-pipe-message (program)
9798   "Pipe the current article through PROGRAM."
9799   (interactive "sProgram: ")
9800   (gnus-summary-select-article)
9801   (let ((mail-header-separator ""))
9802     (gnus-eval-in-buffer-window gnus-article-buffer
9803       (save-restriction
9804         (widen)
9805         (let ((start (window-start))
9806               buffer-read-only)
9807           (message-pipe-buffer-body program)
9808           (set-window-start (get-buffer-window (current-buffer)) start))))))
9809
9810 (defun gnus-get-split-value (methods)
9811   "Return a value based on the split METHODS."
9812   (let (split-name method result match)
9813     (when methods
9814       (save-excursion
9815         (set-buffer gnus-original-article-buffer)
9816         (save-restriction
9817           (nnheader-narrow-to-headers)
9818           (while (and methods (not split-name))
9819             (goto-char (point-min))
9820             (setq method (pop methods))
9821             (setq match (car method))
9822             (when (cond
9823                    ((stringp match)
9824                     ;; Regular expression.
9825                     (ignore-errors
9826                       (re-search-forward match nil t)))
9827                    ((gnus-functionp match)
9828                     ;; Function.
9829                     (save-restriction
9830                       (widen)
9831                       (setq result (funcall match gnus-newsgroup-name))))
9832                    ((consp match)
9833                     ;; Form.
9834                     (save-restriction
9835                       (widen)
9836                       (setq result (eval match)))))
9837               (setq split-name (cdr method))
9838               (cond ((stringp result)
9839                      (push (expand-file-name
9840                             result gnus-article-save-directory)
9841                            split-name))
9842                     ((consp result)
9843                      (setq split-name (append result split-name)))))))))
9844     (nreverse split-name)))
9845
9846 (defun gnus-valid-move-group-p (group)
9847   (and (boundp group)
9848        (symbol-name group)
9849        (symbol-value group)
9850        (gnus-get-function (gnus-find-method-for-group
9851                            (symbol-name group)) 'request-accept-article t)))
9852
9853 (defun gnus-read-move-group-name (prompt default articles prefix)
9854   "Read a group name."
9855   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9856          (minibuffer-confirm-incomplete nil) ; XEmacs
9857          (prom
9858           (format "%s %s to:"
9859                   prompt
9860                   (if (> (length articles) 1)
9861                       (format "these %d articles" (length articles))
9862                     "this article")))
9863          (to-newsgroup
9864           (cond
9865            ((null split-name)
9866             (gnus-completing-read default prom
9867                                   gnus-active-hashtb
9868                                   'gnus-valid-move-group-p
9869                                   nil prefix
9870                                   'gnus-group-history))
9871            ((= 1 (length split-name))
9872             (gnus-completing-read (car split-name) prom
9873                                   gnus-active-hashtb
9874                                   'gnus-valid-move-group-p
9875                                   nil nil
9876                                   'gnus-group-history))
9877            (t
9878             (gnus-completing-read nil prom
9879                                   (mapcar (lambda (el) (list el))
9880                                           (nreverse split-name))
9881                                   nil nil nil
9882                                   'gnus-group-history))))
9883          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9884     (when to-newsgroup
9885       (if (or (string= to-newsgroup "")
9886               (string= to-newsgroup prefix))
9887           (setq to-newsgroup default))
9888       (unless to-newsgroup
9889         (error "No group name entered"))
9890       (or (gnus-active to-newsgroup)
9891           (gnus-activate-group to-newsgroup nil nil to-method)
9892           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9893                                      to-newsgroup))
9894               (or (and (gnus-request-create-group to-newsgroup to-method)
9895                        (gnus-activate-group
9896                         to-newsgroup nil nil to-method)
9897                        (gnus-subscribe-group to-newsgroup))
9898                   (error "Couldn't create group %s" to-newsgroup)))
9899           (error "No such group: %s" to-newsgroup)))
9900     to-newsgroup))
9901
9902 (defun gnus-summary-save-parts (type dir n &optional reverse)
9903   "Save parts matching TYPE to DIR.
9904 If REVERSE, save parts that do not match TYPE."
9905   (interactive
9906    (list (read-string "Save parts of type: "
9907                       (or (car gnus-summary-save-parts-type-history)
9908                           gnus-summary-save-parts-default-mime)
9909                       'gnus-summary-save-parts-type-history)
9910          (setq gnus-summary-save-parts-last-directory
9911                (read-file-name "Save to directory: "
9912                                gnus-summary-save-parts-last-directory
9913                                nil t))
9914          current-prefix-arg))
9915   (gnus-summary-iterate n
9916     (let ((gnus-display-mime-function nil)
9917           (gnus-inhibit-treatment t))
9918       (gnus-summary-select-article))
9919     (save-excursion
9920       (set-buffer gnus-article-buffer)
9921       (let ((handles (or gnus-article-mime-handles
9922                          (mm-dissect-buffer) (mm-uu-dissect))))
9923         (when handles
9924           (gnus-summary-save-parts-1 type dir handles reverse)
9925           (unless gnus-article-mime-handles ;; Don't destroy this case.
9926             (mm-destroy-parts handles)))))))
9927
9928 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9929   (if (stringp (car handle))
9930       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9931               (cdr handle))
9932     (when (if reverse
9933               (not (string-match type (mm-handle-media-type handle)))
9934             (string-match type (mm-handle-media-type handle)))
9935       (let ((file (expand-file-name
9936                    (file-name-nondirectory
9937                     (or
9938                      (mail-content-type-get
9939                       (mm-handle-disposition handle) 'filename)
9940                      (concat gnus-newsgroup-name
9941                              "." (number-to-string
9942                                   (cdr gnus-article-current)))))
9943                    dir)))
9944         (unless (file-exists-p file)
9945           (mm-save-part-to-file handle file))))))
9946
9947 ;; Summary extract commands
9948
9949 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9950   (let ((buffer-read-only nil)
9951         (article (gnus-summary-article-number))
9952         after-article b e)
9953     (unless (gnus-summary-goto-subject article)
9954       (error "No such article: %d" article))
9955     (gnus-summary-position-point)
9956     ;; If all commands are to be bunched up on one line, we collect
9957     ;; them here.
9958     (unless gnus-view-pseudos-separately
9959       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9960             files action)
9961         (while ps
9962           (setq action (cdr (assq 'action (car ps))))
9963           (setq files (list (cdr (assq 'name (car ps)))))
9964           (while (and ps (cdr ps)
9965                       (string= (or action "1")
9966                                (or (cdr (assq 'action (cadr ps))) "2")))
9967             (push (cdr (assq 'name (cadr ps))) files)
9968             (setcdr ps (cddr ps)))
9969           (when files
9970             (when (not (string-match "%s" action))
9971               (push " " files))
9972             (push " " files)
9973             (when (assq 'execute (car ps))
9974               (setcdr (assq 'execute (car ps))
9975                       (funcall (if (string-match "%s" action)
9976                                    'format 'concat)
9977                                action
9978                                (mapconcat
9979                                 (lambda (f)
9980                                   (if (equal f " ")
9981                                       f
9982                                     (mm-quote-arg f)))
9983                                 files " ")))))
9984           (setq ps (cdr ps)))))
9985     (if (and gnus-view-pseudos (not not-view))
9986         (while pslist
9987           (when (assq 'execute (car pslist))
9988             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9989                                   (eq gnus-view-pseudos 'not-confirm)))
9990           (setq pslist (cdr pslist)))
9991       (save-excursion
9992         (while pslist
9993           (setq after-article (or (cdr (assq 'article (car pslist)))
9994                                   (gnus-summary-article-number)))
9995           (gnus-summary-goto-subject after-article)
9996           (forward-line 1)
9997           (setq b (point))
9998           (insert "    " (file-name-nondirectory
9999                           (cdr (assq 'name (car pslist))))
10000                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10001           (setq e (point))
10002           (forward-line -1)             ; back to `b'
10003           (gnus-add-text-properties
10004            b (1- e) (list 'gnus-number gnus-reffed-article-number
10005                           gnus-mouse-face-prop gnus-mouse-face))
10006           (gnus-data-enter
10007            after-article gnus-reffed-article-number
10008            gnus-unread-mark b (car pslist) 0 (- e b))
10009           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10010           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10011           (setq pslist (cdr pslist)))))))
10012
10013 (defun gnus-pseudos< (p1 p2)
10014   (let ((c1 (cdr (assq 'action p1)))
10015         (c2 (cdr (assq 'action p2))))
10016     (and c1 c2 (string< c1 c2))))
10017
10018 (defun gnus-request-pseudo-article (props)
10019   (cond ((assq 'execute props)
10020          (gnus-execute-command (cdr (assq 'execute props)))))
10021   (let ((gnus-current-article (gnus-summary-article-number)))
10022     (gnus-run-hooks 'gnus-mark-article-hook)))
10023
10024 (defun gnus-execute-command (command &optional automatic)
10025   (save-excursion
10026     (gnus-article-setup-buffer)
10027     (set-buffer gnus-article-buffer)
10028     (setq buffer-read-only nil)
10029     (let ((command (if automatic command
10030                      (read-string "Command: " (cons command 0)))))
10031       (erase-buffer)
10032       (insert "$ " command "\n\n")
10033       (if gnus-view-pseudo-asynchronously
10034           (start-process "gnus-execute" (current-buffer) shell-file-name
10035                          shell-command-switch command)
10036         (call-process shell-file-name nil t nil
10037                       shell-command-switch command)))))
10038
10039 ;; Summary kill commands.
10040
10041 (defun gnus-summary-edit-global-kill (article)
10042   "Edit the \"global\" kill file."
10043   (interactive (list (gnus-summary-article-number)))
10044   (gnus-group-edit-global-kill article))
10045
10046 (defun gnus-summary-edit-local-kill ()
10047   "Edit a local kill file applied to the current newsgroup."
10048   (interactive)
10049   (setq gnus-current-headers (gnus-summary-article-header))
10050   (gnus-group-edit-local-kill
10051    (gnus-summary-article-number) gnus-newsgroup-name))
10052
10053 ;;; Header reading.
10054
10055 (defun gnus-read-header (id &optional header)
10056   "Read the headers of article ID and enter them into the Gnus system."
10057   (let ((group gnus-newsgroup-name)
10058         (gnus-override-method
10059          (or
10060           gnus-override-method
10061           (and (gnus-news-group-p gnus-newsgroup-name)
10062                (car (gnus-refer-article-methods)))))
10063         where)
10064     ;; First we check to see whether the header in question is already
10065     ;; fetched.
10066     (if (stringp id)
10067         ;; This is a Message-ID.
10068         (setq header (or header (gnus-id-to-header id)))
10069       ;; This is an article number.
10070       (setq header (or header (gnus-summary-article-header id))))
10071     (if (and header
10072              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10073         ;; We have found the header.
10074         header
10075       ;; If this is a sparse article, we have to nix out its
10076       ;; previous entry in the thread hashtb.
10077       (when (and header
10078                  (gnus-summary-article-sparse-p (mail-header-number header)))
10079         (let* ((parent (gnus-parent-id (mail-header-references header)))
10080                (thread (and parent (gnus-id-to-thread parent))))
10081           (when thread
10082             (delq (assq header thread) thread))))
10083       ;; We have to really fetch the header to this article.
10084       (save-excursion
10085         (set-buffer nntp-server-buffer)
10086         (when (setq where (gnus-request-head id group))
10087           (nnheader-fold-continuation-lines)
10088           (goto-char (point-max))
10089           (insert ".\n")
10090           (goto-char (point-min))
10091           (insert "211 ")
10092           (princ (cond
10093                   ((numberp id) id)
10094                   ((cdr where) (cdr where))
10095                   (header (mail-header-number header))
10096                   (t gnus-reffed-article-number))
10097                  (current-buffer))
10098           (insert " Article retrieved.\n"))
10099         (if (or (not where)
10100                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10101             ()                          ; Malformed head.
10102           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10103             (when (and (stringp id)
10104                        (not (string= (gnus-group-real-name group)
10105                                      (car where))))
10106               ;; If we fetched by Message-ID and the article came
10107               ;; from a different group, we fudge some bogus article
10108               ;; numbers for this article.
10109               (mail-header-set-number header gnus-reffed-article-number))
10110             (save-excursion
10111               (set-buffer gnus-summary-buffer)
10112               (decf gnus-reffed-article-number)
10113               (gnus-remove-header (mail-header-number header))
10114               (push header gnus-newsgroup-headers)
10115               (setq gnus-current-headers header)
10116               (push (mail-header-number header) gnus-newsgroup-limit)))
10117           header)))))
10118
10119 (defun gnus-remove-header (number)
10120   "Remove header NUMBER from `gnus-newsgroup-headers'."
10121   (if (and gnus-newsgroup-headers
10122            (= number (mail-header-number (car gnus-newsgroup-headers))))
10123       (pop gnus-newsgroup-headers)
10124     (let ((headers gnus-newsgroup-headers))
10125       (while (and (cdr headers)
10126                   (not (= number (mail-header-number (cadr headers)))))
10127         (pop headers))
10128       (when (cdr headers)
10129         (setcdr headers (cddr headers))))))
10130
10131 ;;;
10132 ;;; summary highlights
10133 ;;;
10134
10135 (defun gnus-highlight-selected-summary ()
10136   "Highlight selected article in summary buffer."
10137   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10138   (when gnus-summary-selected-face
10139     (save-excursion
10140       (let* ((beg (progn (beginning-of-line) (point)))
10141              (end (progn (end-of-line) (point)))
10142              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10143              (from (if (get-text-property beg gnus-mouse-face-prop)
10144                        beg
10145                      (or (next-single-property-change
10146                           beg gnus-mouse-face-prop nil end)
10147                          beg)))
10148              (to
10149               (if (= from end)
10150                   (- from 2)
10151                 (or (next-single-property-change
10152                      from gnus-mouse-face-prop nil end)
10153                     end))))
10154         ;; If no mouse-face prop on line we will have to = from = end,
10155         ;; so we highlight the entire line instead.
10156         (when (= (+ to 2) from)
10157           (setq from beg)
10158           (setq to end))
10159         (if gnus-newsgroup-selected-overlay
10160             ;; Move old overlay.
10161             (gnus-move-overlay
10162              gnus-newsgroup-selected-overlay from to (current-buffer))
10163           ;; Create new overlay.
10164           (gnus-overlay-put
10165            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10166            'face gnus-summary-selected-face))))))
10167
10168 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10169 (defun gnus-summary-highlight-line ()
10170   "Highlight current line according to `gnus-summary-highlight'."
10171   (let* ((list gnus-summary-highlight)
10172          (p (point))
10173          (end (progn (end-of-line) (point)))
10174          ;; now find out where the line starts and leave point there.
10175          (beg (progn (beginning-of-line) (point)))
10176          (article (gnus-summary-article-number))
10177          (score (or (cdr (assq (or article gnus-current-article)
10178                                gnus-newsgroup-scored))
10179                     gnus-summary-default-score 0))
10180          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10181          (inhibit-read-only t))
10182     ;; Eval the cars of the lists until we find a match.
10183     (let ((default gnus-summary-default-score)
10184           (default-high gnus-summary-default-high-score)
10185           (default-low gnus-summary-default-low-score))
10186       (while (and list
10187                   (not (eval (caar list))))
10188         (setq list (cdr list))))
10189     (let ((face (cdar list)))
10190       (unless (eq face (get-text-property beg 'face))
10191         (gnus-put-text-property-excluding-characters-with-faces
10192          beg end 'face
10193          (setq face (if (boundp face) (symbol-value face) face)))
10194         (when gnus-summary-highlight-line-function
10195           (funcall gnus-summary-highlight-line-function article face))))
10196     (goto-char p)))
10197
10198 (defun gnus-update-read-articles (group unread &optional compute)
10199   "Update the list of read articles in GROUP."
10200   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10201          (entry (gnus-gethash group gnus-newsrc-hashtb))
10202          (info (nth 2 entry))
10203          (prev 1)
10204          (unread (sort (copy-sequence unread) '<))
10205          read)
10206     (if (or (not info) (not active))
10207         ;; There is no info on this group if it was, in fact,
10208         ;; killed.  Gnus stores no information on killed groups, so
10209         ;; there's nothing to be done.
10210         ;; One could store the information somewhere temporarily,
10211         ;; perhaps...  Hmmm...
10212         ()
10213       ;; Remove any negative articles numbers.
10214       (while (and unread (< (car unread) 0))
10215         (setq unread (cdr unread)))
10216       ;; Remove any expired article numbers
10217       (while (and unread (< (car unread) (car active)))
10218         (setq unread (cdr unread)))
10219       ;; Compute the ranges of read articles by looking at the list of
10220       ;; unread articles.
10221       (while unread
10222         (when (/= (car unread) prev)
10223           (push (if (= prev (1- (car unread))) prev
10224                   (cons prev (1- (car unread))))
10225                 read))
10226         (setq prev (1+ (car unread)))
10227         (setq unread (cdr unread)))
10228       (when (<= prev (cdr active))
10229         (push (cons prev (cdr active)) read))
10230       (setq read (if (> (length read) 1) (nreverse read) read))
10231       (if compute
10232           read
10233         (save-excursion
10234           (let (setmarkundo)
10235             ;; Propagate the read marks to the backend.
10236             (when (gnus-check-backend-function 'request-set-mark group)
10237               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10238                     (add (gnus-remove-from-range read (gnus-info-read info))))
10239                 (when (or add del)
10240                   (unless (gnus-check-group group)
10241                     (error "Can't open server for %s" group))
10242                   (gnus-request-set-mark
10243                    group (delq nil (list (if add (list add 'add '(read)))
10244                                          (if del (list del 'del '(read))))))
10245                   (setq setmarkundo
10246                         `(gnus-request-set-mark
10247                           ,group
10248                           ',(delq nil (list
10249                                        (if del (list del 'add '(read)))
10250                                        (if add (list add 'del '(read))))))))))
10251             (set-buffer gnus-group-buffer)
10252             (gnus-undo-register
10253               `(progn
10254                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10255                  (gnus-info-set-read ',info ',(gnus-info-read info))
10256                  (gnus-get-unread-articles-in-group ',info
10257                                                     (gnus-active ,group))
10258                  (gnus-group-update-group ,group t)
10259                  ,setmarkundo))))
10260         ;; Enter this list into the group info.
10261         (gnus-info-set-read info read)
10262         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10263         (gnus-get-unread-articles-in-group info (gnus-active group))
10264         t))))
10265
10266 (defun gnus-offer-save-summaries ()
10267   "Offer to save all active summary buffers."
10268   (let (buffers)
10269     ;; Go through all buffers and find all summaries.
10270     (dolist (buffer (buffer-list))
10271       (when (and (setq buffer (buffer-name buffer))
10272                  (string-match "Summary" buffer)
10273                  (save-excursion
10274                    (set-buffer buffer)
10275                    ;; We check that this is, indeed, a summary buffer.
10276                    (and (eq major-mode 'gnus-summary-mode)
10277                         ;; Also make sure this isn't bogus.
10278                         gnus-newsgroup-prepared
10279                         ;; Also make sure that this isn't a
10280                         ;; dead summary buffer.
10281                         (not gnus-dead-summary-mode))))
10282         (push buffer buffers)))
10283     ;; Go through all these summary buffers and offer to save them.
10284     (when buffers
10285       (save-excursion
10286         (map-y-or-n-p
10287          "Update summary buffer %s? "
10288          (lambda (buf)
10289            (switch-to-buffer buf)
10290            (gnus-summary-exit))
10291          buffers)))))
10292
10293 (defun gnus-summary-setup-default-charset ()
10294   "Setup newsgroup default charset."
10295   (if (equal gnus-newsgroup-name "nndraft:drafts")
10296       (setq gnus-newsgroup-charset nil)
10297     (let* ((ignored-charsets
10298             (or gnus-newsgroup-ephemeral-ignored-charsets
10299                 (append
10300                  (and gnus-newsgroup-name
10301                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10302                  gnus-newsgroup-ignored-charsets))))
10303       (setq gnus-newsgroup-charset
10304             (or gnus-newsgroup-ephemeral-charset
10305                 (and gnus-newsgroup-name
10306                      (gnus-parameter-charset gnus-newsgroup-name))
10307                 gnus-default-charset))
10308       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10309            ignored-charsets))))
10310
10311 ;;;
10312 ;;; Mime Commands
10313 ;;;
10314
10315 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10316   "Display the current article buffer fully MIME-buttonized.
10317 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10318 treated as multipart/mixed."
10319   (interactive "P")
10320   (require 'gnus-art)
10321   (let ((gnus-unbuttonized-mime-types nil)
10322         (gnus-mime-display-multipart-as-mixed show-all-parts))
10323     (gnus-summary-show-article)))
10324
10325 (defun gnus-summary-repair-multipart (article)
10326   "Add a Content-Type header to a multipart article without one."
10327   (interactive (list (gnus-summary-article-number)))
10328   (gnus-with-article article
10329     (message-narrow-to-head)
10330     (message-remove-header "Mime-Version")
10331     (goto-char (point-max))
10332     (insert "Mime-Version: 1.0\n")
10333     (widen)
10334     (when (search-forward "\n--" nil t)
10335       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10336         (message-narrow-to-head)
10337         (message-remove-header "Content-Type")
10338         (goto-char (point-max))
10339         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10340                         separator))
10341         (widen))))
10342   (let (gnus-mark-article-hook)
10343     (gnus-summary-select-article t t nil article)))
10344
10345 (defun gnus-summary-toggle-display-buttonized ()
10346   "Toggle the buttonizing of the article buffer."
10347   (interactive)
10348   (require 'gnus-art)
10349   (if (setq gnus-inhibit-mime-unbuttonizing
10350             (not gnus-inhibit-mime-unbuttonizing))
10351       (let ((gnus-unbuttonized-mime-types nil))
10352         (gnus-summary-show-article))
10353     (gnus-summary-show-article)))
10354
10355 ;;;
10356 ;;; Generic summary marking commands
10357 ;;;
10358
10359 (defvar gnus-summary-marking-alist
10360   '((read gnus-del-mark "d")
10361     (unread gnus-unread-mark "u")
10362     (ticked gnus-ticked-mark "!")
10363     (dormant gnus-dormant-mark "?")
10364     (expirable gnus-expirable-mark "e"))
10365   "An alist of names/marks/keystrokes.")
10366
10367 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10368 (defvar gnus-summary-mark-map)
10369
10370 (defun gnus-summary-make-all-marking-commands ()
10371   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10372   (dolist (elem gnus-summary-marking-alist)
10373     (apply 'gnus-summary-make-marking-command elem)))
10374
10375 (defun gnus-summary-make-marking-command (name mark keystroke)
10376   (let ((map (make-sparse-keymap)))
10377     (define-key gnus-summary-generic-mark-map keystroke map)
10378     (dolist (lway `((next "next" next nil "n")
10379                     (next-unread "next unread" next t "N")
10380                     (prev "previous" prev nil "p")
10381                     (prev-unread "previous unread" prev t "P")
10382                     (nomove "" nil nil ,keystroke)))
10383       (let ((func (gnus-summary-make-marking-command-1
10384                    mark (car lway) lway name)))
10385         (setq func (eval func))
10386         (define-key map (nth 4 lway) func)))))
10387
10388 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10389   `(defun ,(intern
10390             (format "gnus-summary-put-mark-as-%s%s"
10391                     name (if (eq way 'nomove)
10392                              ""
10393                            (concat "-" (symbol-name way)))))
10394      (n)
10395      ,(format
10396        "Mark the current article as %s%s.
10397 If N, the prefix, then repeat N times.
10398 If N is negative, move in reverse order.
10399 The difference between N and the actual number of articles marked is
10400 returned."
10401        name (cadr lway))
10402      (interactive "p")
10403      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10404
10405 (defun gnus-summary-generic-mark (n mark move unread)
10406   "Mark N articles with MARK."
10407   (unless (eq major-mode 'gnus-summary-mode)
10408     (error "This command can only be used in the summary buffer"))
10409   (gnus-summary-show-thread)
10410   (let ((nummove
10411          (cond
10412           ((eq move 'next) 1)
10413           ((eq move 'prev) -1)
10414           (t 0))))
10415     (if (zerop nummove)
10416         (setq n 1)
10417       (when (< n 0)
10418         (setq n (abs n)
10419               nummove (* -1 nummove))))
10420     (while (and (> n 0)
10421                 (gnus-summary-mark-article nil mark)
10422                 (zerop (gnus-summary-next-subject nummove unread t)))
10423       (setq n (1- n)))
10424     (when (/= 0 n)
10425       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10426     (gnus-summary-recenter)
10427     (gnus-summary-position-point)
10428     (gnus-set-mode-line 'summary)
10429     n))
10430
10431 (defun gnus-summary-insert-articles (articles)
10432   (when (setq articles
10433               (gnus-set-difference articles
10434                                    (mapcar (lambda (h) (mail-header-number h))
10435                                            gnus-newsgroup-headers)))
10436     (setq gnus-newsgroup-headers
10437           (merge 'list
10438                  gnus-newsgroup-headers
10439                  (gnus-fetch-headers articles)
10440                  'gnus-article-sort-by-number))
10441     ;; Suppress duplicates?
10442     (when gnus-suppress-duplicates
10443       (gnus-dup-suppress-articles))
10444
10445     ;; We might want to build some more threads first.
10446     (when (and gnus-fetch-old-headers
10447                (eq gnus-headers-retrieved-by 'nov))
10448       (if (eq gnus-fetch-old-headers 'invisible)
10449           (gnus-build-all-threads)
10450         (gnus-build-old-threads)))
10451     ;; Let the Gnus agent mark articles as read.
10452     (when gnus-agent
10453       (gnus-agent-get-undownloaded-list))
10454     ;; Remove list identifiers from subject
10455     (when gnus-list-identifiers
10456       (gnus-summary-remove-list-identifiers))
10457     ;; First and last article in this newsgroup.
10458     (when gnus-newsgroup-headers
10459       (setq gnus-newsgroup-begin
10460             (mail-header-number (car gnus-newsgroup-headers))
10461             gnus-newsgroup-end
10462             (mail-header-number
10463              (gnus-last-element gnus-newsgroup-headers))))
10464     (when gnus-use-scoring
10465       (gnus-possibly-score-headers))))
10466
10467 (defun gnus-summary-insert-old-articles (&optional all)
10468   "Insert all old articles in this group.
10469 If ALL is non-nil, already read articles become readable.
10470 If ALL is a number, fetch this number of articles."
10471   (interactive "P")
10472   (prog1
10473       (let ((old (mapcar 'car gnus-newsgroup-data))
10474             (i (car gnus-newsgroup-active))
10475             older len)
10476         (while (<= i (cdr gnus-newsgroup-active))
10477           (or (memq i old) (push i older))
10478           (incf i))
10479         (setq len (length older))
10480         (cond
10481          ((null older) nil)
10482          ((numberp all)
10483           (if (< all len)
10484               (setq older (subseq older 0 all))))
10485          (all nil)
10486          (t
10487           (if (and (numberp gnus-large-newsgroup)
10488                    (> len gnus-large-newsgroup))
10489               (let ((input
10490                      (read-string
10491                       (format
10492                        "How many articles from %s (default %d): "
10493                        (gnus-limit-string
10494                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10495                        len))))
10496                 (unless (string-match "^[ \t]*$" input)
10497                   (setq all (string-to-number input))
10498                   (if (< all len)
10499                       (setq older (subseq older 0 all))))))))
10500         (if (not older)
10501             (message "No old news.")
10502           (gnus-summary-insert-articles older)
10503           (gnus-summary-limit (gnus-union older old))))
10504     (gnus-summary-position-point)))
10505
10506 (defun gnus-summary-insert-new-articles ()
10507   "Insert all new articles in this group."
10508   (interactive)
10509   (prog1
10510       (let ((old (mapcar 'car gnus-newsgroup-data))
10511             (old-active gnus-newsgroup-active)
10512             (nnmail-fetched-sources (list t))
10513             i new)
10514         (setq gnus-newsgroup-active
10515               (gnus-activate-group gnus-newsgroup-name 'scan))
10516         (setq i (1+ (cdr old-active)))
10517         (while (<= i (cdr gnus-newsgroup-active))
10518           (push i new)
10519           (incf i))
10520         (if (not new)
10521             (message "No gnus is bad news.")
10522           (setq new (nreverse new))
10523           (gnus-summary-insert-articles new)
10524           (setq gnus-newsgroup-unreads
10525                 (append gnus-newsgroup-unreads new))
10526           (gnus-summary-limit (gnus-union old new))))
10527     (gnus-summary-position-point)))
10528
10529 (gnus-summary-make-all-marking-commands)
10530
10531 (gnus-ems-redefine)
10532
10533 (provide 'gnus-sum)
10534
10535 (run-hooks 'gnus-sum-load-hook)
10536
10537 ;;; gnus-sum.el ends here