* gnus-sum.el (gnus-articles-to-read): Comments.
[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 ?O
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       ;; Then we have to re-compute how many unread
5080       ;; articles there are in this group.
5081       (when active
5082         (cond
5083          ((not range)
5084           (setq num (- (1+ (cdr active)) (car active))))
5085          ((not (listp (cdr range)))
5086           (setq num (- (cdr active) (- (1+ (cdr range))
5087                                        (car range)))))
5088          (t
5089           (while range
5090             (if (numberp (car range))
5091                 (setq num (1+ num))
5092               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5093             (setq range (cdr range)))
5094           (setq num (- (cdr active) num))))
5095         ;; Update the number of unread articles.
5096         (setcar entry num)
5097         ;; Update the group buffer.
5098         (gnus-group-update-group group t)))))
5099
5100 (defvar gnus-newsgroup-none-id 0)
5101
5102 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5103   (let ((cur nntp-server-buffer)
5104         (dependencies
5105          (or dependencies
5106              (save-excursion (set-buffer gnus-summary-buffer)
5107                              gnus-newsgroup-dependencies)))
5108         headers id end ref
5109         (mail-parse-charset gnus-newsgroup-charset)
5110         (mail-parse-ignored-charsets
5111          (save-excursion (condition-case nil
5112                              (set-buffer gnus-summary-buffer)
5113                            (error))
5114                          gnus-newsgroup-ignored-charsets)))
5115     (save-excursion
5116       (set-buffer nntp-server-buffer)
5117       ;; Translate all TAB characters into SPACE characters.
5118       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5119       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5120       (gnus-run-hooks 'gnus-parse-headers-hook)
5121       (let ((case-fold-search t)
5122             in-reply-to header p lines chars)
5123         (goto-char (point-min))
5124         ;; Search to the beginning of the next header.  Error messages
5125         ;; do not begin with 2 or 3.
5126         (while (re-search-forward "^[23][0-9]+ " nil t)
5127           (setq id nil
5128                 ref nil)
5129           ;; This implementation of this function, with nine
5130           ;; search-forwards instead of the one re-search-forward and
5131           ;; a case (which basically was the old function) is actually
5132           ;; about twice as fast, even though it looks messier.  You
5133           ;; can't have everything, I guess.  Speed and elegance
5134           ;; doesn't always go hand in hand.
5135           (setq
5136            header
5137            (vector
5138             ;; Number.
5139             (prog1
5140                 (read cur)
5141               (end-of-line)
5142               (setq p (point))
5143               (narrow-to-region (point)
5144                                 (or (and (search-forward "\n.\n" nil t)
5145                                          (- (point) 2))
5146                                     (point))))
5147             ;; Subject.
5148             (progn
5149               (goto-char p)
5150               (if (search-forward "\nsubject:" nil t)
5151                   (funcall gnus-decode-encoded-word-function
5152                            (nnheader-header-value))
5153                 "(none)"))
5154             ;; From.
5155             (progn
5156               (goto-char p)
5157               (if (search-forward "\nfrom:" nil t)
5158                   (funcall gnus-decode-encoded-word-function
5159                            (nnheader-header-value))
5160                 "(nobody)"))
5161             ;; Date.
5162             (progn
5163               (goto-char p)
5164               (if (search-forward "\ndate:" nil t)
5165                   (nnheader-header-value) ""))
5166             ;; Message-ID.
5167             (progn
5168               (goto-char p)
5169               (setq id (if (re-search-forward
5170                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5171                       ;; We do it this way to make sure the Message-ID
5172                            ;; is (somewhat) syntactically valid.
5173                            (buffer-substring (match-beginning 1)
5174                                              (match-end 1))
5175                        ;; If there was no message-id, we just fake one
5176                          ;; to make subsequent routines simpler.
5177                          (nnheader-generate-fake-message-id))))
5178             ;; References.
5179             (progn
5180               (goto-char p)
5181               (if (search-forward "\nreferences:" nil t)
5182                   (progn
5183                     (setq end (point))
5184                     (prog1
5185                         (nnheader-header-value)
5186                       (setq ref
5187                             (buffer-substring
5188                              (progn
5189                                (end-of-line)
5190                                (search-backward ">" end t)
5191                                (1+ (point)))
5192                              (progn
5193                                (search-backward "<" end t)
5194                                (point))))))
5195             ;; Get the references from the in-reply-to header if there
5196                 ;; were no references and the in-reply-to header looks
5197                 ;; promising.
5198                 (if (and (search-forward "\nin-reply-to:" nil t)
5199                          (setq in-reply-to (nnheader-header-value))
5200                          (string-match "<[^>]+>" in-reply-to))
5201                     (let (ref2)
5202                       (setq ref (substring in-reply-to (match-beginning 0)
5203                                            (match-end 0)))
5204                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5205                         (setq ref2 (substring in-reply-to (match-beginning 0)
5206                                               (match-end 0)))
5207                         (when (> (length ref2) (length ref))
5208                           (setq ref ref2)))
5209                       ref)
5210                   (setq ref nil))))
5211             ;; Chars.
5212             (progn
5213               (goto-char p)
5214               (if (search-forward "\nchars: " nil t)
5215                   (if (numberp (setq chars (ignore-errors (read cur))))
5216                       chars -1)
5217                 -1))
5218             ;; Lines.
5219             (progn
5220               (goto-char p)
5221               (if (search-forward "\nlines: " nil t)
5222                   (if (numberp (setq lines (ignore-errors (read cur))))
5223                       lines -1)
5224                 -1))
5225             ;; Xref.
5226             (progn
5227               (goto-char p)
5228               (and (search-forward "\nxref:" nil t)
5229                    (nnheader-header-value)))
5230             ;; Extra.
5231             (when gnus-extra-headers
5232               (let ((extra gnus-extra-headers)
5233                     out)
5234                 (while extra
5235                   (goto-char p)
5236                   (when (search-forward
5237                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5238                     (push (cons (car extra) (nnheader-header-value))
5239                           out))
5240                   (pop extra))
5241                 out))))
5242           (when (equal id ref)
5243             (setq ref nil))
5244
5245           (when gnus-alter-header-function
5246             (funcall gnus-alter-header-function header)
5247             (setq id (mail-header-id header)
5248                   ref (gnus-parent-id (mail-header-references header))))
5249
5250           (when (setq header
5251                       (gnus-dependencies-add-header
5252                        header dependencies force-new))
5253             (push header headers))
5254           (goto-char (point-max))
5255           (widen))
5256         (nreverse headers)))))
5257
5258 ;; Goes through the xover lines and returns a list of vectors
5259 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5260                                                   force-new dependencies
5261                                                   group also-fetch-heads)
5262   "Parse the news overview data in the server buffer.
5263 Return a list of headers that match SEQUENCE (see
5264 `nntp-retrieve-headers')."
5265   ;; Get the Xref when the users reads the articles since most/some
5266   ;; NNTP servers do not include Xrefs when using XOVER.
5267   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5268   (let ((mail-parse-charset gnus-newsgroup-charset)
5269         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5270         (cur nntp-server-buffer)
5271         (dependencies (or dependencies gnus-newsgroup-dependencies))
5272         (allp (cond
5273                ((eq gnus-read-all-available-headers t)
5274                 t)
5275                ((stringp gnus-read-all-available-headers)
5276                 (string-match gnus-read-all-available-headers group))
5277                (t
5278                 nil)))
5279         number headers header)
5280     (save-excursion
5281       (set-buffer nntp-server-buffer)
5282       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5283       ;; Allow the user to mangle the headers before parsing them.
5284       (gnus-run-hooks 'gnus-parse-headers-hook)
5285       (goto-char (point-min))
5286       (while (not (eobp))
5287         (condition-case ()
5288             (while (and (or sequence allp)
5289                         (not (eobp)))
5290               (setq number (read cur))
5291               (when (not allp)
5292                 (while (and sequence
5293                             (< (car sequence) number))
5294                   (setq sequence (cdr sequence))))
5295               (when (and (or allp
5296                              (and sequence
5297                                   (eq number (car sequence))))
5298                          (progn
5299                            (setq sequence (cdr sequence))
5300                            (setq header (inline
5301                                           (gnus-nov-parse-line
5302                                            number dependencies force-new)))))
5303                 (push header headers))
5304               (forward-line 1))
5305           (error
5306            (gnus-error 4 "Strange nov line (%d)"
5307                        (count-lines (point-min) (point)))))
5308         (forward-line 1))
5309       ;; A common bug in inn is that if you have posted an article and
5310       ;; then retrieves the active file, it will answer correctly --
5311       ;; the new article is included.  However, a NOV entry for the
5312       ;; article may not have been generated yet, so this may fail.
5313       ;; We work around this problem by retrieving the last few
5314       ;; headers using HEAD.
5315       (if (or (not also-fetch-heads)
5316               (not sequence))
5317           ;; We (probably) got all the headers.
5318           (nreverse headers)
5319         (let ((gnus-nov-is-evil t))
5320           (nconc
5321            (nreverse headers)
5322            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5323              (gnus-get-newsgroup-headers))))))))
5324
5325 (defun gnus-article-get-xrefs ()
5326   "Fill in the Xref value in `gnus-current-headers', if necessary.
5327 This is meant to be called in `gnus-article-internal-prepare-hook'."
5328   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5329                                  gnus-current-headers)))
5330     (or (not gnus-use-cross-reference)
5331         (not headers)
5332         (and (mail-header-xref headers)
5333              (not (string= (mail-header-xref headers) "")))
5334         (let ((case-fold-search t)
5335               xref)
5336           (save-restriction
5337             (nnheader-narrow-to-headers)
5338             (goto-char (point-min))
5339             (when (or (and (not (eobp))
5340                            (eq (downcase (char-after)) ?x)
5341                            (looking-at "Xref:"))
5342                       (search-forward "\nXref:" nil t))
5343               (goto-char (1+ (match-end 0)))
5344               (setq xref (buffer-substring (point)
5345                                            (progn (end-of-line) (point))))
5346               (mail-header-set-xref headers xref)))))))
5347
5348 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5349   "Find article ID and insert the summary line for that article.
5350 OLD-HEADER can either be a header or a line number to insert
5351 the subject line on."
5352   (let* ((line (and (numberp old-header) old-header))
5353          (old-header (and (vectorp old-header) old-header))
5354          (header (cond ((and old-header use-old-header)
5355                         old-header)
5356                        ((and (numberp id)
5357                              (gnus-number-to-header id))
5358                         (gnus-number-to-header id))
5359                        (t
5360                         (gnus-read-header id))))
5361          (number (and (numberp id) id))
5362          d)
5363     (when header
5364       ;; Rebuild the thread that this article is part of and go to the
5365       ;; article we have fetched.
5366       (when (and (not gnus-show-threads)
5367                  old-header)
5368         (when (and number
5369                    (setq d (gnus-data-find (mail-header-number old-header))))
5370           (goto-char (gnus-data-pos d))
5371           (gnus-data-remove
5372            number
5373            (- (gnus-point-at-bol)
5374               (prog1
5375                   (1+ (gnus-point-at-eol))
5376                 (gnus-delete-line))))))
5377       (when old-header
5378         (mail-header-set-number header (mail-header-number old-header)))
5379       (setq gnus-newsgroup-sparse
5380             (delq (setq number (mail-header-number header))
5381                   gnus-newsgroup-sparse))
5382       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5383       (push number gnus-newsgroup-limit)
5384       (gnus-rebuild-thread (mail-header-id header) line)
5385       (gnus-summary-goto-subject number nil t))
5386     (when (and (numberp number)
5387                (> number 0))
5388       ;; We have to update the boundaries even if we can't fetch the
5389       ;; article if ID is a number -- so that the next `P' or `N'
5390       ;; command will fetch the previous (or next) article even
5391       ;; if the one we tried to fetch this time has been canceled.
5392       (when (> number gnus-newsgroup-end)
5393         (setq gnus-newsgroup-end number))
5394       (when (< number gnus-newsgroup-begin)
5395         (setq gnus-newsgroup-begin number))
5396       (setq gnus-newsgroup-unselected
5397             (delq number gnus-newsgroup-unselected)))
5398     ;; Report back a success?
5399     (and header (mail-header-number header))))
5400
5401 ;;; Process/prefix in the summary buffer
5402
5403 (defun gnus-summary-work-articles (n)
5404   "Return a list of articles to be worked upon.
5405 The prefix argument, the list of process marked articles, and the
5406 current article will be taken into consideration."
5407   (save-excursion
5408     (set-buffer gnus-summary-buffer)
5409     (cond
5410      (n
5411       ;; A numerical prefix has been given.
5412       (setq n (prefix-numeric-value n))
5413       (let ((backward (< n 0))
5414             (n (abs (prefix-numeric-value n)))
5415             articles article)
5416         (save-excursion
5417           (while
5418               (and (> n 0)
5419                    (push (setq article (gnus-summary-article-number))
5420                          articles)
5421                    (if backward
5422                        (gnus-summary-find-prev nil article)
5423                      (gnus-summary-find-next nil article)))
5424             (decf n)))
5425         (nreverse articles)))
5426      ((and (gnus-region-active-p) (mark))
5427       (message "region active")
5428       ;; Work on the region between point and mark.
5429       (let ((max (max (point) (mark)))
5430             articles article)
5431         (save-excursion
5432           (goto-char (min (min (point) (mark))))
5433           (while
5434               (and
5435                (push (setq article (gnus-summary-article-number)) articles)
5436                (gnus-summary-find-next nil article)
5437                (< (point) max)))
5438           (nreverse articles))))
5439      (gnus-newsgroup-processable
5440       ;; There are process-marked articles present.
5441       ;; Save current state.
5442       (gnus-summary-save-process-mark)
5443       ;; Return the list.
5444       (reverse gnus-newsgroup-processable))
5445      (t
5446       ;; Just return the current article.
5447       (list (gnus-summary-article-number))))))
5448
5449 (defmacro gnus-summary-iterate (arg &rest forms)
5450   "Iterate over the process/prefixed articles and do FORMS.
5451 ARG is the interactive prefix given to the command.  FORMS will be
5452 executed with point over the summary line of the articles."
5453   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5454     `(let ((,articles (gnus-summary-work-articles ,arg)))
5455        (while ,articles
5456          (gnus-summary-goto-subject (car ,articles))
5457          ,@forms
5458          (pop ,articles)))))
5459
5460 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5461 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5462
5463 (defun gnus-summary-save-process-mark ()
5464   "Push the current set of process marked articles on the stack."
5465   (interactive)
5466   (push (copy-sequence gnus-newsgroup-processable)
5467         gnus-newsgroup-process-stack))
5468
5469 (defun gnus-summary-kill-process-mark ()
5470   "Push the current set of process marked articles on the stack and unmark."
5471   (interactive)
5472   (gnus-summary-save-process-mark)
5473   (gnus-summary-unmark-all-processable))
5474
5475 (defun gnus-summary-yank-process-mark ()
5476   "Pop the last process mark state off the stack and restore it."
5477   (interactive)
5478   (unless gnus-newsgroup-process-stack
5479     (error "Empty mark stack"))
5480   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5481
5482 (defun gnus-summary-process-mark-set (set)
5483   "Make SET into the current process marked articles."
5484   (gnus-summary-unmark-all-processable)
5485   (while set
5486     (gnus-summary-set-process-mark (pop set))))
5487
5488 ;;; Searching and stuff
5489
5490 (defun gnus-summary-search-group (&optional backward use-level)
5491   "Search for next unread newsgroup.
5492 If optional argument BACKWARD is non-nil, search backward instead."
5493   (save-excursion
5494     (set-buffer gnus-group-buffer)
5495     (when (gnus-group-search-forward
5496            backward nil (if use-level (gnus-group-group-level) nil))
5497       (gnus-group-group-name))))
5498
5499 (defun gnus-summary-best-group (&optional exclude-group)
5500   "Find the name of the best unread group.
5501 If EXCLUDE-GROUP, do not go to this group."
5502   (save-excursion
5503     (set-buffer gnus-group-buffer)
5504     (save-excursion
5505       (gnus-group-best-unread-group exclude-group))))
5506
5507 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5508   (if backward (gnus-summary-find-prev)
5509     (let* ((dummy (gnus-summary-article-intangible-p))
5510            (article (or article (gnus-summary-article-number)))
5511            (arts (gnus-data-find-list article))
5512            result)
5513       (when (and (not dummy)
5514                  (or (not gnus-summary-check-current)
5515                      (not unread)
5516                      (not (gnus-data-unread-p (car arts)))))
5517         (setq arts (cdr arts)))
5518       (when (setq result
5519                   (if unread
5520                       (progn
5521                         (while arts
5522                           (when (or (and undownloaded
5523                                          (eq gnus-undownloaded-mark
5524                                              (gnus-data-mark (car arts))))
5525                                     (gnus-data-unread-p (car arts)))
5526                             (setq result (car arts)
5527                                   arts nil))
5528                           (setq arts (cdr arts)))
5529                         result)
5530                     (car arts)))
5531         (goto-char (gnus-data-pos result))
5532         (gnus-data-number result)))))
5533
5534 (defun gnus-summary-find-prev (&optional unread article)
5535   (let* ((eobp (eobp))
5536          (article (or article (gnus-summary-article-number)))
5537          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5538          result)
5539     (when (and (not eobp)
5540                (or (not gnus-summary-check-current)
5541                    (not unread)
5542                    (not (gnus-data-unread-p (car arts)))))
5543       (setq arts (cdr arts)))
5544     (when (setq result
5545                 (if unread
5546                     (progn
5547                       (while arts
5548                         (when (gnus-data-unread-p (car arts))
5549                           (setq result (car arts)
5550                                 arts nil))
5551                         (setq arts (cdr arts)))
5552                       result)
5553                   (car arts)))
5554       (goto-char (gnus-data-pos result))
5555       (gnus-data-number result))))
5556
5557 (defun gnus-summary-find-subject (subject &optional unread backward article)
5558   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5559          (article (or article (gnus-summary-article-number)))
5560          (articles (gnus-data-list backward))
5561          (arts (gnus-data-find-list article articles))
5562          result)
5563     (when (or (not gnus-summary-check-current)
5564               (not unread)
5565               (not (gnus-data-unread-p (car arts))))
5566       (setq arts (cdr arts)))
5567     (while arts
5568       (and (or (not unread)
5569                (gnus-data-unread-p (car arts)))
5570            (vectorp (gnus-data-header (car arts)))
5571            (gnus-subject-equal
5572             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5573            (setq result (car arts)
5574                  arts nil))
5575       (setq arts (cdr arts)))
5576     (and result
5577          (goto-char (gnus-data-pos result))
5578          (gnus-data-number result))))
5579
5580 (defun gnus-summary-search-forward (&optional unread subject backward)
5581   "Search forward for an article.
5582 If UNREAD, look for unread articles.  If SUBJECT, look for
5583 articles with that subject.  If BACKWARD, search backward instead."
5584   (cond (subject (gnus-summary-find-subject subject unread backward))
5585         (backward (gnus-summary-find-prev unread))
5586         (t (gnus-summary-find-next unread))))
5587
5588 (defun gnus-recenter (&optional n)
5589   "Center point in window and redisplay frame.
5590 Also do horizontal recentering."
5591   (interactive "P")
5592   (when (and gnus-auto-center-summary
5593              (not (eq gnus-auto-center-summary 'vertical)))
5594     (gnus-horizontal-recenter))
5595   (recenter n))
5596
5597 (defun gnus-summary-recenter ()
5598   "Center point in the summary window.
5599 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5600 displayed, no centering will be performed."
5601   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5602 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5603   (interactive)
5604   (let* ((top (cond ((< (window-height) 4) 0)
5605                     ((< (window-height) 7) 1)
5606                     (t (if (numberp gnus-auto-center-summary)
5607                            gnus-auto-center-summary
5608                          2))))
5609          (height (1- (window-height)))
5610          (bottom (save-excursion (goto-char (point-max))
5611                                  (forward-line (- height))
5612                                  (point)))
5613          (window (get-buffer-window (current-buffer))))
5614     ;; The user has to want it.
5615     (when gnus-auto-center-summary
5616       (when (get-buffer-window gnus-article-buffer)
5617         ;; Only do recentering when the article buffer is displayed,
5618       ;; Set the window start to either `bottom', which is the biggest
5619         ;; possible valid number, or the second line from the top,
5620         ;; whichever is the least.
5621         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5622           (if (> bottom top-pos)
5623               ;; Keep the second line from the top visible
5624               (set-window-start window top-pos t)
5625             ;; Try to keep the bottom line visible; if it's partially
5626             ;; obscured, either scroll one more line to make it fully
5627             ;; visible, or revert to using TOP-POS.
5628             (save-excursion
5629               (goto-char (point-max))
5630               (forward-line -1)
5631               (let ((last-line-start (point)))
5632                 (goto-char bottom)
5633                 (set-window-start window (point) t)
5634                 (when (not (pos-visible-in-window-p last-line-start window))
5635                   (forward-line 1)
5636                   (set-window-start window (min (point) top-pos) t)))))))
5637       ;; Do horizontal recentering while we're at it.
5638       (when (and (get-buffer-window (current-buffer) t)
5639                  (not (eq gnus-auto-center-summary 'vertical)))
5640         (let ((selected (selected-window)))
5641           (select-window (get-buffer-window (current-buffer) t))
5642           (gnus-summary-position-point)
5643           (gnus-horizontal-recenter)
5644           (select-window selected))))))
5645
5646 (defun gnus-summary-jump-to-group (newsgroup)
5647   "Move point to NEWSGROUP in group mode buffer."
5648   ;; Keep update point of group mode buffer if visible.
5649   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5650       (save-window-excursion
5651         ;; Take care of tree window mode.
5652         (when (get-buffer-window gnus-group-buffer)
5653           (pop-to-buffer gnus-group-buffer))
5654         (gnus-group-jump-to-group newsgroup))
5655     (save-excursion
5656       ;; Take care of tree window mode.
5657       (if (get-buffer-window gnus-group-buffer)
5658           (pop-to-buffer gnus-group-buffer)
5659         (set-buffer gnus-group-buffer))
5660       (gnus-group-jump-to-group newsgroup))))
5661
5662 ;; This function returns a list of article numbers based on the
5663 ;; difference between the ranges of read articles in this group and
5664 ;; the range of active articles.
5665 (defun gnus-list-of-unread-articles (group)
5666   (let* ((read (gnus-info-read (gnus-get-info group)))
5667          (active (or (gnus-active group) (gnus-activate-group group)))
5668          (last (cdr active))
5669          first nlast unread)
5670     ;; If none are read, then all are unread.
5671     (if (not read)
5672         (setq first (car active))
5673       ;; If the range of read articles is a single range, then the
5674       ;; first unread article is the article after the last read
5675       ;; article.  Sounds logical, doesn't it?
5676       (if (and (not (listp (cdr read)))
5677                (or (< (car read) (car active))
5678                    (progn (setq read (list read))
5679                           nil)))
5680           (setq first (max (car active) (1+ (cdr read))))
5681         ;; `read' is a list of ranges.
5682         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5683                                   (caar read)))
5684                   1)
5685           (setq first (car active)))
5686         (while read
5687           (when first
5688             (while (< first nlast)
5689               (push first unread)
5690               (setq first (1+ first))))
5691           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5692           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5693           (setq read (cdr read)))))
5694     ;; And add the last unread articles.
5695     (while (<= first last)
5696       (push first unread)
5697       (setq first (1+ first)))
5698     ;; Return the list of unread articles.
5699     (delq 0 (nreverse unread))))
5700
5701 (defun gnus-list-of-read-articles (group)
5702   "Return a list of unread, unticked and non-dormant articles."
5703   (let* ((info (gnus-get-info group))
5704          (marked (gnus-info-marks info))
5705          (active (gnus-active group)))
5706     (and info active
5707          (gnus-set-difference
5708           (gnus-sorted-complement
5709            (gnus-uncompress-range active)
5710            (gnus-list-of-unread-articles group))
5711           (append
5712            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5713            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5714
5715 ;; Various summary commands
5716
5717 (defun gnus-summary-select-article-buffer ()
5718   "Reconfigure windows to show article buffer."
5719   (interactive)
5720   (if (not (gnus-buffer-live-p gnus-article-buffer))
5721       (error "There is no article buffer for this summary buffer")
5722     (gnus-configure-windows 'article)
5723     (select-window (get-buffer-window gnus-article-buffer))))
5724
5725 (defun gnus-summary-universal-argument (arg)
5726   "Perform any operation on all articles that are process/prefixed."
5727   (interactive "P")
5728   (let ((articles (gnus-summary-work-articles arg))
5729         func article)
5730     (if (eq
5731          (setq
5732           func
5733           (key-binding
5734            (read-key-sequence
5735             (substitute-command-keys
5736              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5737          'undefined)
5738         (gnus-error 1 "Undefined key")
5739       (save-excursion
5740         (while articles
5741           (gnus-summary-goto-subject (setq article (pop articles)))
5742           (let (gnus-newsgroup-processable)
5743             (command-execute func))
5744           (gnus-summary-remove-process-mark article)))))
5745   (gnus-summary-position-point))
5746
5747 (defun gnus-summary-toggle-truncation (&optional arg)
5748   "Toggle truncation of summary lines.
5749 With arg, turn line truncation on iff arg is positive."
5750   (interactive "P")
5751   (setq truncate-lines
5752         (if (null arg) (not truncate-lines)
5753           (> (prefix-numeric-value arg) 0)))
5754   (redraw-display))
5755
5756 (defun gnus-summary-reselect-current-group (&optional all rescan)
5757   "Exit and then reselect the current newsgroup.
5758 The prefix argument ALL means to select all articles."
5759   (interactive "P")
5760   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5761     (error "Ephemeral groups can't be reselected"))
5762   (let ((current-subject (gnus-summary-article-number))
5763         (group gnus-newsgroup-name))
5764     (setq gnus-newsgroup-begin nil)
5765     (gnus-summary-exit)
5766     ;; We have to adjust the point of group mode buffer because
5767     ;; point was moved to the next unread newsgroup by exiting.
5768     (gnus-summary-jump-to-group group)
5769     (when rescan
5770       (save-excursion
5771         (gnus-group-get-new-news-this-group 1)))
5772     (gnus-group-read-group all t)
5773     (gnus-summary-goto-subject current-subject nil t)))
5774
5775 (defun gnus-summary-rescan-group (&optional all)
5776   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5777   (interactive "P")
5778   (gnus-summary-reselect-current-group all t))
5779
5780 (defun gnus-summary-update-info (&optional non-destructive)
5781   (save-excursion
5782     (let ((group gnus-newsgroup-name))
5783       (when group
5784         (when gnus-newsgroup-kill-headers
5785           (setq gnus-newsgroup-killed
5786                 (gnus-compress-sequence
5787                  (nconc
5788                   (gnus-set-sorted-intersection
5789                    (gnus-uncompress-range gnus-newsgroup-killed)
5790                    (setq gnus-newsgroup-unselected
5791                          (sort gnus-newsgroup-unselected '<)))
5792                   (setq gnus-newsgroup-unreads
5793                         (sort gnus-newsgroup-unreads '<)))
5794                  t)))
5795         (unless (listp (cdr gnus-newsgroup-killed))
5796           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5797         (let ((headers gnus-newsgroup-headers))
5798           ;; Set the new ranges of read articles.
5799           (save-excursion
5800             (set-buffer gnus-group-buffer)
5801             (gnus-undo-force-boundary))
5802           (gnus-update-read-articles
5803            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5804           ;; Set the current article marks.
5805           (let ((gnus-newsgroup-scored
5806                  (if (and (not gnus-save-score)
5807                           (not non-destructive))
5808                      nil
5809                    gnus-newsgroup-scored)))
5810             (save-excursion
5811               (gnus-update-marks)))
5812           ;; Do the cross-ref thing.
5813           (when gnus-use-cross-reference
5814             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5815           ;; Do not switch windows but change the buffer to work.
5816           (set-buffer gnus-group-buffer)
5817           (unless (gnus-ephemeral-group-p group)
5818             (gnus-group-update-group group)))))))
5819
5820 (defun gnus-summary-save-newsrc (&optional force)
5821   "Save the current number of read/marked articles in the dribble buffer.
5822 The dribble buffer will then be saved.
5823 If FORCE (the prefix), also save the .newsrc file(s)."
5824   (interactive "P")
5825   (gnus-summary-update-info t)
5826   (if force
5827       (gnus-save-newsrc-file)
5828     (gnus-dribble-save)))
5829
5830 (defun gnus-summary-exit (&optional temporary)
5831   "Exit reading current newsgroup, and then return to group selection mode.
5832 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5833   (interactive)
5834   (gnus-set-global-variables)
5835   (when (gnus-buffer-live-p gnus-article-buffer)
5836     (save-excursion
5837       (set-buffer gnus-article-buffer)
5838       (mm-destroy-parts gnus-article-mime-handles)
5839       ;; Set it to nil for safety reason.
5840       (setq gnus-article-mime-handle-alist nil)
5841       (setq gnus-article-mime-handles nil)))
5842   (gnus-kill-save-kill-buffer)
5843   (gnus-async-halt-prefetch)
5844   (let* ((group gnus-newsgroup-name)
5845          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5846          (mode major-mode)
5847          (group-point nil)
5848          (buf (current-buffer)))
5849     (unless quit-config
5850       ;; Do adaptive scoring, and possibly save score files.
5851       (when gnus-newsgroup-adaptive
5852         (gnus-score-adaptive))
5853       (when gnus-use-scoring
5854         (gnus-score-save)))
5855     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5856     ;; If we have several article buffers, we kill them at exit.
5857     (unless gnus-single-article-buffer
5858       (gnus-kill-buffer gnus-original-article-buffer)
5859       (setq gnus-article-current nil))
5860     (when gnus-use-cache
5861       (gnus-cache-possibly-remove-articles)
5862       (gnus-cache-save-buffers))
5863     (gnus-async-prefetch-remove-group group)
5864     (when gnus-suppress-duplicates
5865       (gnus-dup-enter-articles))
5866     (when gnus-use-trees
5867       (gnus-tree-close group))
5868     (when gnus-use-cache
5869       (gnus-cache-write-active))
5870     ;; Remove entries for this group.
5871     (nnmail-purge-split-history (gnus-group-real-name group))
5872     ;; Make all changes in this group permanent.
5873     (unless quit-config
5874       (gnus-run-hooks 'gnus-exit-group-hook)
5875       (gnus-summary-update-info))
5876     (gnus-close-group group)
5877     ;; Make sure where we were, and go to next newsgroup.
5878     (set-buffer gnus-group-buffer)
5879     (unless quit-config
5880       (gnus-group-jump-to-group group))
5881     (gnus-run-hooks 'gnus-summary-exit-hook)
5882     (unless (or quit-config
5883                 ;; If this group has disappeared from the summary
5884                 ;; buffer, don't skip forwards.
5885                 (not (string= group (gnus-group-group-name))))
5886       (gnus-group-next-unread-group 1))
5887     (setq group-point (point))
5888     (if temporary
5889         nil                             ;Nothing to do.
5890       ;; If we have several article buffers, we kill them at exit.
5891       (unless gnus-single-article-buffer
5892         (gnus-kill-buffer gnus-article-buffer)
5893         (gnus-kill-buffer gnus-original-article-buffer)
5894         (setq gnus-article-current nil))
5895       (set-buffer buf)
5896       (if (not gnus-kill-summary-on-exit)
5897           (progn
5898             (gnus-deaden-summary)
5899             (setq mode nil))
5900        ;; We set all buffer-local variables to nil.  It is unclear why
5901         ;; this is needed, but if we don't, buffer-local variables are
5902         ;; not garbage-collected, it seems.  This would the lead to en
5903         ;; ever-growing Emacs.
5904         (gnus-summary-clear-local-variables)
5905         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5906           (gnus-summary-clear-local-variables))
5907         (when (get-buffer gnus-article-buffer)
5908           (bury-buffer gnus-article-buffer))
5909         ;; We clear the global counterparts of the buffer-local
5910         ;; variables as well, just to be on the safe side.
5911         (set-buffer gnus-group-buffer)
5912         (gnus-summary-clear-local-variables)
5913         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5914           (gnus-summary-clear-local-variables)))
5915       (setq gnus-current-select-method gnus-select-method)
5916       (pop-to-buffer gnus-group-buffer)
5917       (if (not quit-config)
5918           (progn
5919             (goto-char group-point)
5920             (gnus-configure-windows 'group 'force))
5921         (gnus-handle-ephemeral-exit quit-config))
5922       ;; Return to group mode buffer.
5923       (when (eq mode 'gnus-summary-mode)
5924         (gnus-kill-buffer buf))
5925       ;; Clear the current group name.
5926       (unless quit-config
5927         (setq gnus-newsgroup-name nil)))))
5928
5929 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5930 (defun gnus-summary-exit-no-update (&optional no-questions)
5931   "Quit reading current newsgroup without updating read article info."
5932   (interactive)
5933   (let* ((group gnus-newsgroup-name)
5934          (quit-config (gnus-group-quit-config group)))
5935     (when (or no-questions
5936               gnus-expert-user
5937               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5938       (gnus-async-halt-prefetch)
5939       (mapcar 'funcall
5940               (delq 'gnus-summary-expire-articles
5941                     (copy-sequence gnus-summary-prepare-exit-hook)))
5942       (when (gnus-buffer-live-p gnus-article-buffer)
5943         (save-excursion
5944           (set-buffer gnus-article-buffer)
5945           (mm-destroy-parts gnus-article-mime-handles)
5946           ;; Set it to nil for safety reason.
5947           (setq gnus-article-mime-handle-alist nil)
5948           (setq gnus-article-mime-handles nil)))
5949       ;; If we have several article buffers, we kill them at exit.
5950       (unless gnus-single-article-buffer
5951         (gnus-kill-buffer gnus-article-buffer)
5952         (gnus-kill-buffer gnus-original-article-buffer)
5953         (setq gnus-article-current nil))
5954       (if (not gnus-kill-summary-on-exit)
5955           (gnus-deaden-summary)
5956         (gnus-close-group group)
5957         (gnus-summary-clear-local-variables)
5958         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5959           (gnus-summary-clear-local-variables))
5960         (set-buffer gnus-group-buffer)
5961         (gnus-summary-clear-local-variables)
5962         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5963           (gnus-summary-clear-local-variables))
5964         (when (get-buffer gnus-summary-buffer)
5965           (kill-buffer gnus-summary-buffer)))
5966       (unless gnus-single-article-buffer
5967         (setq gnus-article-current nil))
5968       (when gnus-use-trees
5969         (gnus-tree-close group))
5970       (gnus-async-prefetch-remove-group group)
5971       (when (get-buffer gnus-article-buffer)
5972         (bury-buffer gnus-article-buffer))
5973       ;; Return to the group buffer.
5974       (gnus-configure-windows 'group 'force)
5975       ;; Clear the current group name.
5976       (setq gnus-newsgroup-name nil)
5977       (when (equal (gnus-group-group-name) group)
5978         (gnus-group-next-unread-group 1))
5979       (when quit-config
5980         (gnus-handle-ephemeral-exit quit-config)))))
5981
5982 (defun gnus-handle-ephemeral-exit (quit-config)
5983   "Handle movement when leaving an ephemeral group.
5984 The state which existed when entering the ephemeral is reset."
5985   (if (not (buffer-name (car quit-config)))
5986       (gnus-configure-windows 'group 'force)
5987     (set-buffer (car quit-config))
5988     (cond ((eq major-mode 'gnus-summary-mode)
5989            (gnus-set-global-variables))
5990           ((eq major-mode 'gnus-article-mode)
5991            (save-excursion
5992              ;; The `gnus-summary-buffer' variable may point
5993              ;; to the old summary buffer when using a single
5994              ;; article buffer.
5995              (unless (gnus-buffer-live-p gnus-summary-buffer)
5996                (set-buffer gnus-group-buffer))
5997              (set-buffer gnus-summary-buffer)
5998              (gnus-set-global-variables))))
5999     (if (or (eq (cdr quit-config) 'article)
6000             (eq (cdr quit-config) 'pick))
6001         (progn
6002           ;; The current article may be from the ephemeral group
6003           ;; thus it is best that we reload this article
6004           (gnus-summary-show-article)
6005           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6006               (gnus-configure-windows 'pick 'force)
6007             (gnus-configure-windows (cdr quit-config) 'force)))
6008       (gnus-configure-windows (cdr quit-config) 'force))
6009     (when (eq major-mode 'gnus-summary-mode)
6010       (gnus-summary-next-subject 1 nil t)
6011       (gnus-summary-recenter)
6012       (gnus-summary-position-point))))
6013
6014 ;;; Dead summaries.
6015
6016 (defvar gnus-dead-summary-mode-map nil)
6017
6018 (unless gnus-dead-summary-mode-map
6019   (setq gnus-dead-summary-mode-map (make-keymap))
6020   (suppress-keymap gnus-dead-summary-mode-map)
6021   (substitute-key-definition
6022    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6023   (let ((keys '("\C-d" "\r" "\177" [delete])))
6024     (while keys
6025       (define-key gnus-dead-summary-mode-map
6026         (pop keys) 'gnus-summary-wake-up-the-dead))))
6027
6028 (defvar gnus-dead-summary-mode nil
6029   "Minor mode for Gnus summary buffers.")
6030
6031 (defun gnus-dead-summary-mode (&optional arg)
6032   "Minor mode for Gnus summary buffers."
6033   (interactive "P")
6034   (when (eq major-mode 'gnus-summary-mode)
6035     (make-local-variable 'gnus-dead-summary-mode)
6036     (setq gnus-dead-summary-mode
6037           (if (null arg) (not gnus-dead-summary-mode)
6038             (> (prefix-numeric-value arg) 0)))
6039     (when gnus-dead-summary-mode
6040       (gnus-add-minor-mode
6041        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6042
6043 (defun gnus-deaden-summary ()
6044   "Make the current summary buffer into a dead summary buffer."
6045   ;; Kill any previous dead summary buffer.
6046   (when (and gnus-dead-summary
6047              (buffer-name gnus-dead-summary))
6048     (save-excursion
6049       (set-buffer gnus-dead-summary)
6050       (when gnus-dead-summary-mode
6051         (kill-buffer (current-buffer)))))
6052   ;; Make this the current dead summary.
6053   (setq gnus-dead-summary (current-buffer))
6054   (gnus-dead-summary-mode 1)
6055   (let ((name (buffer-name)))
6056     (when (string-match "Summary" name)
6057       (rename-buffer
6058        (concat (substring name 0 (match-beginning 0)) "Dead "
6059                (substring name (match-beginning 0)))
6060        t)
6061       (bury-buffer))))
6062
6063 (defun gnus-kill-or-deaden-summary (buffer)
6064   "Kill or deaden the summary BUFFER."
6065   (save-excursion
6066     (when (and (buffer-name buffer)
6067                (not gnus-single-article-buffer))
6068       (save-excursion
6069         (set-buffer buffer)
6070         (gnus-kill-buffer gnus-article-buffer)
6071         (gnus-kill-buffer gnus-original-article-buffer)))
6072     (cond (gnus-kill-summary-on-exit
6073            (when (and gnus-use-trees
6074                       (gnus-buffer-exists-p buffer))
6075              (save-excursion
6076                (set-buffer buffer)
6077                (gnus-tree-close gnus-newsgroup-name)))
6078            (gnus-kill-buffer buffer))
6079           ((gnus-buffer-exists-p buffer)
6080            (save-excursion
6081              (set-buffer buffer)
6082              (gnus-deaden-summary))))))
6083
6084 (defun gnus-summary-wake-up-the-dead (&rest args)
6085   "Wake up the dead summary buffer."
6086   (interactive)
6087   (gnus-dead-summary-mode -1)
6088   (let ((name (buffer-name)))
6089     (when (string-match "Dead " name)
6090       (rename-buffer
6091        (concat (substring name 0 (match-beginning 0))
6092                (substring name (match-end 0)))
6093        t)))
6094   (gnus-message 3 "This dead summary is now alive again"))
6095
6096 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6097 (defun gnus-summary-fetch-faq (&optional faq-dir)
6098   "Fetch the FAQ for the current group.
6099 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6100 in."
6101   (interactive
6102    (list
6103     (when current-prefix-arg
6104       (completing-read
6105        "Faq dir: " (and (listp gnus-group-faq-directory)
6106                         (mapcar (lambda (file) (list file))
6107                                 gnus-group-faq-directory))))))
6108   (let (gnus-faq-buffer)
6109     (when (setq gnus-faq-buffer
6110                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6111       (gnus-configure-windows 'summary-faq))))
6112
6113 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6114 (defun gnus-summary-describe-group (&optional force)
6115   "Describe the current newsgroup."
6116   (interactive "P")
6117   (gnus-group-describe-group force gnus-newsgroup-name))
6118
6119 (defun gnus-summary-describe-briefly ()
6120   "Describe summary mode commands briefly."
6121   (interactive)
6122   (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")))
6123
6124 ;; Walking around group mode buffer from summary mode.
6125
6126 (defun gnus-summary-next-group (&optional no-article target-group backward)
6127   "Exit current newsgroup and then select next unread newsgroup.
6128 If prefix argument NO-ARTICLE is non-nil, no article is selected
6129 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6130 previous group instead."
6131   (interactive "P")
6132   ;; Stop pre-fetching.
6133   (gnus-async-halt-prefetch)
6134   (let ((current-group gnus-newsgroup-name)
6135         (current-buffer (current-buffer))
6136         entered)
6137    ;; First we semi-exit this group to update Xrefs and all variables.
6138     ;; We can't do a real exit, because the window conf must remain
6139     ;; the same in case the user is prompted for info, and we don't
6140     ;; want the window conf to change before that...
6141     (gnus-summary-exit t)
6142     (while (not entered)
6143       ;; Then we find what group we are supposed to enter.
6144       (set-buffer gnus-group-buffer)
6145       (gnus-group-jump-to-group current-group)
6146       (setq target-group
6147             (or target-group
6148                 (if (eq gnus-keep-same-level 'best)
6149                     (gnus-summary-best-group gnus-newsgroup-name)
6150                   (gnus-summary-search-group backward gnus-keep-same-level))))
6151       (if (not target-group)
6152           ;; There are no further groups, so we return to the group
6153           ;; buffer.
6154           (progn
6155             (gnus-message 5 "Returning to the group buffer")
6156             (setq entered t)
6157             (when (gnus-buffer-live-p current-buffer)
6158               (set-buffer current-buffer)
6159               (gnus-summary-exit))
6160             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6161         ;; We try to enter the target group.
6162         (gnus-group-jump-to-group target-group)
6163         (let ((unreads (gnus-group-group-unread)))
6164           (if (and (or (eq t unreads)
6165                        (and unreads (not (zerop unreads))))
6166                    (gnus-summary-read-group
6167                     target-group nil no-article
6168                     (and (buffer-name current-buffer) current-buffer)
6169                     nil backward))
6170               (setq entered t)
6171             (setq current-group target-group
6172                   target-group nil)))))))
6173
6174 (defun gnus-summary-prev-group (&optional no-article)
6175   "Exit current newsgroup and then select previous unread newsgroup.
6176 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6177   (interactive "P")
6178   (gnus-summary-next-group no-article nil t))
6179
6180 ;; Walking around summary lines.
6181
6182 (defun gnus-summary-first-subject (&optional unread undownloaded)
6183   "Go to the first unread subject.
6184 If UNREAD is non-nil, go to the first unread article.
6185 Returns the article selected or nil if there are no unread articles."
6186   (interactive "P")
6187   (prog1
6188       (cond
6189        ;; Empty summary.
6190        ((null gnus-newsgroup-data)
6191         (gnus-message 3 "No articles in the group")
6192         nil)
6193        ;; Pick the first article.
6194        ((not unread)
6195         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6196         (gnus-data-number (car gnus-newsgroup-data)))
6197        ;; No unread articles.
6198        ((null gnus-newsgroup-unreads)
6199         (gnus-message 3 "No more unread articles")
6200         nil)
6201        ;; Find the first unread article.
6202        (t
6203         (let ((data gnus-newsgroup-data))
6204           (while (and data
6205                       (and (not (and undownloaded
6206                                      (eq gnus-undownloaded-mark
6207                                          (gnus-data-mark (car data)))))
6208                            (not (gnus-data-unread-p (car data)))))
6209             (setq data (cdr data)))
6210           (when data
6211             (goto-char (gnus-data-pos (car data)))
6212             (gnus-data-number (car data))))))
6213     (gnus-summary-position-point)))
6214
6215 (defun gnus-summary-next-subject (n &optional unread dont-display)
6216   "Go to next N'th summary line.
6217 If N is negative, go to the previous N'th subject line.
6218 If UNREAD is non-nil, only unread articles are selected.
6219 The difference between N and the actual number of steps taken is
6220 returned."
6221   (interactive "p")
6222   (let ((backward (< n 0))
6223         (n (abs n)))
6224     (while (and (> n 0)
6225                 (if backward
6226                     (gnus-summary-find-prev unread)
6227                   (gnus-summary-find-next unread)))
6228       (unless (zerop (setq n (1- n)))
6229         (gnus-summary-show-thread)))
6230     (when (/= 0 n)
6231       (gnus-message 7 "No more%s articles"
6232                     (if unread " unread" "")))
6233     (unless dont-display
6234       (gnus-summary-recenter)
6235       (gnus-summary-position-point))
6236     n))
6237
6238 (defun gnus-summary-next-unread-subject (n)
6239   "Go to next N'th unread summary line."
6240   (interactive "p")
6241   (gnus-summary-next-subject n t))
6242
6243 (defun gnus-summary-prev-subject (n &optional unread)
6244   "Go to previous N'th summary line.
6245 If optional argument UNREAD is non-nil, only unread article is selected."
6246   (interactive "p")
6247   (gnus-summary-next-subject (- n) unread))
6248
6249 (defun gnus-summary-prev-unread-subject (n)
6250   "Go to previous N'th unread summary line."
6251   (interactive "p")
6252   (gnus-summary-next-subject (- n) t))
6253
6254 (defun gnus-summary-goto-subject (article &optional force silent)
6255   "Go the subject line of ARTICLE.
6256 If FORCE, also allow jumping to articles not currently shown."
6257   (interactive "nArticle number: ")
6258   (let ((b (point))
6259         (data (gnus-data-find article)))
6260     ;; We read in the article if we have to.
6261     (and (not data)
6262          force
6263          (gnus-summary-insert-subject
6264           article
6265           (if (or (numberp force) (vectorp force)) force)
6266           t)
6267          (setq data (gnus-data-find article)))
6268     (goto-char b)
6269     (if (not data)
6270         (progn
6271           (unless silent
6272             (gnus-message 3 "Can't find article %d" article))
6273           nil)
6274       (let ((pt (gnus-data-pos data)))
6275         (goto-char pt)
6276         (gnus-summary-set-article-display-arrow pt))
6277       (gnus-summary-position-point)
6278       article)))
6279
6280 ;; Walking around summary lines with displaying articles.
6281
6282 (defun gnus-summary-expand-window (&optional arg)
6283   "Make the summary buffer take up the entire Emacs frame.
6284 Given a prefix, will force an `article' buffer configuration."
6285   (interactive "P")
6286   (if arg
6287       (gnus-configure-windows 'article 'force)
6288     (gnus-configure-windows 'summary 'force)))
6289
6290 (defun gnus-summary-display-article (article &optional all-header)
6291   "Display ARTICLE in article buffer."
6292   (when (gnus-buffer-live-p gnus-article-buffer)
6293     (with-current-buffer gnus-article-buffer
6294       (mm-enable-multibyte-mule4)))
6295   (gnus-set-global-variables)
6296   (when (gnus-buffer-live-p gnus-article-buffer)
6297     (with-current-buffer gnus-article-buffer
6298       (setq gnus-article-charset gnus-newsgroup-charset)
6299       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6300       (mm-enable-multibyte-mule4)))
6301   (if (null article)
6302       nil
6303     (prog1
6304         (if gnus-summary-display-article-function
6305             (funcall gnus-summary-display-article-function article all-header)
6306           (gnus-article-prepare article all-header))
6307       (gnus-run-hooks 'gnus-select-article-hook)
6308       (when (and gnus-current-article
6309                  (not (zerop gnus-current-article)))
6310         (gnus-summary-goto-subject gnus-current-article))
6311       (gnus-summary-recenter)
6312       (when (and gnus-use-trees gnus-show-threads)
6313         (gnus-possibly-generate-tree article)
6314         (gnus-highlight-selected-tree article))
6315       ;; Successfully display article.
6316       (gnus-article-set-window-start
6317        (cdr (assq article gnus-newsgroup-bookmarks))))))
6318
6319 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6320   "Select the current article.
6321 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6322 non-nil, the article will be re-fetched even if it already present in
6323 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6324 be displayed."
6325   ;; Make sure we are in the summary buffer to work around bbdb bug.
6326   (unless (eq major-mode 'gnus-summary-mode)
6327     (set-buffer gnus-summary-buffer))
6328   (let ((article (or article (gnus-summary-article-number)))
6329         (all-headers (not (not all-headers))) ;Must be T or NIL.
6330         gnus-summary-display-article-function)
6331     (and (not pseudo)
6332          (gnus-summary-article-pseudo-p article)
6333          (error "This is a pseudo-article"))
6334     (save-excursion
6335       (set-buffer gnus-summary-buffer)
6336       (if (or (and gnus-single-article-buffer
6337                    (or (null gnus-current-article)
6338                        (null gnus-article-current)
6339                        (null (get-buffer gnus-article-buffer))
6340                        (not (eq article (cdr gnus-article-current)))
6341                        (not (equal (car gnus-article-current)
6342                                    gnus-newsgroup-name))))
6343               (and (not gnus-single-article-buffer)
6344                    (or (null gnus-current-article)
6345                        (not (eq gnus-current-article article))))
6346               force)
6347        ;; The requested article is different from the current article.
6348           (progn
6349             (gnus-summary-display-article article all-headers)
6350             (when (gnus-buffer-live-p gnus-article-buffer)
6351               (with-current-buffer gnus-article-buffer
6352                 (if (not gnus-article-decoded-p) ;; a local variable
6353                     (mm-disable-multibyte-mule4))))
6354             (when (or all-headers gnus-show-all-headers)
6355               (gnus-article-show-all-headers))
6356             (gnus-article-set-window-start
6357              (cdr (assq article gnus-newsgroup-bookmarks)))
6358             article)
6359         (when (or all-headers gnus-show-all-headers)
6360           (gnus-article-show-all-headers))
6361         'old))))
6362
6363 (defun gnus-summary-force-verify-and-decrypt ()
6364   (interactive)
6365   (let ((mm-verify-option 'known)
6366         (mm-decrypt-option 'known))
6367     (gnus-summary-select-article nil 'force)))
6368
6369 (defun gnus-summary-set-current-mark (&optional current-mark)
6370   "Obsolete function."
6371   nil)
6372
6373 (defun gnus-summary-next-article (&optional unread subject backward push)
6374   "Select the next article.
6375 If UNREAD, only unread articles are selected.
6376 If SUBJECT, only articles with SUBJECT are selected.
6377 If BACKWARD, the previous article is selected instead of the next."
6378   (interactive "P")
6379   (cond
6380    ;; Is there such an article?
6381    ((and (gnus-summary-search-forward unread subject backward)
6382          (or (gnus-summary-display-article (gnus-summary-article-number))
6383              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6384     (gnus-summary-position-point))
6385    ;; If not, we try the first unread, if that is wanted.
6386    ((and subject
6387          gnus-auto-select-same
6388          (gnus-summary-first-unread-article))
6389     (gnus-summary-position-point)
6390     (gnus-message 6 "Wrapped"))
6391    ;; Try to get next/previous article not displayed in this group.
6392    ((and gnus-auto-extend-newsgroup
6393          (not unread) (not subject))
6394     (gnus-summary-goto-article
6395      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6396      nil (count-lines (point-min) (point))))
6397    ;; Go to next/previous group.
6398    (t
6399     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6400       (gnus-summary-jump-to-group gnus-newsgroup-name))
6401     (let ((cmd last-command-char)
6402           (point
6403            (save-excursion
6404              (set-buffer gnus-group-buffer)
6405              (point)))
6406           (group
6407            (if (eq gnus-keep-same-level 'best)
6408                (gnus-summary-best-group gnus-newsgroup-name)
6409              (gnus-summary-search-group backward gnus-keep-same-level))))
6410       ;; For some reason, the group window gets selected.  We change
6411       ;; it back.
6412       (select-window (get-buffer-window (current-buffer)))
6413       ;; Select next unread newsgroup automagically.
6414       (cond
6415        ((or (not gnus-auto-select-next)
6416             (not cmd))
6417         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6418        ((or (eq gnus-auto-select-next 'quietly)
6419             (and (eq gnus-auto-select-next 'slightly-quietly)
6420                  push)
6421             (and (eq gnus-auto-select-next 'almost-quietly)
6422                  (gnus-summary-last-article-p)))
6423         ;; Select quietly.
6424         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6425             (gnus-summary-exit)
6426           (gnus-message 7 "No more%s articles (%s)..."
6427                         (if unread " unread" "")
6428                         (if group (concat "selecting " group)
6429                           "exiting"))
6430           (gnus-summary-next-group nil group backward)))
6431        (t
6432         (when (gnus-key-press-event-p last-input-event)
6433           (gnus-summary-walk-group-buffer
6434            gnus-newsgroup-name cmd unread backward point))))))))
6435
6436 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6437   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6438                       (?\C-p (gnus-group-prev-unread-group 1))))
6439         (cursor-in-echo-area t)
6440         keve key group ended)
6441     (save-excursion
6442       (set-buffer gnus-group-buffer)
6443       (goto-char start)
6444       (setq group
6445             (if (eq gnus-keep-same-level 'best)
6446                 (gnus-summary-best-group gnus-newsgroup-name)
6447               (gnus-summary-search-group backward gnus-keep-same-level))))
6448     (while (not ended)
6449       (gnus-message
6450        5 "No more%s articles%s" (if unread " unread" "")
6451        (if (and group
6452                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6453            (format " (Type %s for %s [%s])"
6454                    (single-key-description cmd) group
6455                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6456          (format " (Type %s to exit %s)"
6457                  (single-key-description cmd)
6458                  gnus-newsgroup-name)))
6459       ;; Confirm auto selection.
6460       (setq key (car (setq keve (gnus-read-event-char))))
6461       (setq ended t)
6462       (cond
6463        ((assq key keystrokes)
6464         (let ((obuf (current-buffer)))
6465           (switch-to-buffer gnus-group-buffer)
6466           (when group
6467             (gnus-group-jump-to-group group))
6468           (eval (cadr (assq key keystrokes)))
6469           (setq group (gnus-group-group-name))
6470           (switch-to-buffer obuf))
6471         (setq ended nil))
6472        ((equal key cmd)
6473         (if (or (not group)
6474                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6475             (gnus-summary-exit)
6476           (gnus-summary-next-group nil group backward)))
6477        (t
6478         (push (cdr keve) unread-command-events))))))
6479
6480 (defun gnus-summary-next-unread-article ()
6481   "Select unread article after current one."
6482   (interactive)
6483   (gnus-summary-next-article
6484    (or (not (eq gnus-summary-goto-unread 'never))
6485        (gnus-summary-last-article-p (gnus-summary-article-number)))
6486    (and gnus-auto-select-same
6487         (gnus-summary-article-subject))))
6488
6489 (defun gnus-summary-prev-article (&optional unread subject)
6490   "Select the article after the current one.
6491 If UNREAD is non-nil, only unread articles are selected."
6492   (interactive "P")
6493   (gnus-summary-next-article unread subject t))
6494
6495 (defun gnus-summary-prev-unread-article ()
6496   "Select unread article before current one."
6497   (interactive)
6498   (gnus-summary-prev-article
6499    (or (not (eq gnus-summary-goto-unread 'never))
6500        (gnus-summary-first-article-p (gnus-summary-article-number)))
6501    (and gnus-auto-select-same
6502         (gnus-summary-article-subject))))
6503
6504 (defun gnus-summary-next-page (&optional lines circular)
6505   "Show next page of the selected article.
6506 If at the end of the current article, select the next article.
6507 LINES says how many lines should be scrolled up.
6508
6509 If CIRCULAR is non-nil, go to the start of the article instead of
6510 selecting the next article when reaching the end of the current
6511 article."
6512   (interactive "P")
6513   (setq gnus-summary-buffer (current-buffer))
6514   (gnus-set-global-variables)
6515   (let ((article (gnus-summary-article-number))
6516         (article-window (get-buffer-window gnus-article-buffer t))
6517         endp)
6518     ;; If the buffer is empty, we have no article.
6519     (unless article
6520       (error "No article to select"))
6521     (gnus-configure-windows 'article)
6522     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6523         (if (and (eq gnus-summary-goto-unread 'never)
6524                  (not (gnus-summary-last-article-p article)))
6525             (gnus-summary-next-article)
6526           (gnus-summary-next-unread-article))
6527       (if (or (null gnus-current-article)
6528               (null gnus-article-current)
6529               (/= article (cdr gnus-article-current))
6530               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6531           ;; Selected subject is different from current article's.
6532           (gnus-summary-display-article article)
6533         (when article-window
6534           (gnus-eval-in-buffer-window gnus-article-buffer
6535             (setq endp (gnus-article-next-page lines)))
6536           (when endp
6537             (cond (circular
6538                    (gnus-summary-beginning-of-article))
6539                   (lines
6540                    (gnus-message 3 "End of message"))
6541                   ((null lines)
6542                    (if (and (eq gnus-summary-goto-unread 'never)
6543                             (not (gnus-summary-last-article-p article)))
6544                        (gnus-summary-next-article)
6545                      (gnus-summary-next-unread-article))))))))
6546     (gnus-summary-recenter)
6547     (gnus-summary-position-point)))
6548
6549 (defun gnus-summary-prev-page (&optional lines move)
6550   "Show previous page of selected article.
6551 Argument LINES specifies lines to be scrolled down.
6552 If MOVE, move to the previous unread article if point is at
6553 the beginning of the buffer."
6554   (interactive "P")
6555   (let ((article (gnus-summary-article-number))
6556         (article-window (get-buffer-window gnus-article-buffer t))
6557         endp)
6558     (gnus-configure-windows 'article)
6559     (if (or (null gnus-current-article)
6560             (null gnus-article-current)
6561             (/= article (cdr gnus-article-current))
6562             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6563         ;; Selected subject is different from current article's.
6564         (gnus-summary-display-article article)
6565       (gnus-summary-recenter)
6566       (when article-window
6567         (gnus-eval-in-buffer-window gnus-article-buffer
6568           (setq endp (gnus-article-prev-page lines)))
6569         (when (and move endp)
6570           (cond (lines
6571                  (gnus-message 3 "Beginning of message"))
6572                 ((null lines)
6573                  (if (and (eq gnus-summary-goto-unread 'never)
6574                           (not (gnus-summary-first-article-p article)))
6575                      (gnus-summary-prev-article)
6576                    (gnus-summary-prev-unread-article))))))))
6577   (gnus-summary-position-point))
6578
6579 (defun gnus-summary-prev-page-or-article (&optional lines)
6580   "Show previous page of selected article.
6581 Argument LINES specifies lines to be scrolled down.
6582 If at the beginning of the article, go to the next article."
6583   (interactive "P")
6584   (gnus-summary-prev-page lines t))
6585
6586 (defun gnus-summary-scroll-up (lines)
6587   "Scroll up (or down) one line current article.
6588 Argument LINES specifies lines to be scrolled up (or down if negative)."
6589   (interactive "p")
6590   (gnus-configure-windows 'article)
6591   (gnus-summary-show-thread)
6592   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6593     (gnus-eval-in-buffer-window gnus-article-buffer
6594       (cond ((> lines 0)
6595              (when (gnus-article-next-page lines)
6596                (gnus-message 3 "End of message")))
6597             ((< lines 0)
6598              (gnus-article-prev-page (- lines))))))
6599   (gnus-summary-recenter)
6600   (gnus-summary-position-point))
6601
6602 (defun gnus-summary-scroll-down (lines)
6603   "Scroll down (or up) one line current article.
6604 Argument LINES specifies lines to be scrolled down (or up if negative)."
6605   (interactive "p")
6606   (gnus-summary-scroll-up (- lines)))
6607
6608 (defun gnus-summary-next-same-subject ()
6609   "Select next article which has the same subject as current one."
6610   (interactive)
6611   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6612
6613 (defun gnus-summary-prev-same-subject ()
6614   "Select previous article which has the same subject as current one."
6615   (interactive)
6616   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6617
6618 (defun gnus-summary-next-unread-same-subject ()
6619   "Select next unread article which has the same subject as current one."
6620   (interactive)
6621   (gnus-summary-next-article t (gnus-summary-article-subject)))
6622
6623 (defun gnus-summary-prev-unread-same-subject ()
6624   "Select previous unread article which has the same subject as current one."
6625   (interactive)
6626   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6627
6628 (defun gnus-summary-first-unread-article ()
6629   "Select the first unread article.
6630 Return nil if there are no unread articles."
6631   (interactive)
6632   (prog1
6633       (when (gnus-summary-first-subject t)
6634         (gnus-summary-show-thread)
6635         (gnus-summary-first-subject t)
6636         (gnus-summary-display-article (gnus-summary-article-number)))
6637     (gnus-summary-position-point)))
6638
6639 (defun gnus-summary-first-unread-subject ()
6640   "Place the point on the subject line of the first unread article.
6641 Return nil if there are no unread articles."
6642   (interactive)
6643   (prog1
6644       (when (gnus-summary-first-subject t)
6645         (gnus-summary-show-thread)
6646         (gnus-summary-first-subject t))
6647     (gnus-summary-position-point)))
6648
6649 (defun gnus-summary-first-article ()
6650   "Select the first article.
6651 Return nil if there are no articles."
6652   (interactive)
6653   (prog1
6654       (when (gnus-summary-first-subject)
6655         (gnus-summary-show-thread)
6656         (gnus-summary-first-subject)
6657         (gnus-summary-display-article (gnus-summary-article-number)))
6658     (gnus-summary-position-point)))
6659
6660 (defun gnus-summary-best-unread-article ()
6661   "Select the unread article with the highest score."
6662   (interactive)
6663   (let ((best -1000000)
6664         (data gnus-newsgroup-data)
6665         article score)
6666     (while data
6667       (and (gnus-data-unread-p (car data))
6668            (> (setq score
6669                     (gnus-summary-article-score (gnus-data-number (car data))))
6670               best)
6671            (setq best score
6672                  article (gnus-data-number (car data))))
6673       (setq data (cdr data)))
6674     (prog1
6675         (if article
6676             (gnus-summary-goto-article article)
6677           (error "No unread articles"))
6678       (gnus-summary-position-point))))
6679
6680 (defun gnus-summary-last-subject ()
6681   "Go to the last displayed subject line in the group."
6682   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6683     (when article
6684       (gnus-summary-goto-subject article))))
6685
6686 (defun gnus-summary-goto-article (article &optional all-headers force)
6687   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6688 If ALL-HEADERS is non-nil, no header lines are hidden.
6689 If FORCE, go to the article even if it isn't displayed.  If FORCE
6690 is a number, it is the line the article is to be displayed on."
6691   (interactive
6692    (list
6693     (completing-read
6694      "Article number or Message-ID: "
6695      (mapcar (lambda (number) (list (int-to-string number)))
6696              gnus-newsgroup-limit))
6697     current-prefix-arg
6698     t))
6699   (prog1
6700       (if (and (stringp article)
6701                (string-match "@" article))
6702           (gnus-summary-refer-article article)
6703         (when (stringp article)
6704           (setq article (string-to-number article)))
6705         (if (gnus-summary-goto-subject article force)
6706             (gnus-summary-display-article article all-headers)
6707           (gnus-message 4 "Couldn't go to article %s" article) nil))
6708     (gnus-summary-position-point)))
6709
6710 (defun gnus-summary-goto-last-article ()
6711   "Go to the previously read article."
6712   (interactive)
6713   (prog1
6714       (when gnus-last-article
6715         (gnus-summary-goto-article gnus-last-article nil t))
6716     (gnus-summary-position-point)))
6717
6718 (defun gnus-summary-pop-article (number)
6719   "Pop one article off the history and go to the previous.
6720 NUMBER articles will be popped off."
6721   (interactive "p")
6722   (let (to)
6723     (setq gnus-newsgroup-history
6724           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6725     (if to
6726         (gnus-summary-goto-article (car to) nil t)
6727       (error "Article history empty")))
6728   (gnus-summary-position-point))
6729
6730 ;; Summary commands and functions for limiting the summary buffer.
6731
6732 (defun gnus-summary-limit-to-articles (n)
6733   "Limit the summary buffer to the next N articles.
6734 If not given a prefix, use the process marked articles instead."
6735   (interactive "P")
6736   (prog1
6737       (let ((articles (gnus-summary-work-articles n)))
6738         (setq gnus-newsgroup-processable nil)
6739         (gnus-summary-limit articles))
6740     (gnus-summary-position-point)))
6741
6742 (defun gnus-summary-pop-limit (&optional total)
6743   "Restore the previous limit.
6744 If given a prefix, remove all limits."
6745   (interactive "P")
6746   (when total
6747     (setq gnus-newsgroup-limits
6748           (list (mapcar (lambda (h) (mail-header-number h))
6749                         gnus-newsgroup-headers))))
6750   (unless gnus-newsgroup-limits
6751     (error "No limit to pop"))
6752   (prog1
6753       (gnus-summary-limit nil 'pop)
6754     (gnus-summary-position-point)))
6755
6756 (defun gnus-summary-limit-to-subject (subject &optional header)
6757   "Limit the summary buffer to articles that have subjects that match a regexp."
6758   (interactive "sLimit to subject (regexp): ")
6759   (unless header
6760     (setq header "subject"))
6761   (when (not (equal "" subject))
6762     (prog1
6763         (let ((articles (gnus-summary-find-matching
6764                          (or header "subject") subject 'all)))
6765           (unless articles
6766             (error "Found no matches for \"%s\"" subject))
6767           (gnus-summary-limit articles))
6768       (gnus-summary-position-point))))
6769
6770 (defun gnus-summary-limit-to-author (from)
6771   "Limit the summary buffer to articles that have authors that match a regexp."
6772   (interactive "sLimit to author (regexp): ")
6773   (gnus-summary-limit-to-subject from "from"))
6774
6775 (defun gnus-summary-limit-to-age (age &optional younger-p)
6776   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6777 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6778 articles that are younger than AGE days."
6779   (interactive
6780    (let ((younger current-prefix-arg)
6781          (days-got nil)
6782          days)
6783      (while (not days-got)
6784        (setq days (if younger
6785                       (read-string "Limit to articles within (in days): ")
6786                     (read-string "Limit to articles older than (in days): ")))
6787        (when (> (length days) 0)
6788          (setq days (read days)))
6789        (if (numberp days)
6790            (setq days-got t)
6791          (message "Please enter a number.")
6792          (sleep-for 1)))
6793      (list days younger)))
6794   (prog1
6795       (let ((data gnus-newsgroup-data)
6796             (cutoff (days-to-time age))
6797             articles d date is-younger)
6798         (while (setq d (pop data))
6799           (when (and (vectorp (gnus-data-header d))
6800                      (setq date (mail-header-date (gnus-data-header d))))
6801             (setq is-younger (time-less-p
6802                               (time-since (condition-case ()
6803                                               (date-to-time date)
6804                                             (error '(0 0))))
6805                               cutoff))
6806             (when (if younger-p
6807                       is-younger
6808                     (not is-younger))
6809               (push (gnus-data-number d) articles))))
6810         (gnus-summary-limit (nreverse articles)))
6811     (gnus-summary-position-point)))
6812
6813 (defun gnus-summary-limit-to-extra (header regexp)
6814   "Limit the summary buffer to articles that match an 'extra' header."
6815   (interactive
6816    (let ((header
6817           (intern
6818            (gnus-completing-read
6819             (symbol-name (car gnus-extra-headers))
6820             "Limit extra header:"
6821             (mapcar (lambda (x)
6822                       (cons (symbol-name x) x))
6823                     gnus-extra-headers)
6824             nil
6825             t))))
6826      (list header
6827            (read-string (format "Limit to header %s (regexp): " header)))))
6828   (when (not (equal "" regexp))
6829     (prog1
6830         (let ((articles (gnus-summary-find-matching
6831                          (cons 'extra header) regexp 'all)))
6832           (unless articles
6833             (error "Found no matches for \"%s\"" regexp))
6834           (gnus-summary-limit articles))
6835       (gnus-summary-position-point))))
6836
6837 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6838 (make-obsolete
6839  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6840
6841 (defun gnus-summary-limit-to-unread (&optional all)
6842   "Limit the summary buffer to articles that are not marked as read.
6843 If ALL is non-nil, limit strictly to unread articles."
6844   (interactive "P")
6845   (if all
6846       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6847     (gnus-summary-limit-to-marks
6848      ;; Concat all the marks that say that an article is read and have
6849      ;; those removed.
6850      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6851            gnus-killed-mark gnus-kill-file-mark
6852            gnus-low-score-mark gnus-expirable-mark
6853            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6854            gnus-duplicate-mark gnus-souped-mark)
6855      'reverse)))
6856
6857 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6858 (make-obsolete 'gnus-summary-delete-marked-with
6859                'gnus-summary-limit-exlude-marks)
6860
6861 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6862   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6863 If REVERSE, limit the summary buffer to articles that are marked
6864 with MARKS.  MARKS can either be a string of marks or a list of marks.
6865 Returns how many articles were removed."
6866   (interactive "sMarks: ")
6867   (gnus-summary-limit-to-marks marks t))
6868
6869 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6870   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6871 If REVERSE (the prefix), limit the summary buffer to articles that are
6872 not marked with MARKS.  MARKS can either be a string of marks or a
6873 list of marks.
6874 Returns how many articles were removed."
6875   (interactive "sMarks: \nP")
6876   (prog1
6877       (let ((data gnus-newsgroup-data)
6878             (marks (if (listp marks) marks
6879                      (append marks nil))) ; Transform to list.
6880             articles)
6881         (while data
6882           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6883                   (memq (gnus-data-mark (car data)) marks))
6884             (push (gnus-data-number (car data)) articles))
6885           (setq data (cdr data)))
6886         (gnus-summary-limit articles))
6887     (gnus-summary-position-point)))
6888
6889 (defun gnus-summary-limit-to-score (&optional score)
6890   "Limit to articles with score at or above SCORE."
6891   (interactive "P")
6892   (setq score (if score
6893                   (prefix-numeric-value score)
6894                 (or gnus-summary-default-score 0)))
6895   (let ((data gnus-newsgroup-data)
6896         articles)
6897     (while data
6898       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6899                 score)
6900         (push (gnus-data-number (car data)) articles))
6901       (setq data (cdr data)))
6902     (prog1
6903         (gnus-summary-limit articles)
6904       (gnus-summary-position-point))))
6905
6906 (defun gnus-summary-limit-include-thread (id)
6907   "Display all the hidden articles that is in the thread with ID in it.
6908 When called interactively, ID is the Message-ID of the current
6909 article."
6910   (interactive (list (mail-header-id (gnus-summary-article-header))))
6911   (let ((articles (gnus-articles-in-thread
6912                    (gnus-id-to-thread (gnus-root-id id)))))
6913     (prog1
6914         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6915       (gnus-summary-limit-include-matching-articles
6916        "subject"
6917        (regexp-quote (gnus-simplify-subject-re
6918                       (mail-header-subject (gnus-id-to-header id)))))
6919       (gnus-summary-position-point))))
6920
6921 (defun gnus-summary-limit-include-matching-articles (header regexp)
6922   "Display all the hidden articles that have HEADERs that match REGEXP."
6923   (interactive (list (read-string "Match on header: ")
6924                      (read-string "Regexp: ")))
6925   (let ((articles (gnus-find-matching-articles header regexp)))
6926     (prog1
6927         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6928       (gnus-summary-position-point))))
6929
6930 (defun gnus-summary-limit-include-dormant ()
6931   "Display all the hidden articles that are marked as dormant.
6932 Note that this command only works on a subset of the articles currently
6933 fetched for this group."
6934   (interactive)
6935   (unless gnus-newsgroup-dormant
6936     (error "There are no dormant articles in this group"))
6937   (prog1
6938       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6939     (gnus-summary-position-point)))
6940
6941 (defun gnus-summary-limit-exclude-dormant ()
6942   "Hide all dormant articles."
6943   (interactive)
6944   (prog1
6945       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6946     (gnus-summary-position-point)))
6947
6948 (defun gnus-summary-limit-exclude-childless-dormant ()
6949   "Hide all dormant articles that have no children."
6950   (interactive)
6951   (let ((data (gnus-data-list t))
6952         articles d children)
6953     ;; Find all articles that are either not dormant or have
6954     ;; children.
6955     (while (setq d (pop data))
6956       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6957                 (and (setq children
6958                            (gnus-article-children (gnus-data-number d)))
6959                      (let (found)
6960                        (while children
6961                          (when (memq (car children) articles)
6962                            (setq children nil
6963                                  found t))
6964                          (pop children))
6965                        found)))
6966         (push (gnus-data-number d) articles)))
6967     ;; Do the limiting.
6968     (prog1
6969         (gnus-summary-limit articles)
6970       (gnus-summary-position-point))))
6971
6972 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6973   "Mark all unread excluded articles as read.
6974 If ALL, mark even excluded ticked and dormants as read."
6975   (interactive "P")
6976   (let ((articles (gnus-sorted-complement
6977                    (sort
6978                     (mapcar (lambda (h) (mail-header-number h))
6979                             gnus-newsgroup-headers)
6980                     '<)
6981                    (sort gnus-newsgroup-limit '<)))
6982         article)
6983     (setq gnus-newsgroup-unreads
6984           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6985     (if all
6986         (setq gnus-newsgroup-dormant nil
6987               gnus-newsgroup-marked nil
6988               gnus-newsgroup-reads
6989               (nconc
6990                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6991                gnus-newsgroup-reads))
6992       (while (setq article (pop articles))
6993         (unless (or (memq article gnus-newsgroup-dormant)
6994                     (memq article gnus-newsgroup-marked))
6995           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6996
6997 (defun gnus-summary-limit (articles &optional pop)
6998   (if pop
6999       ;; We pop the previous limit off the stack and use that.
7000       (setq articles (car gnus-newsgroup-limits)
7001             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7002     ;; We use the new limit, so we push the old limit on the stack.
7003     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7004   ;; Set the limit.
7005   (setq gnus-newsgroup-limit articles)
7006   (let ((total (length gnus-newsgroup-data))
7007         (data (gnus-data-find-list (gnus-summary-article-number)))
7008         (gnus-summary-mark-below nil)   ; Inhibit this.
7009         found)
7010     ;; This will do all the work of generating the new summary buffer
7011     ;; according to the new limit.
7012     (gnus-summary-prepare)
7013     ;; Hide any threads, possibly.
7014     (and gnus-show-threads
7015          gnus-thread-hide-subtree
7016          (gnus-summary-hide-all-threads))
7017     ;; Try to return to the article you were at, or one in the
7018     ;; neighborhood.
7019     (when data
7020       ;; We try to find some article after the current one.
7021       (while data
7022         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7023           (setq data nil
7024                 found t))
7025         (setq data (cdr data))))
7026     (unless found
7027       ;; If there is no data, that means that we were after the last
7028       ;; article.  The same goes when we can't find any articles
7029       ;; after the current one.
7030       (goto-char (point-max))
7031       (gnus-summary-find-prev))
7032     (gnus-set-mode-line 'summary)
7033     ;; We return how many articles were removed from the summary
7034     ;; buffer as a result of the new limit.
7035     (- total (length gnus-newsgroup-data))))
7036
7037 (defsubst gnus-invisible-cut-children (threads)
7038   (let ((num 0))
7039     (while threads
7040       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7041         (incf num))
7042       (pop threads))
7043     (< num 2)))
7044
7045 (defsubst gnus-cut-thread (thread)
7046   "Go forwards in the thread until we find an article that we want to display."
7047   (when (or (eq gnus-fetch-old-headers 'some)
7048             (eq gnus-fetch-old-headers 'invisible)
7049             (numberp gnus-fetch-old-headers)
7050             (eq gnus-build-sparse-threads 'some)
7051             (eq gnus-build-sparse-threads 'more))
7052     ;; Deal with old-fetched headers and sparse threads.
7053     (while (and
7054             thread
7055             (or
7056              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7057              (gnus-summary-article-ancient-p
7058               (mail-header-number (car thread))))
7059             (if (or (<= (length (cdr thread)) 1)
7060                     (eq gnus-fetch-old-headers 'invisible))
7061                 (setq gnus-newsgroup-limit
7062                       (delq (mail-header-number (car thread))
7063                             gnus-newsgroup-limit)
7064                       thread (cadr thread))
7065               (when (gnus-invisible-cut-children (cdr thread))
7066                 (let ((th (cdr thread)))
7067                   (while th
7068                     (if (memq (mail-header-number (caar th))
7069                               gnus-newsgroup-limit)
7070                         (setq thread (car th)
7071                               th nil)
7072                       (setq th (cdr th))))))))))
7073   thread)
7074
7075 (defun gnus-cut-threads (threads)
7076   "Cut off all uninteresting articles from the beginning of threads."
7077   (when (or (eq gnus-fetch-old-headers 'some)
7078             (eq gnus-fetch-old-headers 'invisible)
7079             (numberp gnus-fetch-old-headers)
7080             (eq gnus-build-sparse-threads 'some)
7081             (eq gnus-build-sparse-threads 'more))
7082     (let ((th threads))
7083       (while th
7084         (setcar th (gnus-cut-thread (car th)))
7085         (setq th (cdr th)))))
7086   ;; Remove nixed out threads.
7087   (delq nil threads))
7088
7089 (defun gnus-summary-initial-limit (&optional show-if-empty)
7090   "Figure out what the initial limit is supposed to be on group entry.
7091 This entails weeding out unwanted dormants, low-scored articles,
7092 fetch-old-headers verbiage, and so on."
7093   ;; Most groups have nothing to remove.
7094   (if (or gnus-inhibit-limiting
7095           (and (null gnus-newsgroup-dormant)
7096                (eq gnus-newsgroup-display 'identity)
7097                (not (eq gnus-fetch-old-headers 'some))
7098                (not (numberp gnus-fetch-old-headers))
7099                (not (eq gnus-fetch-old-headers 'invisible))
7100                (null gnus-summary-expunge-below)
7101                (not (eq gnus-build-sparse-threads 'some))
7102                (not (eq gnus-build-sparse-threads 'more))
7103                (null gnus-thread-expunge-below)
7104                (not gnus-use-nocem)))
7105       ()                                ; Do nothing.
7106     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7107     (setq gnus-newsgroup-limit nil)
7108     (mapatoms
7109      (lambda (node)
7110        (unless (car (symbol-value node))
7111          ;; These threads have no parents -- they are roots.
7112          (let ((nodes (cdr (symbol-value node)))
7113                thread)
7114            (while nodes
7115              (if (and gnus-thread-expunge-below
7116                       (< (gnus-thread-total-score (car nodes))
7117                          gnus-thread-expunge-below))
7118                  (gnus-expunge-thread (pop nodes))
7119                (setq thread (pop nodes))
7120                (gnus-summary-limit-children thread))))))
7121      gnus-newsgroup-dependencies)
7122     ;; If this limitation resulted in an empty group, we might
7123     ;; pop the previous limit and use it instead.
7124     (when (and (not gnus-newsgroup-limit)
7125                show-if-empty)
7126       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7127     gnus-newsgroup-limit))
7128
7129 (defun gnus-summary-limit-children (thread)
7130   "Return 1 if this subthread is visible and 0 if it is not."
7131   ;; First we get the number of visible children to this thread.  This
7132   ;; is done by recursing down the thread using this function, so this
7133   ;; will really go down to a leaf article first, before slowly
7134   ;; working its way up towards the root.
7135   (when thread
7136     (let ((children
7137            (if (cdr thread)
7138                (apply '+ (mapcar 'gnus-summary-limit-children
7139                                  (cdr thread)))
7140              0))
7141           (number (mail-header-number (car thread)))
7142           score)
7143       (if (and
7144            (not (memq number gnus-newsgroup-marked))
7145            (or
7146             ;; If this article is dormant and has absolutely no visible
7147             ;; children, then this article isn't visible.
7148             (and (memq number gnus-newsgroup-dormant)
7149                  (zerop children))
7150             ;; If this is "fetch-old-headered" and there is no
7151             ;; visible children, then we don't want this article.
7152             (and (or (eq gnus-fetch-old-headers 'some)
7153                      (numberp gnus-fetch-old-headers))
7154                  (gnus-summary-article-ancient-p number)
7155                  (zerop children))
7156             ;; If this is "fetch-old-headered" and `invisible', then
7157             ;; we don't want this article.
7158             (and (eq gnus-fetch-old-headers 'invisible)
7159                  (gnus-summary-article-ancient-p number))
7160             ;; If this is a sparsely inserted article with no children,
7161             ;; we don't want it.
7162             (and (eq gnus-build-sparse-threads 'some)
7163                  (gnus-summary-article-sparse-p number)
7164                  (zerop children))
7165             ;; If we use expunging, and this article is really
7166             ;; low-scored, then we don't want this article.
7167             (when (and gnus-summary-expunge-below
7168                        (< (setq score
7169                                 (or (cdr (assq number gnus-newsgroup-scored))
7170                                     gnus-summary-default-score))
7171                           gnus-summary-expunge-below))
7172               ;; We increase the expunge-tally here, but that has
7173               ;; nothing to do with the limits, really.
7174               (incf gnus-newsgroup-expunged-tally)
7175               ;; We also mark as read here, if that's wanted.
7176               (when (and gnus-summary-mark-below
7177                          (< score gnus-summary-mark-below))
7178                 (setq gnus-newsgroup-unreads
7179                       (delq number gnus-newsgroup-unreads))
7180                 (if gnus-newsgroup-auto-expire
7181                     (push number gnus-newsgroup-expirable)
7182                   (push (cons number gnus-low-score-mark)
7183                         gnus-newsgroup-reads)))
7184               t)
7185             ;; Do the `display' group parameter.
7186             (and gnus-newsgroup-display
7187                  (not (funcall gnus-newsgroup-display)))
7188             ;; Check NoCeM things.
7189             (if (and gnus-use-nocem
7190                      (gnus-nocem-unwanted-article-p
7191                       (mail-header-id (car thread))))
7192                 (progn
7193                   (setq gnus-newsgroup-unreads
7194                         (delq number gnus-newsgroup-unreads))
7195                   t))))
7196           ;; Nope, invisible article.
7197           0
7198         ;; Ok, this article is to be visible, so we add it to the limit
7199         ;; and return 1.
7200         (push number gnus-newsgroup-limit)
7201         1))))
7202
7203 (defun gnus-expunge-thread (thread)
7204   "Mark all articles in THREAD as read."
7205   (let* ((number (mail-header-number (car thread))))
7206     (incf gnus-newsgroup-expunged-tally)
7207     ;; We also mark as read here, if that's wanted.
7208     (setq gnus-newsgroup-unreads
7209           (delq number gnus-newsgroup-unreads))
7210     (if gnus-newsgroup-auto-expire
7211         (push number gnus-newsgroup-expirable)
7212       (push (cons number gnus-low-score-mark)
7213             gnus-newsgroup-reads)))
7214   ;; Go recursively through all subthreads.
7215   (mapcar 'gnus-expunge-thread (cdr thread)))
7216
7217 ;; Summary article oriented commands
7218
7219 (defun gnus-summary-refer-parent-article (n)
7220   "Refer parent article N times.
7221 If N is negative, go to ancestor -N instead.
7222 The difference between N and the number of articles fetched is returned."
7223   (interactive "p")
7224   (let ((skip 1)
7225         error header ref)
7226     (when (not (natnump n))
7227       (setq skip (abs n)
7228             n 1))
7229     (while (and (> n 0)
7230                 (not error))
7231       (setq header (gnus-summary-article-header))
7232       (if (and (eq (mail-header-number header)
7233                    (cdr gnus-article-current))
7234                (equal gnus-newsgroup-name
7235                       (car gnus-article-current)))
7236           ;; If we try to find the parent of the currently
7237           ;; displayed article, then we take a look at the actual
7238           ;; References header, since this is slightly more
7239           ;; reliable than the References field we got from the
7240           ;; server.
7241           (save-excursion
7242             (set-buffer gnus-original-article-buffer)
7243             (nnheader-narrow-to-headers)
7244             (unless (setq ref (message-fetch-field "references"))
7245               (setq ref (message-fetch-field "in-reply-to")))
7246             (widen))
7247         (setq ref
7248               ;; It's not the current article, so we take a bet on
7249               ;; the value we got from the server.
7250               (mail-header-references header)))
7251       (if (and ref
7252                (not (equal ref "")))
7253           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7254             (gnus-message 1 "Couldn't find parent"))
7255         (gnus-message 1 "No references in article %d"
7256                       (gnus-summary-article-number))
7257         (setq error t))
7258       (decf n))
7259     (gnus-summary-position-point)
7260     n))
7261
7262 (defun gnus-summary-refer-references ()
7263   "Fetch all articles mentioned in the References header.
7264 Return the number of articles fetched."
7265   (interactive)
7266   (let ((ref (mail-header-references (gnus-summary-article-header)))
7267         (current (gnus-summary-article-number))
7268         (n 0))
7269     (if (or (not ref)
7270             (equal ref ""))
7271         (error "No References in the current article")
7272       ;; For each Message-ID in the References header...
7273       (while (string-match "<[^>]*>" ref)
7274         (incf n)
7275         ;; ... fetch that article.
7276         (gnus-summary-refer-article
7277          (prog1 (match-string 0 ref)
7278            (setq ref (substring ref (match-end 0))))))
7279       (gnus-summary-goto-subject current)
7280       (gnus-summary-position-point)
7281       n)))
7282
7283 (defun gnus-summary-refer-thread (&optional limit)
7284   "Fetch all articles in the current thread.
7285 If LIMIT (the numerical prefix), fetch that many old headers instead
7286 of what's specified by the `gnus-refer-thread-limit' variable."
7287   (interactive "P")
7288   (let ((id (mail-header-id (gnus-summary-article-header)))
7289         (limit (if limit (prefix-numeric-value limit)
7290                  gnus-refer-thread-limit)))
7291     ;; We want to fetch LIMIT *old* headers, but we also have to
7292     ;; re-fetch all the headers in the current buffer, because many of
7293     ;; them may be undisplayed.  So we adjust LIMIT.
7294     (when (numberp limit)
7295       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7296     (unless (eq gnus-fetch-old-headers 'invisible)
7297       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7298       ;; Retrieve the headers and read them in.
7299       (if (eq (gnus-retrieve-headers
7300                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7301               'nov)
7302           (gnus-build-all-threads)
7303         (error "Can't fetch thread from backends that don't support NOV"))
7304       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7305     (gnus-summary-limit-include-thread id)))
7306
7307 (defun gnus-summary-refer-article (message-id)
7308   "Fetch an article specified by MESSAGE-ID."
7309   (interactive "sMessage-ID: ")
7310   (when (and (stringp message-id)
7311              (not (zerop (length message-id))))
7312     ;; Construct the correct Message-ID if necessary.
7313     ;; Suggested by tale@pawl.rpi.edu.
7314     (unless (string-match "^<" message-id)
7315       (setq message-id (concat "<" message-id)))
7316     (unless (string-match ">$" message-id)
7317       (setq message-id (concat message-id ">")))
7318     (let* ((header (gnus-id-to-header message-id))
7319            (sparse (and header
7320                         (gnus-summary-article-sparse-p
7321                          (mail-header-number header))
7322                         (memq (mail-header-number header)
7323                               gnus-newsgroup-limit)))
7324            number)
7325       (cond
7326        ;; If the article is present in the buffer we just go to it.
7327        ((and header
7328              (or (not (gnus-summary-article-sparse-p
7329                        (mail-header-number header)))
7330                  sparse))
7331         (prog1
7332             (gnus-summary-goto-article
7333              (mail-header-number header) nil t)
7334           (when sparse
7335             (gnus-summary-update-article (mail-header-number header)))))
7336        (t
7337         ;; We fetch the article.
7338         (catch 'found
7339           (dolist (gnus-override-method (gnus-refer-article-methods))
7340             (gnus-check-server gnus-override-method)
7341             ;; Fetch the header, and display the article.
7342             (when (setq number (gnus-summary-insert-subject message-id))
7343               (gnus-summary-select-article nil nil nil number)
7344               (throw 'found t)))
7345           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7346
7347 (defun gnus-refer-article-methods ()
7348   "Return a list of referrable methods."
7349   (cond
7350    ;; No method, so we default to current and native.
7351    ((null gnus-refer-article-method)
7352     (list gnus-current-select-method gnus-select-method))
7353    ;; Current.
7354    ((eq 'current gnus-refer-article-method)
7355     (list gnus-current-select-method))
7356    ;; List of select methods.
7357    ((not (and (symbolp (car gnus-refer-article-method))
7358               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7359     (let (out)
7360       (dolist (method gnus-refer-article-method)
7361         (push (if (eq 'current method)
7362                   gnus-current-select-method
7363                 method)
7364               out))
7365       (nreverse out)))
7366    ;; One single select method.
7367    (t
7368     (list gnus-refer-article-method))))
7369
7370 (defun gnus-summary-edit-parameters ()
7371   "Edit the group parameters of the current group."
7372   (interactive)
7373   (gnus-group-edit-group gnus-newsgroup-name 'params))
7374
7375 (defun gnus-summary-customize-parameters ()
7376   "Customize the group parameters of the current group."
7377   (interactive)
7378   (gnus-group-customize gnus-newsgroup-name))
7379
7380 (defun gnus-summary-enter-digest-group (&optional force)
7381   "Enter an nndoc group based on the current article.
7382 If FORCE, force a digest interpretation.  If not, try
7383 to guess what the document format is."
7384   (interactive "P")
7385   (let ((conf gnus-current-window-configuration))
7386     (save-excursion
7387       (gnus-summary-select-article))
7388     (setq gnus-current-window-configuration conf)
7389     (let* ((name (format "%s-%d"
7390                          (gnus-group-prefixed-name
7391                           gnus-newsgroup-name (list 'nndoc ""))
7392                          (save-excursion
7393                            (set-buffer gnus-summary-buffer)
7394                            gnus-current-article)))
7395            (ogroup gnus-newsgroup-name)
7396            (params (append (gnus-info-params (gnus-get-info ogroup))
7397                            (list (cons 'to-group ogroup))
7398                            (list (cons 'save-article-group ogroup))))
7399            (case-fold-search t)
7400            (buf (current-buffer))
7401            dig to-address)
7402       (save-excursion
7403         (set-buffer gnus-original-article-buffer)
7404         ;; Have the digest group inherit the main mail address of
7405         ;; the parent article.
7406         (when (setq to-address (or (message-fetch-field "reply-to")
7407                                    (message-fetch-field "from")))
7408           (setq params (append
7409                         (list (cons 'to-address
7410                                     (funcall gnus-decode-encoded-word-function
7411                                              to-address))))))
7412         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7413         (insert-buffer-substring gnus-original-article-buffer)
7414         ;; Remove lines that may lead nndoc to misinterpret the
7415         ;; document type.
7416         (narrow-to-region
7417          (goto-char (point-min))
7418          (or (search-forward "\n\n" nil t) (point)))
7419         (goto-char (point-min))
7420         (delete-matching-lines "^Path:\\|^From ")
7421         (widen))
7422       (unwind-protect
7423           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7424                     (gnus-newsgroup-ephemeral-ignored-charsets
7425                      gnus-newsgroup-ignored-charsets))
7426                 (gnus-group-read-ephemeral-group
7427                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7428                               (nndoc-article-type
7429                                ,(if force 'mbox 'guess))) t))
7430             ;; Make all postings to this group go to the parent group.
7431               (nconc (gnus-info-params (gnus-get-info name))
7432                      params)
7433             ;; Couldn't select this doc group.
7434             (switch-to-buffer buf)
7435             (gnus-set-global-variables)
7436             (gnus-configure-windows 'summary)
7437             (gnus-message 3 "Article couldn't be entered?"))
7438         (kill-buffer dig)))))
7439
7440 (defun gnus-summary-read-document (n)
7441   "Open a new group based on the current article(s).
7442 This will allow you to read digests and other similar
7443 documents as newsgroups.
7444 Obeys the standard process/prefix convention."
7445   (interactive "P")
7446   (let* ((articles (gnus-summary-work-articles n))
7447          (ogroup gnus-newsgroup-name)
7448          (params (append (gnus-info-params (gnus-get-info ogroup))
7449                          (list (cons 'to-group ogroup))))
7450          article group egroup groups vgroup)
7451     (while (setq article (pop articles))
7452       (setq group (format "%s-%d" gnus-newsgroup-name article))
7453       (gnus-summary-remove-process-mark article)
7454       (when (gnus-summary-display-article article)
7455         (save-excursion
7456           (with-temp-buffer
7457             (insert-buffer-substring gnus-original-article-buffer)
7458             ;; Remove some headers that may lead nndoc to make
7459             ;; the wrong guess.
7460             (message-narrow-to-head)
7461             (goto-char (point-min))
7462             (delete-matching-lines "^\\(Path\\):\\|^From ")
7463             (widen)
7464             (if (setq egroup
7465                       (gnus-group-read-ephemeral-group
7466                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7467                                      (nndoc-article-type guess))
7468                        t nil t))
7469                 (progn
7470             ;; Make all postings to this group go to the parent group.
7471                   (nconc (gnus-info-params (gnus-get-info egroup))
7472                          params)
7473                   (push egroup groups))
7474               ;; Couldn't select this doc group.
7475               (gnus-error 3 "Article couldn't be entered"))))))
7476     ;; Now we have selected all the documents.
7477     (cond
7478      ((not groups)
7479       (error "None of the articles could be interpreted as documents"))
7480      ((gnus-group-read-ephemeral-group
7481        (setq vgroup (format
7482                      "nnvirtual:%s-%s" gnus-newsgroup-name
7483                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7484        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7485        t
7486        (cons (current-buffer) 'summary)))
7487      (t
7488       (error "Couldn't select virtual nndoc group")))))
7489
7490 (defun gnus-summary-isearch-article (&optional regexp-p)
7491   "Do incremental search forward on the current article.
7492 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7493   (interactive "P")
7494   (gnus-summary-select-article)
7495   (gnus-configure-windows 'article)
7496   (gnus-eval-in-buffer-window gnus-article-buffer
7497     (save-restriction
7498       (widen)
7499       (isearch-forward regexp-p))))
7500
7501 (defun gnus-summary-search-article-forward (regexp &optional backward)
7502   "Search for an article containing REGEXP forward.
7503 If BACKWARD, search backward instead."
7504   (interactive
7505    (list (read-string
7506           (format "Search article %s (regexp%s): "
7507                   (if current-prefix-arg "backward" "forward")
7508                   (if gnus-last-search-regexp
7509                       (concat ", default " gnus-last-search-regexp)
7510                     "")))
7511          current-prefix-arg))
7512   (if (string-equal regexp "")
7513       (setq regexp (or gnus-last-search-regexp ""))
7514     (setq gnus-last-search-regexp regexp)
7515     (setq gnus-article-before-search gnus-current-article))
7516   ;; Intentionally set gnus-last-article.
7517   (setq gnus-last-article gnus-article-before-search)
7518   (let ((gnus-last-article gnus-last-article))
7519     (if (gnus-summary-search-article regexp backward)
7520         (gnus-summary-show-thread)
7521       (error "Search failed: \"%s\"" regexp))))
7522
7523 (defun gnus-summary-search-article-backward (regexp)
7524   "Search for an article containing REGEXP backward."
7525   (interactive
7526    (list (read-string
7527           (format "Search article backward (regexp%s): "
7528                   (if gnus-last-search-regexp
7529                       (concat ", default " gnus-last-search-regexp)
7530                     "")))))
7531   (gnus-summary-search-article-forward regexp 'backward))
7532
7533 (defun gnus-summary-search-article (regexp &optional backward)
7534   "Search for an article containing REGEXP.
7535 Optional argument BACKWARD means do search for backward.
7536 `gnus-select-article-hook' is not called during the search."
7537   ;; We have to require this here to make sure that the following
7538   ;; dynamic binding isn't shadowed by autoloading.
7539   (require 'gnus-async)
7540   (require 'gnus-art)
7541   (let ((gnus-select-article-hook nil)  ;Disable hook.
7542         (gnus-article-prepare-hook nil)
7543         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7544         (gnus-use-article-prefetch nil)
7545         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7546         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7547         (sum (current-buffer))
7548         (gnus-display-mime-function nil)
7549         (found nil)
7550         point)
7551     (gnus-save-hidden-threads
7552       (gnus-summary-select-article)
7553       (set-buffer gnus-article-buffer)
7554       (goto-char (window-point (get-buffer-window (current-buffer))))
7555       (when backward
7556         (forward-line -1))
7557       (while (not found)
7558         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7559         (if (if backward
7560                 (re-search-backward regexp nil t)
7561               (re-search-forward regexp nil t))
7562             ;; We found the regexp.
7563             (progn
7564               (setq found 'found)
7565               (beginning-of-line)
7566               (set-window-start
7567                (get-buffer-window (current-buffer))
7568                (point))
7569               (forward-line 1)
7570               (set-window-point
7571                (get-buffer-window (current-buffer))
7572                (point))
7573               (set-buffer sum)
7574               (setq point (point)))
7575           ;; We didn't find it, so we go to the next article.
7576           (set-buffer sum)
7577           (setq found 'not)
7578           (while (eq found 'not)
7579             (if (not (if backward (gnus-summary-find-prev)
7580                        (gnus-summary-find-next)))
7581                 ;; No more articles.
7582                 (setq found t)
7583               ;; Select the next article and adjust point.
7584               (unless (gnus-summary-article-sparse-p
7585                        (gnus-summary-article-number))
7586                 (setq found nil)
7587                 (gnus-summary-select-article)
7588                 (set-buffer gnus-article-buffer)
7589                 (widen)
7590                 (goto-char (if backward (point-max) (point-min))))))))
7591       (gnus-message 7 ""))
7592     ;; Return whether we found the regexp.
7593     (when (eq found 'found)
7594       (goto-char point)
7595       (gnus-summary-show-thread)
7596       (gnus-summary-goto-subject gnus-current-article)
7597       (gnus-summary-position-point)
7598       t)))
7599
7600 (defun gnus-find-matching-articles (header regexp)
7601   "Return a list of all articles that match REGEXP on HEADER.
7602 This search includes all articles in the current group that Gnus has
7603 fetched headers for, whether they are displayed or not."
7604   (let ((articles nil)
7605         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7606         (case-fold-search t))
7607     (dolist (header gnus-newsgroup-headers)
7608       (when (string-match regexp (funcall func header))
7609         (push (mail-header-number header) articles)))
7610     (nreverse articles)))
7611
7612 (defun gnus-summary-find-matching (header regexp &optional backward unread
7613                                           not-case-fold)
7614   "Return a list of all articles that match REGEXP on HEADER.
7615 The search stars on the current article and goes forwards unless
7616 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7617 If UNREAD is non-nil, only unread articles will
7618 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7619 in the comparisons."
7620   (let ((case-fold-search (not not-case-fold))
7621         articles d func)
7622     (if (consp header)
7623         (if (eq (car header) 'extra)
7624             (setq func
7625                   `(lambda (h)
7626                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7627                          "")))
7628           (error "%s is an invalid header" header))
7629       (unless (fboundp (intern (concat "mail-header-" header)))
7630         (error "%s is not a valid header" header))
7631       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7632     (dolist (d (if (eq backward 'all)
7633                    gnus-newsgroup-data
7634                  (gnus-data-find-list
7635                   (gnus-summary-article-number)
7636                   (gnus-data-list backward))))
7637       (when (and (or (not unread)       ; We want all articles...
7638                      (gnus-data-unread-p d)) ; Or just unreads.
7639                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7640                  (string-match regexp
7641                                (funcall func (gnus-data-header d)))) ; Match.
7642         (push (gnus-data-number d) articles))) ; Success!
7643     (nreverse articles)))
7644
7645 (defun gnus-summary-execute-command (header regexp command &optional backward)
7646   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7647 If HEADER is an empty string (or nil), the match is done on the entire
7648 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7649   (interactive
7650    (list (let ((completion-ignore-case t))
7651            (completing-read
7652             "Header name: "
7653             (mapcar (lambda (header) (list (format "%s" header)))
7654                     (append
7655                      '("Number" "Subject" "From" "Lines" "Date"
7656                        "Message-ID" "Xref" "References" "Body")
7657                      gnus-extra-headers))
7658             nil 'require-match))
7659          (read-string "Regexp: ")
7660          (read-key-sequence "Command: ")
7661          current-prefix-arg))
7662   (when (equal header "Body")
7663     (setq header ""))
7664   ;; Hidden thread subtrees must be searched as well.
7665   (gnus-summary-show-all-threads)
7666   ;; We don't want to change current point nor window configuration.
7667   (save-excursion
7668     (save-window-excursion
7669       (gnus-message 6 "Executing %s..." (key-description command))
7670 ;; We'd like to execute COMMAND interactively so as to give arguments.
7671       (gnus-execute header regexp
7672                     `(call-interactively ',(key-binding command))
7673                     backward)
7674       (gnus-message 6 "Executing %s...done" (key-description command)))))
7675
7676 (defun gnus-summary-beginning-of-article ()
7677   "Scroll the article back to the beginning."
7678   (interactive)
7679   (gnus-summary-select-article)
7680   (gnus-configure-windows 'article)
7681   (gnus-eval-in-buffer-window gnus-article-buffer
7682     (widen)
7683     (goto-char (point-min))
7684     (when gnus-page-broken
7685       (gnus-narrow-to-page))))
7686
7687 (defun gnus-summary-end-of-article ()
7688   "Scroll to the end of the article."
7689   (interactive)
7690   (gnus-summary-select-article)
7691   (gnus-configure-windows 'article)
7692   (gnus-eval-in-buffer-window gnus-article-buffer
7693     (widen)
7694     (goto-char (point-max))
7695     (recenter -3)
7696     (when gnus-page-broken
7697       (gnus-narrow-to-page))))
7698
7699 (defun gnus-summary-print-article (&optional filename n)
7700   "Generate and print a PostScript image of the N next (mail) articles.
7701
7702 If N is negative, print the N previous articles.  If N is nil and articles
7703 have been marked with the process mark, print these instead.
7704
7705 If the optional first argument FILENAME is nil, send the image to the
7706 printer.  If FILENAME is a string, save the PostScript image in a file with
7707 that name.  If FILENAME is a number, prompt the user for the name of the file
7708 to save in."
7709   (interactive (list (ps-print-preprint current-prefix-arg)))
7710   (dolist (article (gnus-summary-work-articles n))
7711     (gnus-summary-select-article nil nil 'pseudo article)
7712     (gnus-eval-in-buffer-window gnus-article-buffer
7713       (let ((buffer (generate-new-buffer " *print*")))
7714         (unwind-protect
7715             (progn
7716               (copy-to-buffer buffer (point-min) (point-max))
7717               (set-buffer buffer)
7718               (gnus-article-delete-invisible-text)
7719               (when (gnus-visual-p 'article-highlight 'highlight)
7720                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7721                 ;; highlight.
7722                 (let ((gnus-article-buffer buffer))
7723                   (gnus-article-highlight-citation t)
7724                   (gnus-article-highlight-signature)))
7725               (let ((ps-left-header
7726                      (list
7727                       (concat "("
7728                               (mail-header-subject gnus-current-headers) ")")
7729                       (concat "("
7730                               (mail-header-from gnus-current-headers) ")")))
7731                     (ps-right-header
7732                      (list
7733                       "/pagenumberstring load"
7734                       (concat "("
7735                               (mail-header-date gnus-current-headers) ")"))))
7736                 (gnus-run-hooks 'gnus-ps-print-hook)
7737                 (save-excursion
7738                   (if window-system
7739                       (ps-spool-buffer-with-faces)
7740                     (ps-spool-buffer)))))
7741           (kill-buffer buffer))))
7742     (gnus-summary-remove-process-mark article))
7743   (ps-despool filename))
7744
7745 (defun gnus-summary-show-article (&optional arg)
7746   "Force re-fetching of the current article.
7747 If ARG (the prefix) is a number, show the article with the charset
7748 defined in `gnus-summary-show-article-charset-alist', or the charset
7749 inputed.
7750 If ARG (the prefix) is non-nil and not a number, show the raw article
7751 without any article massaging functions being run."
7752   (interactive "P")
7753   (cond
7754    ((numberp arg)
7755     (let ((gnus-newsgroup-charset
7756            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7757                (mm-read-coding-system "Charset: ")))
7758           (gnus-newsgroup-ignored-charsets 'gnus-all))
7759       (gnus-summary-select-article nil 'force)
7760       (let ((deps gnus-newsgroup-dependencies)
7761             head header)
7762         (save-excursion
7763           (set-buffer gnus-original-article-buffer)
7764           (save-restriction
7765             (message-narrow-to-head)
7766             (setq head (buffer-string)))
7767           (with-temp-buffer
7768             (insert (format "211 %d Article retrieved.\n"
7769                             (cdr gnus-article-current)))
7770             (insert head)
7771             (insert ".\n")
7772             (let ((nntp-server-buffer (current-buffer)))
7773               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7774         (gnus-data-set-header
7775          (gnus-data-find (cdr gnus-article-current))
7776          header)
7777         (gnus-summary-update-article-line
7778          (cdr gnus-article-current) header)
7779         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7780             (gnus-summary-update-secondary-mark
7781              (cdr gnus-article-current))))))
7782    ((not arg)
7783     ;; Select the article the normal way.
7784     (gnus-summary-select-article nil 'force))
7785    (t
7786     ;; We have to require this here to make sure that the following
7787     ;; dynamic binding isn't shadowed by autoloading.
7788     (require 'gnus-async)
7789     (require 'gnus-art)
7790     ;; Bind the article treatment functions to nil.
7791     (let ((gnus-have-all-headers t)
7792           gnus-article-prepare-hook
7793           gnus-article-decode-hook
7794           gnus-display-mime-function
7795           gnus-break-pages)
7796       ;; Destroy any MIME parts.
7797       (when (gnus-buffer-live-p gnus-article-buffer)
7798         (save-excursion
7799           (set-buffer gnus-article-buffer)
7800           (mm-destroy-parts gnus-article-mime-handles)
7801           ;; Set it to nil for safety reason.
7802           (setq gnus-article-mime-handle-alist nil)
7803           (setq gnus-article-mime-handles nil)))
7804       (gnus-summary-select-article nil 'force))))
7805   (gnus-summary-goto-subject gnus-current-article)
7806   (gnus-summary-position-point))
7807
7808 (defun gnus-summary-show-raw-article ()
7809   "Show the raw article without any article massaging functions being run."
7810   (interactive)
7811   (gnus-summary-show-article t))
7812
7813 (defun gnus-summary-verbose-headers (&optional arg)
7814   "Toggle permanent full header display.
7815 If ARG is a positive number, turn header display on.
7816 If ARG is a negative number, turn header display off."
7817   (interactive "P")
7818   (setq gnus-show-all-headers
7819         (cond ((or (not (numberp arg))
7820                    (zerop arg))
7821                (not gnus-show-all-headers))
7822               ((natnump arg)
7823                t)))
7824   (gnus-summary-show-article))
7825
7826 (defun gnus-summary-toggle-header (&optional arg)
7827   "Show the headers if they are hidden, or hide them if they are shown.
7828 If ARG is a positive number, show the entire header.
7829 If ARG is a negative number, hide the unwanted header lines."
7830   (interactive "P")
7831   (save-excursion
7832     (set-buffer gnus-article-buffer)
7833     (save-restriction
7834       (let* ((buffer-read-only nil)
7835              (inhibit-point-motion-hooks t)
7836              hidden e)
7837         (setq hidden
7838               (if (numberp arg)
7839                   (>= arg 0)
7840                 (save-restriction
7841                   (article-narrow-to-head)
7842                   (gnus-article-hidden-text-p 'headers))))
7843         (goto-char (point-min))
7844         (when (search-forward "\n\n" nil t)
7845           (delete-region (point-min) (1- (point))))
7846         (goto-char (point-min))
7847         (save-excursion
7848           (set-buffer gnus-original-article-buffer)
7849           (goto-char (point-min))
7850           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7851         (insert-buffer-substring gnus-original-article-buffer 1 e)
7852         (save-restriction
7853           (narrow-to-region (point-min) (point))
7854           (article-decode-encoded-words)
7855           (if  hidden
7856               (let ((gnus-treat-hide-headers nil)
7857                     (gnus-treat-hide-boring-headers nil))
7858                 (setq gnus-article-wash-types
7859                       (delq 'headers gnus-article-wash-types))
7860                 (gnus-treat-article 'head))
7861             (gnus-treat-article 'head)))
7862         (gnus-set-mode-line 'article)))))
7863
7864 (defun gnus-summary-show-all-headers ()
7865   "Make all header lines visible."
7866   (interactive)
7867   (gnus-summary-toggle-header 1))
7868
7869 (defun gnus-summary-caesar-message (&optional arg)
7870   "Caesar rotate the current article by 13.
7871 The numerical prefix specifies how many places to rotate each letter
7872 forward."
7873   (interactive "P")
7874   (gnus-summary-select-article)
7875   (let ((mail-header-separator ""))
7876     (gnus-eval-in-buffer-window gnus-article-buffer
7877       (save-restriction
7878         (widen)
7879         (let ((start (window-start))
7880               buffer-read-only)
7881           (message-caesar-buffer-body arg)
7882           (set-window-start (get-buffer-window (current-buffer)) start))))))
7883
7884 (defun gnus-summary-stop-page-breaking ()
7885   "Stop page breaking in the current article."
7886   (interactive)
7887   (gnus-summary-select-article)
7888   (gnus-eval-in-buffer-window gnus-article-buffer
7889     (widen)
7890     (when (gnus-visual-p 'page-marker)
7891       (let ((buffer-read-only nil))
7892         (gnus-remove-text-with-property 'gnus-prev)
7893         (gnus-remove-text-with-property 'gnus-next))
7894       (setq gnus-page-broken nil))))
7895
7896 (defun gnus-summary-move-article (&optional n to-newsgroup
7897                                             select-method action)
7898   "Move the current article to a different newsgroup.
7899 If N is a positive number, move the N next articles.
7900 If N is a negative number, move the N previous articles.
7901 If N is nil and any articles have been marked with the process mark,
7902 move those articles instead.
7903 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7904 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7905 re-spool using this method.
7906
7907 For this function to work, both the current newsgroup and the
7908 newsgroup that you want to move to have to support the `request-move'
7909 and `request-accept' functions.
7910
7911 ACTION can be either `move' (the default), `crosspost' or `copy'."
7912   (interactive "P")
7913   (unless action
7914     (setq action 'move))
7915   ;; Disable marking as read.
7916   (let (gnus-mark-article-hook)
7917     (save-window-excursion
7918       (gnus-summary-select-article)))
7919   ;; Check whether the source group supports the required functions.
7920   (cond ((and (eq action 'move)
7921               (not (gnus-check-backend-function
7922                     'request-move-article gnus-newsgroup-name)))
7923          (error "The current group does not support article moving"))
7924         ((and (eq action 'crosspost)
7925               (not (gnus-check-backend-function
7926                     'request-replace-article gnus-newsgroup-name)))
7927          (error "The current group does not support article editing")))
7928   (let ((articles (gnus-summary-work-articles n))
7929         (prefix (if (gnus-check-backend-function
7930                      'request-move-article gnus-newsgroup-name)
7931                     (gnus-group-real-prefix gnus-newsgroup-name)
7932                   ""))
7933         (names '((move "Move" "Moving")
7934                  (copy "Copy" "Copying")
7935                  (crosspost "Crosspost" "Crossposting")))
7936         (copy-buf (save-excursion
7937                     (nnheader-set-temp-buffer " *copy article*")))
7938         art-group to-method new-xref article to-groups)
7939     (unless (assq action names)
7940       (error "Unknown action %s" action))
7941     ;; Read the newsgroup name.
7942     (when (and (not to-newsgroup)
7943                (not select-method))
7944       (setq to-newsgroup
7945             (gnus-read-move-group-name
7946              (cadr (assq action names))
7947              (symbol-value (intern (format "gnus-current-%s-group" action)))
7948              articles prefix))
7949       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7950     (setq to-method (or select-method
7951                         (gnus-server-to-method
7952                          (gnus-group-method to-newsgroup))))
7953     ;; Check the method we are to move this article to...
7954     (unless (gnus-check-backend-function
7955              'request-accept-article (car to-method))
7956       (error "%s does not support article copying" (car to-method)))
7957     (unless (gnus-check-server to-method)
7958       (error "Can't open server %s" (car to-method)))
7959     (gnus-message 6 "%s to %s: %s..."
7960                   (caddr (assq action names))
7961                   (or (car select-method) to-newsgroup) articles)
7962     (while articles
7963       (setq article (pop articles))
7964       (setq
7965        art-group
7966        (cond
7967         ;; Move the article.
7968         ((eq action 'move)
7969          ;; Remove this article from future suppression.
7970          (gnus-dup-unsuppress-article article)
7971          (gnus-request-move-article
7972           article                       ; Article to move
7973           gnus-newsgroup-name           ; From newsgroup
7974           (nth 1 (gnus-find-method-for-group
7975                   gnus-newsgroup-name)) ; Server
7976           (list 'gnus-request-accept-article
7977                 to-newsgroup (list 'quote select-method)
7978                 (not articles) t)       ; Accept form
7979           (not articles)))              ; Only save nov last time
7980         ;; Copy the article.
7981         ((eq action 'copy)
7982          (save-excursion
7983            (set-buffer copy-buf)
7984            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7985              (gnus-request-accept-article
7986               to-newsgroup select-method (not articles) t))))
7987         ;; Crosspost the article.
7988         ((eq action 'crosspost)
7989          (let ((xref (message-tokenize-header
7990                       (mail-header-xref (gnus-summary-article-header article))
7991                       " ")))
7992            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7993                                   ":" article))
7994            (unless xref
7995              (setq xref (list (system-name))))
7996            (setq new-xref
7997                  (concat
7998                   (mapconcat 'identity
7999                              (delete "Xref:" (delete new-xref xref))
8000                              " ")
8001                   " " new-xref))
8002            (save-excursion
8003              (set-buffer copy-buf)
8004              ;; First put the article in the destination group.
8005              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8006              (when (consp (setq art-group
8007                                 (gnus-request-accept-article
8008                                  to-newsgroup select-method (not articles))))
8009                (setq new-xref (concat new-xref " " (car art-group)
8010                                       ":" (cdr art-group)))
8011                ;; Now we have the new Xrefs header, so we insert
8012                ;; it and replace the new article.
8013                (nnheader-replace-header "Xref" new-xref)
8014                (gnus-request-replace-article
8015                 (cdr art-group) to-newsgroup (current-buffer))
8016                art-group))))))
8017       (cond
8018        ((not art-group)
8019         (gnus-message 1 "Couldn't %s article %s: %s"
8020                       (cadr (assq action names)) article
8021                       (nnheader-get-report (car to-method))))
8022        ((eq art-group 'junk)
8023         (when (eq action 'move)
8024           (gnus-summary-mark-article article gnus-canceled-mark)
8025           (gnus-message 4 "Deleted article %s" article)))
8026        (t
8027         (let* ((pto-group (gnus-group-prefixed-name
8028                            (car art-group) to-method))
8029                (entry
8030                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8031                (info (nth 2 entry))
8032                (to-group (gnus-info-group info))
8033                to-marks)
8034           ;; Update the group that has been moved to.
8035           (when (and info
8036                      (memq action '(move copy)))
8037             (unless (member to-group to-groups)
8038               (push to-group to-groups))
8039
8040             (unless (memq article gnus-newsgroup-unreads)
8041               (push 'read to-marks)
8042               (gnus-info-set-read
8043                info (gnus-add-to-range (gnus-info-read info)
8044                                        (list (cdr art-group)))))
8045
8046             ;; See whether the article is to be put in the cache.
8047             (let ((marks gnus-article-mark-lists)
8048                   (to-article (cdr art-group)))
8049
8050               ;; Enter the article into the cache in the new group,
8051               ;; if that is required.
8052               (when gnus-use-cache
8053                 (gnus-cache-possibly-enter-article
8054                  to-group to-article
8055                  (memq article gnus-newsgroup-marked)
8056                  (memq article gnus-newsgroup-dormant)
8057                  (memq article gnus-newsgroup-unreads)))
8058
8059               (when gnus-preserve-marks
8060                 ;; Copy any marks over to the new group.
8061                 (when (and (equal to-group gnus-newsgroup-name)
8062                            (not (memq article gnus-newsgroup-unreads)))
8063                   ;; Mark this article as read in this group.
8064                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8065                   (setcdr (gnus-active to-group) to-article)
8066                   (setcdr gnus-newsgroup-active to-article))
8067
8068                 (while marks
8069                   (when (memq article (symbol-value
8070                                        (intern (format "gnus-newsgroup-%s"
8071                                                        (caar marks)))))
8072                     (push (cdar marks) to-marks)
8073                     ;; If the other group is the same as this group,
8074                     ;; then we have to add the mark to the list.
8075                     (when (equal to-group gnus-newsgroup-name)
8076                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8077                            (cons to-article
8078                                  (symbol-value
8079                                   (intern (format "gnus-newsgroup-%s"
8080                                                   (caar marks)))))))
8081                     ;; Copy the marks to other group.
8082                     (gnus-add-marked-articles
8083                      to-group (cdar marks) (list to-article) info))
8084                   (setq marks (cdr marks)))
8085
8086                 (gnus-request-set-mark to-group (list (list (list to-article)
8087                                                             'set
8088                                                             to-marks))))
8089
8090               (gnus-dribble-enter
8091                (concat "(gnus-group-set-info '"
8092                        (gnus-prin1-to-string (gnus-get-info to-group))
8093                        ")"))))
8094
8095           ;; Update the Xref header in this article to point to
8096           ;; the new crossposted article we have just created.
8097           (when (eq action 'crosspost)
8098             (save-excursion
8099               (set-buffer copy-buf)
8100               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8101               (nnheader-replace-header "Xref" new-xref)
8102               (gnus-request-replace-article
8103                article gnus-newsgroup-name (current-buffer)))))
8104
8105         ;;;!!!Why is this necessary?
8106         (set-buffer gnus-summary-buffer)
8107
8108         (gnus-summary-goto-subject article)
8109         (when (eq action 'move)
8110           (gnus-summary-mark-article article gnus-canceled-mark))))
8111       (gnus-summary-remove-process-mark article))
8112     ;; Re-activate all groups that have been moved to.
8113     (while to-groups
8114       (save-excursion
8115         (set-buffer gnus-group-buffer)
8116         (when (gnus-group-goto-group (car to-groups) t)
8117           (gnus-group-get-new-news-this-group 1 t))
8118         (pop to-groups)))
8119
8120     (gnus-kill-buffer copy-buf)
8121     (gnus-summary-position-point)
8122     (gnus-set-mode-line 'summary)))
8123
8124 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8125   "Move the current article to a different newsgroup.
8126 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8127 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8128 re-spool using this method."
8129   (interactive "P")
8130   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8131
8132 (defun gnus-summary-crosspost-article (&optional n)
8133   "Crosspost the current article to some other group."
8134   (interactive "P")
8135   (gnus-summary-move-article n nil nil 'crosspost))
8136
8137 (defcustom gnus-summary-respool-default-method nil
8138   "Default method for respooling an article.
8139 If nil, use to the current newsgroup method."
8140   :type '(choice (gnus-select-method :value (nnml ""))
8141                  (const nil))
8142   :group 'gnus-summary-mail)
8143
8144 (defun gnus-summary-respool-article (&optional n method)
8145   "Respool the current article.
8146 The article will be squeezed through the mail spooling process again,
8147 which means that it will be put in some mail newsgroup or other
8148 depending on `nnmail-split-methods'.
8149 If N is a positive number, respool the N next articles.
8150 If N is a negative number, respool the N previous articles.
8151 If N is nil and any articles have been marked with the process mark,
8152 respool those articles instead.
8153
8154 Respooling can be done both from mail groups and \"real\" newsgroups.
8155 In the former case, the articles in question will be moved from the
8156 current group into whatever groups they are destined to.  In the
8157 latter case, they will be copied into the relevant groups."
8158   (interactive
8159    (list current-prefix-arg
8160          (let* ((methods (gnus-methods-using 'respool))
8161                 (methname
8162                  (symbol-name (or gnus-summary-respool-default-method
8163                                   (car (gnus-find-method-for-group
8164                                         gnus-newsgroup-name)))))
8165                 (method
8166                  (gnus-completing-read
8167                   methname "What backend do you want to use when respooling?"
8168                   methods nil t nil 'gnus-mail-method-history))
8169                 ms)
8170            (cond
8171             ((zerop (length (setq ms (gnus-servers-using-backend
8172                                       (intern method)))))
8173              (list (intern method) ""))
8174             ((= 1 (length ms))
8175              (car ms))
8176             (t
8177              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8178                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8179                            ms-alist))))))))
8180   (unless method
8181     (error "No method given for respooling"))
8182   (if (assoc (symbol-name
8183               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8184              (gnus-methods-using 'respool))
8185       (gnus-summary-move-article n nil method)
8186     (gnus-summary-copy-article n nil method)))
8187
8188 (defun gnus-summary-import-article (file &optional edit)
8189   "Import an arbitrary file into a mail newsgroup."
8190   (interactive "fImport file: \nP")
8191   (let ((group gnus-newsgroup-name)
8192         (now (current-time))
8193         atts lines group-art)
8194     (unless (gnus-check-backend-function 'request-accept-article group)
8195       (error "%s does not support article importing" group))
8196     (or (file-readable-p file)
8197         (not (file-regular-p file))
8198         (error "Can't read %s" file))
8199     (save-excursion
8200       (set-buffer (gnus-get-buffer-create " *import file*"))
8201       (erase-buffer)
8202       (nnheader-insert-file-contents file)
8203       (goto-char (point-min))
8204       (if (nnheader-article-p)
8205           (save-restriction
8206             (goto-char (point-min))
8207             (search-forward "\n\n" nil t)
8208             (narrow-to-region (point-min) (1- (point)))
8209             (goto-char (point-min))
8210             (unless (re-search-forward "^date:" nil t)
8211               (goto-char (point-max))
8212               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8213        ;; This doesn't look like an article, so we fudge some headers.
8214         (setq atts (file-attributes file)
8215               lines (count-lines (point-min) (point-max)))
8216         (insert "From: " (read-string "From: ") "\n"
8217                 "Subject: " (read-string "Subject: ") "\n"
8218                 "Date: " (message-make-date (nth 5 atts)) "\n"
8219                 "Message-ID: " (message-make-message-id) "\n"
8220                 "Lines: " (int-to-string lines) "\n"
8221                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8222       (setq group-art (gnus-request-accept-article group nil t))
8223       (kill-buffer (current-buffer)))
8224     (setq gnus-newsgroup-active (gnus-activate-group group))
8225     (forward-line 1)
8226     (gnus-summary-goto-article (cdr group-art) nil t)
8227     (when edit
8228       (gnus-summary-edit-article))))
8229
8230 (defun gnus-summary-create-article ()
8231   "Create an article in a mail newsgroup."
8232   (interactive)
8233   (let ((group gnus-newsgroup-name)
8234         (now (current-time))
8235         group-art)
8236     (unless (gnus-check-backend-function 'request-accept-article group)
8237       (error "%s does not support article importing" group))
8238     (save-excursion
8239       (set-buffer (gnus-get-buffer-create " *import file*"))
8240       (erase-buffer)
8241       (goto-char (point-min))
8242       ;; This doesn't look like an article, so we fudge some headers.
8243       (insert "From: " (read-string "From: ") "\n"
8244               "Subject: " (read-string "Subject: ") "\n"
8245               "Date: " (message-make-date now) "\n"
8246               "Message-ID: " (message-make-message-id) "\n")
8247       (setq group-art (gnus-request-accept-article group nil t))
8248       (kill-buffer (current-buffer)))
8249     (setq gnus-newsgroup-active (gnus-activate-group group))
8250     (forward-line 1)
8251     (gnus-summary-goto-article (cdr group-art) nil t)
8252     (gnus-summary-edit-article)))
8253
8254 (defun gnus-summary-article-posted-p ()
8255   "Say whether the current (mail) article is available from news as well.
8256 This will be the case if the article has both been mailed and posted."
8257   (interactive)
8258   (let ((id (mail-header-references (gnus-summary-article-header)))
8259         (gnus-override-method (car (gnus-refer-article-methods))))
8260     (if (gnus-request-head id "")
8261         (gnus-message 2 "The current message was found on %s"
8262                       gnus-override-method)
8263       (gnus-message 2 "The current message couldn't be found on %s"
8264                     gnus-override-method)
8265       nil)))
8266
8267 (defun gnus-summary-expire-articles (&optional now)
8268   "Expire all articles that are marked as expirable in the current group."
8269   (interactive)
8270   (when (gnus-check-backend-function
8271          'request-expire-articles gnus-newsgroup-name)
8272     ;; This backend supports expiry.
8273     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8274            (expirable (if total
8275                           (progn
8276                             ;; We need to update the info for
8277                         ;; this group for `gnus-list-of-read-articles'
8278                             ;; to give us the right answer.
8279                             (gnus-run-hooks 'gnus-exit-group-hook)
8280                             (gnus-summary-update-info)
8281                             (gnus-list-of-read-articles gnus-newsgroup-name))
8282                         (setq gnus-newsgroup-expirable
8283                               (sort gnus-newsgroup-expirable '<))))
8284            (expiry-wait (if now 'immediate
8285                           (gnus-group-find-parameter
8286                            gnus-newsgroup-name 'expiry-wait)))
8287            (nnmail-expiry-target
8288             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8289                 nnmail-expiry-target))
8290            es)
8291       (when expirable
8292         ;; There are expirable articles in this group, so we run them
8293         ;; through the expiry process.
8294         (gnus-message 6 "Expiring articles...")
8295         (unless (gnus-check-group gnus-newsgroup-name)
8296           (error "Can't open server for %s" gnus-newsgroup-name))
8297         ;; The list of articles that weren't expired is returned.
8298         (save-excursion
8299           (if expiry-wait
8300               (let ((nnmail-expiry-wait-function nil)
8301                     (nnmail-expiry-wait expiry-wait))
8302                 (setq es (gnus-request-expire-articles
8303                           expirable gnus-newsgroup-name)))
8304             (setq es (gnus-request-expire-articles
8305                       expirable gnus-newsgroup-name)))
8306           (unless total
8307             (setq gnus-newsgroup-expirable es))
8308           ;; We go through the old list of expirable, and mark all
8309           ;; really expired articles as nonexistent.
8310           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8311             (let ((gnus-use-cache nil))
8312               (while expirable
8313                 (unless (memq (car expirable) es)
8314                   (when (gnus-data-find (car expirable))
8315                     (gnus-summary-mark-article
8316                      (car expirable) gnus-canceled-mark)))
8317                 (setq expirable (cdr expirable))))))
8318         (gnus-message 6 "Expiring articles...done")))))
8319
8320 (defun gnus-summary-expire-articles-now ()
8321   "Expunge all expirable articles in the current group.
8322 This means that *all* articles that are marked as expirable will be
8323 deleted forever, right now."
8324   (interactive)
8325   (or gnus-expert-user
8326       (gnus-yes-or-no-p
8327        "Are you really, really, really sure you want to delete all these messages? ")
8328       (error "Phew!"))
8329   (gnus-summary-expire-articles t))
8330
8331 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8332 (defun gnus-summary-delete-article (&optional n)
8333   "Delete the N next (mail) articles.
8334 This command actually deletes articles.  This is not a marking
8335 command.  The article will disappear forever from your life, never to
8336 return.
8337 If N is negative, delete backwards.
8338 If N is nil and articles have been marked with the process mark,
8339 delete these instead."
8340   (interactive "P")
8341   (unless (gnus-check-backend-function 'request-expire-articles
8342                                        gnus-newsgroup-name)
8343     (error "The current newsgroup does not support article deletion"))
8344   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8345     (error "Couldn't open server"))
8346   ;; Compute the list of articles to delete.
8347   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8348         not-deleted)
8349     (if (and gnus-novice-user
8350              (not (gnus-yes-or-no-p
8351                    (format "Do you really want to delete %s forever? "
8352                            (if (> (length articles) 1)
8353                                (format "these %s articles" (length articles))
8354                              "this article")))))
8355         ()
8356       ;; Delete the articles.
8357       (setq not-deleted (gnus-request-expire-articles
8358                          articles gnus-newsgroup-name 'force))
8359       (while articles
8360         (gnus-summary-remove-process-mark (car articles))
8361         ;; The backend might not have been able to delete the article
8362         ;; after all.
8363         (unless (memq (car articles) not-deleted)
8364           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8365         (setq articles (cdr articles)))
8366       (when not-deleted
8367         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8368     (gnus-summary-position-point)
8369     (gnus-set-mode-line 'summary)
8370     not-deleted))
8371
8372 (defun gnus-summary-edit-article (&optional arg)
8373   "Edit the current article.
8374 This will have permanent effect only in mail groups.
8375 If ARG is nil, edit the decoded articles.
8376 If ARG is 1, edit the raw articles.
8377 If ARG is 2, edit the raw articles even in read-only groups.
8378 If ARG is 3, edit the articles with the current handles.
8379 Otherwise, allow editing of articles even in read-only
8380 groups."
8381   (interactive "P")
8382   (let (force raw current-handles)
8383     (cond
8384      ((null arg))
8385      ((eq arg 1) (setq raw t))
8386      ((eq arg 2) (setq raw t
8387                        force t))
8388      ((eq arg 3) (setq current-handles
8389                        (and (gnus-buffer-live-p gnus-article-buffer)
8390                             (with-current-buffer gnus-article-buffer
8391                               (prog1
8392                                   gnus-article-mime-handles
8393                                 (setq gnus-article-mime-handles nil))))))
8394      (t (setq force t)))
8395     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8396         (error "Can't edit the raw article in group nndraft:drafts"))
8397     (save-excursion
8398       (set-buffer gnus-summary-buffer)
8399       (let ((mail-parse-charset gnus-newsgroup-charset)
8400             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8401         (gnus-set-global-variables)
8402         (when (and (not force)
8403                    (gnus-group-read-only-p))
8404           (error "The current newsgroup does not support article editing"))
8405         (gnus-summary-show-article t)
8406         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8407           (with-current-buffer gnus-article-buffer
8408             (mm-enable-multibyte-mule4)))
8409         (if (equal gnus-newsgroup-name "nndraft:drafts")
8410             (setq raw t))
8411         (gnus-article-edit-article
8412          (if raw 'ignore
8413            `(lambda ()
8414               (let ((mbl mml-buffer-list))
8415                 (setq mml-buffer-list nil)
8416                 (mime-to-mml ,'current-handles)
8417                 (let ((mbl1 mml-buffer-list))
8418                   (setq mml-buffer-list mbl)
8419                   (set (make-local-variable 'mml-buffer-list) mbl1))
8420                 (make-local-hook 'kill-buffer-hook)
8421                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8422          `(lambda (no-highlight)
8423             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8424                   (message-options message-options)
8425                   (message-options-set-recipient)
8426                   (mail-parse-ignored-charsets
8427                    ',gnus-newsgroup-ignored-charsets))
8428               ,(if (not raw) '(progn
8429                                 (mml-to-mime)
8430                                 (mml-destroy-buffers)
8431                                 (remove-hook 'kill-buffer-hook
8432                                              'mml-destroy-buffers t)
8433                                 (kill-local-variable 'mml-buffer-list)))
8434               (gnus-summary-edit-article-done
8435                ,(or (mail-header-references gnus-current-headers) "")
8436                ,(gnus-group-read-only-p)
8437                ,gnus-summary-buffer no-highlight))))))))
8438
8439 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8440
8441 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8442                                                  no-highlight)
8443   "Make edits to the current article permanent."
8444   (interactive)
8445   (save-excursion
8446    ;; The buffer restriction contains the entire article if it exists.
8447     (when (article-goto-body)
8448       (let ((lines (count-lines (point) (point-max)))
8449             (length (- (point-max) (point)))
8450             (case-fold-search t)
8451             (body (copy-marker (point))))
8452         (goto-char (point-min))
8453         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8454           (delete-region (match-beginning 1) (match-end 1))
8455           (insert (number-to-string length)))
8456         (goto-char (point-min))
8457         (when (re-search-forward
8458                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8459           (delete-region (match-beginning 1) (match-end 1))
8460           (insert (number-to-string length)))
8461         (goto-char (point-min))
8462         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8463           (delete-region (match-beginning 1) (match-end 1))
8464           (insert (number-to-string lines))))))
8465   ;; Replace the article.
8466   (let ((buf (current-buffer)))
8467     (with-temp-buffer
8468       (insert-buffer-substring buf)
8469
8470       (if (and (not read-only)
8471                (not (gnus-request-replace-article
8472                      (cdr gnus-article-current) (car gnus-article-current)
8473                      (current-buffer) t)))
8474           (error "Couldn't replace article")
8475         ;; Update the summary buffer.
8476         (if (and references
8477                  (equal (message-tokenize-header references " ")
8478                         (message-tokenize-header
8479                          (or (message-fetch-field "references") "") " ")))
8480             ;; We only have to update this line.
8481             (save-excursion
8482               (save-restriction
8483                 (message-narrow-to-head)
8484                 (let ((head (buffer-string))
8485                       header)
8486                   (with-temp-buffer
8487                     (insert (format "211 %d Article retrieved.\n"
8488                                     (cdr gnus-article-current)))
8489                     (insert head)
8490                     (insert ".\n")
8491                     (let ((nntp-server-buffer (current-buffer)))
8492                       (setq header (car (gnus-get-newsgroup-headers
8493                                          (save-excursion
8494                                            (set-buffer gnus-summary-buffer)
8495                                            gnus-newsgroup-dependencies)
8496                                          t))))
8497                     (save-excursion
8498                       (set-buffer gnus-summary-buffer)
8499                       (gnus-data-set-header
8500                        (gnus-data-find (cdr gnus-article-current))
8501                        header)
8502                       (gnus-summary-update-article-line
8503                        (cdr gnus-article-current) header)
8504                       (if (gnus-summary-goto-subject
8505                            (cdr gnus-article-current) nil t)
8506                           (gnus-summary-update-secondary-mark
8507                            (cdr gnus-article-current))))))))
8508           ;; Update threads.
8509           (set-buffer (or buffer gnus-summary-buffer))
8510           (gnus-summary-update-article (cdr gnus-article-current))
8511           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8512               (gnus-summary-update-secondary-mark
8513                (cdr gnus-article-current))))
8514         ;; Prettify the article buffer again.
8515         (unless no-highlight
8516           (save-excursion
8517             (set-buffer gnus-article-buffer)
8518             ;;;!!! Fix this -- article should be rehighlighted.
8519             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8520             (set-buffer gnus-original-article-buffer)
8521             (gnus-request-article
8522              (cdr gnus-article-current)
8523              (car gnus-article-current) (current-buffer))))
8524         ;; Prettify the summary buffer line.
8525         (when (gnus-visual-p 'summary-highlight 'highlight)
8526           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8527
8528 (defun gnus-summary-edit-wash (key)
8529   "Perform editing command KEY in the article buffer."
8530   (interactive
8531    (list
8532     (progn
8533       (message "%s" (concat (this-command-keys) "- "))
8534       (read-char))))
8535   (message "")
8536   (gnus-summary-edit-article)
8537   (execute-kbd-macro (concat (this-command-keys) key))
8538   (gnus-article-edit-done))
8539
8540 ;;; Respooling
8541
8542 (defun gnus-summary-respool-query (&optional silent trace)
8543   "Query where the respool algorithm would put this article."
8544   (interactive)
8545   (let (gnus-mark-article-hook)
8546     (gnus-summary-select-article)
8547     (save-excursion
8548       (set-buffer gnus-original-article-buffer)
8549       (save-restriction
8550         (message-narrow-to-head)
8551         (let ((groups (nnmail-article-group 'identity trace)))
8552           (unless silent
8553             (if groups
8554                 (message "This message would go to %s"
8555                          (mapconcat 'car groups ", "))
8556               (message "This message would go to no groups"))
8557             groups))))))
8558
8559 (defun gnus-summary-respool-trace ()
8560   "Trace where the respool algorithm would put this article.
8561 Display a buffer showing all fancy splitting patterns which matched."
8562   (interactive)
8563   (gnus-summary-respool-query nil t))
8564
8565 ;; Summary marking commands.
8566
8567 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8568   "Mark articles which has the same subject as read, and then select the next.
8569 If UNMARK is positive, remove any kind of mark.
8570 If UNMARK is negative, tick articles."
8571   (interactive "P")
8572   (when unmark
8573     (setq unmark (prefix-numeric-value unmark)))
8574   (let ((count
8575          (gnus-summary-mark-same-subject
8576           (gnus-summary-article-subject) unmark)))
8577     ;; Select next unread article.  If auto-select-same mode, should
8578     ;; select the first unread article.
8579     (gnus-summary-next-article t (and gnus-auto-select-same
8580                                       (gnus-summary-article-subject)))
8581     (gnus-message 7 "%d article%s marked as %s"
8582                   count (if (= count 1) " is" "s are")
8583                   (if unmark "unread" "read"))))
8584
8585 (defun gnus-summary-kill-same-subject (&optional unmark)
8586   "Mark articles which has the same subject as read.
8587 If UNMARK is positive, remove any kind of mark.
8588 If UNMARK is negative, tick articles."
8589   (interactive "P")
8590   (when unmark
8591     (setq unmark (prefix-numeric-value unmark)))
8592   (let ((count
8593          (gnus-summary-mark-same-subject
8594           (gnus-summary-article-subject) unmark)))
8595     ;; If marked as read, go to next unread subject.
8596     (when (null unmark)
8597       ;; Go to next unread subject.
8598       (gnus-summary-next-subject 1 t))
8599     (gnus-message 7 "%d articles are marked as %s"
8600                   count (if unmark "unread" "read"))))
8601
8602 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8603   "Mark articles with same SUBJECT as read, and return marked number.
8604 If optional argument UNMARK is positive, remove any kinds of marks.
8605 If optional argument UNMARK is negative, mark articles as unread instead."
8606   (let ((count 1))
8607     (save-excursion
8608       (cond
8609        ((null unmark)                   ; Mark as read.
8610         (while (and
8611                 (progn
8612                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8613                   (gnus-summary-show-thread) t)
8614                 (gnus-summary-find-subject subject))
8615           (setq count (1+ count))))
8616        ((> unmark 0)                    ; Tick.
8617         (while (and
8618                 (progn
8619                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8620                   (gnus-summary-show-thread) t)
8621                 (gnus-summary-find-subject subject))
8622           (setq count (1+ count))))
8623        (t                               ; Mark as unread.
8624         (while (and
8625                 (progn
8626                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8627                   (gnus-summary-show-thread) t)
8628                 (gnus-summary-find-subject subject))
8629           (setq count (1+ count)))))
8630       (gnus-set-mode-line 'summary)
8631       ;; Return the number of marked articles.
8632       count)))
8633
8634 (defun gnus-summary-mark-as-processable (n &optional unmark)
8635   "Set the process mark on the next N articles.
8636 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8637 the process mark instead.  The difference between N and the actual
8638 number of articles marked is returned."
8639   (interactive "P")
8640   (if (and (null n) (gnus-region-active-p))
8641       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8642     (setq n (prefix-numeric-value n))
8643     (let ((backward (< n 0))
8644           (n (abs n)))
8645       (while (and
8646               (> n 0)
8647               (if unmark
8648                   (gnus-summary-remove-process-mark
8649                    (gnus-summary-article-number))
8650                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8651               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8652         (setq n (1- n)))
8653       (when (/= 0 n)
8654         (gnus-message 7 "No more articles"))
8655       (gnus-summary-recenter)
8656       (gnus-summary-position-point)
8657       n)))
8658
8659 (defun gnus-summary-unmark-as-processable (n)
8660   "Remove the process mark from the next N articles.
8661 If N is negative, unmark backward instead.  The difference between N and
8662 the actual number of articles unmarked is returned."
8663   (interactive "P")
8664   (gnus-summary-mark-as-processable n t))
8665
8666 (defun gnus-summary-unmark-all-processable ()
8667   "Remove the process mark from all articles."
8668   (interactive)
8669   (save-excursion
8670     (while gnus-newsgroup-processable
8671       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8672   (gnus-summary-position-point))
8673
8674 (defun gnus-summary-add-mark (article type)
8675   "Mark ARTICLE with a mark of TYPE."
8676   (let ((vtype (car (assq type gnus-article-mark-lists)))
8677         var)
8678     (if (not vtype)
8679         (error "No such mark type: %s" type)
8680       (setq var (intern (format "gnus-newsgroup-%s" type)))
8681       (set var (cons article (symbol-value var)))
8682       (if (memq type '(processable cached replied forwarded recent saved))
8683           (gnus-summary-update-secondary-mark article)
8684         ;;; !!! This is bogus.  We should find out what primary
8685         ;;; !!! mark we want to set.
8686         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8687
8688 (defun gnus-summary-mark-as-expirable (n)
8689   "Mark N articles forward as expirable.
8690 If N is negative, mark backward instead.  The difference between N and
8691 the actual number of articles marked is returned."
8692   (interactive "p")
8693   (gnus-summary-mark-forward n gnus-expirable-mark))
8694
8695 (defun gnus-summary-mark-article-as-replied (article)
8696   "Mark ARTICLE as replied to and update the summary line.
8697 ARTICLE can also be a list of articles."
8698   (interactive (list (gnus-summary-article-number)))
8699   (let ((articles (if (listp article) article (list article))))
8700     (dolist (article articles)
8701       (push article gnus-newsgroup-replied)
8702       (let ((buffer-read-only nil))
8703         (when (gnus-summary-goto-subject article nil t)
8704           (gnus-summary-update-secondary-mark article))))))
8705
8706 (defun gnus-summary-mark-article-as-forwarded (article)
8707   "Mark ARTICLE as forwarded and update the summary line.
8708 ARTICLE can also be a list of articles."
8709   (let ((articles (if (listp article) article (list article))))
8710     (dolist (article articles)
8711       (push article gnus-newsgroup-forwarded)
8712       (let ((buffer-read-only nil))
8713         (when (gnus-summary-goto-subject article nil t)
8714           (gnus-summary-update-secondary-mark article))))))
8715
8716 (defun gnus-summary-set-bookmark (article)
8717   "Set a bookmark in current article."
8718   (interactive (list (gnus-summary-article-number)))
8719   (when (or (not (get-buffer gnus-article-buffer))
8720             (not gnus-current-article)
8721             (not gnus-article-current)
8722             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8723     (error "No current article selected"))
8724   ;; Remove old bookmark, if one exists.
8725   (let ((old (assq article gnus-newsgroup-bookmarks)))
8726     (when old
8727       (setq gnus-newsgroup-bookmarks
8728             (delq old gnus-newsgroup-bookmarks))))
8729   ;; Set the new bookmark, which is on the form
8730   ;; (article-number . line-number-in-body).
8731   (push
8732    (cons article
8733          (save-excursion
8734            (set-buffer gnus-article-buffer)
8735            (count-lines
8736             (min (point)
8737                  (save-excursion
8738                    (goto-char (point-min))
8739                    (search-forward "\n\n" nil t)
8740                    (point)))
8741             (point))))
8742    gnus-newsgroup-bookmarks)
8743   (gnus-message 6 "A bookmark has been added to the current article."))
8744
8745 (defun gnus-summary-remove-bookmark (article)
8746   "Remove the bookmark from the current article."
8747   (interactive (list (gnus-summary-article-number)))
8748   ;; Remove old bookmark, if one exists.
8749   (let ((old (assq article gnus-newsgroup-bookmarks)))
8750     (if old
8751         (progn
8752           (setq gnus-newsgroup-bookmarks
8753                 (delq old gnus-newsgroup-bookmarks))
8754           (gnus-message 6 "Removed bookmark."))
8755       (gnus-message 6 "No bookmark in current article."))))
8756
8757 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8758 (defun gnus-summary-mark-as-dormant (n)
8759   "Mark N articles forward as dormant.
8760 If N is negative, mark backward instead.  The difference between N and
8761 the actual number of articles marked is returned."
8762   (interactive "p")
8763   (gnus-summary-mark-forward n gnus-dormant-mark))
8764
8765 (defun gnus-summary-set-process-mark (article)
8766   "Set the process mark on ARTICLE and update the summary line."
8767   (setq gnus-newsgroup-processable
8768         (cons article
8769               (delq article gnus-newsgroup-processable)))
8770   (when (gnus-summary-goto-subject article)
8771     (gnus-summary-show-thread)
8772     (gnus-summary-goto-subject article)
8773     (gnus-summary-update-secondary-mark article)))
8774
8775 (defun gnus-summary-remove-process-mark (article)
8776   "Remove the process mark from ARTICLE and update the summary line."
8777   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8778   (when (gnus-summary-goto-subject article)
8779     (gnus-summary-show-thread)
8780     (gnus-summary-goto-subject article)
8781     (gnus-summary-update-secondary-mark article)))
8782
8783 (defun gnus-summary-set-saved-mark (article)
8784   "Set the process mark on ARTICLE and update the summary line."
8785   (push article gnus-newsgroup-saved)
8786   (when (gnus-summary-goto-subject article)
8787     (gnus-summary-update-secondary-mark article)))
8788
8789 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8790   "Mark N articles as read forwards.
8791 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8792 The difference between N and the actual number of articles marked is
8793 returned.
8794 Iff NO-EXPIRE, auto-expiry will be inhibited."
8795   (interactive "p")
8796   (gnus-summary-show-thread)
8797   (let ((backward (< n 0))
8798         (gnus-summary-goto-unread
8799          (and gnus-summary-goto-unread
8800               (not (eq gnus-summary-goto-unread 'never))
8801               (not (memq mark (list gnus-unread-mark
8802                                     gnus-ticked-mark gnus-dormant-mark)))))
8803         (n (abs n))
8804         (mark (or mark gnus-del-mark)))
8805     (while (and (> n 0)
8806                 (gnus-summary-mark-article nil mark no-expire)
8807                 (zerop (gnus-summary-next-subject
8808                         (if backward -1 1)
8809                         (and gnus-summary-goto-unread
8810                              (not (eq gnus-summary-goto-unread 'never)))
8811                         t)))
8812       (setq n (1- n)))
8813     (when (/= 0 n)
8814       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8815     (gnus-summary-recenter)
8816     (gnus-summary-position-point)
8817     (gnus-set-mode-line 'summary)
8818     n))
8819
8820 (defun gnus-summary-mark-article-as-read (mark)
8821   "Mark the current article quickly as read with MARK."
8822   (let ((article (gnus-summary-article-number)))
8823     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8824     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8825     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8826     (push (cons article mark) gnus-newsgroup-reads)
8827     ;; Possibly remove from cache, if that is used.
8828     (when gnus-use-cache
8829       (gnus-cache-enter-remove-article article))
8830     ;; Allow the backend to change the mark.
8831     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8832     ;; Check for auto-expiry.
8833     (when (and gnus-newsgroup-auto-expire
8834                (memq mark gnus-auto-expirable-marks))
8835       (setq mark gnus-expirable-mark)
8836       ;; Let the backend know about the mark change.
8837       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8838       (push article gnus-newsgroup-expirable))
8839     ;; Set the mark in the buffer.
8840     (gnus-summary-update-mark mark 'unread)
8841     t))
8842
8843 (defun gnus-summary-mark-article-as-unread (mark)
8844   "Mark the current article quickly as unread with MARK."
8845   (let* ((article (gnus-summary-article-number))
8846          (old-mark (gnus-summary-article-mark article)))
8847     ;; Allow the backend to change the mark.
8848     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8849     (if (eq mark old-mark)
8850         t
8851       (if (<= article 0)
8852           (progn
8853             (gnus-error 1 "Can't mark negative article numbers")
8854             nil)
8855         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8856         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8857         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8858         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8859         (cond ((= mark gnus-ticked-mark)
8860                (push article gnus-newsgroup-marked))
8861               ((= mark gnus-dormant-mark)
8862                (push article gnus-newsgroup-dormant))
8863               (t
8864                (push article gnus-newsgroup-unreads)))
8865         (gnus-pull article gnus-newsgroup-reads)
8866
8867         ;; See whether the article is to be put in the cache.
8868         (and gnus-use-cache
8869              (vectorp (gnus-summary-article-header article))
8870              (save-excursion
8871                (gnus-cache-possibly-enter-article
8872                 gnus-newsgroup-name article
8873                 (= mark gnus-ticked-mark)
8874                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8875
8876         ;; Fix the mark.
8877         (gnus-summary-update-mark mark 'unread)
8878         t))))
8879
8880 (defun gnus-summary-mark-article (&optional article mark no-expire)
8881   "Mark ARTICLE with MARK.  MARK can be any character.
8882 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8883 `??' (dormant) and `?E' (expirable).
8884 If MARK is nil, then the default character `?r' is used.
8885 If ARTICLE is nil, then the article on the current line will be
8886 marked.
8887 Iff NO-EXPIRE, auto-expiry will be inhibited."
8888   ;; The mark might be a string.
8889   (when (stringp mark)
8890     (setq mark (aref mark 0)))
8891   ;; If no mark is given, then we check auto-expiring.
8892   (when (null mark)
8893     (setq mark gnus-del-mark))
8894   (when (and (not no-expire)
8895              gnus-newsgroup-auto-expire
8896              (memq mark gnus-auto-expirable-marks))
8897     (setq mark gnus-expirable-mark))
8898   (let ((article (or article (gnus-summary-article-number)))
8899         (old-mark (gnus-summary-article-mark article)))
8900     ;; Allow the backend to change the mark.
8901     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8902     (if (eq mark old-mark)
8903         t
8904       (unless article
8905         (error "No article on current line"))
8906       (if (not (if (or (= mark gnus-unread-mark)
8907                        (= mark gnus-ticked-mark)
8908                        (= mark gnus-dormant-mark))
8909                    (gnus-mark-article-as-unread article mark)
8910                  (gnus-mark-article-as-read article mark)))
8911           t
8912         ;; See whether the article is to be put in the cache.
8913         (and gnus-use-cache
8914              (not (= mark gnus-canceled-mark))
8915              (vectorp (gnus-summary-article-header article))
8916              (save-excursion
8917                (gnus-cache-possibly-enter-article
8918                 gnus-newsgroup-name article
8919                 (= mark gnus-ticked-mark)
8920                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8921
8922         (when (gnus-summary-goto-subject article nil t)
8923           (let ((buffer-read-only nil))
8924             (gnus-summary-show-thread)
8925             ;; Fix the mark.
8926             (gnus-summary-update-mark mark 'unread)
8927             t))))))
8928
8929 (defun gnus-summary-update-secondary-mark (article)
8930   "Update the secondary (read, process, cache) mark."
8931   (gnus-summary-update-mark
8932    (cond ((memq article gnus-newsgroup-processable)
8933           gnus-process-mark)
8934          ((memq article gnus-newsgroup-cached)
8935           gnus-cached-mark)
8936          ((memq article gnus-newsgroup-replied)
8937           gnus-replied-mark)
8938          ((memq article gnus-newsgroup-forwarded)
8939           gnus-forwarded-mark)
8940          ((memq article gnus-newsgroup-saved)
8941           gnus-saved-mark)
8942          ((memq article gnus-newsgroup-recent)
8943           gnus-recent-mark)
8944          (t gnus-no-mark))
8945    'replied)
8946   (when (gnus-visual-p 'summary-highlight 'highlight)
8947     (gnus-run-hooks 'gnus-summary-update-hook))
8948   t)
8949
8950 (defun gnus-summary-update-mark (mark type)
8951   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8952         (buffer-read-only nil))
8953     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8954     (when forward
8955       (when (looking-at "\r")
8956         (incf forward))
8957       (when (<= (+ forward (point)) (point-max))
8958         ;; Go to the right position on the line.
8959         (goto-char (+ forward (point)))
8960         ;; Replace the old mark with the new mark.
8961         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8962         ;; Optionally update the marks by some user rule.
8963         (when (eq type 'unread)
8964           (gnus-data-set-mark
8965            (gnus-data-find (gnus-summary-article-number)) mark)
8966           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8967
8968 (defun gnus-mark-article-as-read (article &optional mark)
8969   "Enter ARTICLE in the pertinent lists and remove it from others."
8970   ;; Make the article expirable.
8971   (let ((mark (or mark gnus-del-mark)))
8972     (if (= mark gnus-expirable-mark)
8973         (push article gnus-newsgroup-expirable)
8974       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8975     ;; Remove from unread and marked lists.
8976     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8977     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8978     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8979     (push (cons article mark) gnus-newsgroup-reads)
8980     ;; Possibly remove from cache, if that is used.
8981     (when gnus-use-cache
8982       (gnus-cache-enter-remove-article article))
8983     t))
8984
8985 (defun gnus-mark-article-as-unread (article &optional mark)
8986   "Enter ARTICLE in the pertinent lists and remove it from others."
8987   (let ((mark (or mark gnus-ticked-mark)))
8988     (if (<= article 0)
8989         (progn
8990           (gnus-error 1 "Can't mark negative article numbers")
8991           nil)
8992       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8993             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8994             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8995             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8996
8997       ;; Unsuppress duplicates?
8998       (when gnus-suppress-duplicates
8999         (gnus-dup-unsuppress-article article))
9000
9001       (cond ((= mark gnus-ticked-mark)
9002              (push article gnus-newsgroup-marked))
9003             ((= mark gnus-dormant-mark)
9004              (push article gnus-newsgroup-dormant))
9005             (t
9006              (push article gnus-newsgroup-unreads)))
9007       (gnus-pull article gnus-newsgroup-reads)
9008       t)))
9009
9010 (defalias 'gnus-summary-mark-as-unread-forward
9011   'gnus-summary-tick-article-forward)
9012 (make-obsolete 'gnus-summary-mark-as-unread-forward
9013                'gnus-summary-tick-article-forward)
9014 (defun gnus-summary-tick-article-forward (n)
9015   "Tick N articles forwards.
9016 If N is negative, tick backwards instead.
9017 The difference between N and the number of articles ticked is returned."
9018   (interactive "p")
9019   (gnus-summary-mark-forward n gnus-ticked-mark))
9020
9021 (defalias 'gnus-summary-mark-as-unread-backward
9022   'gnus-summary-tick-article-backward)
9023 (make-obsolete 'gnus-summary-mark-as-unread-backward
9024                'gnus-summary-tick-article-backward)
9025 (defun gnus-summary-tick-article-backward (n)
9026   "Tick N articles backwards.
9027 The difference between N and the number of articles ticked is returned."
9028   (interactive "p")
9029   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9030
9031 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9032 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9033 (defun gnus-summary-tick-article (&optional article clear-mark)
9034   "Mark current article as unread.
9035 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9036 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9037   (interactive)
9038   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9039                                        gnus-ticked-mark)))
9040
9041 (defun gnus-summary-mark-as-read-forward (n)
9042   "Mark N articles as read forwards.
9043 If N is negative, mark backwards instead.
9044 The difference between N and the actual number of articles marked is
9045 returned."
9046   (interactive "p")
9047   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9048
9049 (defun gnus-summary-mark-as-read-backward (n)
9050   "Mark the N articles as read backwards.
9051 The difference between N and the actual number of articles marked is
9052 returned."
9053   (interactive "p")
9054   (gnus-summary-mark-forward
9055    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9056
9057 (defun gnus-summary-mark-as-read (&optional article mark)
9058   "Mark current article as read.
9059 ARTICLE specifies the article to be marked as read.
9060 MARK specifies a string to be inserted at the beginning of the line."
9061   (gnus-summary-mark-article article mark))
9062
9063 (defun gnus-summary-clear-mark-forward (n)
9064   "Clear marks from N articles forward.
9065 If N is negative, clear backward instead.
9066 The difference between N and the number of marks cleared is returned."
9067   (interactive "p")
9068   (gnus-summary-mark-forward n gnus-unread-mark))
9069
9070 (defun gnus-summary-clear-mark-backward (n)
9071   "Clear marks from N articles backward.
9072 The difference between N and the number of marks cleared is returned."
9073   (interactive "p")
9074   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9075
9076 (defun gnus-summary-mark-unread-as-read ()
9077   "Intended to be used by `gnus-summary-mark-article-hook'."
9078   (when (memq gnus-current-article gnus-newsgroup-unreads)
9079     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9080
9081 (defun gnus-summary-mark-read-and-unread-as-read ()
9082   "Intended to be used by `gnus-summary-mark-article-hook'."
9083   (let ((mark (gnus-summary-article-mark)))
9084     (when (or (gnus-unread-mark-p mark)
9085               (gnus-read-mark-p mark))
9086       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9087
9088 (defun gnus-summary-mark-unread-as-ticked ()
9089   "Intended to be used by `gnus-summary-mark-article-hook'."
9090   (when (memq gnus-current-article gnus-newsgroup-unreads)
9091     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9092
9093 (defun gnus-summary-mark-region-as-read (point mark all)
9094   "Mark all unread articles between point and mark as read.
9095 If given a prefix, mark all articles between point and mark as read,
9096 even ticked and dormant ones."
9097   (interactive "r\nP")
9098   (save-excursion
9099     (let (article)
9100       (goto-char point)
9101       (beginning-of-line)
9102       (while (and
9103               (< (point) mark)
9104               (progn
9105                 (when (or all
9106                           (memq (setq article (gnus-summary-article-number))
9107                                 gnus-newsgroup-unreads))
9108                   (gnus-summary-mark-article article gnus-del-mark))
9109                 t)
9110               (gnus-summary-find-next))))))
9111
9112 (defun gnus-summary-mark-below (score mark)
9113   "Mark articles with score less than SCORE with MARK."
9114   (interactive "P\ncMark: ")
9115   (setq score (if score
9116                   (prefix-numeric-value score)
9117                 (or gnus-summary-default-score 0)))
9118   (save-excursion
9119     (set-buffer gnus-summary-buffer)
9120     (goto-char (point-min))
9121     (while
9122         (progn
9123           (and (< (gnus-summary-article-score) score)
9124                (gnus-summary-mark-article nil mark))
9125           (gnus-summary-find-next)))))
9126
9127 (defun gnus-summary-kill-below (&optional score)
9128   "Mark articles with score below SCORE as read."
9129   (interactive "P")
9130   (gnus-summary-mark-below score gnus-killed-mark))
9131
9132 (defun gnus-summary-clear-above (&optional score)
9133   "Clear all marks from articles with score above SCORE."
9134   (interactive "P")
9135   (gnus-summary-mark-above score gnus-unread-mark))
9136
9137 (defun gnus-summary-tick-above (&optional score)
9138   "Tick all articles with score above SCORE."
9139   (interactive "P")
9140   (gnus-summary-mark-above score gnus-ticked-mark))
9141
9142 (defun gnus-summary-mark-above (score mark)
9143   "Mark articles with score over SCORE with MARK."
9144   (interactive "P\ncMark: ")
9145   (setq score (if score
9146                   (prefix-numeric-value score)
9147                 (or gnus-summary-default-score 0)))
9148   (save-excursion
9149     (set-buffer gnus-summary-buffer)
9150     (goto-char (point-min))
9151     (while (and (progn
9152                   (when (> (gnus-summary-article-score) score)
9153                     (gnus-summary-mark-article nil mark))
9154                   t)
9155                 (gnus-summary-find-next)))))
9156
9157 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9158 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9159 (defun gnus-summary-limit-include-expunged (&optional no-error)
9160   "Display all the hidden articles that were expunged for low scores."
9161   (interactive)
9162   (let ((buffer-read-only nil))
9163     (let ((scored gnus-newsgroup-scored)
9164           headers h)
9165       (while scored
9166         (unless (gnus-summary-article-header (caar scored))
9167           (and (setq h (gnus-number-to-header (caar scored)))
9168                (< (cdar scored) gnus-summary-expunge-below)
9169                (push h headers)))
9170         (setq scored (cdr scored)))
9171       (if (not headers)
9172           (when (not no-error)
9173             (error "No expunged articles hidden"))
9174         (goto-char (point-min))
9175         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9176         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9177         (mapcar (lambda (x) (push (mail-header-number x)
9178                                   gnus-newsgroup-limit))
9179                 headers)
9180         (gnus-summary-prepare-unthreaded (nreverse headers))
9181         (goto-char (point-min))
9182         (gnus-summary-position-point)
9183         t))))
9184
9185 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9186   "Mark all unread articles in this newsgroup as read.
9187 If prefix argument ALL is non-nil, ticked and dormant articles will
9188 also be marked as read.
9189 If QUIETLY is non-nil, no questions will be asked.
9190 If TO-HERE is non-nil, it should be a point in the buffer.  All
9191 articles before (after, if REVERSE is set) this point will be marked as read.
9192 Note that this function will only catch up the unread article
9193 in the current summary buffer limitation.
9194 The number of articles marked as read is returned."
9195   (interactive "P")
9196   (prog1
9197       (save-excursion
9198         (when (or quietly
9199                   (not gnus-interactive-catchup) ;Without confirmation?
9200                   gnus-expert-user
9201                   (gnus-y-or-n-p
9202                    (if all
9203                        "Mark absolutely all articles as read? "
9204                      "Mark all unread articles as read? ")))
9205           (if (and not-mark
9206                    (not gnus-newsgroup-adaptive)
9207                    (not gnus-newsgroup-auto-expire)
9208                    (not gnus-suppress-duplicates)
9209                    (or (not gnus-use-cache)
9210                        (eq gnus-use-cache 'passive)))
9211               (progn
9212                 (when all
9213                   (setq gnus-newsgroup-marked nil
9214                         gnus-newsgroup-dormant nil))
9215                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9216             ;; We actually mark all articles as canceled, which we
9217             ;; have to do when using auto-expiry or adaptive scoring.
9218             (gnus-summary-show-all-threads)
9219             (if (and to-here reverse)
9220                 (progn
9221                   (goto-char to-here)
9222                   (while (and
9223                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9224                           (gnus-summary-find-next (not all) nil nil t))))
9225               (when (gnus-summary-first-subject (not all) t)
9226                 (while (and
9227                         (if to-here (< (point) to-here) t)
9228                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9229                         (gnus-summary-find-next (not all) nil nil t)))))
9230             (gnus-set-mode-line 'summary))
9231           t))
9232     (gnus-summary-position-point)))
9233
9234 (defun gnus-summary-catchup-to-here (&optional all)
9235   "Mark all unticked articles before the current one as read.
9236 If ALL is non-nil, also mark ticked and dormant articles as read."
9237   (interactive "P")
9238   (save-excursion
9239     (gnus-save-hidden-threads
9240       (let ((beg (point)))
9241         ;; We check that there are unread articles.
9242         (when (or all (gnus-summary-find-prev))
9243           (gnus-summary-catchup all t beg)))))
9244   (gnus-summary-position-point))
9245
9246 (defun gnus-summary-catchup-from-here (&optional all)
9247   "Mark all unticked articles after the current one as read.
9248 If ALL is non-nil, also mark ticked and dormant articles as read."
9249   (interactive "P")
9250   (save-excursion
9251     (gnus-save-hidden-threads
9252       (let ((beg (point)))
9253         ;; We check that there are unread articles.
9254         (when (or all (gnus-summary-find-next))
9255           (gnus-summary-catchup all t beg nil t)))))
9256
9257   (gnus-summary-position-point))
9258 (defun gnus-summary-catchup-all (&optional quietly)
9259   "Mark all articles in this newsgroup as read."
9260   (interactive "P")
9261   (gnus-summary-catchup t quietly))
9262
9263 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9264   "Mark all unread articles in this group as read, then exit.
9265 If prefix argument ALL is non-nil, all articles are marked as read.
9266 If QUIETLY is non-nil, no questions will be asked."
9267   (interactive "P")
9268   (when (gnus-summary-catchup all quietly nil 'fast)
9269     ;; Select next newsgroup or exit.
9270     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9271              (eq gnus-auto-select-next 'quietly))
9272         (gnus-summary-next-group nil)
9273       (gnus-summary-exit))))
9274
9275 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9276   "Mark all articles in this newsgroup as read, and then exit."
9277   (interactive "P")
9278   (gnus-summary-catchup-and-exit t quietly))
9279
9280 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9281   "Mark all articles in this group as read and select the next group.
9282 If given a prefix, mark all articles, unread as well as ticked, as
9283 read."
9284   (interactive "P")
9285   (save-excursion
9286     (gnus-summary-catchup all))
9287   (gnus-summary-next-group))
9288
9289 ;;;
9290 ;;; with article
9291 ;;;
9292
9293 (defmacro gnus-with-article (article &rest forms)
9294   "Select ARTICLE and perform FORMS in the original article buffer.
9295 Then replace the article with the result."
9296   `(progn
9297      ;; We don't want the article to be marked as read.
9298      (let (gnus-mark-article-hook)
9299        (gnus-summary-select-article t t nil ,article))
9300      (set-buffer gnus-original-article-buffer)
9301      ,@forms
9302      (if (not (gnus-check-backend-function
9303                'request-replace-article (car gnus-article-current)))
9304          (gnus-message 5 "Read-only group; not replacing")
9305        (unless (gnus-request-replace-article
9306                 ,article (car gnus-article-current)
9307                 (current-buffer) t)
9308          (error "Couldn't replace article")))
9309      ;; The cache and backlog have to be flushed somewhat.
9310      (when gnus-keep-backlog
9311        (gnus-backlog-remove-article
9312         (car gnus-article-current) (cdr gnus-article-current)))
9313      (when gnus-use-cache
9314        (gnus-cache-update-article
9315         (car gnus-article-current) (cdr gnus-article-current)))))
9316
9317 (put 'gnus-with-article 'lisp-indent-function 1)
9318 (put 'gnus-with-article 'edebug-form-spec '(form body))
9319
9320 ;; Thread-based commands.
9321
9322 (defun gnus-summary-articles-in-thread (&optional article)
9323   "Return a list of all articles in the current thread.
9324 If ARTICLE is non-nil, return all articles in the thread that starts
9325 with that article."
9326   (let* ((article (or article (gnus-summary-article-number)))
9327          (data (gnus-data-find-list article))
9328          (top-level (gnus-data-level (car data)))
9329          (top-subject
9330           (cond ((null gnus-thread-operation-ignore-subject)
9331                  (gnus-simplify-subject-re
9332                   (mail-header-subject (gnus-data-header (car data)))))
9333                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9334                  (gnus-simplify-subject-fuzzy
9335                   (mail-header-subject (gnus-data-header (car data)))))
9336                 (t nil)))
9337          (end-point (save-excursion
9338                       (if (gnus-summary-go-to-next-thread)
9339                           (point) (point-max))))
9340          articles)
9341     (while (and data
9342                 (< (gnus-data-pos (car data)) end-point))
9343       (when (or (not top-subject)
9344                 (string= top-subject
9345                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9346                              (gnus-simplify-subject-fuzzy
9347                               (mail-header-subject
9348                                (gnus-data-header (car data))))
9349                            (gnus-simplify-subject-re
9350                             (mail-header-subject
9351                              (gnus-data-header (car data)))))))
9352         (push (gnus-data-number (car data)) articles))
9353       (unless (and (setq data (cdr data))
9354                    (> (gnus-data-level (car data)) top-level))
9355         (setq data nil)))
9356     ;; Return the list of articles.
9357     (nreverse articles)))
9358
9359 (defun gnus-summary-rethread-current ()
9360   "Rethread the thread the current article is part of."
9361   (interactive)
9362   (let* ((gnus-show-threads t)
9363          (article (gnus-summary-article-number))
9364          (id (mail-header-id (gnus-summary-article-header)))
9365          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9366     (unless id
9367       (error "No article on the current line"))
9368     (gnus-rebuild-thread id)
9369     (gnus-summary-goto-subject article)))
9370
9371 (defun gnus-summary-reparent-thread ()
9372   "Make the current article child of the marked (or previous) article.
9373
9374 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9375 is non-nil or the Subject: of both articles are the same."
9376   (interactive)
9377   (unless (not (gnus-group-read-only-p))
9378     (error "The current newsgroup does not support article editing"))
9379   (unless (<= (length gnus-newsgroup-processable) 1)
9380     (error "No more than one article may be marked"))
9381   (save-window-excursion
9382     (let ((gnus-article-buffer " *reparent*")
9383           (current-article (gnus-summary-article-number))
9384           ;; First grab the marked article, otherwise one line up.
9385           (parent-article (if (not (null gnus-newsgroup-processable))
9386                               (car gnus-newsgroup-processable)
9387                             (save-excursion
9388                               (if (eq (forward-line -1) 0)
9389                                   (gnus-summary-article-number)
9390                                 (error "Beginning of summary buffer"))))))
9391       (unless (not (eq current-article parent-article))
9392         (error "An article may not be self-referential"))
9393       (let ((message-id (mail-header-id
9394                          (gnus-summary-article-header parent-article))))
9395         (unless (and message-id (not (equal message-id "")))
9396           (error "No message-id in desired parent"))
9397         (gnus-with-article current-article
9398           (save-restriction
9399             (goto-char (point-min))
9400             (message-narrow-to-head)
9401             (if (re-search-forward "^References: " nil t)
9402                 (progn
9403                   (re-search-forward "^[^ \t]" nil t)
9404                   (forward-line -1)
9405                   (end-of-line)
9406                   (insert " " message-id))
9407               (insert "References: " message-id "\n"))))
9408         (set-buffer gnus-summary-buffer)
9409         (gnus-summary-unmark-all-processable)
9410         (gnus-summary-update-article current-article)
9411         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9412             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9413         (gnus-summary-rethread-current)
9414         (gnus-message 3 "Article %d is now the child of article %d"
9415                       current-article parent-article)))))
9416
9417 (defun gnus-summary-toggle-threads (&optional arg)
9418   "Toggle showing conversation threads.
9419 If ARG is positive number, turn showing conversation threads on."
9420   (interactive "P")
9421   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9422     (setq gnus-show-threads
9423           (if (null arg) (not gnus-show-threads)
9424             (> (prefix-numeric-value arg) 0)))
9425     (gnus-summary-prepare)
9426     (gnus-summary-goto-subject current)
9427     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9428     (gnus-summary-position-point)))
9429
9430 (defun gnus-summary-show-all-threads ()
9431   "Show all threads."
9432   (interactive)
9433   (save-excursion
9434     (let ((buffer-read-only nil))
9435       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9436   (gnus-summary-position-point))
9437
9438 (defun gnus-summary-show-thread ()
9439   "Show thread subtrees.
9440 Returns nil if no thread was there to be shown."
9441   (interactive)
9442   (let ((buffer-read-only nil)
9443         (orig (point))
9444         ;; first goto end then to beg, to have point at beg after let
9445         (end (progn (end-of-line) (point)))
9446         (beg (progn (beginning-of-line) (point))))
9447     (prog1
9448         ;; Any hidden lines here?
9449         (search-forward "\r" end t)
9450       (subst-char-in-region beg end ?\^M ?\n t)
9451       (goto-char orig)
9452       (gnus-summary-position-point))))
9453
9454 (defun gnus-summary-hide-all-threads ()
9455   "Hide all thread subtrees."
9456   (interactive)
9457   (save-excursion
9458     (goto-char (point-min))
9459     (gnus-summary-hide-thread)
9460     (while (zerop (gnus-summary-next-thread 1 t))
9461       (gnus-summary-hide-thread)))
9462   (gnus-summary-position-point))
9463
9464 (defun gnus-summary-hide-thread ()
9465   "Hide thread subtrees.
9466 Returns nil if no threads were there to be hidden."
9467   (interactive)
9468   (let ((buffer-read-only nil)
9469         (start (point))
9470         (article (gnus-summary-article-number)))
9471     (goto-char start)
9472     ;; Go forward until either the buffer ends or the subthread
9473     ;; ends.
9474     (when (and (not (eobp))
9475                (or (zerop (gnus-summary-next-thread 1 t))
9476                    (goto-char (point-max))))
9477       (prog1
9478           (if (and (> (point) start)
9479                    (search-backward "\n" start t))
9480               (progn
9481                 (subst-char-in-region start (point) ?\n ?\^M)
9482                 (gnus-summary-goto-subject article))
9483             (goto-char start)
9484             nil)))))
9485
9486 (defun gnus-summary-go-to-next-thread (&optional previous)
9487   "Go to the same level (or less) next thread.
9488 If PREVIOUS is non-nil, go to previous thread instead.
9489 Return the article number moved to, or nil if moving was impossible."
9490   (let ((level (gnus-summary-thread-level))
9491         (way (if previous -1 1))
9492         (beg (point)))
9493     (forward-line way)
9494     (while (and (not (eobp))
9495                 (< level (gnus-summary-thread-level)))
9496       (forward-line way))
9497     (if (eobp)
9498         (progn
9499           (goto-char beg)
9500           nil)
9501       (setq beg (point))
9502       (prog1
9503           (gnus-summary-article-number)
9504         (goto-char beg)))))
9505
9506 (defun gnus-summary-next-thread (n &optional silent)
9507   "Go to the same level next N'th thread.
9508 If N is negative, search backward instead.
9509 Returns the difference between N and the number of skips actually
9510 done.
9511
9512 If SILENT, don't output messages."
9513   (interactive "p")
9514   (let ((backward (< n 0))
9515         (n (abs n)))
9516     (while (and (> n 0)
9517                 (gnus-summary-go-to-next-thread backward))
9518       (decf n))
9519     (unless silent
9520       (gnus-summary-position-point))
9521     (when (and (not silent) (/= 0 n))
9522       (gnus-message 7 "No more threads"))
9523     n))
9524
9525 (defun gnus-summary-prev-thread (n)
9526   "Go to the same level previous N'th thread.
9527 Returns the difference between N and the number of skips actually
9528 done."
9529   (interactive "p")
9530   (gnus-summary-next-thread (- n)))
9531
9532 (defun gnus-summary-go-down-thread ()
9533   "Go down one level in the current thread."
9534   (let ((children (gnus-summary-article-children)))
9535     (when children
9536       (gnus-summary-goto-subject (car children)))))
9537
9538 (defun gnus-summary-go-up-thread ()
9539   "Go up one level in the current thread."
9540   (let ((parent (gnus-summary-article-parent)))
9541     (when parent
9542       (gnus-summary-goto-subject parent))))
9543
9544 (defun gnus-summary-down-thread (n)
9545   "Go down thread N steps.
9546 If N is negative, go up instead.
9547 Returns the difference between N and how many steps down that were
9548 taken."
9549   (interactive "p")
9550   (let ((up (< n 0))
9551         (n (abs n)))
9552     (while (and (> n 0)
9553                 (if up (gnus-summary-go-up-thread)
9554                   (gnus-summary-go-down-thread)))
9555       (setq n (1- n)))
9556     (gnus-summary-position-point)
9557     (when (/= 0 n)
9558       (gnus-message 7 "Can't go further"))
9559     n))
9560
9561 (defun gnus-summary-up-thread (n)
9562   "Go up thread N steps.
9563 If N is negative, go down instead.
9564 Returns the difference between N and how many steps down that were
9565 taken."
9566   (interactive "p")
9567   (gnus-summary-down-thread (- n)))
9568
9569 (defun gnus-summary-top-thread ()
9570   "Go to the top of the thread."
9571   (interactive)
9572   (while (gnus-summary-go-up-thread))
9573   (gnus-summary-article-number))
9574
9575 (defun gnus-summary-kill-thread (&optional unmark)
9576   "Mark articles under current thread as read.
9577 If the prefix argument is positive, remove any kinds of marks.
9578 If the prefix argument is negative, tick articles instead."
9579   (interactive "P")
9580   (when unmark
9581     (setq unmark (prefix-numeric-value unmark)))
9582   (let ((articles (gnus-summary-articles-in-thread)))
9583     (save-excursion
9584       ;; Expand the thread.
9585       (gnus-summary-show-thread)
9586       ;; Mark all the articles.
9587       (while articles
9588         (gnus-summary-goto-subject (car articles))
9589         (cond ((null unmark)
9590                (gnus-summary-mark-article-as-read gnus-killed-mark))
9591               ((> unmark 0)
9592                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9593               (t
9594                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9595         (setq articles (cdr articles))))
9596     ;; Hide killed subtrees.
9597     (and (null unmark)
9598          gnus-thread-hide-killed
9599          (gnus-summary-hide-thread))
9600     ;; If marked as read, go to next unread subject.
9601     (when (null unmark)
9602       ;; Go to next unread subject.
9603       (gnus-summary-next-subject 1 t)))
9604   (gnus-set-mode-line 'summary))
9605
9606 ;; Summary sorting commands
9607
9608 (defun gnus-summary-sort-by-number (&optional reverse)
9609   "Sort the summary buffer by article number.
9610 Argument REVERSE means reverse order."
9611   (interactive "P")
9612   (gnus-summary-sort 'number reverse))
9613
9614 (defun gnus-summary-sort-by-author (&optional reverse)
9615   "Sort the summary buffer by author name alphabetically.
9616 If `case-fold-search' is non-nil, case of letters is ignored.
9617 Argument REVERSE means reverse order."
9618   (interactive "P")
9619   (gnus-summary-sort 'author reverse))
9620
9621 (defun gnus-summary-sort-by-subject (&optional reverse)
9622   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9623 If `case-fold-search' is non-nil, case of letters is ignored.
9624 Argument REVERSE means reverse order."
9625   (interactive "P")
9626   (gnus-summary-sort 'subject reverse))
9627
9628 (defun gnus-summary-sort-by-date (&optional reverse)
9629   "Sort the summary buffer by date.
9630 Argument REVERSE means reverse order."
9631   (interactive "P")
9632   (gnus-summary-sort 'date reverse))
9633
9634 (defun gnus-summary-sort-by-score (&optional reverse)
9635   "Sort the summary buffer by score.
9636 Argument REVERSE means reverse order."
9637   (interactive "P")
9638   (gnus-summary-sort 'score reverse))
9639
9640 (defun gnus-summary-sort-by-lines (&optional reverse)
9641   "Sort the summary buffer by the number of lines.
9642 Argument REVERSE means reverse order."
9643   (interactive "P")
9644   (gnus-summary-sort 'lines reverse))
9645
9646 (defun gnus-summary-sort-by-chars (&optional reverse)
9647   "Sort the summary buffer by article length.
9648 Argument REVERSE means reverse order."
9649   (interactive "P")
9650   (gnus-summary-sort 'chars reverse))
9651
9652 (defun gnus-summary-sort-by-original (&optional reverse)
9653   "Sort the summary buffer using the default sorting method.
9654 Argument REVERSE means reverse order."
9655   (interactive "P")
9656   (let* ((buffer-read-only)
9657          (gnus-summary-prepare-hook nil))
9658     ;; We do the sorting by regenerating the threads.
9659     (gnus-summary-prepare)
9660     ;; Hide subthreads if needed.
9661     (when (and gnus-show-threads gnus-thread-hide-subtree)
9662       (gnus-summary-hide-all-threads))))
9663
9664 (defun gnus-summary-sort (predicate reverse)
9665   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9666   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9667          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9668          (gnus-thread-sort-functions
9669           (if (not reverse)
9670               thread
9671             `(lambda (t1 t2)
9672                (,thread t2 t1))))
9673          (gnus-sort-gathered-threads-function
9674           gnus-thread-sort-functions)
9675          (gnus-article-sort-functions
9676           (if (not reverse)
9677               article
9678             `(lambda (t1 t2)
9679                (,article t2 t1))))
9680          (buffer-read-only)
9681          (gnus-summary-prepare-hook nil))
9682     ;; We do the sorting by regenerating the threads.
9683     (gnus-summary-prepare)
9684     ;; Hide subthreads if needed.
9685     (when (and gnus-show-threads gnus-thread-hide-subtree)
9686       (gnus-summary-hide-all-threads))))
9687
9688 ;; Summary saving commands.
9689
9690 (defun gnus-summary-save-article (&optional n not-saved)
9691   "Save the current article using the default saver function.
9692 If N is a positive number, save the N next articles.
9693 If N is a negative number, save the N previous articles.
9694 If N is nil and any articles have been marked with the process mark,
9695 save those articles instead.
9696 The variable `gnus-default-article-saver' specifies the saver function."
9697   (interactive "P")
9698   (let* ((articles (gnus-summary-work-articles n))
9699          (save-buffer (save-excursion
9700                         (nnheader-set-temp-buffer " *Gnus Save*")))
9701          (num (length articles))
9702          header file)
9703     (dolist (article articles)
9704       (setq header (gnus-summary-article-header article))
9705       (if (not (vectorp header))
9706           ;; This is a pseudo-article.
9707           (if (assq 'name header)
9708               (gnus-copy-file (cdr (assq 'name header)))
9709             (gnus-message 1 "Article %d is unsaveable" article))
9710         ;; This is a real article.
9711         (save-window-excursion
9712           (gnus-summary-select-article t nil nil article))
9713         (save-excursion
9714           (set-buffer save-buffer)
9715           (erase-buffer)
9716           (insert-buffer-substring gnus-original-article-buffer))
9717         (setq file (gnus-article-save save-buffer file num))
9718         (gnus-summary-remove-process-mark article)
9719         (unless not-saved
9720           (gnus-summary-set-saved-mark article))))
9721     (gnus-kill-buffer save-buffer)
9722     (gnus-summary-position-point)
9723     (gnus-set-mode-line 'summary)
9724     n))
9725
9726 (defun gnus-summary-pipe-output (&optional arg)
9727   "Pipe the current article to a subprocess.
9728 If N is a positive number, pipe the N next articles.
9729 If N is a negative number, pipe the N previous articles.
9730 If N is nil and any articles have been marked with the process mark,
9731 pipe those articles instead."
9732   (interactive "P")
9733   (require 'gnus-art)
9734   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9735     (gnus-summary-save-article arg t))
9736   (let ((buffer (get-buffer "*Shell Command Output*")))
9737     (if (and buffer
9738              (with-current-buffer buffer (> (point-max) (point-min))))
9739         (gnus-configure-windows 'pipe))))
9740
9741 (defun gnus-summary-save-article-mail (&optional arg)
9742   "Append the current article to an mail file.
9743 If N is a positive number, save the N next articles.
9744 If N is a negative number, save the N previous articles.
9745 If N is nil and any articles have been marked with the process mark,
9746 save those articles instead."
9747   (interactive "P")
9748   (require 'gnus-art)
9749   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9750     (gnus-summary-save-article arg)))
9751
9752 (defun gnus-summary-save-article-rmail (&optional arg)
9753   "Append the current article to an rmail file.
9754 If N is a positive number, save the N next articles.
9755 If N is a negative number, save the N previous articles.
9756 If N is nil and any articles have been marked with the process mark,
9757 save those articles instead."
9758   (interactive "P")
9759   (require 'gnus-art)
9760   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9761     (gnus-summary-save-article arg)))
9762
9763 (defun gnus-summary-save-article-file (&optional arg)
9764   "Append the current article to a file.
9765 If N is a positive number, save the N next articles.
9766 If N is a negative number, save the N previous articles.
9767 If N is nil and any articles have been marked with the process mark,
9768 save those articles instead."
9769   (interactive "P")
9770   (require 'gnus-art)
9771   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9772     (gnus-summary-save-article arg)))
9773
9774 (defun gnus-summary-write-article-file (&optional arg)
9775   "Write the current article to a file, deleting the previous file.
9776 If N is a positive number, save the N next articles.
9777 If N is a negative number, save the N previous articles.
9778 If N is nil and any articles have been marked with the process mark,
9779 save those articles instead."
9780   (interactive "P")
9781   (require 'gnus-art)
9782   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9783     (gnus-summary-save-article arg)))
9784
9785 (defun gnus-summary-save-article-body-file (&optional arg)
9786   "Append the current article body to a file.
9787 If N is a positive number, save the N next articles.
9788 If N is a negative number, save the N previous articles.
9789 If N is nil and any articles have been marked with the process mark,
9790 save those articles instead."
9791   (interactive "P")
9792   (require 'gnus-art)
9793   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9794     (gnus-summary-save-article arg)))
9795
9796 (defun gnus-summary-pipe-message (program)
9797   "Pipe the current article through PROGRAM."
9798   (interactive "sProgram: ")
9799   (gnus-summary-select-article)
9800   (let ((mail-header-separator ""))
9801     (gnus-eval-in-buffer-window gnus-article-buffer
9802       (save-restriction
9803         (widen)
9804         (let ((start (window-start))
9805               buffer-read-only)
9806           (message-pipe-buffer-body program)
9807           (set-window-start (get-buffer-window (current-buffer)) start))))))
9808
9809 (defun gnus-get-split-value (methods)
9810   "Return a value based on the split METHODS."
9811   (let (split-name method result match)
9812     (when methods
9813       (save-excursion
9814         (set-buffer gnus-original-article-buffer)
9815         (save-restriction
9816           (nnheader-narrow-to-headers)
9817           (while (and methods (not split-name))
9818             (goto-char (point-min))
9819             (setq method (pop methods))
9820             (setq match (car method))
9821             (when (cond
9822                    ((stringp match)
9823                     ;; Regular expression.
9824                     (ignore-errors
9825                       (re-search-forward match nil t)))
9826                    ((gnus-functionp match)
9827                     ;; Function.
9828                     (save-restriction
9829                       (widen)
9830                       (setq result (funcall match gnus-newsgroup-name))))
9831                    ((consp match)
9832                     ;; Form.
9833                     (save-restriction
9834                       (widen)
9835                       (setq result (eval match)))))
9836               (setq split-name (cdr method))
9837               (cond ((stringp result)
9838                      (push (expand-file-name
9839                             result gnus-article-save-directory)
9840                            split-name))
9841                     ((consp result)
9842                      (setq split-name (append result split-name)))))))))
9843     (nreverse split-name)))
9844
9845 (defun gnus-valid-move-group-p (group)
9846   (and (boundp group)
9847        (symbol-name group)
9848        (symbol-value group)
9849        (gnus-get-function (gnus-find-method-for-group
9850                            (symbol-name group)) 'request-accept-article t)))
9851
9852 (defun gnus-read-move-group-name (prompt default articles prefix)
9853   "Read a group name."
9854   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9855          (minibuffer-confirm-incomplete nil) ; XEmacs
9856          (prom
9857           (format "%s %s to:"
9858                   prompt
9859                   (if (> (length articles) 1)
9860                       (format "these %d articles" (length articles))
9861                     "this article")))
9862          (to-newsgroup
9863           (cond
9864            ((null split-name)
9865             (gnus-completing-read default prom
9866                                   gnus-active-hashtb
9867                                   'gnus-valid-move-group-p
9868                                   nil prefix
9869                                   'gnus-group-history))
9870            ((= 1 (length split-name))
9871             (gnus-completing-read (car split-name) prom
9872                                   gnus-active-hashtb
9873                                   'gnus-valid-move-group-p
9874                                   nil nil
9875                                   'gnus-group-history))
9876            (t
9877             (gnus-completing-read nil prom
9878                                   (mapcar (lambda (el) (list el))
9879                                           (nreverse split-name))
9880                                   nil nil nil
9881                                   'gnus-group-history))))
9882          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9883     (when to-newsgroup
9884       (if (or (string= to-newsgroup "")
9885               (string= to-newsgroup prefix))
9886           (setq to-newsgroup default))
9887       (unless to-newsgroup
9888         (error "No group name entered"))
9889       (or (gnus-active to-newsgroup)
9890           (gnus-activate-group to-newsgroup nil nil to-method)
9891           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9892                                      to-newsgroup))
9893               (or (and (gnus-request-create-group to-newsgroup to-method)
9894                        (gnus-activate-group
9895                         to-newsgroup nil nil to-method)
9896                        (gnus-subscribe-group to-newsgroup))
9897                   (error "Couldn't create group %s" to-newsgroup)))
9898           (error "No such group: %s" to-newsgroup)))
9899     to-newsgroup))
9900
9901 (defun gnus-summary-save-parts (type dir n &optional reverse)
9902   "Save parts matching TYPE to DIR.
9903 If REVERSE, save parts that do not match TYPE."
9904   (interactive
9905    (list (read-string "Save parts of type: "
9906                       (or (car gnus-summary-save-parts-type-history)
9907                           gnus-summary-save-parts-default-mime)
9908                       'gnus-summary-save-parts-type-history)
9909          (setq gnus-summary-save-parts-last-directory
9910                (read-file-name "Save to directory: "
9911                                gnus-summary-save-parts-last-directory
9912                                nil t))
9913          current-prefix-arg))
9914   (gnus-summary-iterate n
9915     (let ((gnus-display-mime-function nil)
9916           (gnus-inhibit-treatment t))
9917       (gnus-summary-select-article))
9918     (save-excursion
9919       (set-buffer gnus-article-buffer)
9920       (let ((handles (or gnus-article-mime-handles
9921                          (mm-dissect-buffer) (mm-uu-dissect))))
9922         (when handles
9923           (gnus-summary-save-parts-1 type dir handles reverse)
9924           (unless gnus-article-mime-handles ;; Don't destroy this case.
9925             (mm-destroy-parts handles)))))))
9926
9927 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9928   (if (stringp (car handle))
9929       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9930               (cdr handle))
9931     (when (if reverse
9932               (not (string-match type (mm-handle-media-type handle)))
9933             (string-match type (mm-handle-media-type handle)))
9934       (let ((file (expand-file-name
9935                    (file-name-nondirectory
9936                     (or
9937                      (mail-content-type-get
9938                       (mm-handle-disposition handle) 'filename)
9939                      (concat gnus-newsgroup-name
9940                              "." (number-to-string
9941                                   (cdr gnus-article-current)))))
9942                    dir)))
9943         (unless (file-exists-p file)
9944           (mm-save-part-to-file handle file))))))
9945
9946 ;; Summary extract commands
9947
9948 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9949   (let ((buffer-read-only nil)
9950         (article (gnus-summary-article-number))
9951         after-article b e)
9952     (unless (gnus-summary-goto-subject article)
9953       (error "No such article: %d" article))
9954     (gnus-summary-position-point)
9955     ;; If all commands are to be bunched up on one line, we collect
9956     ;; them here.
9957     (unless gnus-view-pseudos-separately
9958       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9959             files action)
9960         (while ps
9961           (setq action (cdr (assq 'action (car ps))))
9962           (setq files (list (cdr (assq 'name (car ps)))))
9963           (while (and ps (cdr ps)
9964                       (string= (or action "1")
9965                                (or (cdr (assq 'action (cadr ps))) "2")))
9966             (push (cdr (assq 'name (cadr ps))) files)
9967             (setcdr ps (cddr ps)))
9968           (when files
9969             (when (not (string-match "%s" action))
9970               (push " " files))
9971             (push " " files)
9972             (when (assq 'execute (car ps))
9973               (setcdr (assq 'execute (car ps))
9974                       (funcall (if (string-match "%s" action)
9975                                    'format 'concat)
9976                                action
9977                                (mapconcat
9978                                 (lambda (f)
9979                                   (if (equal f " ")
9980                                       f
9981                                     (mm-quote-arg f)))
9982                                 files " ")))))
9983           (setq ps (cdr ps)))))
9984     (if (and gnus-view-pseudos (not not-view))
9985         (while pslist
9986           (when (assq 'execute (car pslist))
9987             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9988                                   (eq gnus-view-pseudos 'not-confirm)))
9989           (setq pslist (cdr pslist)))
9990       (save-excursion
9991         (while pslist
9992           (setq after-article (or (cdr (assq 'article (car pslist)))
9993                                   (gnus-summary-article-number)))
9994           (gnus-summary-goto-subject after-article)
9995           (forward-line 1)
9996           (setq b (point))
9997           (insert "    " (file-name-nondirectory
9998                           (cdr (assq 'name (car pslist))))
9999                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10000           (setq e (point))
10001           (forward-line -1)             ; back to `b'
10002           (gnus-add-text-properties
10003            b (1- e) (list 'gnus-number gnus-reffed-article-number
10004                           gnus-mouse-face-prop gnus-mouse-face))
10005           (gnus-data-enter
10006            after-article gnus-reffed-article-number
10007            gnus-unread-mark b (car pslist) 0 (- e b))
10008           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10009           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10010           (setq pslist (cdr pslist)))))))
10011
10012 (defun gnus-pseudos< (p1 p2)
10013   (let ((c1 (cdr (assq 'action p1)))
10014         (c2 (cdr (assq 'action p2))))
10015     (and c1 c2 (string< c1 c2))))
10016
10017 (defun gnus-request-pseudo-article (props)
10018   (cond ((assq 'execute props)
10019          (gnus-execute-command (cdr (assq 'execute props)))))
10020   (let ((gnus-current-article (gnus-summary-article-number)))
10021     (gnus-run-hooks 'gnus-mark-article-hook)))
10022
10023 (defun gnus-execute-command (command &optional automatic)
10024   (save-excursion
10025     (gnus-article-setup-buffer)
10026     (set-buffer gnus-article-buffer)
10027     (setq buffer-read-only nil)
10028     (let ((command (if automatic command
10029                      (read-string "Command: " (cons command 0)))))
10030       (erase-buffer)
10031       (insert "$ " command "\n\n")
10032       (if gnus-view-pseudo-asynchronously
10033           (start-process "gnus-execute" (current-buffer) shell-file-name
10034                          shell-command-switch command)
10035         (call-process shell-file-name nil t nil
10036                       shell-command-switch command)))))
10037
10038 ;; Summary kill commands.
10039
10040 (defun gnus-summary-edit-global-kill (article)
10041   "Edit the \"global\" kill file."
10042   (interactive (list (gnus-summary-article-number)))
10043   (gnus-group-edit-global-kill article))
10044
10045 (defun gnus-summary-edit-local-kill ()
10046   "Edit a local kill file applied to the current newsgroup."
10047   (interactive)
10048   (setq gnus-current-headers (gnus-summary-article-header))
10049   (gnus-group-edit-local-kill
10050    (gnus-summary-article-number) gnus-newsgroup-name))
10051
10052 ;;; Header reading.
10053
10054 (defun gnus-read-header (id &optional header)
10055   "Read the headers of article ID and enter them into the Gnus system."
10056   (let ((group gnus-newsgroup-name)
10057         (gnus-override-method
10058          (or
10059           gnus-override-method
10060           (and (gnus-news-group-p gnus-newsgroup-name)
10061                (car (gnus-refer-article-methods)))))
10062         where)
10063     ;; First we check to see whether the header in question is already
10064     ;; fetched.
10065     (if (stringp id)
10066         ;; This is a Message-ID.
10067         (setq header (or header (gnus-id-to-header id)))
10068       ;; This is an article number.
10069       (setq header (or header (gnus-summary-article-header id))))
10070     (if (and header
10071              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10072         ;; We have found the header.
10073         header
10074       ;; If this is a sparse article, we have to nix out its
10075       ;; previous entry in the thread hashtb.
10076       (when (and header
10077                  (gnus-summary-article-sparse-p (mail-header-number header)))
10078         (let* ((parent (gnus-parent-id (mail-header-references header)))
10079                (thread (and parent (gnus-id-to-thread parent))))
10080           (when thread
10081             (delq (assq header thread) thread))))
10082       ;; We have to really fetch the header to this article.
10083       (save-excursion
10084         (set-buffer nntp-server-buffer)
10085         (when (setq where (gnus-request-head id group))
10086           (nnheader-fold-continuation-lines)
10087           (goto-char (point-max))
10088           (insert ".\n")
10089           (goto-char (point-min))
10090           (insert "211 ")
10091           (princ (cond
10092                   ((numberp id) id)
10093                   ((cdr where) (cdr where))
10094                   (header (mail-header-number header))
10095                   (t gnus-reffed-article-number))
10096                  (current-buffer))
10097           (insert " Article retrieved.\n"))
10098         (if (or (not where)
10099                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10100             ()                          ; Malformed head.
10101           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10102             (when (and (stringp id)
10103                        (not (string= (gnus-group-real-name group)
10104                                      (car where))))
10105               ;; If we fetched by Message-ID and the article came
10106               ;; from a different group, we fudge some bogus article
10107               ;; numbers for this article.
10108               (mail-header-set-number header gnus-reffed-article-number))
10109             (save-excursion
10110               (set-buffer gnus-summary-buffer)
10111               (decf gnus-reffed-article-number)
10112               (gnus-remove-header (mail-header-number header))
10113               (push header gnus-newsgroup-headers)
10114               (setq gnus-current-headers header)
10115               (push (mail-header-number header) gnus-newsgroup-limit)))
10116           header)))))
10117
10118 (defun gnus-remove-header (number)
10119   "Remove header NUMBER from `gnus-newsgroup-headers'."
10120   (if (and gnus-newsgroup-headers
10121            (= number (mail-header-number (car gnus-newsgroup-headers))))
10122       (pop gnus-newsgroup-headers)
10123     (let ((headers gnus-newsgroup-headers))
10124       (while (and (cdr headers)
10125                   (not (= number (mail-header-number (cadr headers)))))
10126         (pop headers))
10127       (when (cdr headers)
10128         (setcdr headers (cddr headers))))))
10129
10130 ;;;
10131 ;;; summary highlights
10132 ;;;
10133
10134 (defun gnus-highlight-selected-summary ()
10135   "Highlight selected article in summary buffer."
10136   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10137   (when gnus-summary-selected-face
10138     (save-excursion
10139       (let* ((beg (progn (beginning-of-line) (point)))
10140              (end (progn (end-of-line) (point)))
10141              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10142              (from (if (get-text-property beg gnus-mouse-face-prop)
10143                        beg
10144                      (or (next-single-property-change
10145                           beg gnus-mouse-face-prop nil end)
10146                          beg)))
10147              (to
10148               (if (= from end)
10149                   (- from 2)
10150                 (or (next-single-property-change
10151                      from gnus-mouse-face-prop nil end)
10152                     end))))
10153         ;; If no mouse-face prop on line we will have to = from = end,
10154         ;; so we highlight the entire line instead.
10155         (when (= (+ to 2) from)
10156           (setq from beg)
10157           (setq to end))
10158         (if gnus-newsgroup-selected-overlay
10159             ;; Move old overlay.
10160             (gnus-move-overlay
10161              gnus-newsgroup-selected-overlay from to (current-buffer))
10162           ;; Create new overlay.
10163           (gnus-overlay-put
10164            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10165            'face gnus-summary-selected-face))))))
10166
10167 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10168 (defun gnus-summary-highlight-line ()
10169   "Highlight current line according to `gnus-summary-highlight'."
10170   (let* ((list gnus-summary-highlight)
10171          (p (point))
10172          (end (progn (end-of-line) (point)))
10173          ;; now find out where the line starts and leave point there.
10174          (beg (progn (beginning-of-line) (point)))
10175          (article (gnus-summary-article-number))
10176          (score (or (cdr (assq (or article gnus-current-article)
10177                                gnus-newsgroup-scored))
10178                     gnus-summary-default-score 0))
10179          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10180          (inhibit-read-only t))
10181     ;; Eval the cars of the lists until we find a match.
10182     (let ((default gnus-summary-default-score)
10183           (default-high gnus-summary-default-high-score)
10184           (default-low gnus-summary-default-low-score))
10185       (while (and list
10186                   (not (eval (caar list))))
10187         (setq list (cdr list))))
10188     (let ((face (cdar list)))
10189       (unless (eq face (get-text-property beg 'face))
10190         (gnus-put-text-property-excluding-characters-with-faces
10191          beg end 'face
10192          (setq face (if (boundp face) (symbol-value face) face)))
10193         (when gnus-summary-highlight-line-function
10194           (funcall gnus-summary-highlight-line-function article face))))
10195     (goto-char p)))
10196
10197 (defun gnus-update-read-articles (group unread &optional compute)
10198   "Update the list of read articles in GROUP."
10199   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10200          (entry (gnus-gethash group gnus-newsrc-hashtb))
10201          (info (nth 2 entry))
10202          (prev 1)
10203          (unread (sort (copy-sequence unread) '<))
10204          read)
10205     (if (or (not info) (not active))
10206         ;; There is no info on this group if it was, in fact,
10207         ;; killed.  Gnus stores no information on killed groups, so
10208         ;; there's nothing to be done.
10209         ;; One could store the information somewhere temporarily,
10210         ;; perhaps...  Hmmm...
10211         ()
10212       ;; Remove any negative articles numbers.
10213       (while (and unread (< (car unread) 0))
10214         (setq unread (cdr unread)))
10215       ;; Remove any expired article numbers
10216       (while (and unread (< (car unread) (car active)))
10217         (setq unread (cdr unread)))
10218       ;; Compute the ranges of read articles by looking at the list of
10219       ;; unread articles.
10220       (while unread
10221         (when (/= (car unread) prev)
10222           (push (if (= prev (1- (car unread))) prev
10223                   (cons prev (1- (car unread))))
10224                 read))
10225         (setq prev (1+ (car unread)))
10226         (setq unread (cdr unread)))
10227       (when (<= prev (cdr active))
10228         (push (cons prev (cdr active)) read))
10229       (setq read (if (> (length read) 1) (nreverse read) read))
10230       (if compute
10231           read
10232         (save-excursion
10233           (let (setmarkundo)
10234             ;; Propagate the read marks to the backend.
10235             (when (gnus-check-backend-function 'request-set-mark group)
10236               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10237                     (add (gnus-remove-from-range read (gnus-info-read info))))
10238                 (when (or add del)
10239                   (unless (gnus-check-group group)
10240                     (error "Can't open server for %s" group))
10241                   (gnus-request-set-mark
10242                    group (delq nil (list (if add (list add 'add '(read)))
10243                                          (if del (list del 'del '(read))))))
10244                   (setq setmarkundo
10245                         `(gnus-request-set-mark
10246                           ,group
10247                           ',(delq nil (list
10248                                        (if del (list del 'add '(read)))
10249                                        (if add (list add 'del '(read))))))))))
10250             (set-buffer gnus-group-buffer)
10251             (gnus-undo-register
10252               `(progn
10253                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10254                  (gnus-info-set-read ',info ',(gnus-info-read info))
10255                  (gnus-get-unread-articles-in-group ',info
10256                                                     (gnus-active ,group))
10257                  (gnus-group-update-group ,group t)
10258                  ,setmarkundo))))
10259         ;; Enter this list into the group info.
10260         (gnus-info-set-read info read)
10261         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10262         (gnus-get-unread-articles-in-group info (gnus-active group))
10263         t))))
10264
10265 (defun gnus-offer-save-summaries ()
10266   "Offer to save all active summary buffers."
10267   (let (buffers)
10268     ;; Go through all buffers and find all summaries.
10269     (dolist (buffer (buffer-list))
10270       (when (and (setq buffer (buffer-name buffer))
10271                  (string-match "Summary" buffer)
10272                  (save-excursion
10273                    (set-buffer buffer)
10274                    ;; We check that this is, indeed, a summary buffer.
10275                    (and (eq major-mode 'gnus-summary-mode)
10276                         ;; Also make sure this isn't bogus.
10277                         gnus-newsgroup-prepared
10278                         ;; Also make sure that this isn't a
10279                         ;; dead summary buffer.
10280                         (not gnus-dead-summary-mode))))
10281         (push buffer buffers)))
10282     ;; Go through all these summary buffers and offer to save them.
10283     (when buffers
10284       (save-excursion
10285         (map-y-or-n-p
10286          "Update summary buffer %s? "
10287          (lambda (buf)
10288            (switch-to-buffer buf)
10289            (gnus-summary-exit))
10290          buffers)))))
10291
10292 (defun gnus-summary-setup-default-charset ()
10293   "Setup newsgroup default charset."
10294   (if (equal gnus-newsgroup-name "nndraft:drafts")
10295       (setq gnus-newsgroup-charset nil)
10296     (let* ((ignored-charsets
10297             (or gnus-newsgroup-ephemeral-ignored-charsets
10298                 (append
10299                  (and gnus-newsgroup-name
10300                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10301                  gnus-newsgroup-ignored-charsets))))
10302       (setq gnus-newsgroup-charset
10303             (or gnus-newsgroup-ephemeral-charset
10304                 (and gnus-newsgroup-name
10305                      (gnus-parameter-charset gnus-newsgroup-name))
10306                 gnus-default-charset))
10307       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10308            ignored-charsets))))
10309
10310 ;;;
10311 ;;; Mime Commands
10312 ;;;
10313
10314 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10315   "Display the current article buffer fully MIME-buttonized.
10316 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10317 treated as multipart/mixed."
10318   (interactive "P")
10319   (require 'gnus-art)
10320   (let ((gnus-unbuttonized-mime-types nil)
10321         (gnus-mime-display-multipart-as-mixed show-all-parts))
10322     (gnus-summary-show-article)))
10323
10324 (defun gnus-summary-repair-multipart (article)
10325   "Add a Content-Type header to a multipart article without one."
10326   (interactive (list (gnus-summary-article-number)))
10327   (gnus-with-article article
10328     (message-narrow-to-head)
10329     (message-remove-header "Mime-Version")
10330     (goto-char (point-max))
10331     (insert "Mime-Version: 1.0\n")
10332     (widen)
10333     (when (search-forward "\n--" nil t)
10334       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10335         (message-narrow-to-head)
10336         (message-remove-header "Content-Type")
10337         (goto-char (point-max))
10338         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10339                         separator))
10340         (widen))))
10341   (let (gnus-mark-article-hook)
10342     (gnus-summary-select-article t t nil article)))
10343
10344 (defun gnus-summary-toggle-display-buttonized ()
10345   "Toggle the buttonizing of the article buffer."
10346   (interactive)
10347   (require 'gnus-art)
10348   (if (setq gnus-inhibit-mime-unbuttonizing
10349             (not gnus-inhibit-mime-unbuttonizing))
10350       (let ((gnus-unbuttonized-mime-types nil))
10351         (gnus-summary-show-article))
10352     (gnus-summary-show-article)))
10353
10354 ;;;
10355 ;;; Generic summary marking commands
10356 ;;;
10357
10358 (defvar gnus-summary-marking-alist
10359   '((read gnus-del-mark "d")
10360     (unread gnus-unread-mark "u")
10361     (ticked gnus-ticked-mark "!")
10362     (dormant gnus-dormant-mark "?")
10363     (expirable gnus-expirable-mark "e"))
10364   "An alist of names/marks/keystrokes.")
10365
10366 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10367 (defvar gnus-summary-mark-map)
10368
10369 (defun gnus-summary-make-all-marking-commands ()
10370   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10371   (dolist (elem gnus-summary-marking-alist)
10372     (apply 'gnus-summary-make-marking-command elem)))
10373
10374 (defun gnus-summary-make-marking-command (name mark keystroke)
10375   (let ((map (make-sparse-keymap)))
10376     (define-key gnus-summary-generic-mark-map keystroke map)
10377     (dolist (lway `((next "next" next nil "n")
10378                     (next-unread "next unread" next t "N")
10379                     (prev "previous" prev nil "p")
10380                     (prev-unread "previous unread" prev t "P")
10381                     (nomove "" nil nil ,keystroke)))
10382       (let ((func (gnus-summary-make-marking-command-1
10383                    mark (car lway) lway name)))
10384         (setq func (eval func))
10385         (define-key map (nth 4 lway) func)))))
10386
10387 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10388   `(defun ,(intern
10389             (format "gnus-summary-put-mark-as-%s%s"
10390                     name (if (eq way 'nomove)
10391                              ""
10392                            (concat "-" (symbol-name way)))))
10393      (n)
10394      ,(format
10395        "Mark the current article as %s%s.
10396 If N, the prefix, then repeat N times.
10397 If N is negative, move in reverse order.
10398 The difference between N and the actual number of articles marked is
10399 returned."
10400        name (cadr lway))
10401      (interactive "p")
10402      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10403
10404 (defun gnus-summary-generic-mark (n mark move unread)
10405   "Mark N articles with MARK."
10406   (unless (eq major-mode 'gnus-summary-mode)
10407     (error "This command can only be used in the summary buffer"))
10408   (gnus-summary-show-thread)
10409   (let ((nummove
10410          (cond
10411           ((eq move 'next) 1)
10412           ((eq move 'prev) -1)
10413           (t 0))))
10414     (if (zerop nummove)
10415         (setq n 1)
10416       (when (< n 0)
10417         (setq n (abs n)
10418               nummove (* -1 nummove))))
10419     (while (and (> n 0)
10420                 (gnus-summary-mark-article nil mark)
10421                 (zerop (gnus-summary-next-subject nummove unread t)))
10422       (setq n (1- n)))
10423     (when (/= 0 n)
10424       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10425     (gnus-summary-recenter)
10426     (gnus-summary-position-point)
10427     (gnus-set-mode-line 'summary)
10428     n))
10429
10430 (defun gnus-summary-insert-articles (articles)
10431   (when (setq articles
10432               (gnus-set-difference articles
10433                                    (mapcar (lambda (h) (mail-header-number h))
10434                                            gnus-newsgroup-headers)))
10435     (setq gnus-newsgroup-headers
10436           (merge 'list
10437                  gnus-newsgroup-headers
10438                  (gnus-fetch-headers articles)
10439                  'gnus-article-sort-by-number))
10440     ;; Suppress duplicates?
10441     (when gnus-suppress-duplicates
10442       (gnus-dup-suppress-articles))
10443
10444     ;; We might want to build some more threads first.
10445     (when (and gnus-fetch-old-headers
10446                (eq gnus-headers-retrieved-by 'nov))
10447       (if (eq gnus-fetch-old-headers 'invisible)
10448           (gnus-build-all-threads)
10449         (gnus-build-old-threads)))
10450     ;; Let the Gnus agent mark articles as read.
10451     (when gnus-agent
10452       (gnus-agent-get-undownloaded-list))
10453     ;; Remove list identifiers from subject
10454     (when gnus-list-identifiers
10455       (gnus-summary-remove-list-identifiers))
10456     ;; First and last article in this newsgroup.
10457     (when gnus-newsgroup-headers
10458       (setq gnus-newsgroup-begin
10459             (mail-header-number (car gnus-newsgroup-headers))
10460             gnus-newsgroup-end
10461             (mail-header-number
10462              (gnus-last-element gnus-newsgroup-headers))))
10463     (when gnus-use-scoring
10464       (gnus-possibly-score-headers))))
10465
10466 (defun gnus-summary-insert-old-articles (&optional all)
10467   "Insert all old articles in this group.
10468 If ALL is non-nil, already read articles become readable.
10469 If ALL is a number, fetch this number of articles."
10470   (interactive "P")
10471   (prog1
10472       (let ((old (mapcar 'car gnus-newsgroup-data))
10473             (i (car gnus-newsgroup-active))
10474             older len)
10475         (while (<= i (cdr gnus-newsgroup-active))
10476           (or (memq i old) (push i older))
10477           (incf i))
10478         (setq len (length older))
10479         (cond
10480          ((null older) nil)
10481          ((numberp all)
10482           (if (< all len)
10483               (setq older (subseq older 0 all))))
10484          (all nil)
10485          (t
10486           (if (and (numberp gnus-large-newsgroup)
10487                    (> len gnus-large-newsgroup))
10488               (let ((input
10489                      (read-string
10490                       (format
10491                        "How many articles from %s (default %d): "
10492                        (gnus-limit-string
10493                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10494                        len))))
10495                 (unless (string-match "^[ \t]*$" input)
10496                   (setq all (string-to-number input))
10497                   (if (< all len)
10498                       (setq older (subseq older 0 all))))))))
10499         (if (not older)
10500             (message "No old news.")
10501           (gnus-summary-insert-articles older)
10502           (gnus-summary-limit (gnus-union older old))))
10503     (gnus-summary-position-point)))
10504
10505 (defun gnus-summary-insert-new-articles ()
10506   "Insert all new articles in this group."
10507   (interactive)
10508   (prog1
10509       (let ((old (mapcar 'car gnus-newsgroup-data))
10510             (old-active gnus-newsgroup-active)
10511             (nnmail-fetched-sources (list t))
10512             i new)
10513         (setq gnus-newsgroup-active
10514               (gnus-activate-group gnus-newsgroup-name 'scan))
10515         (setq i (1+ (cdr old-active)))
10516         (while (<= i (cdr gnus-newsgroup-active))
10517           (push i new)
10518           (incf i))
10519         (if (not new)
10520             (message "No gnus is bad news.")
10521           (setq new (nreverse new))
10522           (gnus-summary-insert-articles new)
10523           (setq gnus-newsgroup-unreads
10524                 (append gnus-newsgroup-unreads new))
10525           (gnus-summary-limit (gnus-union old new))))
10526     (gnus-summary-position-point)))
10527
10528 (gnus-summary-make-all-marking-commands)
10529
10530 (gnus-ems-redefine)
10531
10532 (provide 'gnus-sum)
10533
10534 (run-hooks 'gnus-sum-load-hook)
10535
10536 ;;; gnus-sum.el ends here