*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, many commands will go to the next unread article.
172 This applies to marking commands as well as other commands that
173 \"naturally\" select the next article, like, for instance, `SPC' at
174 the end of an article.
175 If nil, only the marking commands will go to the next (un)read article.
176 If `never', commands that usually go to the next unread article, will
177 go to the next article, whether it is read or not."
178   :group 'gnus-summary-marks
179   :link '(custom-manual "(gnus)Setting Marks")
180   :type '(choice (const :tag "off" nil)
181                  (const never)
182                  (sexp :menu-tag "on" t)))
183
184 (defcustom gnus-summary-default-score 0
185   "*Default article score level.
186 All scores generated by the score files will be added to this score.
187 If this variable is nil, scoring will be disabled."
188   :group 'gnus-score-default
189   :type '(choice (const :tag "disable")
190                  integer))
191
192 (defcustom gnus-summary-zcore-fuzz 0
193   "*Fuzziness factor for the zcore in the summary buffer.
194 Articles with scores closer than this to `gnus-summary-default-score'
195 will not be marked."
196   :group 'gnus-summary-format
197   :type 'integer)
198
199 (defcustom gnus-simplify-subject-fuzzy-regexp nil
200   "*Strings to be removed when doing fuzzy matches.
201 This can either be a regular expression or list of regular expressions
202 that will be removed from subject strings if fuzzy subject
203 simplification is selected."
204   :group 'gnus-thread
205   :type '(repeat regexp))
206
207 (defcustom gnus-show-threads t
208   "*If non-nil, display threads in summary mode."
209   :group 'gnus-thread
210   :type 'boolean)
211
212 (defcustom gnus-thread-hide-subtree nil
213   "*If non-nil, hide all threads initially.
214 If threads are hidden, you have to run the command
215 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
216 to expose hidden threads."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-killed t
221   "*If non-nil, hide killed threads automatically."
222   :group 'gnus-thread
223   :type 'boolean)
224
225 (defcustom gnus-thread-ignore-subject t
226   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
227 If nil, articles that have different subjects from their parents will
228 start separate threads."
229   :group 'gnus-thread
230   :type 'boolean)
231
232 (defcustom gnus-thread-operation-ignore-subject t
233   "*If non-nil, subjects will be ignored when doing thread commands.
234 This affects commands like `gnus-summary-kill-thread' and
235 `gnus-summary-lower-thread'.
236
237 If this variable is nil, articles in the same thread with different
238 subjects will not be included in the operation in question.  If this
239 variable is `fuzzy', only articles that have subjects that are fuzzily
240 equal will be included."
241   :group 'gnus-thread
242   :type '(choice (const :tag "off" nil)
243                  (const fuzzy)
244                  (sexp :tag "on" t)))
245
246 (defcustom gnus-thread-indent-level 4
247   "*Number that says how much each sub-thread should be indented."
248   :group 'gnus-thread
249   :type 'integer)
250
251 (defcustom gnus-auto-extend-newsgroup t
252   "*If non-nil, extend newsgroup forward and backward when requested."
253   :group 'gnus-summary-choose
254   :type 'boolean)
255
256 (defcustom gnus-auto-select-first t
257   "*If nil, don't select the first unread article when entering a group.
258 If this variable is `best', select the highest-scored unread article
259 in the group.  If t, select the first unread article.
260
261 This variable can also be a function to place point on a likely
262 subject line.  Useful values include `gnus-summary-first-unread-subject',
263 `gnus-summary-first-unread-article' and
264 `gnus-summary-best-unread-article'.
265
266 If you want to prevent automatic selection of the first unread article
267 in some newsgroups, set the variable to nil in
268 `gnus-select-group-hook'."
269   :group 'gnus-group-select
270   :type '(choice (const :tag "none" nil)
271                  (const best)
272                  (sexp :menu-tag "first" t)
273                  (function-item gnus-summary-first-unread-subject)
274                  (function-item gnus-summary-first-unread-article)
275                  (function-item gnus-summary-best-unread-article)))
276
277 (defcustom gnus-auto-select-next t
278   "*If non-nil, offer to go to the next group from the end of the previous.
279 If the value is t and the next newsgroup is empty, Gnus will exit
280 summary mode and go back to group mode.  If the value is neither nil
281 nor t, Gnus will select the following unread newsgroup.  In
282 particular, if the value is the symbol `quietly', the next unread
283 newsgroup will be selected without any confirmation, and if it is
284 `almost-quietly', the next group will be selected without any
285 confirmation if you are located on the last article in the group.
286 Finally, if this variable is `slightly-quietly', the `Z n' command
287 will go to the next group without confirmation."
288   :group 'gnus-summary-maneuvering
289   :type '(choice (const :tag "off" nil)
290                  (const quietly)
291                  (const almost-quietly)
292                  (const slightly-quietly)
293                  (sexp :menu-tag "on" t)))
294
295 (defcustom gnus-auto-select-same nil
296   "*If non-nil, select the next article with the same subject.
297 If there are no more articles with the same subject, go to
298 the first unread article."
299   :group 'gnus-summary-maneuvering
300   :type 'boolean)
301
302 (defcustom gnus-summary-check-current nil
303   "*If non-nil, consider the current article when moving.
304 The \"unread\" movement commands will stay on the same line if the
305 current article is unread."
306   :group 'gnus-summary-maneuvering
307   :type 'boolean)
308
309 (defcustom gnus-auto-center-summary t
310   "*If non-nil, always center the current summary buffer.
311 In particular, if `vertical' do only vertical recentering.  If non-nil
312 and non-`vertical', do both horizontal and vertical recentering."
313   :group 'gnus-summary-maneuvering
314   :type '(choice (const :tag "none" nil)
315                  (const vertical)
316                  (integer :tag "height")
317                  (sexp :menu-tag "both" t)))
318
319 (defcustom gnus-show-all-headers nil
320   "*If non-nil, don't hide any headers."
321   :group 'gnus-article-hiding
322   :group 'gnus-article-headers
323   :type 'boolean)
324
325 (defcustom gnus-summary-ignore-duplicates nil
326   "*If non-nil, ignore articles with identical Message-ID headers."
327   :group 'gnus-summary
328   :type 'boolean)
329
330 (defcustom gnus-single-article-buffer t
331   "*If non-nil, display all articles in the same buffer.
332 If nil, each group will get its own article buffer."
333   :group 'gnus-article-various
334   :type 'boolean)
335
336 (defcustom gnus-break-pages t
337   "*If non-nil, do page breaking on articles.
338 The page delimiter is specified by the `gnus-page-delimiter'
339 variable."
340   :group 'gnus-article-various
341   :type 'boolean)
342
343 (defcustom gnus-move-split-methods nil
344   "*Variable used to suggest where articles are to be moved to.
345 It uses the same syntax as the `gnus-split-methods' variable."
346   :group 'gnus-summary-mail
347   :type '(repeat (choice (list :value (fun) function)
348                          (cons :value ("" "") regexp (repeat string))
349                          (sexp :value nil))))
350
351 (defcustom gnus-unread-mark ?  ;Whitespace
352   "*Mark used for unread articles."
353   :group 'gnus-summary-marks
354   :type 'character)
355
356 (defcustom gnus-ticked-mark ?!
357   "*Mark used for ticked articles."
358   :group 'gnus-summary-marks
359   :type 'character)
360
361 (defcustom gnus-dormant-mark ??
362   "*Mark used for dormant articles."
363   :group 'gnus-summary-marks
364   :type 'character)
365
366 (defcustom gnus-del-mark ?r
367   "*Mark used for del'd articles."
368   :group 'gnus-summary-marks
369   :type 'character)
370
371 (defcustom gnus-read-mark ?R
372   "*Mark used for read articles."
373   :group 'gnus-summary-marks
374   :type 'character)
375
376 (defcustom gnus-expirable-mark ?E
377   "*Mark used for expirable articles."
378   :group 'gnus-summary-marks
379   :type 'character)
380
381 (defcustom gnus-killed-mark ?K
382   "*Mark used for killed articles."
383   :group 'gnus-summary-marks
384   :type 'character)
385
386 (defcustom gnus-souped-mark ?F
387   "*Mark used for killed articles."
388   :group 'gnus-summary-marks
389   :type 'character)
390
391 (defcustom gnus-kill-file-mark ?X
392   "*Mark used for articles killed by kill files."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-low-score-mark ?Y
397   "*Mark used for articles with a low score."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-catchup-mark ?C
402   "*Mark used for articles that are caught up."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-replied-mark ?A
407   "*Mark used for articles that have been replied to."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-cached-mark ?*
412   "*Mark used for articles that are in the cache."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-saved-mark ?S
417   "*Mark used for articles that have been saved to."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-ancient-mark ?O
422   "*Mark used for ancient articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-sparse-mark ?Q
427   "*Mark used for sparsely reffed articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-canceled-mark ?G
432   "*Mark used for canceled articles."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-duplicate-mark ?M
437   "*Mark used for duplicate articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-undownloaded-mark ?@
442   "*Mark used for articles that weren't downloaded."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-downloadable-mark ?%
447   "*Mark used for articles that are to be downloaded."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-unsendable-mark ?=
452   "*Mark used for articles that won't be sent."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-score-over-mark ?+
457   "*Score mark used for articles with high scores."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-score-below-mark ?-
462   "*Score mark used for articles with low scores."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-empty-thread-mark ?  ;Whitespace
467   "*There is no thread under the article."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-not-empty-thread-mark ?=
472   "*There is a thread under the article."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-view-pseudo-asynchronously nil
477   "*If non-nil, Gnus will view pseudo-articles asynchronously."
478   :group 'gnus-extract-view
479   :type 'boolean)
480
481 (defcustom gnus-auto-expirable-marks
482   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
483         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
484         gnus-souped-mark gnus-duplicate-mark)
485   "*The list of marks converted into expiration if a group is auto-expirable."
486   :group 'gnus-summary
487   :type '(repeat character))
488
489 (defcustom gnus-inhibit-user-auto-expire t
490   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
491   :group 'gnus-summary
492   :type 'boolean)
493
494 (defcustom gnus-view-pseudos nil
495   "*If `automatic', pseudo-articles will be viewed automatically.
496 If `not-confirm', pseudos will be viewed automatically, and the user
497 will not be asked to confirm the command."
498   :group 'gnus-extract-view
499   :type '(choice (const :tag "off" nil)
500                  (const automatic)
501                  (const not-confirm)))
502
503 (defcustom gnus-view-pseudos-separately t
504   "*If non-nil, one pseudo-article will be created for each file to be viewed.
505 If nil, all files that use the same viewing command will be given as a
506 list of parameters to that command."
507   :group 'gnus-extract-view
508   :type 'boolean)
509
510 (defcustom gnus-insert-pseudo-articles t
511   "*If non-nil, insert pseudo-articles when decoding articles."
512   :group 'gnus-extract-view
513   :type 'boolean)
514
515 (defcustom gnus-summary-dummy-line-format
516   "  %(:                          :%) %S\n"
517   "*The format specification for the dummy roots in the summary buffer.
518 It works along the same lines as a normal formatting string,
519 with some simple extensions.
520
521 %S  The subject"
522   :group 'gnus-threading
523   :type 'string)
524
525 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
526   "*The format specification for the summary mode line.
527 It works along the same lines as a normal formatting string,
528 with some simple extensions:
529
530 %G  Group name
531 %p  Unprefixed group name
532 %A  Current article number
533 %z  Current article score
534 %V  Gnus version
535 %U  Number of unread articles in the group
536 %e  Number of unselected articles in the group
537 %Z  A string with unread/unselected article counts
538 %g  Shortish group name
539 %S  Subject of the current article
540 %u  User-defined spec
541 %s  Current score file name
542 %d  Number of dormant articles
543 %r  Number of articles that have been marked as read in this session
544 %E  Number of articles expunged by the score files"
545   :group 'gnus-summary-format
546   :type 'string)
547
548 (defcustom gnus-list-identifiers nil
549   "Regexp that matches list identifiers to be removed from subject.
550 This can also be a list of regexps."
551   :group 'gnus-summary-format
552   :group 'gnus-article-hiding
553   :type '(choice (const :tag "none" nil)
554                  (regexp :value ".*")
555                  (repeat :value (".*") regexp)))
556
557 (defcustom gnus-summary-mark-below 0
558   "*Mark all articles with a score below this variable as read.
559 This variable is local to each summary buffer and usually set by the
560 score file."
561   :group 'gnus-score-default
562   :type 'integer)
563
564 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
565   "*List of functions used for sorting articles in the summary buffer.
566 This variable is only used when not using a threaded display."
567   :group 'gnus-summary-sort
568   :type '(repeat (choice (function-item gnus-article-sort-by-number)
569                          (function-item gnus-article-sort-by-author)
570                          (function-item gnus-article-sort-by-subject)
571                          (function-item gnus-article-sort-by-date)
572                          (function-item gnus-article-sort-by-score)
573                          (function :tag "other"))))
574
575 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
576   "*List of functions used for sorting threads in the summary buffer.
577 By default, threads are sorted by article number.
578
579 Each function takes two threads and return non-nil if the first thread
580 should be sorted before the other.  If you use more than one function,
581 the primary sort function should be the last.  You should probably
582 always include `gnus-thread-sort-by-number' in the list of sorting
583 functions -- preferably first.
584
585 Ready-made functions include `gnus-thread-sort-by-number',
586 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
587 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
588 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
589   :group 'gnus-summary-sort
590   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
591                          (function-item gnus-thread-sort-by-author)
592                          (function-item gnus-thread-sort-by-subject)
593                          (function-item gnus-thread-sort-by-date)
594                          (function-item gnus-thread-sort-by-score)
595                          (function-item gnus-thread-sort-by-total-score)
596                          (function :tag "other"))))
597
598 (defcustom gnus-thread-score-function '+
599   "*Function used for calculating the total score of a thread.
600
601 The function is called with the scores of the article and each
602 subthread and should then return the score of the thread.
603
604 Some functions you can use are `+', `max', or `min'."
605   :group 'gnus-summary-sort
606   :type 'function)
607
608 (defcustom gnus-summary-expunge-below nil
609   "All articles that have a score less than this variable will be expunged.
610 This variable is local to the summary buffers."
611   :group 'gnus-score-default
612   :type '(choice (const :tag "off" nil)
613                  integer))
614
615 (defcustom gnus-thread-expunge-below nil
616   "All threads that have a total score less than this variable will be expunged.
617 See `gnus-thread-score-function' for en explanation of what a
618 \"thread score\" is.
619
620 This variable is local to the summary buffers."
621   :group 'gnus-threading
622   :group 'gnus-score-default
623   :type '(choice (const :tag "off" nil)
624                  integer))
625
626 (defcustom gnus-summary-mode-hook nil
627   "*A hook for Gnus summary mode.
628 This hook is run before any variables are set in the summary buffer."
629   :group 'gnus-summary-various
630   :type 'hook)
631
632 (defcustom gnus-summary-menu-hook nil
633   "*Hook run after the creation of the summary mode menu."
634   :group 'gnus-summary-visual
635   :type 'hook)
636
637 (defcustom gnus-summary-exit-hook nil
638   "*A hook called on exit from the summary buffer.
639 It will be called with point in the group buffer."
640   :group 'gnus-summary-exit
641   :type 'hook)
642
643 (defcustom gnus-summary-prepare-hook nil
644   "*A hook called after the summary buffer has been generated.
645 If you want to modify the summary buffer, you can use this hook."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-summary-prepared-hook nil
650   "*A hook called as the last thing after the summary buffer has been generated."
651   :group 'gnus-summary-various
652   :type 'hook)
653
654 (defcustom gnus-summary-generate-hook nil
655   "*A hook run just before generating the summary buffer.
656 This hook is commonly used to customize threading variables and the
657 like."
658   :group 'gnus-summary-various
659   :type 'hook)
660
661 (defcustom gnus-select-group-hook nil
662   "*A hook called when a newsgroup is selected.
663
664 If you'd like to simplify subjects like the
665 `gnus-summary-next-same-subject' command does, you can use the
666 following hook:
667
668  (setq gnus-select-group-hook
669       (list
670         (lambda ()
671           (mapcar (lambda (header)
672                      (mail-header-set-subject
673                       header
674                       (gnus-simplify-subject
675                        (mail-header-subject header) 're-only)))
676                   gnus-newsgroup-headers))))"
677   :group 'gnus-group-select
678   :type 'hook)
679
680 (defcustom gnus-select-article-hook nil
681   "*A hook called when an article is selected."
682   :group 'gnus-summary-choose
683   :type 'hook)
684
685 (defcustom gnus-visual-mark-article-hook
686   (list 'gnus-highlight-selected-summary)
687   "*Hook run after selecting an article in the summary buffer.
688 It is meant to be used for highlighting the article in some way.  It
689 is not run if `gnus-visual' is nil."
690   :group 'gnus-summary-visual
691   :type 'hook)
692
693 (defcustom gnus-parse-headers-hook nil
694   "*A hook called before parsing the headers."
695   :group 'gnus-various
696   :type 'hook)
697
698 (defcustom gnus-exit-group-hook nil
699   "*A hook called when exiting summary mode.
700 This hook is not called from the non-updating exit commands like `Q'."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-summary-update-hook
705   (list 'gnus-summary-highlight-line)
706   "*A hook called when a summary line is changed.
707 The hook will not be called if `gnus-visual' is nil.
708
709 The default function `gnus-summary-highlight-line' will
710 highlight the line according to the `gnus-summary-highlight'
711 variable."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
716   "*A hook called when an article is selected for the first time.
717 The hook is intended to mark an article as read (or unread)
718 automatically when it is selected."
719   :group 'gnus-summary-choose
720   :type 'hook)
721
722 (defcustom gnus-group-no-more-groups-hook nil
723   "*A hook run when returning to group mode having no more (unread) groups."
724   :group 'gnus-group-select
725   :type 'hook)
726
727 (defcustom gnus-ps-print-hook nil
728   "*A hook run before ps-printing something from Gnus."
729   :group 'gnus-summary
730   :type 'hook)
731
732 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
733   "Face used for highlighting the current article in the summary buffer."
734   :group 'gnus-summary-visual
735   :type 'face)
736
737 (defcustom gnus-summary-highlight
738   '(((= mark gnus-canceled-mark)
739      . gnus-summary-cancelled-face)
740     ((and (> score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-high-ticked-face)
744     ((and (< score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-low-ticked-face)
748     ((or (= mark gnus-dormant-mark)
749          (= mark gnus-ticked-mark))
750      . gnus-summary-normal-ticked-face)
751     ((and (> score default) (= mark gnus-ancient-mark))
752      . gnus-summary-high-ancient-face)
753     ((and (< score default) (= mark gnus-ancient-mark))
754      . gnus-summary-low-ancient-face)
755     ((= mark gnus-ancient-mark)
756      . gnus-summary-normal-ancient-face)
757     ((and (> score default) (= mark gnus-unread-mark))
758      . gnus-summary-high-unread-face)
759     ((and (< score default) (= mark gnus-unread-mark))
760      . gnus-summary-low-unread-face)
761     ((= mark gnus-unread-mark)
762      . gnus-summary-normal-unread-face)
763     ((and (> score default) (memq mark (list gnus-downloadable-mark
764                                              gnus-undownloaded-mark)))
765      . gnus-summary-high-unread-face)
766     ((and (< score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-low-unread-face)
769     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
770      . gnus-summary-normal-unread-face)
771     ((> score default)
772      . gnus-summary-high-read-face)
773     ((< score default)
774      . gnus-summary-low-read-face)
775     (t
776      . gnus-summary-normal-read-face))
777   "*Controls the highlighting of summary buffer lines.
778
779 A list of (FORM . FACE) pairs.  When deciding how a a particular
780 summary line should be displayed, each form is evaluated.  The content
781 of the face field after the first true form is used.  You can change
782 how those summary lines are displayed, by editing the face field.
783
784 You can use the following variables in the FORM field.
785
786 score:   The articles score
787 default: The default article score.
788 below:   The score below which articles are automatically marked as read.
789 mark:    The articles mark."
790   :group 'gnus-summary-visual
791   :type '(repeat (cons (sexp :tag "Form" nil)
792                        face)))
793
794 (defcustom gnus-alter-header-function nil
795   "Function called to allow alteration of article header structures.
796 The function is called with one parameter, the article header vector,
797 which it may alter in any way.")
798
799 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
800   "Variable that says which function should be used to decode a string with encoded words.")
801
802 (defcustom gnus-extra-headers nil
803   "*Extra headers to parse."
804   :group 'gnus-summary
805   :type '(repeat symbol))
806
807 (defcustom gnus-ignored-from-addresses
808   (and user-mail-address (regexp-quote user-mail-address))
809   "*Regexp of From headers that may be suppressed in favor of To headers."
810   :group 'gnus-summary
811   :type 'regexp)
812
813 (defcustom gnus-group-charset-alist
814   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
815     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
816     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
817     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
818     ("^relcom\\>" koi8-r)
819     ("^fido7\\>" koi8-r)
820     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
821     ("^israel\\>" iso-8859-1)
822     ("^han\\>" euc-kr)
823     ("^alt.chinese.text.big5\\>" chinese-big5)
824     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
825     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
826     (".*" iso-8859-1))
827   "Alist of regexps (to match group names) and default charsets to be used when reading."
828   :type '(repeat (list (regexp :tag "Group")
829                        (symbol :tag "Charset")))
830   :group 'gnus-charset)
831
832 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
833   "List of charsets that should be ignored.
834 When these charsets are used in the \"charset\" parameter, the
835 default charset will be used instead."
836   :type '(repeat symbol)
837   :group 'gnus-charset)
838
839 (defcustom gnus-group-ignored-charsets-alist 
840   '(("alt\\.chinese\\.text" iso-8859-1))
841   "Alist of regexps (to match group names) and charsets that should be ignored.
842 When these charsets are used in the \"charset\" parameter, the
843 default charset will be used instead."
844   :type '(repeat (cons (regexp :tag "Group")
845                        (repeat symbol)))
846   :group 'gnus-charset)
847
848 (defcustom gnus-group-highlight-words-alist nil
849   "Alist of group regexps and highlight regexps.
850 This variable uses the same syntax as `gnus-emphasis-alist'."
851   :type '(repeat (cons (regexp :tag "Group")
852                        (repeat (list (regexp :tag "Highlight regexp")
853                                      (number :tag "Group for entire word" 0)
854                                      (number :tag "Group for displayed part" 0)
855                                      (symbol :tag "Face" 
856                                              gnus-emphasis-highlight-words)))))
857   :group 'gnus-summary-visual)
858
859 (defcustom gnus-summary-show-article-charset-alist
860   nil
861   "Alist of number and charset.
862 The article will be shown with the charset corresponding to the
863 numbered argument.
864 For example: ((1 . cn-gb-2312) (2 . big5))."
865   :type '(repeat (cons (number :tag "Argument" 1)
866                        (symbol :tag "Charset")))
867   :group 'gnus-charset)
868
869 (defcustom gnus-preserve-marks t
870   "Whether marks are preserved when moving, copying and respooling messages."
871   :type 'boolean
872   :group 'gnus-summary-marks)
873
874 ;;; Internal variables
875
876 (defvar gnus-article-mime-handles nil)
877 (defvar gnus-article-decoded-p nil)
878 (defvar gnus-scores-exclude-files nil)
879 (defvar gnus-page-broken nil)
880 (defvar gnus-inhibit-mime-unbuttonizing nil)
881
882 (defvar gnus-original-article nil)
883 (defvar gnus-article-internal-prepare-hook nil)
884 (defvar gnus-newsgroup-process-stack nil)
885
886 (defvar gnus-thread-indent-array nil)
887 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
888 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
889   "Function called to sort the articles within a thread after it has been gathered together.")
890
891 ;; Avoid highlighting in kill files.
892 (defvar gnus-summary-inhibit-highlight nil)
893 (defvar gnus-newsgroup-selected-overlay nil)
894 (defvar gnus-inhibit-limiting nil)
895 (defvar gnus-newsgroup-adaptive-score-file nil)
896 (defvar gnus-current-score-file nil)
897 (defvar gnus-current-move-group nil)
898 (defvar gnus-current-copy-group nil)
899 (defvar gnus-current-crosspost-group nil)
900
901 (defvar gnus-newsgroup-dependencies nil)
902 (defvar gnus-newsgroup-adaptive nil)
903 (defvar gnus-summary-display-article-function nil)
904 (defvar gnus-summary-highlight-line-function nil
905   "Function called after highlighting a summary line.")
906
907 (defvar gnus-summary-line-format-alist
908   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
909     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
910     (?s gnus-tmp-subject-or-nil ?s)
911     (?n gnus-tmp-name ?s)
912     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
913         ?s)
914     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
915             gnus-tmp-from) ?s)
916     (?F gnus-tmp-from ?s)
917     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
918     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
919     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
920     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
921     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
922     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
923     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
924     (?L gnus-tmp-lines ?d)
925     (?I gnus-tmp-indentation ?s)
926     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
927     (?R gnus-tmp-replied ?c)
928     (?\[ gnus-tmp-opening-bracket ?c)
929     (?\] gnus-tmp-closing-bracket ?c)
930     (?\> (make-string gnus-tmp-level ? ) ?s)
931     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
932     (?i gnus-tmp-score ?d)
933     (?z gnus-tmp-score-char ?c)
934     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
935     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
936     (?U gnus-tmp-unread ?c)
937     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
938     (?t (gnus-summary-number-of-articles-in-thread
939          (and (boundp 'thread) (car thread)) gnus-tmp-level)
940         ?d)
941     (?e (gnus-summary-number-of-articles-in-thread
942          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
943         ?c)
944     (?u gnus-tmp-user-defined ?s)
945     (?P (gnus-pick-line-number) ?d))
946   "An alist of format specifications that can appear in summary lines,
947 and what variables they correspond with, along with the type of the
948 variable (string, integer, character, etc).")
949
950 (defvar gnus-summary-dummy-line-format-alist
951   `((?S gnus-tmp-subject ?s)
952     (?N gnus-tmp-number ?d)
953     (?u gnus-tmp-user-defined ?s)))
954
955 (defvar gnus-summary-mode-line-format-alist
956   `((?G gnus-tmp-group-name ?s)
957     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
958     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
959     (?A gnus-tmp-article-number ?d)
960     (?Z gnus-tmp-unread-and-unselected ?s)
961     (?V gnus-version ?s)
962     (?U gnus-tmp-unread-and-unticked ?d)
963     (?S gnus-tmp-subject ?s)
964     (?e gnus-tmp-unselected ?d)
965     (?u gnus-tmp-user-defined ?s)
966     (?d (length gnus-newsgroup-dormant) ?d)
967     (?t (length gnus-newsgroup-marked) ?d)
968     (?r (length gnus-newsgroup-reads) ?d)
969     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
970     (?E gnus-newsgroup-expunged-tally ?d)
971     (?s (gnus-current-score-file-nondirectory) ?s)))
972
973 (defvar gnus-last-search-regexp nil
974   "Default regexp for article search command.")
975
976 (defvar gnus-last-shell-command nil
977   "Default shell command on article.")
978
979 (defvar gnus-newsgroup-begin nil)
980 (defvar gnus-newsgroup-end nil)
981 (defvar gnus-newsgroup-last-rmail nil)
982 (defvar gnus-newsgroup-last-mail nil)
983 (defvar gnus-newsgroup-last-folder nil)
984 (defvar gnus-newsgroup-last-file nil)
985 (defvar gnus-newsgroup-auto-expire nil)
986 (defvar gnus-newsgroup-active nil)
987
988 (defvar gnus-newsgroup-data nil)
989 (defvar gnus-newsgroup-data-reverse nil)
990 (defvar gnus-newsgroup-limit nil)
991 (defvar gnus-newsgroup-limits nil)
992
993 (defvar gnus-newsgroup-unreads nil
994   "List of unread articles in the current newsgroup.")
995
996 (defvar gnus-newsgroup-unselected nil
997   "List of unselected unread articles in the current newsgroup.")
998
999 (defvar gnus-newsgroup-reads nil
1000   "Alist of read articles and article marks in the current newsgroup.")
1001
1002 (defvar gnus-newsgroup-expunged-tally nil)
1003
1004 (defvar gnus-newsgroup-marked nil
1005   "List of ticked articles in the current newsgroup (a subset of unread art).")
1006
1007 (defvar gnus-newsgroup-killed nil
1008   "List of ranges of articles that have been through the scoring process.")
1009
1010 (defvar gnus-newsgroup-cached nil
1011   "List of articles that come from the article cache.")
1012
1013 (defvar gnus-newsgroup-saved nil
1014   "List of articles that have been saved.")
1015
1016 (defvar gnus-newsgroup-kill-headers nil)
1017
1018 (defvar gnus-newsgroup-replied nil
1019   "List of articles that have been replied to in the current newsgroup.")
1020
1021 (defvar gnus-newsgroup-expirable nil
1022   "List of articles in the current newsgroup that can be expired.")
1023
1024 (defvar gnus-newsgroup-processable nil
1025   "List of articles in the current newsgroup that can be processed.")
1026
1027 (defvar gnus-newsgroup-downloadable nil
1028   "List of articles in the current newsgroup that can be processed.")
1029
1030 (defvar gnus-newsgroup-undownloaded nil
1031   "List of articles in the current newsgroup that haven't been downloaded..")
1032
1033 (defvar gnus-newsgroup-unsendable nil
1034   "List of articles in the current newsgroup that won't be sent.")
1035
1036 (defvar gnus-newsgroup-bookmarks nil
1037   "List of articles in the current newsgroup that have bookmarks.")
1038
1039 (defvar gnus-newsgroup-dormant nil
1040   "List of dormant articles in the current newsgroup.")
1041
1042 (defvar gnus-newsgroup-scored nil
1043   "List of scored articles in the current newsgroup.")
1044
1045 (defvar gnus-newsgroup-headers nil
1046   "List of article headers in the current newsgroup.")
1047
1048 (defvar gnus-newsgroup-threads nil)
1049
1050 (defvar gnus-newsgroup-prepared nil
1051   "Whether the current group has been prepared properly.")
1052
1053 (defvar gnus-newsgroup-ancient nil
1054   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-sparse nil)
1057
1058 (defvar gnus-current-article nil)
1059 (defvar gnus-article-current nil)
1060 (defvar gnus-current-headers nil)
1061 (defvar gnus-have-all-headers nil)
1062 (defvar gnus-last-article nil)
1063 (defvar gnus-newsgroup-history nil)
1064 (defvar gnus-newsgroup-charset nil)
1065 (defvar gnus-newsgroup-ephemeral-charset nil)
1066 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1067
1068 (defconst gnus-summary-local-variables
1069   '(gnus-newsgroup-name
1070     gnus-newsgroup-begin gnus-newsgroup-end
1071     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1072     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1073     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1074     gnus-newsgroup-unselected gnus-newsgroup-marked
1075     gnus-newsgroup-reads gnus-newsgroup-saved
1076     gnus-newsgroup-replied gnus-newsgroup-expirable
1077     gnus-newsgroup-processable gnus-newsgroup-killed
1078     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1079     gnus-newsgroup-unsendable
1080     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1081     gnus-newsgroup-headers gnus-newsgroup-threads
1082     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1083     gnus-current-article gnus-current-headers gnus-have-all-headers
1084     gnus-last-article gnus-article-internal-prepare-hook
1085     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1086     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1087     gnus-thread-expunge-below
1088     gnus-score-alist gnus-current-score-file
1089     (gnus-summary-expunge-below . global)
1090     (gnus-summary-mark-below . global)
1091     gnus-newsgroup-active gnus-scores-exclude-files
1092     gnus-newsgroup-history gnus-newsgroup-ancient
1093     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1094     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1095     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1096     (gnus-newsgroup-expunged-tally . 0)
1097     gnus-cache-removable-articles gnus-newsgroup-cached
1098     gnus-newsgroup-data gnus-newsgroup-data-reverse
1099     gnus-newsgroup-limit gnus-newsgroup-limits
1100     gnus-newsgroup-charset)
1101   "Variables that are buffer-local to the summary buffers.")
1102
1103 ;; Byte-compiler warning.
1104 (defvar gnus-article-mode-map)
1105
1106 ;; MIME stuff.
1107
1108 (defvar gnus-decode-encoded-word-methods
1109   '(mail-decode-encoded-word-string)
1110   "List of methods used to decode encoded words.
1111
1112 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1113 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1114 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1115 whose names match REGEXP.
1116
1117 For example:
1118 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1119  mail-decode-encoded-word-string
1120  (\"chinese\" . rfc1843-decode-string))
1121 ")
1122
1123 (defvar gnus-decode-encoded-word-methods-cache nil)
1124
1125 (defun gnus-multi-decode-encoded-word-string (string)
1126   "Apply the functions from `gnus-encoded-word-methods' that match."
1127   (unless (and gnus-decode-encoded-word-methods-cache
1128                (eq gnus-newsgroup-name
1129                    (car gnus-decode-encoded-word-methods-cache)))
1130     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1131     (mapcar (lambda (x)
1132               (if (symbolp x)
1133                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1134                 (if (and gnus-newsgroup-name
1135                          (string-match (car x) gnus-newsgroup-name))
1136                     (nconc gnus-decode-encoded-word-methods-cache
1137                            (list (cdr x))))))
1138           gnus-decode-encoded-word-methods))
1139   (let ((xlist gnus-decode-encoded-word-methods-cache))
1140     (pop xlist)
1141     (while xlist
1142       (setq string (funcall (pop xlist) string))))
1143   string)
1144
1145 ;; Subject simplification.
1146
1147 (defun gnus-simplify-whitespace (str)
1148   "Remove excessive whitespace."
1149   (let ((mystr str))
1150     ;; Multiple spaces.
1151     (while (string-match "[ \t][ \t]+" mystr)
1152       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1153                           " "
1154                           (substring mystr (match-end 0)))))
1155     ;; Leading spaces.
1156     (when (string-match "^[ \t]+" mystr)
1157       (setq mystr (substring mystr (match-end 0))))
1158     ;; Trailing spaces.
1159     (when (string-match "[ \t]+$" mystr)
1160       (setq mystr (substring mystr 0 (match-beginning 0))))
1161     mystr))
1162
1163 (defsubst gnus-simplify-subject-re (subject)
1164   "Remove \"Re:\" from subject lines."
1165   (if (string-match "^[Rr][Ee]: *" subject)
1166       (substring subject (match-end 0))
1167     subject))
1168
1169 (defun gnus-simplify-subject (subject &optional re-only)
1170   "Remove `Re:' and words in parentheses.
1171 If RE-ONLY is non-nil, strip leading `Re:'s only."
1172   (let ((case-fold-search t))           ;Ignore case.
1173     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1174     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1175       (setq subject (substring subject (match-end 0))))
1176     ;; Remove uninteresting prefixes.
1177     (when (and (not re-only)
1178                gnus-simplify-ignored-prefixes
1179                (string-match gnus-simplify-ignored-prefixes subject))
1180       (setq subject (substring subject (match-end 0))))
1181     ;; Remove words in parentheses from end.
1182     (unless re-only
1183       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1184         (setq subject (substring subject 0 (match-beginning 0)))))
1185     ;; Return subject string.
1186     subject))
1187
1188 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1189 ;; all whitespace.
1190 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1191   (goto-char (point-min))
1192   (while (re-search-forward regexp nil t)
1193     (replace-match (or newtext ""))))
1194
1195 (defun gnus-simplify-buffer-fuzzy ()
1196   "Simplify string in the buffer fuzzily.
1197 The string in the accessible portion of the current buffer is simplified.
1198 It is assumed to be a single-line subject.
1199 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1200 matter is removed.  Additional things can be deleted by setting
1201 gnus-simplify-subject-fuzzy-regexp."
1202   (let ((case-fold-search t)
1203         (modified-tick))
1204     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1205
1206     (while (not (eq modified-tick (buffer-modified-tick)))
1207       (setq modified-tick (buffer-modified-tick))
1208       (cond
1209        ((listp gnus-simplify-subject-fuzzy-regexp)
1210         (mapcar 'gnus-simplify-buffer-fuzzy-step
1211                 gnus-simplify-subject-fuzzy-regexp))
1212        (gnus-simplify-subject-fuzzy-regexp
1213         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1214       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1215       (gnus-simplify-buffer-fuzzy-step
1216        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1217       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1218
1219     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1220     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1221     (gnus-simplify-buffer-fuzzy-step " $")
1222     (gnus-simplify-buffer-fuzzy-step "^ +")))
1223
1224 (defun gnus-simplify-subject-fuzzy (subject)
1225   "Simplify a subject string fuzzily.
1226 See `gnus-simplify-buffer-fuzzy' for details."
1227   (save-excursion
1228     (gnus-set-work-buffer)
1229     (let ((case-fold-search t))
1230       ;; Remove uninteresting prefixes.
1231       (when (and gnus-simplify-ignored-prefixes
1232                  (string-match gnus-simplify-ignored-prefixes subject))
1233         (setq subject (substring subject (match-end 0))))
1234       (insert subject)
1235       (inline (gnus-simplify-buffer-fuzzy))
1236       (buffer-string))))
1237
1238 (defsubst gnus-simplify-subject-fully (subject)
1239   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1240   (cond
1241    (gnus-simplify-subject-functions
1242     (gnus-map-function gnus-simplify-subject-functions subject))
1243    ((null gnus-summary-gather-subject-limit)
1244     (gnus-simplify-subject-re subject))
1245    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1246     (gnus-simplify-subject-fuzzy subject))
1247    ((numberp gnus-summary-gather-subject-limit)
1248     (gnus-limit-string (gnus-simplify-subject-re subject)
1249                        gnus-summary-gather-subject-limit))
1250    (t
1251     subject)))
1252
1253 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1254   "Check whether two subjects are equal.
1255 If optional argument simple-first is t, first argument is already
1256 simplified."
1257   (cond
1258    ((null simple-first)
1259     (equal (gnus-simplify-subject-fully s1)
1260            (gnus-simplify-subject-fully s2)))
1261    (t
1262     (equal s1
1263            (gnus-simplify-subject-fully s2)))))
1264
1265 (defun gnus-summary-bubble-group ()
1266   "Increase the score of the current group.
1267 This is a handy function to add to `gnus-summary-exit-hook' to
1268 increase the score of each group you read."
1269   (gnus-group-add-score gnus-newsgroup-name))
1270
1271 \f
1272 ;;;
1273 ;;; Gnus summary mode
1274 ;;;
1275
1276 (put 'gnus-summary-mode 'mode-class 'special)
1277
1278 (when t
1279   ;; Non-orthogonal keys
1280
1281   (gnus-define-keys gnus-summary-mode-map
1282     " " gnus-summary-next-page
1283     "\177" gnus-summary-prev-page
1284     [delete] gnus-summary-prev-page
1285     [backspace] gnus-summary-prev-page
1286     "\r" gnus-summary-scroll-up
1287     "\M-\r" gnus-summary-scroll-down
1288     "n" gnus-summary-next-unread-article
1289     "p" gnus-summary-prev-unread-article
1290     "N" gnus-summary-next-article
1291     "P" gnus-summary-prev-article
1292     "\M-\C-n" gnus-summary-next-same-subject
1293     "\M-\C-p" gnus-summary-prev-same-subject
1294     "\M-n" gnus-summary-next-unread-subject
1295     "\M-p" gnus-summary-prev-unread-subject
1296     "." gnus-summary-first-unread-article
1297     "," gnus-summary-best-unread-article
1298     "\M-s" gnus-summary-search-article-forward
1299     "\M-r" gnus-summary-search-article-backward
1300     "<" gnus-summary-beginning-of-article
1301     ">" gnus-summary-end-of-article
1302     "j" gnus-summary-goto-article
1303     "^" gnus-summary-refer-parent-article
1304     "\M-^" gnus-summary-refer-article
1305     "u" gnus-summary-tick-article-forward
1306     "!" gnus-summary-tick-article-forward
1307     "U" gnus-summary-tick-article-backward
1308     "d" gnus-summary-mark-as-read-forward
1309     "D" gnus-summary-mark-as-read-backward
1310     "E" gnus-summary-mark-as-expirable
1311     "\M-u" gnus-summary-clear-mark-forward
1312     "\M-U" gnus-summary-clear-mark-backward
1313     "k" gnus-summary-kill-same-subject-and-select
1314     "\C-k" gnus-summary-kill-same-subject
1315     "\M-\C-k" gnus-summary-kill-thread
1316     "\M-\C-l" gnus-summary-lower-thread
1317     "e" gnus-summary-edit-article
1318     "#" gnus-summary-mark-as-processable
1319     "\M-#" gnus-summary-unmark-as-processable
1320     "\M-\C-t" gnus-summary-toggle-threads
1321     "\M-\C-s" gnus-summary-show-thread
1322     "\M-\C-h" gnus-summary-hide-thread
1323     "\M-\C-f" gnus-summary-next-thread
1324     "\M-\C-b" gnus-summary-prev-thread
1325     "\M-\C-u" gnus-summary-up-thread
1326     "\M-\C-d" gnus-summary-down-thread
1327     "&" gnus-summary-execute-command
1328     "c" gnus-summary-catchup-and-exit
1329     "\C-w" gnus-summary-mark-region-as-read
1330     "\C-t" gnus-summary-toggle-truncation
1331     "?" gnus-summary-mark-as-dormant
1332     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1333     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1334     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1335     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1336     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1337     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1338     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1339     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1340     "=" gnus-summary-expand-window
1341     "\C-x\C-s" gnus-summary-reselect-current-group
1342     "\M-g" gnus-summary-rescan-group
1343     "w" gnus-summary-stop-page-breaking
1344     "\C-c\C-r" gnus-summary-caesar-message
1345     "f" gnus-summary-followup
1346     "F" gnus-summary-followup-with-original
1347     "C" gnus-summary-cancel-article
1348     "r" gnus-summary-reply
1349     "R" gnus-summary-reply-with-original
1350     "\C-c\C-f" gnus-summary-mail-forward
1351     "o" gnus-summary-save-article
1352     "\C-o" gnus-summary-save-article-mail
1353     "|" gnus-summary-pipe-output
1354     "\M-k" gnus-summary-edit-local-kill
1355     "\M-K" gnus-summary-edit-global-kill
1356     ;; "V" gnus-version
1357     "\C-c\C-d" gnus-summary-describe-group
1358     "q" gnus-summary-exit
1359     "Q" gnus-summary-exit-no-update
1360     "\C-c\C-i" gnus-info-find-node
1361     gnus-mouse-2 gnus-mouse-pick-article
1362     "m" gnus-summary-mail-other-window
1363     "a" gnus-summary-post-news
1364     "x" gnus-summary-limit-to-unread
1365     "s" gnus-summary-isearch-article
1366     "t" gnus-summary-toggle-header
1367     "g" gnus-summary-show-article
1368     "l" gnus-summary-goto-last-article
1369     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1370     "\C-d" gnus-summary-enter-digest-group
1371     "\M-\C-d" gnus-summary-read-document
1372     "\M-\C-e" gnus-summary-edit-parameters
1373     "\M-\C-a" gnus-summary-customize-parameters
1374     "\C-c\C-b" gnus-bug
1375     "*" gnus-cache-enter-article
1376     "\M-*" gnus-cache-remove-article
1377     "\M-&" gnus-summary-universal-argument
1378     "\C-l" gnus-recenter
1379     "I" gnus-summary-increase-score
1380     "L" gnus-summary-lower-score
1381     "\M-i" gnus-symbolic-argument
1382     "h" gnus-summary-select-article-buffer
1383
1384     "b" gnus-article-view-part
1385     "\M-t" gnus-summary-toggle-display-buttonized
1386
1387     "V" gnus-summary-score-map
1388     "X" gnus-uu-extract-map
1389     "S" gnus-summary-send-map)
1390
1391   ;; Sort of orthogonal keymap
1392   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1393     "t" gnus-summary-tick-article-forward
1394     "!" gnus-summary-tick-article-forward
1395     "d" gnus-summary-mark-as-read-forward
1396     "r" gnus-summary-mark-as-read-forward
1397     "c" gnus-summary-clear-mark-forward
1398     " " gnus-summary-clear-mark-forward
1399     "e" gnus-summary-mark-as-expirable
1400     "x" gnus-summary-mark-as-expirable
1401     "?" gnus-summary-mark-as-dormant
1402     "b" gnus-summary-set-bookmark
1403     "B" gnus-summary-remove-bookmark
1404     "#" gnus-summary-mark-as-processable
1405     "\M-#" gnus-summary-unmark-as-processable
1406     "S" gnus-summary-limit-include-expunged
1407     "C" gnus-summary-catchup
1408     "H" gnus-summary-catchup-to-here
1409     "\C-c" gnus-summary-catchup-all
1410     "k" gnus-summary-kill-same-subject-and-select
1411     "K" gnus-summary-kill-same-subject
1412     "P" gnus-uu-mark-map)
1413
1414   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1415     "c" gnus-summary-clear-above
1416     "u" gnus-summary-tick-above
1417     "m" gnus-summary-mark-above
1418     "k" gnus-summary-kill-below)
1419
1420   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1421     "/" gnus-summary-limit-to-subject
1422     "n" gnus-summary-limit-to-articles
1423     "w" gnus-summary-pop-limit
1424     "s" gnus-summary-limit-to-subject
1425     "a" gnus-summary-limit-to-author
1426     "u" gnus-summary-limit-to-unread
1427     "m" gnus-summary-limit-to-marks
1428     "M" gnus-summary-limit-exclude-marks
1429     "v" gnus-summary-limit-to-score
1430     "*" gnus-summary-limit-include-cached
1431     "D" gnus-summary-limit-include-dormant
1432     "T" gnus-summary-limit-include-thread
1433     "d" gnus-summary-limit-exclude-dormant
1434     "t" gnus-summary-limit-to-age
1435     "x" gnus-summary-limit-to-extra 
1436     "E" gnus-summary-limit-include-expunged
1437     "c" gnus-summary-limit-exclude-childless-dormant
1438     "C" gnus-summary-limit-mark-excluded-as-read)
1439
1440   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1441     "n" gnus-summary-next-unread-article
1442     "p" gnus-summary-prev-unread-article
1443     "N" gnus-summary-next-article
1444     "P" gnus-summary-prev-article
1445     "\C-n" gnus-summary-next-same-subject
1446     "\C-p" gnus-summary-prev-same-subject
1447     "\M-n" gnus-summary-next-unread-subject
1448     "\M-p" gnus-summary-prev-unread-subject
1449     "f" gnus-summary-first-unread-article
1450     "b" gnus-summary-best-unread-article
1451     "j" gnus-summary-goto-article
1452     "g" gnus-summary-goto-subject
1453     "l" gnus-summary-goto-last-article
1454     "o" gnus-summary-pop-article)
1455
1456   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1457     "k" gnus-summary-kill-thread
1458     "l" gnus-summary-lower-thread
1459     "i" gnus-summary-raise-thread
1460     "T" gnus-summary-toggle-threads
1461     "t" gnus-summary-rethread-current
1462     "^" gnus-summary-reparent-thread
1463     "s" gnus-summary-show-thread
1464     "S" gnus-summary-show-all-threads
1465     "h" gnus-summary-hide-thread
1466     "H" gnus-summary-hide-all-threads
1467     "n" gnus-summary-next-thread
1468     "p" gnus-summary-prev-thread
1469     "u" gnus-summary-up-thread
1470     "o" gnus-summary-top-thread
1471     "d" gnus-summary-down-thread
1472     "#" gnus-uu-mark-thread
1473     "\M-#" gnus-uu-unmark-thread)
1474
1475   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1476     "g" gnus-summary-prepare
1477     "c" gnus-summary-insert-cached-articles)
1478
1479   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1480     "c" gnus-summary-catchup-and-exit
1481     "C" gnus-summary-catchup-all-and-exit
1482     "E" gnus-summary-exit-no-update
1483     "Q" gnus-summary-exit
1484     "Z" gnus-summary-exit
1485     "n" gnus-summary-catchup-and-goto-next-group
1486     "R" gnus-summary-reselect-current-group
1487     "G" gnus-summary-rescan-group
1488     "N" gnus-summary-next-group
1489     "s" gnus-summary-save-newsrc
1490     "P" gnus-summary-prev-group)
1491
1492   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1493     " " gnus-summary-next-page
1494     "n" gnus-summary-next-page
1495     "\177" gnus-summary-prev-page
1496     [delete] gnus-summary-prev-page
1497     "p" gnus-summary-prev-page
1498     "\r" gnus-summary-scroll-up
1499     "\M-\r" gnus-summary-scroll-down
1500     "<" gnus-summary-beginning-of-article
1501     ">" gnus-summary-end-of-article
1502     "b" gnus-summary-beginning-of-article
1503     "e" gnus-summary-end-of-article
1504     "^" gnus-summary-refer-parent-article
1505     "r" gnus-summary-refer-parent-article
1506     "D" gnus-summary-enter-digest-group
1507     "R" gnus-summary-refer-references
1508     "T" gnus-summary-refer-thread
1509     "g" gnus-summary-show-article
1510     "s" gnus-summary-isearch-article
1511     "P" gnus-summary-print-article
1512     "t" gnus-article-babel)
1513
1514   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1515     "b" gnus-article-add-buttons
1516     "B" gnus-article-add-buttons-to-head
1517     "o" gnus-article-treat-overstrike
1518     "e" gnus-article-emphasize
1519     "w" gnus-article-fill-cited-article
1520     "Q" gnus-article-fill-long-lines
1521     "C" gnus-article-capitalize-sentences
1522     "c" gnus-article-remove-cr
1523     "q" gnus-article-de-quoted-unreadable
1524     "f" gnus-article-display-x-face
1525     "l" gnus-summary-stop-page-breaking
1526     "r" gnus-summary-caesar-message
1527     "t" gnus-article-hide-headers
1528     "v" gnus-summary-verbose-headers
1529     "H" gnus-article-strip-headers-in-body
1530     "d" gnus-article-treat-dumbquotes)
1531
1532   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1533     "a" gnus-article-hide
1534     "h" gnus-article-hide-headers
1535     "b" gnus-article-hide-boring-headers
1536     "s" gnus-article-hide-signature
1537     "c" gnus-article-hide-citation
1538     "C" gnus-article-hide-citation-in-followups
1539     "l" gnus-article-hide-list-identifiers
1540     "p" gnus-article-hide-pgp
1541     "B" gnus-article-strip-banner
1542     "P" gnus-article-hide-pem
1543     "\C-c" gnus-article-hide-citation-maybe)
1544
1545   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1546     "a" gnus-article-highlight
1547     "h" gnus-article-highlight-headers
1548     "c" gnus-article-highlight-citation
1549     "s" gnus-article-highlight-signature)
1550
1551   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1552     "w" gnus-article-decode-mime-words
1553     "c" gnus-article-decode-charset
1554     "v" gnus-mime-view-all-parts
1555     "b" gnus-article-view-part)
1556
1557   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1558     "z" gnus-article-date-ut
1559     "u" gnus-article-date-ut
1560     "l" gnus-article-date-local
1561     "e" gnus-article-date-lapsed
1562     "o" gnus-article-date-original
1563     "i" gnus-article-date-iso8601
1564     "s" gnus-article-date-user)
1565
1566   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1567     "t" gnus-article-remove-trailing-blank-lines
1568     "l" gnus-article-strip-leading-blank-lines
1569     "m" gnus-article-strip-multiple-blank-lines
1570     "a" gnus-article-strip-blank-lines
1571     "A" gnus-article-strip-all-blank-lines
1572     "s" gnus-article-strip-leading-space
1573     "e" gnus-article-strip-trailing-space)
1574
1575   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1576     "v" gnus-version
1577     "f" gnus-summary-fetch-faq
1578     "d" gnus-summary-describe-group
1579     "h" gnus-summary-describe-briefly
1580     "i" gnus-info-find-node)
1581
1582   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1583     "e" gnus-summary-expire-articles
1584     "\M-\C-e" gnus-summary-expire-articles-now
1585     "\177" gnus-summary-delete-article
1586     [delete] gnus-summary-delete-article
1587     [backspace] gnus-summary-delete-article
1588     "m" gnus-summary-move-article
1589     "r" gnus-summary-respool-article
1590     "w" gnus-summary-edit-article
1591     "c" gnus-summary-copy-article
1592     "B" gnus-summary-crosspost-article
1593     "q" gnus-summary-respool-query
1594     "t" gnus-summary-respool-trace
1595     "i" gnus-summary-import-article
1596     "p" gnus-summary-article-posted-p)
1597
1598   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1599     "o" gnus-summary-save-article
1600     "m" gnus-summary-save-article-mail
1601     "F" gnus-summary-write-article-file
1602     "r" gnus-summary-save-article-rmail
1603     "f" gnus-summary-save-article-file
1604     "b" gnus-summary-save-article-body-file
1605     "h" gnus-summary-save-article-folder
1606     "v" gnus-summary-save-article-vm
1607     "p" gnus-summary-pipe-output
1608     "s" gnus-soup-add-article)
1609
1610   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1611     "b" gnus-summary-display-buttonized
1612     "m" gnus-summary-repair-multipart
1613     "v" gnus-article-view-part
1614     "o" gnus-article-save-part
1615     "c" gnus-article-copy-part
1616     "e" gnus-article-externalize-part
1617     "i" gnus-article-inline-part
1618     "|" gnus-article-pipe-part))
1619
1620 (defun gnus-summary-make-menu-bar ()
1621   (gnus-turn-off-edit-menu 'summary)
1622
1623   (unless (boundp 'gnus-summary-misc-menu)
1624
1625     (easy-menu-define
1626      gnus-summary-kill-menu gnus-summary-mode-map ""
1627      (cons
1628       "Score"
1629       (nconc
1630        (list
1631         ["Enter score..." gnus-summary-score-entry t]
1632         ["Customize" gnus-score-customize t])
1633        (gnus-make-score-map 'increase)
1634        (gnus-make-score-map 'lower)
1635        '(("Mark"
1636           ["Kill below" gnus-summary-kill-below t]
1637           ["Mark above" gnus-summary-mark-above t]
1638           ["Tick above" gnus-summary-tick-above t]
1639           ["Clear above" gnus-summary-clear-above t])
1640          ["Current score" gnus-summary-current-score t]
1641          ["Set score" gnus-summary-set-score t]
1642          ["Switch current score file..." gnus-score-change-score-file t]
1643          ["Set mark below..." gnus-score-set-mark-below t]
1644          ["Set expunge below..." gnus-score-set-expunge-below t]
1645          ["Edit current score file" gnus-score-edit-current-scores t]
1646          ["Edit score file" gnus-score-edit-file t]
1647          ["Trace score" gnus-score-find-trace t]
1648          ["Find words" gnus-score-find-favourite-words t]
1649          ["Rescore buffer" gnus-summary-rescore t]
1650          ["Increase score..." gnus-summary-increase-score t]
1651          ["Lower score..." gnus-summary-lower-score t]))))
1652
1653     ;; Define both the Article menu in the summary buffer and the equivalent
1654     ;; Commands menu in the article buffer here for consistency.
1655     (let ((innards
1656            '(("Hide"
1657               ["All" gnus-article-hide t]
1658               ["Headers" gnus-article-hide-headers t]
1659               ["Signature" gnus-article-hide-signature t]
1660               ["Citation" gnus-article-hide-citation t]
1661               ["List identifiers" gnus-article-hide-list-identifiers t]
1662               ["PGP" gnus-article-hide-pgp t]
1663               ["Banner" gnus-article-strip-banner t]
1664               ["Boring headers" gnus-article-hide-boring-headers t])
1665              ("Highlight"
1666               ["All" gnus-article-highlight t]
1667               ["Headers" gnus-article-highlight-headers t]
1668               ["Signature" gnus-article-highlight-signature t]
1669               ["Citation" gnus-article-highlight-citation t])
1670              ("MIME"
1671               ["Words" gnus-article-decode-mime-words t]
1672               ["Charset" gnus-article-decode-charset t]
1673               ["QP" gnus-article-de-quoted-unreadable t]
1674               ["View all" gnus-mime-view-all-parts t])
1675              ("Date"
1676               ["Local" gnus-article-date-local t]
1677               ["ISO8601" gnus-article-date-iso8601 t]
1678               ["UT" gnus-article-date-ut t]
1679               ["Original" gnus-article-date-original t]
1680               ["Lapsed" gnus-article-date-lapsed t]
1681               ["User-defined" gnus-article-date-user t])
1682              ("Washing"
1683               ("Remove Blanks"
1684                ["Leading" gnus-article-strip-leading-blank-lines t]
1685                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1686                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1687                ["All of the above" gnus-article-strip-blank-lines t]
1688                ["All" gnus-article-strip-all-blank-lines t]
1689                ["Leading space" gnus-article-strip-leading-space t]
1690                ["Trailing space" gnus-article-strip-trailing-space t])
1691               ["Overstrike" gnus-article-treat-overstrike t]
1692               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1693               ["Emphasis" gnus-article-emphasize t]
1694               ["Word wrap" gnus-article-fill-cited-article t]
1695               ["Fill long lines" gnus-article-fill-long-lines t]
1696               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1697               ["CR" gnus-article-remove-cr t]
1698               ["Show X-Face" gnus-article-display-x-face t]
1699               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1700               ["Rot 13" gnus-summary-caesar-message t]
1701               ["Unix pipe" gnus-summary-pipe-message t]
1702               ["Add buttons" gnus-article-add-buttons t]
1703               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1704               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1705               ["Verbose header" gnus-summary-verbose-headers t]
1706               ["Toggle header" gnus-summary-toggle-header t]
1707               ["HZ" gnus-article-decode-HZ t])
1708              ("Output"
1709               ["Save in default format" gnus-summary-save-article t]
1710               ["Save in file" gnus-summary-save-article-file t]
1711               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1712               ["Save in MH folder" gnus-summary-save-article-folder t]
1713               ["Save in VM folder" gnus-summary-save-article-vm t]
1714               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1715               ["Save body in file" gnus-summary-save-article-body-file t]
1716               ["Pipe through a filter" gnus-summary-pipe-output t]
1717               ["Add to SOUP packet" gnus-soup-add-article t]
1718               ["Print" gnus-summary-print-article t])
1719              ("Backend"
1720               ["Respool article..." gnus-summary-respool-article t]
1721               ["Move article..." gnus-summary-move-article
1722                (gnus-check-backend-function
1723                 'request-move-article gnus-newsgroup-name)]
1724               ["Copy article..." gnus-summary-copy-article t]
1725               ["Crosspost article..." gnus-summary-crosspost-article
1726                (gnus-check-backend-function
1727                 'request-replace-article gnus-newsgroup-name)]
1728               ["Import file..." gnus-summary-import-article t]
1729               ["Check if posted" gnus-summary-article-posted-p t]
1730               ["Edit article" gnus-summary-edit-article
1731                (not (gnus-group-read-only-p))]
1732               ["Delete article" gnus-summary-delete-article
1733                (gnus-check-backend-function
1734                 'request-expire-articles gnus-newsgroup-name)]
1735               ["Query respool" gnus-summary-respool-query t]
1736               ["Trace respool" gnus-summary-respool-trace t]
1737               ["Delete expirable articles" gnus-summary-expire-articles-now
1738                (gnus-check-backend-function
1739                 'request-expire-articles gnus-newsgroup-name)])
1740              ("Extract"
1741               ["Uudecode" gnus-uu-decode-uu t]
1742               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1743               ["Unshar" gnus-uu-decode-unshar t]
1744               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1745               ["Save" gnus-uu-decode-save t]
1746               ["Binhex" gnus-uu-decode-binhex t]
1747               ["Postscript" gnus-uu-decode-postscript t])
1748              ("Cache"
1749               ["Enter article" gnus-cache-enter-article t]
1750               ["Remove article" gnus-cache-remove-article t])
1751              ["Translate" gnus-article-babel t]
1752              ["Select article buffer" gnus-summary-select-article-buffer t]
1753              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1754              ["Isearch article..." gnus-summary-isearch-article t]
1755              ["Beginning of the article" gnus-summary-beginning-of-article t]
1756              ["End of the article" gnus-summary-end-of-article t]
1757              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1758              ["Fetch referenced articles" gnus-summary-refer-references t]
1759              ["Fetch current thread" gnus-summary-refer-thread t]
1760              ["Fetch article with id..." gnus-summary-refer-article t]
1761              ["Redisplay" gnus-summary-show-article t])))
1762       (easy-menu-define
1763        gnus-summary-article-menu gnus-summary-mode-map ""
1764        (cons "Article" innards))
1765
1766       (easy-menu-define
1767        gnus-article-commands-menu gnus-article-mode-map ""
1768        (cons "Commands" innards)))
1769
1770     (easy-menu-define
1771      gnus-summary-thread-menu gnus-summary-mode-map ""
1772      '("Threads"
1773        ["Toggle threading" gnus-summary-toggle-threads t]
1774        ["Hide threads" gnus-summary-hide-all-threads t]
1775        ["Show threads" gnus-summary-show-all-threads t]
1776        ["Hide thread" gnus-summary-hide-thread t]
1777        ["Show thread" gnus-summary-show-thread t]
1778        ["Go to next thread" gnus-summary-next-thread t]
1779        ["Go to previous thread" gnus-summary-prev-thread t]
1780        ["Go down thread" gnus-summary-down-thread t]
1781        ["Go up thread" gnus-summary-up-thread t]
1782        ["Top of thread" gnus-summary-top-thread t]
1783        ["Mark thread as read" gnus-summary-kill-thread t]
1784        ["Lower thread score" gnus-summary-lower-thread t]
1785        ["Raise thread score" gnus-summary-raise-thread t]
1786        ["Rethread current" gnus-summary-rethread-current t]))
1787
1788     (easy-menu-define
1789      gnus-summary-post-menu gnus-summary-mode-map ""
1790      '("Post"
1791        ["Post an article" gnus-summary-post-news t]
1792        ["Followup" gnus-summary-followup t]
1793        ["Followup and yank" gnus-summary-followup-with-original t]
1794        ["Supersede article" gnus-summary-supersede-article t]
1795        ["Cancel article" gnus-summary-cancel-article t]
1796        ["Reply" gnus-summary-reply t]
1797        ["Reply and yank" gnus-summary-reply-with-original t]
1798        ["Wide reply" gnus-summary-wide-reply t]
1799        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1800        ["Mail forward" gnus-summary-mail-forward t]
1801        ["Post forward" gnus-summary-post-forward t]
1802        ["Digest and mail" gnus-uu-digest-mail-forward t]
1803        ["Digest and post" gnus-uu-digest-post-forward t]
1804        ["Resend message" gnus-summary-resend-message t]
1805        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1806        ["Send a mail" gnus-summary-mail-other-window t]
1807        ["Uuencode and post" gnus-uu-post-news t]
1808        ["Followup via news" gnus-summary-followup-to-mail t]
1809        ["Followup via news and yank"
1810         gnus-summary-followup-to-mail-with-original t]
1811        ;;("Draft"
1812        ;;["Send" gnus-summary-send-draft t]
1813        ;;["Send bounced" gnus-resend-bounced-mail t])
1814        ))
1815
1816     (easy-menu-define
1817      gnus-summary-misc-menu gnus-summary-mode-map ""
1818      '("Misc"
1819        ("Mark Read"
1820         ["Mark as read" gnus-summary-mark-as-read-forward t]
1821         ["Mark same subject and select"
1822          gnus-summary-kill-same-subject-and-select t]
1823         ["Mark same subject" gnus-summary-kill-same-subject t]
1824         ["Catchup" gnus-summary-catchup t]
1825         ["Catchup all" gnus-summary-catchup-all t]
1826         ["Catchup to here" gnus-summary-catchup-to-here t]
1827         ["Catchup region" gnus-summary-mark-region-as-read t]
1828         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1829        ("Mark Various"
1830         ["Tick" gnus-summary-tick-article-forward t]
1831         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1832         ["Remove marks" gnus-summary-clear-mark-forward t]
1833         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1834         ["Set bookmark" gnus-summary-set-bookmark t]
1835         ["Remove bookmark" gnus-summary-remove-bookmark t])
1836        ("Mark Limit"
1837         ["Marks..." gnus-summary-limit-to-marks t]
1838         ["Subject..." gnus-summary-limit-to-subject t]
1839         ["Author..." gnus-summary-limit-to-author t]
1840         ["Age..." gnus-summary-limit-to-age t]
1841         ["Extra..." gnus-summary-limit-to-extra t]
1842         ["Score" gnus-summary-limit-to-score t]
1843         ["Unread" gnus-summary-limit-to-unread t]
1844         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1845         ["Articles" gnus-summary-limit-to-articles t]
1846         ["Pop limit" gnus-summary-pop-limit t]
1847         ["Show dormant" gnus-summary-limit-include-dormant t]
1848         ["Hide childless dormant"
1849          gnus-summary-limit-exclude-childless-dormant t]
1850         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1851         ["Hide marked" gnus-summary-limit-exclude-marks t]
1852         ["Show expunged" gnus-summary-show-all-expunged t])
1853        ("Process Mark"
1854         ["Set mark" gnus-summary-mark-as-processable t]
1855         ["Remove mark" gnus-summary-unmark-as-processable t]
1856         ["Remove all marks" gnus-summary-unmark-all-processable t]
1857         ["Mark above" gnus-uu-mark-over t]
1858         ["Mark series" gnus-uu-mark-series t]
1859         ["Mark region" gnus-uu-mark-region t]
1860         ["Unmark region" gnus-uu-unmark-region t]
1861         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1862         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1863         ["Mark all" gnus-uu-mark-all t]
1864         ["Mark buffer" gnus-uu-mark-buffer t]
1865         ["Mark sparse" gnus-uu-mark-sparse t]
1866         ["Mark thread" gnus-uu-mark-thread t]
1867         ["Unmark thread" gnus-uu-unmark-thread t]
1868         ("Process Mark Sets"
1869          ["Kill" gnus-summary-kill-process-mark t]
1870          ["Yank" gnus-summary-yank-process-mark
1871           gnus-newsgroup-process-stack]
1872          ["Save" gnus-summary-save-process-mark t]))
1873        ("Scroll article"
1874         ["Page forward" gnus-summary-next-page t]
1875         ["Page backward" gnus-summary-prev-page t]
1876         ["Line forward" gnus-summary-scroll-up t])
1877        ("Move"
1878         ["Next unread article" gnus-summary-next-unread-article t]
1879         ["Previous unread article" gnus-summary-prev-unread-article t]
1880         ["Next article" gnus-summary-next-article t]
1881         ["Previous article" gnus-summary-prev-article t]
1882         ["Next unread subject" gnus-summary-next-unread-subject t]
1883         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1884         ["Next article same subject" gnus-summary-next-same-subject t]
1885         ["Previous article same subject" gnus-summary-prev-same-subject t]
1886         ["First unread article" gnus-summary-first-unread-article t]
1887         ["Best unread article" gnus-summary-best-unread-article t]
1888         ["Go to subject number..." gnus-summary-goto-subject t]
1889         ["Go to article number..." gnus-summary-goto-article t]
1890         ["Go to the last article" gnus-summary-goto-last-article t]
1891         ["Pop article off history" gnus-summary-pop-article t])
1892        ("Sort"
1893         ["Sort by number" gnus-summary-sort-by-number t]
1894         ["Sort by author" gnus-summary-sort-by-author t]
1895         ["Sort by subject" gnus-summary-sort-by-subject t]
1896         ["Sort by date" gnus-summary-sort-by-date t]
1897         ["Sort by score" gnus-summary-sort-by-score t]
1898         ["Sort by lines" gnus-summary-sort-by-lines t]
1899         ["Sort by characters" gnus-summary-sort-by-chars t])
1900        ("Help"
1901         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1902         ["Describe group" gnus-summary-describe-group t]
1903         ["Read manual" gnus-info-find-node t])
1904        ("Modes"
1905         ["Pick and read" gnus-pick-mode t]
1906         ["Binary" gnus-binary-mode t])
1907        ("Regeneration"
1908         ["Regenerate" gnus-summary-prepare t]
1909         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1910         ["Toggle threading" gnus-summary-toggle-threads t])
1911        ["Filter articles..." gnus-summary-execute-command t]
1912        ["Run command on subjects..." gnus-summary-universal-argument t]
1913        ["Search articles forward..." gnus-summary-search-article-forward t]
1914        ["Search articles backward..." gnus-summary-search-article-backward t]
1915        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1916        ["Expand window" gnus-summary-expand-window t]
1917        ["Expire expirable articles" gnus-summary-expire-articles
1918         (gnus-check-backend-function
1919          'request-expire-articles gnus-newsgroup-name)]
1920        ["Edit local kill file" gnus-summary-edit-local-kill t]
1921        ["Edit main kill file" gnus-summary-edit-global-kill t]
1922        ["Edit group parameters" gnus-summary-edit-parameters t]
1923        ["Customize group parameters" gnus-summary-customize-parameters t]
1924        ["Send a bug report" gnus-bug t]
1925        ("Exit"
1926         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1927         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1928         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1929         ["Exit group" gnus-summary-exit t]
1930         ["Exit group without updating" gnus-summary-exit-no-update t]
1931         ["Exit and goto next group" gnus-summary-next-group t]
1932         ["Exit and goto prev group" gnus-summary-prev-group t]
1933         ["Reselect group" gnus-summary-reselect-current-group t]
1934         ["Rescan group" gnus-summary-rescan-group t]
1935         ["Update dribble" gnus-summary-save-newsrc t])))
1936
1937     (gnus-run-hooks 'gnus-summary-menu-hook)))
1938
1939 (defun gnus-score-set-default (var value)
1940   "A version of set that updates the GNU Emacs menu-bar."
1941   (set var value)
1942   ;; It is the message that forces the active status to be updated.
1943   (message ""))
1944
1945 (defun gnus-make-score-map (type)
1946   "Make a summary score map of type TYPE."
1947   (if t
1948       nil
1949     (let ((headers '(("author" "from" string)
1950                      ("subject" "subject" string)
1951                      ("article body" "body" string)
1952                      ("article head" "head" string)
1953                      ("xref" "xref" string)
1954                      ("extra header" "extra" string)
1955                      ("lines" "lines" number)
1956                      ("followups to author" "followup" string)))
1957           (types '((number ("less than" <)
1958                            ("greater than" >)
1959                            ("equal" =))
1960                    (string ("substring" s)
1961                            ("exact string" e)
1962                            ("fuzzy string" f)
1963                            ("regexp" r))))
1964           (perms '(("temporary" (current-time-string))
1965                    ("permanent" nil)
1966                    ("immediate" now)))
1967           header)
1968       (list
1969        (apply
1970         'nconc
1971         (list
1972          (if (eq type 'lower)
1973              "Lower score"
1974            "Increase score"))
1975         (let (outh)
1976           (while headers
1977             (setq header (car headers))
1978             (setq outh
1979                   (cons
1980                    (apply
1981                     'nconc
1982                     (list (car header))
1983                     (let ((ts (cdr (assoc (nth 2 header) types)))
1984                           outt)
1985                       (while ts
1986                         (setq outt
1987                               (cons
1988                                (apply
1989                                 'nconc
1990                                 (list (caar ts))
1991                                 (let ((ps perms)
1992                                       outp)
1993                                   (while ps
1994                                     (setq outp
1995                                           (cons
1996                                            (vector
1997                                             (caar ps)
1998                                             (list
1999                                              'gnus-summary-score-entry
2000                                              (nth 1 header)
2001                                              (if (or (string= (nth 1 header)
2002                                                               "head")
2003                                                      (string= (nth 1 header)
2004                                                               "body"))
2005                                                  ""
2006                                                (list 'gnus-summary-header
2007                                                      (nth 1 header)))
2008                                              (list 'quote (nth 1 (car ts)))
2009                                              (list 'gnus-score-delta-default
2010                                                    nil)
2011                                              (nth 1 (car ps))
2012                                              t)
2013                                             t)
2014                                            outp))
2015                                     (setq ps (cdr ps)))
2016                                   (list (nreverse outp))))
2017                                outt))
2018                         (setq ts (cdr ts)))
2019                       (list (nreverse outt))))
2020                    outh))
2021             (setq headers (cdr headers)))
2022           (list (nreverse outh))))))))
2023
2024 \f
2025
2026 (defun gnus-summary-mode (&optional group)
2027   "Major mode for reading articles.
2028
2029 All normal editing commands are switched off.
2030 \\<gnus-summary-mode-map>
2031 Each line in this buffer represents one article.  To read an
2032 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2033 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2034 respectively.
2035
2036 You can also post articles and send mail from this buffer.  To
2037 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2038 of an article, type `\\[gnus-summary-reply]'.
2039
2040 There are approx. one gazillion commands you can execute in this
2041 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2042
2043 The following commands are available:
2044
2045 \\{gnus-summary-mode-map}"
2046   (interactive)
2047   (when (gnus-visual-p 'summary-menu 'menu)
2048     (gnus-summary-make-menu-bar))
2049   (kill-all-local-variables)
2050   (gnus-summary-make-local-variables)
2051   (gnus-make-thread-indent-array)
2052   (gnus-simplify-mode-line)
2053   (setq major-mode 'gnus-summary-mode)
2054   (setq mode-name "Summary")
2055   (make-local-variable 'minor-mode-alist)
2056   (use-local-map gnus-summary-mode-map)
2057   (buffer-disable-undo)
2058   (setq buffer-read-only t)             ;Disable modification
2059   (setq truncate-lines t)
2060   (setq selective-display t)
2061   (setq selective-display-ellipses t)   ;Display `...'
2062   (gnus-summary-set-display-table)
2063   (gnus-set-default-directory)
2064   (setq gnus-newsgroup-name group)
2065   (make-local-variable 'gnus-summary-line-format)
2066   (make-local-variable 'gnus-summary-line-format-spec)
2067   (make-local-variable 'gnus-summary-dummy-line-format)
2068   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2069   (make-local-variable 'gnus-summary-mark-positions)
2070   (make-local-hook 'pre-command-hook)
2071   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2072   (gnus-run-hooks 'gnus-summary-mode-hook)
2073   (mm-enable-multibyte)
2074   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2075   (gnus-update-summary-mark-positions))
2076
2077 (defun gnus-summary-make-local-variables ()
2078   "Make all the local summary buffer variables."
2079   (let (global)
2080     (dolist (local gnus-summary-local-variables)
2081       (if (consp local)
2082           (progn
2083             (if (eq (cdr local) 'global)
2084                 ;; Copy the global value of the variable.
2085                 (setq global (symbol-value (car local)))
2086               ;; Use the value from the list.
2087               (setq global (eval (cdr local))))
2088             (set (make-local-variable (car local)) global))
2089         ;; Simple nil-valued local variable.
2090         (set (make-local-variable local) nil)))))
2091
2092 (defun gnus-summary-clear-local-variables ()
2093   (let ((locals gnus-summary-local-variables))
2094     (while locals
2095       (if (consp (car locals))
2096           (and (vectorp (caar locals))
2097                (set (caar locals) nil))
2098         (and (vectorp (car locals))
2099              (set (car locals) nil)))
2100       (setq locals (cdr locals)))))
2101
2102 ;; Summary data functions.
2103
2104 (defmacro gnus-data-number (data)
2105   `(car ,data))
2106
2107 (defmacro gnus-data-set-number (data number)
2108   `(setcar ,data ,number))
2109
2110 (defmacro gnus-data-mark (data)
2111   `(nth 1 ,data))
2112
2113 (defmacro gnus-data-set-mark (data mark)
2114   `(setcar (nthcdr 1 ,data) ,mark))
2115
2116 (defmacro gnus-data-pos (data)
2117   `(nth 2 ,data))
2118
2119 (defmacro gnus-data-set-pos (data pos)
2120   `(setcar (nthcdr 2 ,data) ,pos))
2121
2122 (defmacro gnus-data-header (data)
2123   `(nth 3 ,data))
2124
2125 (defmacro gnus-data-set-header (data header)
2126   `(setf (nth 3 ,data) ,header))
2127
2128 (defmacro gnus-data-level (data)
2129   `(nth 4 ,data))
2130
2131 (defmacro gnus-data-unread-p (data)
2132   `(= (nth 1 ,data) gnus-unread-mark))
2133
2134 (defmacro gnus-data-read-p (data)
2135   `(/= (nth 1 ,data) gnus-unread-mark))
2136
2137 (defmacro gnus-data-pseudo-p (data)
2138   `(consp (nth 3 ,data)))
2139
2140 (defmacro gnus-data-find (number)
2141   `(assq ,number gnus-newsgroup-data))
2142
2143 (defmacro gnus-data-find-list (number &optional data)
2144   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2145      (memq (assq ,number bdata)
2146            bdata)))
2147
2148 (defmacro gnus-data-make (number mark pos header level)
2149   `(list ,number ,mark ,pos ,header ,level))
2150
2151 (defun gnus-data-enter (after-article number mark pos header level offset)
2152   (let ((data (gnus-data-find-list after-article)))
2153     (unless data
2154       (error "No such article: %d" after-article))
2155     (setcdr data (cons (gnus-data-make number mark pos header level)
2156                        (cdr data)))
2157     (setq gnus-newsgroup-data-reverse nil)
2158     (gnus-data-update-list (cddr data) offset)))
2159
2160 (defun gnus-data-enter-list (after-article list &optional offset)
2161   (when list
2162     (let ((data (and after-article (gnus-data-find-list after-article)))
2163           (ilist list))
2164       (if (not (or data
2165                    after-article))
2166           (let ((odata gnus-newsgroup-data))
2167             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2168             (when offset
2169               (gnus-data-update-list odata offset)))
2170         ;; Find the last element in the list to be spliced into the main
2171         ;; list.
2172         (while (cdr list)
2173           (setq list (cdr list)))
2174         (if (not data)
2175             (progn
2176               (setcdr list gnus-newsgroup-data)
2177               (setq gnus-newsgroup-data ilist)
2178               (when offset
2179                 (gnus-data-update-list (cdr list) offset)))
2180           (setcdr list (cdr data))
2181           (setcdr data ilist)
2182           (when offset
2183             (gnus-data-update-list (cdr list) offset))))
2184       (setq gnus-newsgroup-data-reverse nil))))
2185
2186 (defun gnus-data-remove (article &optional offset)
2187   (let ((data gnus-newsgroup-data))
2188     (if (= (gnus-data-number (car data)) article)
2189         (progn
2190           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2191                 gnus-newsgroup-data-reverse nil)
2192           (when offset
2193             (gnus-data-update-list gnus-newsgroup-data offset)))
2194       (while (cdr data)
2195         (when (= (gnus-data-number (cadr data)) article)
2196           (setcdr data (cddr data))
2197           (when offset
2198             (gnus-data-update-list (cdr data) offset))
2199           (setq data nil
2200                 gnus-newsgroup-data-reverse nil))
2201         (setq data (cdr data))))))
2202
2203 (defmacro gnus-data-list (backward)
2204   `(if ,backward
2205        (or gnus-newsgroup-data-reverse
2206            (setq gnus-newsgroup-data-reverse
2207                  (reverse gnus-newsgroup-data)))
2208      gnus-newsgroup-data))
2209
2210 (defun gnus-data-update-list (data offset)
2211   "Add OFFSET to the POS of all data entries in DATA."
2212   (setq gnus-newsgroup-data-reverse nil)
2213   (while data
2214     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2215     (setq data (cdr data))))
2216
2217 (defun gnus-summary-article-pseudo-p (article)
2218   "Say whether this article is a pseudo article or not."
2219   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2220
2221 (defmacro gnus-summary-article-sparse-p (article)
2222   "Say whether this article is a sparse article or not."
2223   `(memq ,article gnus-newsgroup-sparse))
2224
2225 (defmacro gnus-summary-article-ancient-p (article)
2226   "Say whether this article is a sparse article or not."
2227   `(memq ,article gnus-newsgroup-ancient))
2228
2229 (defun gnus-article-parent-p (number)
2230   "Say whether this article is a parent or not."
2231   (let ((data (gnus-data-find-list number)))
2232     (and (cdr data)                     ; There has to be an article after...
2233          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2234             (gnus-data-level (nth 1 data))))))
2235
2236 (defun gnus-article-children (number)
2237   "Return a list of all children to NUMBER."
2238   (let* ((data (gnus-data-find-list number))
2239          (level (gnus-data-level (car data)))
2240          children)
2241     (setq data (cdr data))
2242     (while (and data
2243                 (= (gnus-data-level (car data)) (1+ level)))
2244       (push (gnus-data-number (car data)) children)
2245       (setq data (cdr data)))
2246     children))
2247
2248 (defmacro gnus-summary-skip-intangible ()
2249   "If the current article is intangible, then jump to a different article."
2250   '(let ((to (get-text-property (point) 'gnus-intangible)))
2251      (and to (gnus-summary-goto-subject to))))
2252
2253 (defmacro gnus-summary-article-intangible-p ()
2254   "Say whether this article is intangible or not."
2255   '(get-text-property (point) 'gnus-intangible))
2256
2257 (defun gnus-article-read-p (article)
2258   "Say whether ARTICLE is read or not."
2259   (not (or (memq article gnus-newsgroup-marked)
2260            (memq article gnus-newsgroup-unreads)
2261            (memq article gnus-newsgroup-unselected)
2262            (memq article gnus-newsgroup-dormant))))
2263
2264 ;; Some summary mode macros.
2265
2266 (defmacro gnus-summary-article-number ()
2267   "The article number of the article on the current line.
2268 If there isn's an article number here, then we return the current
2269 article number."
2270   '(progn
2271      (gnus-summary-skip-intangible)
2272      (or (get-text-property (point) 'gnus-number)
2273          (gnus-summary-last-subject))))
2274
2275 (defmacro gnus-summary-article-header (&optional number)
2276   "Return the header of article NUMBER."
2277   `(gnus-data-header (gnus-data-find
2278                       ,(or number '(gnus-summary-article-number)))))
2279
2280 (defmacro gnus-summary-thread-level (&optional number)
2281   "Return the level of thread that starts with article NUMBER."
2282   `(if (and (eq gnus-summary-make-false-root 'dummy)
2283             (get-text-property (point) 'gnus-intangible))
2284        0
2285      (gnus-data-level (gnus-data-find
2286                        ,(or number '(gnus-summary-article-number))))))
2287
2288 (defmacro gnus-summary-article-mark (&optional number)
2289   "Return the mark of article NUMBER."
2290   `(gnus-data-mark (gnus-data-find
2291                     ,(or number '(gnus-summary-article-number)))))
2292
2293 (defmacro gnus-summary-article-pos (&optional number)
2294   "Return the position of the line of article NUMBER."
2295   `(gnus-data-pos (gnus-data-find
2296                    ,(or number '(gnus-summary-article-number)))))
2297
2298 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2299 (defmacro gnus-summary-article-subject (&optional number)
2300   "Return current subject string or nil if nothing."
2301   `(let ((headers
2302           ,(if number
2303                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2304              '(gnus-data-header (assq (gnus-summary-article-number)
2305                                       gnus-newsgroup-data)))))
2306      (and headers
2307           (vectorp headers)
2308           (mail-header-subject headers))))
2309
2310 (defmacro gnus-summary-article-score (&optional number)
2311   "Return current article score."
2312   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2313                   gnus-newsgroup-scored))
2314        gnus-summary-default-score 0))
2315
2316 (defun gnus-summary-article-children (&optional number)
2317   "Return a list of article numbers that are children of article NUMBER."
2318   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2319          (level (gnus-data-level (car data)))
2320          l children)
2321     (while (and (setq data (cdr data))
2322                 (> (setq l (gnus-data-level (car data))) level))
2323       (and (= (1+ level) l)
2324            (push (gnus-data-number (car data))
2325                  children)))
2326     (nreverse children)))
2327
2328 (defun gnus-summary-article-parent (&optional number)
2329   "Return the article number of the parent of article NUMBER."
2330   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2331                                     (gnus-data-list t)))
2332          (level (gnus-data-level (car data))))
2333     (if (zerop level)
2334         ()                              ; This is a root.
2335       ;; We search until we find an article with a level less than
2336       ;; this one.  That function has to be the parent.
2337       (while (and (setq data (cdr data))
2338                   (not (< (gnus-data-level (car data)) level))))
2339       (and data (gnus-data-number (car data))))))
2340
2341 (defun gnus-unread-mark-p (mark)
2342   "Say whether MARK is the unread mark."
2343   (= mark gnus-unread-mark))
2344
2345 (defun gnus-read-mark-p (mark)
2346   "Say whether MARK is one of the marks that mark as read.
2347 This is all marks except unread, ticked, dormant, and expirable."
2348   (not (or (= mark gnus-unread-mark)
2349            (= mark gnus-ticked-mark)
2350            (= mark gnus-dormant-mark)
2351            (= mark gnus-expirable-mark))))
2352
2353 (defmacro gnus-article-mark (number)
2354   "Return the MARK of article NUMBER.
2355 This macro should only be used when computing the mark the \"first\"
2356 time; i.e., when generating the summary lines.  After that,
2357 `gnus-summary-article-mark' should be used to examine the
2358 marks of articles."
2359   `(cond
2360     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2361     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2362     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2363     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2364     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2365     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2366     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2367     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2368            gnus-ancient-mark))))
2369
2370 ;; Saving hidden threads.
2371
2372 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2373 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2374
2375 (defmacro gnus-save-hidden-threads (&rest forms)
2376   "Save hidden threads, eval FORMS, and restore the hidden threads."
2377   (let ((config (make-symbol "config")))
2378     `(let ((,config (gnus-hidden-threads-configuration)))
2379        (unwind-protect
2380            (save-excursion
2381              ,@forms)
2382          (gnus-restore-hidden-threads-configuration ,config)))))
2383
2384 (defun gnus-data-compute-positions ()
2385   "Compute the positions of all articles."
2386   (setq gnus-newsgroup-data-reverse nil)
2387   (let ((data gnus-newsgroup-data))
2388     (save-excursion
2389       (gnus-save-hidden-threads
2390         (gnus-summary-show-all-threads)
2391         (goto-char (point-min))
2392         (while data
2393           (while (get-text-property (point) 'gnus-intangible)
2394             (forward-line 1))
2395           (gnus-data-set-pos (car data) (+ (point) 3))
2396           (setq data (cdr data))
2397           (forward-line 1))))))
2398
2399 (defun gnus-hidden-threads-configuration ()
2400   "Return the current hidden threads configuration."
2401   (save-excursion
2402     (let (config)
2403       (goto-char (point-min))
2404       (while (search-forward "\r" nil t)
2405         (push (1- (point)) config))
2406       config)))
2407
2408 (defun gnus-restore-hidden-threads-configuration (config)
2409   "Restore hidden threads configuration from CONFIG."
2410   (let (point buffer-read-only)
2411     (while (setq point (pop config))
2412       (when (and (< point (point-max))
2413                  (goto-char point)
2414                  (eq (char-after) ?\n))
2415         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2416
2417 ;; Various summary mode internalish functions.
2418
2419 (defun gnus-mouse-pick-article (e)
2420   (interactive "e")
2421   (mouse-set-point e)
2422   (gnus-summary-next-page nil t))
2423
2424 (defun gnus-summary-set-display-table ()
2425   ;; Change the display table.  Odd characters have a tendency to mess
2426   ;; up nicely formatted displays - we make all possible glyphs
2427   ;; display only a single character.
2428
2429   ;; We start from the standard display table, if any.
2430   (let ((table (or (copy-sequence standard-display-table)
2431                    (make-display-table)))
2432         (i 32))
2433     ;; Nix out all the control chars...
2434     (while (>= (setq i (1- i)) 0)
2435       (aset table i [??]))
2436     ;; ... but not newline and cr, of course.  (cr is necessary for the
2437     ;; selective display).
2438     (aset table ?\n nil)
2439     (aset table ?\r nil)
2440     ;; We keep TAB as well.
2441     (aset table ?\t nil)
2442     ;; We nix out any glyphs over 126 that are not set already.
2443     (let ((i 256))
2444       (while (>= (setq i (1- i)) 127)
2445         ;; Only modify if the entry is nil.
2446         (unless (aref table i)
2447           (aset table i [??]))))
2448     (setq buffer-display-table table)))
2449
2450 (defun gnus-summary-setup-buffer (group)
2451   "Initialize summary buffer."
2452   (let ((buffer (concat "*Summary " group "*")))
2453     (if (get-buffer buffer)
2454         (progn
2455           (set-buffer buffer)
2456           (setq gnus-summary-buffer (current-buffer))
2457           (not gnus-newsgroup-prepared))
2458       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2459       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2460       (gnus-summary-mode group)
2461       (when gnus-carpal
2462         (gnus-carpal-setup-buffer 'summary))
2463       (unless gnus-single-article-buffer
2464         (make-local-variable 'gnus-article-buffer)
2465         (make-local-variable 'gnus-article-current)
2466         (make-local-variable 'gnus-original-article-buffer))
2467       (setq gnus-newsgroup-name group)
2468       t)))
2469
2470 (defun gnus-set-global-variables ()
2471   ;; Set the global equivalents of the summary buffer-local variables
2472   ;; to the latest values they had.  These reflect the summary buffer
2473   ;; that was in action when the last article was fetched.
2474   (when (eq major-mode 'gnus-summary-mode)
2475     (setq gnus-summary-buffer (current-buffer))
2476     (let ((name gnus-newsgroup-name)
2477           (marked gnus-newsgroup-marked)
2478           (unread gnus-newsgroup-unreads)
2479           (headers gnus-current-headers)
2480           (data gnus-newsgroup-data)
2481           (summary gnus-summary-buffer)
2482           (article-buffer gnus-article-buffer)
2483           (original gnus-original-article-buffer)
2484           (gac gnus-article-current)
2485           (reffed gnus-reffed-article-number)
2486           (score-file gnus-current-score-file)
2487           (default-charset gnus-newsgroup-charset))
2488       (save-excursion
2489         (set-buffer gnus-group-buffer)
2490         (setq gnus-newsgroup-name name
2491               gnus-newsgroup-marked marked
2492               gnus-newsgroup-unreads unread
2493               gnus-current-headers headers
2494               gnus-newsgroup-data data
2495               gnus-article-current gac
2496               gnus-summary-buffer summary
2497               gnus-article-buffer article-buffer
2498               gnus-original-article-buffer original
2499               gnus-reffed-article-number reffed
2500               gnus-current-score-file score-file
2501               gnus-newsgroup-charset default-charset)
2502         ;; The article buffer also has local variables.
2503         (when (gnus-buffer-live-p gnus-article-buffer)
2504           (set-buffer gnus-article-buffer)
2505           (setq gnus-summary-buffer summary))))))
2506
2507 (defun gnus-summary-article-unread-p (article)
2508   "Say whether ARTICLE is unread or not."
2509   (memq article gnus-newsgroup-unreads))
2510
2511 (defun gnus-summary-first-article-p (&optional article)
2512   "Return whether ARTICLE is the first article in the buffer."
2513   (if (not (setq article (or article (gnus-summary-article-number))))
2514       nil
2515     (eq article (caar gnus-newsgroup-data))))
2516
2517 (defun gnus-summary-last-article-p (&optional article)
2518   "Return whether ARTICLE is the last article in the buffer."
2519   (if (not (setq article (or article (gnus-summary-article-number))))
2520       ;; All non-existent numbers are the last article.  :-)
2521       t
2522     (not (cdr (gnus-data-find-list article)))))
2523
2524 (defun gnus-make-thread-indent-array ()
2525   (let ((n 200))
2526     (unless (and gnus-thread-indent-array
2527                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2528       (setq gnus-thread-indent-array (make-vector 201 "")
2529             gnus-thread-indent-array-level gnus-thread-indent-level)
2530       (while (>= n 0)
2531         (aset gnus-thread-indent-array n
2532               (make-string (* n gnus-thread-indent-level) ? ))
2533         (setq n (1- n))))))
2534
2535 (defun gnus-update-summary-mark-positions ()
2536   "Compute where the summary marks are to go."
2537   (save-excursion
2538     (when (gnus-buffer-exists-p gnus-summary-buffer)
2539       (set-buffer gnus-summary-buffer))
2540     (let ((gnus-replied-mark 129)
2541           (gnus-score-below-mark 130)
2542           (gnus-score-over-mark 130)
2543           (gnus-download-mark 131)
2544           (spec gnus-summary-line-format-spec)
2545           gnus-visual pos)
2546       (save-excursion
2547         (gnus-set-work-buffer)
2548         (let ((gnus-summary-line-format-spec spec)
2549               (gnus-newsgroup-downloadable '((0 . t))))
2550           (gnus-summary-insert-line
2551            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2552           (goto-char (point-min))
2553           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2554                                              (- (point) 2)))))
2555           (goto-char (point-min))
2556           (push (cons 'replied (and (search-forward "\201" nil t)
2557                                     (- (point) 2)))
2558                 pos)
2559           (goto-char (point-min))
2560           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2561                 pos)
2562           (goto-char (point-min))
2563           (push (cons 'download
2564                       (and (search-forward "\203" nil t) (- (point) 2)))
2565                 pos)))
2566       (setq gnus-summary-mark-positions pos))))
2567
2568 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2569   "Insert a dummy root in the summary buffer."
2570   (beginning-of-line)
2571   (gnus-add-text-properties
2572    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2573    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2574
2575 (defun gnus-summary-from-or-to-or-newsgroups (header)
2576   (let ((to (cdr (assq 'To (mail-header-extra header))))
2577         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2578         (mail-parse-charset gnus-newsgroup-charset)
2579         (mail-parse-ignored-charsets 
2580          (save-excursion (set-buffer gnus-summary-buffer)
2581                          gnus-newsgroup-ignored-charsets)))
2582     (cond
2583      ((and to
2584            gnus-ignored-from-addresses
2585            (string-match gnus-ignored-from-addresses
2586                          (mail-header-from header)))
2587       (concat "-> "
2588               (or (car (funcall gnus-extract-address-components
2589                                 (funcall
2590                                  gnus-decode-encoded-word-function to)))
2591                   (funcall gnus-decode-encoded-word-function to))))
2592      ((and newsgroups
2593            gnus-ignored-from-addresses
2594            (string-match gnus-ignored-from-addresses
2595                          (mail-header-from header)))
2596       (concat "=> " newsgroups))
2597      (t
2598       (or (car (funcall gnus-extract-address-components
2599                         (mail-header-from header)))
2600           (mail-header-from header))))))
2601
2602 (defun gnus-summary-insert-line (gnus-tmp-header
2603                                  gnus-tmp-level gnus-tmp-current
2604                                  gnus-tmp-unread gnus-tmp-replied
2605                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2606                                  &optional gnus-tmp-dummy gnus-tmp-score
2607                                  gnus-tmp-process)
2608   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2609          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2610          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2611          (gnus-tmp-score-char
2612           (if (or (null gnus-summary-default-score)
2613                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2614                       gnus-summary-zcore-fuzz))
2615               ?  ;Whitespace
2616             (if (< gnus-tmp-score gnus-summary-default-score)
2617                 gnus-score-below-mark gnus-score-over-mark)))
2618          (gnus-tmp-replied
2619           (cond (gnus-tmp-process gnus-process-mark)
2620                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2621                  gnus-cached-mark)
2622                 (gnus-tmp-replied gnus-replied-mark)
2623                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2624                  gnus-saved-mark)
2625                 (t gnus-unread-mark)))
2626          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2627          (gnus-tmp-name
2628           (cond
2629            ((string-match "<[^>]+> *$" gnus-tmp-from)
2630             (let ((beg (match-beginning 0)))
2631               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2632                        (substring gnus-tmp-from (1+ (match-beginning 0))
2633                                   (1- (match-end 0))))
2634                   (substring gnus-tmp-from 0 beg))))
2635            ((string-match "(.+)" gnus-tmp-from)
2636             (substring gnus-tmp-from
2637                        (1+ (match-beginning 0)) (1- (match-end 0))))
2638            (t gnus-tmp-from)))
2639          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2640          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2641          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2642          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2643          (buffer-read-only nil))
2644     (when (string= gnus-tmp-name "")
2645       (setq gnus-tmp-name gnus-tmp-from))
2646     (unless (numberp gnus-tmp-lines)
2647       (setq gnus-tmp-lines 0))
2648     (gnus-put-text-property
2649      (point)
2650      (progn (eval gnus-summary-line-format-spec) (point))
2651      'gnus-number gnus-tmp-number)
2652     (when (gnus-visual-p 'summary-highlight 'highlight)
2653       (forward-line -1)
2654       (gnus-run-hooks 'gnus-summary-update-hook)
2655       (forward-line 1))))
2656
2657 (defun gnus-summary-update-line (&optional dont-update)
2658   ;; Update summary line after change.
2659   (when (and gnus-summary-default-score
2660              (not gnus-summary-inhibit-highlight))
2661     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2662            (article (gnus-summary-article-number))
2663            (score (gnus-summary-article-score article)))
2664       (unless dont-update
2665         (if (and gnus-summary-mark-below
2666                  (< (gnus-summary-article-score)
2667                     gnus-summary-mark-below))
2668             ;; This article has a low score, so we mark it as read.
2669             (when (memq article gnus-newsgroup-unreads)
2670               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2671           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2672             ;; This article was previously marked as read on account
2673             ;; of a low score, but now it has risen, so we mark it as
2674             ;; unread.
2675             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2676         (gnus-summary-update-mark
2677          (if (or (null gnus-summary-default-score)
2678                  (<= (abs (- score gnus-summary-default-score))
2679                      gnus-summary-zcore-fuzz))
2680              ?  ;Whitespace
2681            (if (< score gnus-summary-default-score)
2682                gnus-score-below-mark gnus-score-over-mark))
2683          'score))
2684       ;; Do visual highlighting.
2685       (when (gnus-visual-p 'summary-highlight 'highlight)
2686         (gnus-run-hooks 'gnus-summary-update-hook)))))
2687
2688 (defvar gnus-tmp-new-adopts nil)
2689
2690 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2691   "Return the number of articles in THREAD.
2692 This may be 0 in some cases -- if none of the articles in
2693 the thread are to be displayed."
2694   (let* ((number
2695           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2696           (cond
2697            ((not (listp thread))
2698             1)
2699            ((and (consp thread) (cdr thread))
2700             (apply
2701              '+ 1 (mapcar
2702                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2703            ((null thread)
2704             1)
2705            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2706             1)
2707            (t 0))))
2708     (when (and level (zerop level) gnus-tmp-new-adopts)
2709       (incf number
2710             (apply '+ (mapcar
2711                        'gnus-summary-number-of-articles-in-thread
2712                        gnus-tmp-new-adopts))))
2713     (if char
2714         (if (> number 1) gnus-not-empty-thread-mark
2715           gnus-empty-thread-mark)
2716       number)))
2717
2718 (defun gnus-summary-set-local-parameters (group)
2719   "Go through the local params of GROUP and set all variable specs in that list."
2720   (let ((params (gnus-group-find-parameter group))
2721         elem)
2722     (while params
2723       (setq elem (car params)
2724             params (cdr params))
2725       (and (consp elem)                 ; Has to be a cons.
2726            (consp (cdr elem))           ; The cdr has to be a list.
2727            (symbolp (car elem))         ; Has to be a symbol in there.
2728            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2729            (ignore-errors               ; So we set it.
2730              (make-local-variable (car elem))
2731              (set (car elem) (eval (nth 1 elem))))))))
2732
2733 (defun gnus-summary-read-group (group &optional show-all no-article
2734                                       kill-buffer no-display backward
2735                                       select-articles)
2736   "Start reading news in newsgroup GROUP.
2737 If SHOW-ALL is non-nil, already read articles are also listed.
2738 If NO-ARTICLE is non-nil, no article is selected initially.
2739 If NO-DISPLAY, don't generate a summary buffer."
2740   (let (result)
2741     (while (and group
2742                 (null (setq result
2743                             (let ((gnus-auto-select-next nil))
2744                               (or (gnus-summary-read-group-1
2745                                    group show-all no-article
2746                                    kill-buffer no-display
2747                                    select-articles)
2748                                   (setq show-all nil
2749                                         select-articles nil)))))
2750                 (eq gnus-auto-select-next 'quietly))
2751       (set-buffer gnus-group-buffer)
2752       ;; The entry function called above goes to the next
2753       ;; group automatically, so we go two groups back
2754       ;; if we are searching for the previous group.
2755       (when backward
2756         (gnus-group-prev-unread-group 2))
2757       (if (not (equal group (gnus-group-group-name)))
2758           (setq group (gnus-group-group-name))
2759         (setq group nil)))
2760     result))
2761
2762 (defun gnus-summary-read-group-1 (group show-all no-article
2763                                         kill-buffer no-display
2764                                         &optional select-articles)
2765   ;; Killed foreign groups can't be entered.
2766   (when (and (not (gnus-group-native-p group))
2767              (not (gnus-gethash group gnus-newsrc-hashtb)))
2768     (error "Dead non-native groups can't be entered"))
2769   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2770   (let* ((new-group (gnus-summary-setup-buffer group))
2771          (quit-config (gnus-group-quit-config group))
2772          (did-select (and new-group (gnus-select-newsgroup
2773                                      group show-all select-articles))))
2774     (cond
2775      ;; This summary buffer exists already, so we just select it.
2776      ((not new-group)
2777       (gnus-set-global-variables)
2778       (when kill-buffer
2779         (gnus-kill-or-deaden-summary kill-buffer))
2780       (gnus-configure-windows 'summary 'force)
2781       (gnus-set-mode-line 'summary)
2782       (gnus-summary-position-point)
2783       (message "")
2784       t)
2785      ;; We couldn't select this group.
2786      ((null did-select)
2787       (when (and (eq major-mode 'gnus-summary-mode)
2788                  (not (equal (current-buffer) kill-buffer)))
2789         (kill-buffer (current-buffer))
2790         (if (not quit-config)
2791             (progn
2792               ;; Update the info -- marks might need to be removed,
2793               ;; for instance.
2794               (gnus-summary-update-info)
2795               (set-buffer gnus-group-buffer)
2796               (gnus-group-jump-to-group group)
2797               (gnus-group-next-unread-group 1))
2798           (gnus-handle-ephemeral-exit quit-config)))
2799       (gnus-message 3 "Can't select group")
2800       nil)
2801      ;; The user did a `C-g' while prompting for number of articles,
2802      ;; so we exit this group.
2803      ((eq did-select 'quit)
2804       (and (eq major-mode 'gnus-summary-mode)
2805            (not (equal (current-buffer) kill-buffer))
2806            (kill-buffer (current-buffer)))
2807       (when kill-buffer
2808         (gnus-kill-or-deaden-summary kill-buffer))
2809       (if (not quit-config)
2810           (progn
2811             (set-buffer gnus-group-buffer)
2812             (gnus-group-jump-to-group group)
2813             (gnus-group-next-unread-group 1)
2814             (gnus-configure-windows 'group 'force))
2815         (gnus-handle-ephemeral-exit quit-config))
2816       ;; Finally signal the quit.
2817       (signal 'quit nil))
2818      ;; The group was successfully selected.
2819      (t
2820       (gnus-set-global-variables)
2821       ;; Save the active value in effect when the group was entered.
2822       (setq gnus-newsgroup-active
2823             (gnus-copy-sequence
2824              (gnus-active gnus-newsgroup-name)))
2825       ;; You can change the summary buffer in some way with this hook.
2826       (gnus-run-hooks 'gnus-select-group-hook)
2827       ;; Set any local variables in the group parameters.
2828       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2829       (gnus-update-format-specifications
2830        nil 'summary 'summary-mode 'summary-dummy)
2831       (gnus-update-summary-mark-positions)
2832       ;; Do score processing.
2833       (when gnus-use-scoring
2834         (gnus-possibly-score-headers))
2835       ;; Check whether to fill in the gaps in the threads.
2836       (when gnus-build-sparse-threads
2837         (gnus-build-sparse-threads))
2838       ;; Find the initial limit.
2839       (if gnus-show-threads
2840           (if show-all
2841               (let ((gnus-newsgroup-dormant nil))
2842                 (gnus-summary-initial-limit show-all))
2843             (gnus-summary-initial-limit show-all))
2844         ;; When untreaded, all articles are always shown.
2845         (setq gnus-newsgroup-limit
2846               (mapcar
2847                (lambda (header) (mail-header-number header))
2848                gnus-newsgroup-headers)))
2849       ;; Generate the summary buffer.
2850       (unless no-display
2851         (gnus-summary-prepare))
2852       (when gnus-use-trees
2853         (gnus-tree-open group)
2854         (setq gnus-summary-highlight-line-function
2855               'gnus-tree-highlight-article))
2856       ;; If the summary buffer is empty, but there are some low-scored
2857       ;; articles or some excluded dormants, we include these in the
2858       ;; buffer.
2859       (when (and (zerop (buffer-size))
2860                  (not no-display))
2861         (cond (gnus-newsgroup-dormant
2862                (gnus-summary-limit-include-dormant))
2863               ((and gnus-newsgroup-scored show-all)
2864                (gnus-summary-limit-include-expunged t))))
2865       ;; Function `gnus-apply-kill-file' must be called in this hook.
2866       (gnus-run-hooks 'gnus-apply-kill-hook)
2867       (if (and (zerop (buffer-size))
2868                (not no-display))
2869           (progn
2870             ;; This newsgroup is empty.
2871             (gnus-summary-catchup-and-exit nil t)
2872             (gnus-message 6 "No unread news")
2873             (when kill-buffer
2874               (gnus-kill-or-deaden-summary kill-buffer))
2875             ;; Return nil from this function.
2876             nil)
2877         ;; Hide conversation thread subtrees.  We cannot do this in
2878         ;; gnus-summary-prepare-hook since kill processing may not
2879         ;; work with hidden articles.
2880         (and gnus-show-threads
2881              gnus-thread-hide-subtree
2882              (gnus-summary-hide-all-threads))
2883         (when kill-buffer
2884           (gnus-kill-or-deaden-summary kill-buffer))
2885         ;; Show first unread article if requested.
2886         (if (and (not no-article)
2887                  (not no-display)
2888                  gnus-newsgroup-unreads
2889                  gnus-auto-select-first)
2890             (progn
2891               (gnus-configure-windows 'summary)
2892               (cond
2893                ((eq gnus-auto-select-first 'best)
2894                 (gnus-summary-best-unread-article))
2895                ((eq gnus-auto-select-first t)
2896                 (gnus-summary-first-unread-article))
2897                ((gnus-functionp gnus-auto-select-first)
2898                 (funcall gnus-auto-select-first))))
2899           ;; Don't select any articles, just move point to the first
2900           ;; article in the group.
2901           (goto-char (point-min))
2902           (gnus-summary-position-point)
2903           (gnus-configure-windows 'summary 'force)
2904           (gnus-set-mode-line 'summary))
2905         (when (get-buffer-window gnus-group-buffer t)
2906           ;; Gotta use windows, because recenter does weird stuff if
2907           ;; the current buffer ain't the displayed window.
2908           (let ((owin (selected-window)))
2909             (select-window (get-buffer-window gnus-group-buffer t))
2910             (when (gnus-group-goto-group group)
2911               (recenter))
2912             (select-window owin)))
2913         ;; Mark this buffer as "prepared".
2914         (setq gnus-newsgroup-prepared t)
2915         (gnus-run-hooks 'gnus-summary-prepared-hook)
2916         t)))))
2917
2918 (defun gnus-summary-prepare ()
2919   "Generate the summary buffer."
2920   (interactive)
2921   (let ((buffer-read-only nil))
2922     (erase-buffer)
2923     (setq gnus-newsgroup-data nil
2924           gnus-newsgroup-data-reverse nil)
2925     (gnus-run-hooks 'gnus-summary-generate-hook)
2926     ;; Generate the buffer, either with threads or without.
2927     (when gnus-newsgroup-headers
2928       (gnus-summary-prepare-threads
2929        (if gnus-show-threads
2930            (gnus-sort-gathered-threads
2931             (funcall gnus-summary-thread-gathering-function
2932                      (gnus-sort-threads
2933                       (gnus-cut-threads (gnus-make-threads)))))
2934          ;; Unthreaded display.
2935          (gnus-sort-articles gnus-newsgroup-headers))))
2936     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2937     ;; Call hooks for modifying summary buffer.
2938     (goto-char (point-min))
2939     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2940
2941 (defsubst gnus-general-simplify-subject (subject)
2942   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2943   (setq subject
2944         (cond
2945          ;; Truncate the subject.
2946          (gnus-simplify-subject-functions
2947           (gnus-map-function gnus-simplify-subject-functions subject))
2948          ((numberp gnus-summary-gather-subject-limit)
2949           (setq subject (gnus-simplify-subject-re subject))
2950           (if (> (length subject) gnus-summary-gather-subject-limit)
2951               (substring subject 0 gnus-summary-gather-subject-limit)
2952             subject))
2953          ;; Fuzzily simplify it.
2954          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2955           (gnus-simplify-subject-fuzzy subject))
2956          ;; Just remove the leading "Re:".
2957          (t
2958           (gnus-simplify-subject-re subject))))
2959
2960   (if (and gnus-summary-gather-exclude-subject
2961            (string-match gnus-summary-gather-exclude-subject subject))
2962       nil                               ; This article shouldn't be gathered
2963     subject))
2964
2965 (defun gnus-summary-simplify-subject-query ()
2966   "Query where the respool algorithm would put this article."
2967   (interactive)
2968   (gnus-summary-select-article)
2969   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2970
2971 (defun gnus-gather-threads-by-subject (threads)
2972   "Gather threads by looking at Subject headers."
2973   (if (not gnus-summary-make-false-root)
2974       threads
2975     (let ((hashtb (gnus-make-hashtable 1024))
2976           (prev threads)
2977           (result threads)
2978           subject hthread whole-subject)
2979       (while threads
2980         (setq subject (gnus-general-simplify-subject
2981                        (setq whole-subject (mail-header-subject
2982                                             (caar threads)))))
2983         (when subject
2984           (if (setq hthread (gnus-gethash subject hashtb))
2985               (progn
2986                 ;; We enter a dummy root into the thread, if we
2987                 ;; haven't done that already.
2988                 (unless (stringp (caar hthread))
2989                   (setcar hthread (list whole-subject (car hthread))))
2990                 ;; We add this new gathered thread to this gathered
2991                 ;; thread.
2992                 (setcdr (car hthread)
2993                         (nconc (cdar hthread) (list (car threads))))
2994                 ;; Remove it from the list of threads.
2995                 (setcdr prev (cdr threads))
2996                 (setq threads prev))
2997             ;; Enter this thread into the hash table.
2998             (gnus-sethash subject threads hashtb)))
2999         (setq prev threads)
3000         (setq threads (cdr threads)))
3001       result)))
3002
3003 (defun gnus-gather-threads-by-references (threads)
3004   "Gather threads by looking at References headers."
3005   (let ((idhashtb (gnus-make-hashtable 1024))
3006         (thhashtb (gnus-make-hashtable 1024))
3007         (prev threads)
3008         (result threads)
3009         ids references id gthread gid entered ref)
3010     (while threads
3011       (when (setq references (mail-header-references (caar threads)))
3012         (setq id (mail-header-id (caar threads))
3013               ids (gnus-split-references references)
3014               entered nil)
3015         (while (setq ref (pop ids))
3016           (setq ids (delete ref ids))
3017           (if (not (setq gid (gnus-gethash ref idhashtb)))
3018               (progn
3019                 (gnus-sethash ref id idhashtb)
3020                 (gnus-sethash id threads thhashtb))
3021             (setq gthread (gnus-gethash gid thhashtb))
3022             (unless entered
3023               ;; We enter a dummy root into the thread, if we
3024               ;; haven't done that already.
3025               (unless (stringp (caar gthread))
3026                 (setcar gthread (list (mail-header-subject (caar gthread))
3027                                       (car gthread))))
3028               ;; We add this new gathered thread to this gathered
3029               ;; thread.
3030               (setcdr (car gthread)
3031                       (nconc (cdar gthread) (list (car threads)))))
3032             ;; Add it into the thread hash table.
3033             (gnus-sethash id gthread thhashtb)
3034             (setq entered t)
3035             ;; Remove it from the list of threads.
3036             (setcdr prev (cdr threads))
3037             (setq threads prev))))
3038       (setq prev threads)
3039       (setq threads (cdr threads)))
3040     result))
3041
3042 (defun gnus-sort-gathered-threads (threads)
3043   "Sort subtreads inside each gathered thread by article number."
3044   (let ((result threads))
3045     (while threads
3046       (when (stringp (caar threads))
3047         (setcdr (car threads)
3048                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3049       (setq threads (cdr threads)))
3050     result))
3051
3052 (defun gnus-thread-loop-p (root thread)
3053   "Say whether ROOT is in THREAD."
3054   (let ((stack (list thread))
3055         (infloop 0)
3056         th)
3057     (while (setq thread (pop stack))
3058       (setq th (cdr thread))
3059       (while (and th
3060                   (not (eq (caar th) root)))
3061         (pop th))
3062       (if th
3063           ;; We have found a loop.
3064           (let (ref-dep)
3065             (setcdr thread (delq (car th) (cdr thread)))
3066             (if (boundp (setq ref-dep (intern "none"
3067                                               gnus-newsgroup-dependencies)))
3068                 (setcdr (symbol-value ref-dep)
3069                         (nconc (cdr (symbol-value ref-dep))
3070                                (list (car th))))
3071               (set ref-dep (list nil (car th))))
3072             (setq infloop 1
3073                   stack nil))
3074         ;; Push all the subthreads onto the stack.
3075         (push (cdr thread) stack)))
3076     infloop))
3077
3078 (defun gnus-make-threads ()
3079   "Go through the dependency hashtb and find the roots.  Return all threads."
3080   (let (threads)
3081     (while (catch 'infloop
3082              (mapatoms
3083               (lambda (refs)
3084                 ;; Deal with self-referencing References loops.
3085                 (when (and (car (symbol-value refs))
3086                            (not (zerop
3087                                  (apply
3088                                   '+
3089                                   (mapcar
3090                                    (lambda (thread)
3091                                      (gnus-thread-loop-p
3092                                       (car (symbol-value refs)) thread))
3093                                    (cdr (symbol-value refs)))))))
3094                   (setq threads nil)
3095                   (throw 'infloop t))
3096                 (unless (car (symbol-value refs))
3097                   ;; These threads do not refer back to any other articles,
3098                   ;; so they're roots.
3099                   (setq threads (append (cdr (symbol-value refs)) threads))))
3100               gnus-newsgroup-dependencies)))
3101     threads))
3102
3103 ;; Build the thread tree.
3104 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3105   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3106
3107 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3108 if it was already present.
3109
3110 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3111 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3112 Message-IDs will be renamed be renamed to a unique Message-ID before
3113 being entered.
3114
3115 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3116   (let* ((id (mail-header-id header))
3117          (id-dep (and id (intern id dependencies)))
3118          ref ref-dep ref-header)
3119     ;; Enter this `header' in the `dependencies' table.
3120     (cond
3121      ((not id-dep)
3122       (setq header nil))
3123      ;; The first two cases do the normal part: enter a new `header'
3124      ;; in the `dependencies' table.
3125      ((not (boundp id-dep))
3126       (set id-dep (list header)))
3127      ((null (car (symbol-value id-dep)))
3128       (setcar (symbol-value id-dep) header))
3129
3130      ;; From here the `header' was already present in the
3131      ;; `dependencies' table.
3132      (force-new
3133       ;; Overrides an existing entry;
3134       ;; just set the header part of the entry.
3135       (setcar (symbol-value id-dep) header))
3136
3137      ;; Renames the existing `header' to a unique Message-ID.
3138      ((not gnus-summary-ignore-duplicates)
3139       ;; An article with this Message-ID has already been seen.
3140       ;; We rename the Message-ID.
3141       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3142            (list header))
3143       (mail-header-set-id header id))
3144
3145      ;; The last case ignores an existing entry, except it adds any
3146      ;; additional Xrefs (in case the two articles came from different
3147      ;; servers.
3148      ;; Also sets `header' to `nil' meaning that the `dependencies'
3149      ;; table was *not* modified.
3150      (t
3151       (mail-header-set-xref
3152        (car (symbol-value id-dep))
3153        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3154                    "")
3155                (or (mail-header-xref header) "")))
3156       (setq header nil)))
3157
3158     (when header
3159       ;; First check if that we are not creating a References loop.
3160       (setq ref (gnus-parent-id (mail-header-references header)))
3161       (while (and ref
3162                   (setq ref-dep (intern-soft ref dependencies))
3163                   (boundp ref-dep)
3164                   (setq ref-header (car (symbol-value ref-dep))))
3165         (if (string= id ref)
3166             ;; Yuk!  This is a reference loop.  Make the article be a
3167             ;; root article.
3168             (progn
3169               (mail-header-set-references (car (symbol-value id-dep)) "none")
3170               (setq ref nil))
3171           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3172       (setq ref (gnus-parent-id (mail-header-references header)))
3173       (setq ref-dep (intern (or ref "none") dependencies))
3174       (if (boundp ref-dep)
3175           (setcdr (symbol-value ref-dep)
3176                   (nconc (cdr (symbol-value ref-dep))
3177                          (list (symbol-value id-dep))))
3178         (set ref-dep (list nil (symbol-value id-dep)))))
3179     header))
3180
3181 (defun gnus-build-sparse-threads ()
3182   (let ((headers gnus-newsgroup-headers)
3183         (mail-parse-charset gnus-newsgroup-charset)
3184         (gnus-summary-ignore-duplicates t)
3185         header references generation relations
3186         subject child end new-child date)
3187     ;; First we create an alist of generations/relations, where
3188     ;; generations is how much we trust the relation, and the relation
3189     ;; is parent/child.
3190     (gnus-message 7 "Making sparse threads...")
3191     (save-excursion
3192       (nnheader-set-temp-buffer " *gnus sparse threads*")
3193       (while (setq header (pop headers))
3194         (when (and (setq references (mail-header-references header))
3195                    (not (string= references "")))
3196           (insert references)
3197           (setq child (mail-header-id header)
3198                 subject (mail-header-subject header)
3199                 date (mail-header-date header)
3200                 generation 0)
3201           (while (search-backward ">" nil t)
3202             (setq end (1+ (point)))
3203             (when (search-backward "<" nil t)
3204               (setq new-child (buffer-substring (point) end))
3205               (push (list (incf generation)
3206                           child (setq child new-child)
3207                           subject date)
3208                     relations)))
3209           (when child
3210             (push (list (1+ generation) child nil subject) relations))
3211           (erase-buffer)))
3212       (kill-buffer (current-buffer)))
3213     ;; Sort over trustworthiness.
3214     (mapcar
3215      (lambda (relation)
3216        (when (gnus-dependencies-add-header
3217               (make-full-mail-header
3218                gnus-reffed-article-number
3219                (nth 3 relation) "" (or (nth 4 relation) "")
3220                (nth 1 relation)
3221                (or (nth 2 relation) "") 0 0 "")
3222               gnus-newsgroup-dependencies nil)
3223          (push gnus-reffed-article-number gnus-newsgroup-limit)
3224          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3225          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3226                gnus-newsgroup-reads)
3227          (decf gnus-reffed-article-number)))
3228      (sort relations 'car-less-than-car))
3229     (gnus-message 7 "Making sparse threads...done")))
3230
3231 (defun gnus-build-old-threads ()
3232   ;; Look at all the articles that refer back to old articles, and
3233   ;; fetch the headers for the articles that aren't there.  This will
3234   ;; build complete threads - if the roots haven't been expired by the
3235   ;; server, that is.
3236   (let ((mail-parse-charset gnus-newsgroup-charset)
3237         id heads)
3238     (mapatoms
3239      (lambda (refs)
3240        (when (not (car (symbol-value refs)))
3241          (setq heads (cdr (symbol-value refs)))
3242          (while heads
3243            (if (memq (mail-header-number (caar heads))
3244                      gnus-newsgroup-dormant)
3245                (setq heads (cdr heads))
3246              (setq id (symbol-name refs))
3247              (while (and (setq id (gnus-build-get-header id))
3248                          (not (car (gnus-id-to-thread id)))))
3249              (setq heads nil)))))
3250      gnus-newsgroup-dependencies)))
3251
3252 ;; This function has to be called with point after the article number
3253 ;; on the beginning of the line.
3254 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3255   (let ((eol (gnus-point-at-eol))
3256         (buffer (current-buffer))
3257         header)
3258
3259     ;; overview: [num subject from date id refs chars lines misc]
3260     (unwind-protect
3261         (progn
3262           (narrow-to-region (point) eol)
3263           (unless (eobp)
3264             (forward-char))
3265
3266           (setq header
3267                 (make-full-mail-header
3268                  number                 ; number
3269                  (funcall gnus-decode-encoded-word-function
3270                           (nnheader-nov-field)) ; subject
3271                  (funcall gnus-decode-encoded-word-function
3272                           (nnheader-nov-field)) ; from
3273                  (nnheader-nov-field)   ; date
3274                  (nnheader-nov-read-message-id) ; id
3275                  (nnheader-nov-field)   ; refs
3276                  (nnheader-nov-read-integer) ; chars
3277                  (nnheader-nov-read-integer) ; lines
3278                  (unless (eobp)
3279                    (nnheader-nov-field)) ; misc
3280                  (nnheader-nov-parse-extra)))) ; extra
3281
3282       (widen))
3283
3284     (when gnus-alter-header-function
3285       (funcall gnus-alter-header-function header))
3286     (gnus-dependencies-add-header header dependencies force-new)))
3287
3288 (defun gnus-build-get-header (id)
3289   ;; Look through the buffer of NOV lines and find the header to
3290   ;; ID.  Enter this line into the dependencies hash table, and return
3291   ;; the id of the parent article (if any).
3292   (let ((deps gnus-newsgroup-dependencies)
3293         found header)
3294     (prog1
3295         (save-excursion
3296           (set-buffer nntp-server-buffer)
3297           (let ((case-fold-search nil))
3298             (goto-char (point-min))
3299             (while (and (not found)
3300                         (search-forward id nil t))
3301               (beginning-of-line)
3302               (setq found (looking-at
3303                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3304                                    (regexp-quote id))))
3305               (or found (beginning-of-line 2)))
3306             (when found
3307               (beginning-of-line)
3308               (and
3309                (setq header (gnus-nov-parse-line
3310                              (read (current-buffer)) deps))
3311                (gnus-parent-id (mail-header-references header))))))
3312       (when header
3313         (let ((number (mail-header-number header)))
3314           (push number gnus-newsgroup-limit)
3315           (push header gnus-newsgroup-headers)
3316           (if (memq number gnus-newsgroup-unselected)
3317               (progn
3318                 (push number gnus-newsgroup-unreads)
3319                 (setq gnus-newsgroup-unselected
3320                       (delq number gnus-newsgroup-unselected)))
3321             (push number gnus-newsgroup-ancient)))))))
3322
3323 (defun gnus-build-all-threads ()
3324   "Read all the headers."
3325   (let ((gnus-summary-ignore-duplicates t)
3326         (mail-parse-charset gnus-newsgroup-charset)
3327         (dependencies gnus-newsgroup-dependencies)
3328         header article)
3329     (save-excursion
3330       (set-buffer nntp-server-buffer)
3331       (let ((case-fold-search nil))
3332         (goto-char (point-min))
3333         (while (not (eobp))
3334           (ignore-errors
3335             (setq article (read (current-buffer))
3336                   header (gnus-nov-parse-line article dependencies)))
3337           (when header
3338             (save-excursion
3339               (set-buffer gnus-summary-buffer)
3340               (push header gnus-newsgroup-headers)
3341               (if (memq (setq article (mail-header-number header))
3342                         gnus-newsgroup-unselected)
3343                   (progn
3344                     (push article gnus-newsgroup-unreads)
3345                     (setq gnus-newsgroup-unselected
3346                           (delq article gnus-newsgroup-unselected)))
3347                 (push article gnus-newsgroup-ancient)))
3348             (forward-line 1)))))))
3349
3350 (defun gnus-summary-update-article-line (article header)
3351   "Update the line for ARTICLE using HEADERS."
3352   (let* ((id (mail-header-id header))
3353          (thread (gnus-id-to-thread id)))
3354     (unless thread
3355       (error "Article in no thread"))
3356     ;; Update the thread.
3357     (setcar thread header)
3358     (gnus-summary-goto-subject article)
3359     (let* ((datal (gnus-data-find-list article))
3360            (data (car datal))
3361            (length (when (cdr datal)
3362                      (- (gnus-data-pos data)
3363                         (gnus-data-pos (cadr datal)))))
3364            (buffer-read-only nil)
3365            (level (gnus-summary-thread-level)))
3366       (gnus-delete-line)
3367       (gnus-summary-insert-line
3368        header level nil (gnus-article-mark article)
3369        (memq article gnus-newsgroup-replied)
3370        (memq article gnus-newsgroup-expirable)
3371        ;; Only insert the Subject string when it's different
3372        ;; from the previous Subject string.
3373        (if (gnus-subject-equal
3374             (condition-case ()
3375                 (mail-header-subject
3376                  (gnus-data-header
3377                   (cadr
3378                    (gnus-data-find-list
3379                     article
3380                     (gnus-data-list t)))))
3381               ;; Error on the side of excessive subjects.
3382               (error ""))
3383             (mail-header-subject header))
3384            ""
3385          (mail-header-subject header))
3386        nil (cdr (assq article gnus-newsgroup-scored))
3387        (memq article gnus-newsgroup-processable))
3388       (when length
3389         (gnus-data-update-list
3390          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3391
3392 (defun gnus-summary-update-article (article &optional iheader)
3393   "Update ARTICLE in the summary buffer."
3394   (set-buffer gnus-summary-buffer)
3395   (let* ((header (gnus-summary-article-header article))
3396          (id (mail-header-id header))
3397          (data (gnus-data-find article))
3398          (thread (gnus-id-to-thread id))
3399          (references (mail-header-references header))
3400          (parent
3401           (gnus-id-to-thread
3402            (or (gnus-parent-id
3403                 (when (and references
3404                            (not (equal "" references)))
3405                   references))
3406                "none")))
3407          (buffer-read-only nil)
3408          (old (car thread)))
3409     (when thread
3410       (unless iheader
3411         (setcar thread nil)
3412         (when parent
3413           (delq thread parent)))
3414       (if (gnus-summary-insert-subject id header)
3415           ;; Set the (possibly) new article number in the data structure.
3416           (gnus-data-set-number data (gnus-id-to-article id))
3417         (setcar thread old)
3418         nil))))
3419
3420 (defun gnus-rebuild-thread (id &optional line)
3421   "Rebuild the thread containing ID.
3422 If LINE, insert the rebuilt thread starting on line LINE."
3423   (let ((buffer-read-only nil)
3424         old-pos current thread data)
3425     (if (not gnus-show-threads)
3426         (setq thread (list (car (gnus-id-to-thread id))))
3427       ;; Get the thread this article is part of.
3428       (setq thread (gnus-remove-thread id)))
3429     (setq old-pos (gnus-point-at-bol))
3430     (setq current (save-excursion
3431                     (and (zerop (forward-line -1))
3432                          (gnus-summary-article-number))))
3433     ;; If this is a gathered thread, we have to go some re-gathering.
3434     (when (stringp (car thread))
3435       (let ((subject (car thread))
3436             roots thr)
3437         (setq thread (cdr thread))
3438         (while thread
3439           (unless (memq (setq thr (gnus-id-to-thread
3440                                    (gnus-root-id
3441                                     (mail-header-id (caar thread)))))
3442                         roots)
3443             (push thr roots))
3444           (setq thread (cdr thread)))
3445         ;; We now have all (unique) roots.
3446         (if (= (length roots) 1)
3447             ;; All the loose roots are now one solid root.
3448             (setq thread (car roots))
3449           (setq thread (cons subject (gnus-sort-threads roots))))))
3450     (let (threads)
3451       ;; We then insert this thread into the summary buffer.
3452       (when line
3453         (goto-char (point-min))
3454         (forward-line (1- line)))
3455       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3456         (if gnus-show-threads
3457             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3458           (gnus-summary-prepare-unthreaded thread))
3459         (setq data (nreverse gnus-newsgroup-data))
3460         (setq threads gnus-newsgroup-threads))
3461       ;; We splice the new data into the data structure.
3462       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3463       ;;!!! then we want to insert at the beginning of the buffer.
3464       ;;!!! That happens to be true with Gnus now, but that may
3465       ;;!!! change in the future.  Perhaps.
3466       (gnus-data-enter-list
3467        (if line nil current) data (- (point) old-pos))
3468       (setq gnus-newsgroup-threads
3469             (nconc threads gnus-newsgroup-threads))
3470       (gnus-data-compute-positions))))
3471
3472 (defun gnus-number-to-header (number)
3473   "Return the header for article NUMBER."
3474   (let ((headers gnus-newsgroup-headers))
3475     (while (and headers
3476                 (not (= number (mail-header-number (car headers)))))
3477       (pop headers))
3478     (when headers
3479       (car headers))))
3480
3481 (defun gnus-parent-headers (in-headers &optional generation)
3482   "Return the headers of the GENERATIONeth parent of HEADERS."
3483   (unless generation
3484     (setq generation 1))
3485   (let ((parent t)
3486         (headers in-headers)
3487         references)
3488     (while (and parent
3489                 (not (zerop generation))
3490                 (setq references (mail-header-references headers)))
3491       (setq headers (if (and references
3492                              (setq parent (gnus-parent-id references)))
3493                         (car (gnus-id-to-thread parent))
3494                       nil))
3495       (decf generation))
3496     (and (not (eq headers in-headers))
3497          headers)))
3498
3499 (defun gnus-id-to-thread (id)
3500   "Return the (sub-)thread where ID appears."
3501   (gnus-gethash id gnus-newsgroup-dependencies))
3502
3503 (defun gnus-id-to-article (id)
3504   "Return the article number of ID."
3505   (let ((thread (gnus-id-to-thread id)))
3506     (when (and thread
3507                (car thread))
3508       (mail-header-number (car thread)))))
3509
3510 (defun gnus-id-to-header (id)
3511   "Return the article headers of ID."
3512   (car (gnus-id-to-thread id)))
3513
3514 (defun gnus-article-displayed-root-p (article)
3515   "Say whether ARTICLE is a root(ish) article."
3516   (let ((level (gnus-summary-thread-level article))
3517         (refs (mail-header-references  (gnus-summary-article-header article)))
3518         particle)
3519     (cond
3520      ((null level) nil)
3521      ((zerop level) t)
3522      ((null refs) t)
3523      ((null (gnus-parent-id refs)) t)
3524      ((and (= 1 level)
3525            (null (setq particle (gnus-id-to-article
3526                                  (gnus-parent-id refs))))
3527            (null (gnus-summary-thread-level particle)))))))
3528
3529 (defun gnus-root-id (id)
3530   "Return the id of the root of the thread where ID appears."
3531   (let (last-id prev)
3532     (while (and id (setq prev (car (gnus-id-to-thread id))))
3533       (setq last-id id
3534             id (gnus-parent-id (mail-header-references prev))))
3535     last-id))
3536
3537 (defun gnus-articles-in-thread (thread)
3538   "Return the list of articles in THREAD."
3539   (cons (mail-header-number (car thread))
3540         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3541
3542 (defun gnus-remove-thread (id &optional dont-remove)
3543   "Remove the thread that has ID in it."
3544   (let (headers thread last-id)
3545     ;; First go up in this thread until we find the root.
3546     (setq last-id (gnus-root-id id)
3547           headers (message-flatten-list (gnus-id-to-thread last-id)))
3548     ;; We have now found the real root of this thread.  It might have
3549     ;; been gathered into some loose thread, so we have to search
3550     ;; through the threads to find the thread we wanted.
3551     (let ((threads gnus-newsgroup-threads)
3552           sub)
3553       (while threads
3554         (setq sub (car threads))
3555         (if (stringp (car sub))
3556             ;; This is a gathered thread, so we look at the roots
3557             ;; below it to find whether this article is in this
3558             ;; gathered root.
3559             (progn
3560               (setq sub (cdr sub))
3561               (while sub
3562                 (when (member (caar sub) headers)
3563                   (setq thread (car threads)
3564                         threads nil
3565                         sub nil))
3566                 (setq sub (cdr sub))))
3567           ;; It's an ordinary thread, so we check it.
3568           (when (eq (car sub) (car headers))
3569             (setq thread sub
3570                   threads nil)))
3571         (setq threads (cdr threads)))
3572       ;; If this article is in no thread, then it's a root.
3573       (if thread
3574           (unless dont-remove
3575             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3576         (setq thread (gnus-id-to-thread last-id)))
3577       (when thread
3578         (prog1
3579             thread                      ; We return this thread.
3580           (unless dont-remove
3581             (if (stringp (car thread))
3582                 (progn
3583                   ;; If we use dummy roots, then we have to remove the
3584                   ;; dummy root as well.
3585                   (when (eq gnus-summary-make-false-root 'dummy)
3586                     ;; We go to the dummy root by going to
3587                     ;; the first sub-"thread", and then one line up.
3588                     (gnus-summary-goto-article
3589                      (mail-header-number (caadr thread)))
3590                     (forward-line -1)
3591                     (gnus-delete-line)
3592                     (gnus-data-compute-positions))
3593                   (setq thread (cdr thread))
3594                   (while thread
3595                     (gnus-remove-thread-1 (car thread))
3596                     (setq thread (cdr thread))))
3597               (gnus-summary-show-all-threads)
3598               (gnus-remove-thread-1 thread))))))))
3599
3600 (defun gnus-remove-thread-1 (thread)
3601   "Remove the thread THREAD recursively."
3602   (let ((number (mail-header-number (pop thread)))
3603         d)
3604     (setq thread (reverse thread))
3605     (while thread
3606       (gnus-remove-thread-1 (pop thread)))
3607     (when (setq d (gnus-data-find number))
3608       (goto-char (gnus-data-pos d))
3609       (gnus-data-remove
3610        number
3611        (- (gnus-point-at-bol)
3612           (prog1
3613               (1+ (gnus-point-at-eol))
3614             (gnus-delete-line)))))))
3615
3616 (defun gnus-sort-threads (threads)
3617   "Sort THREADS."
3618   (if (not gnus-thread-sort-functions)
3619       threads
3620     (gnus-message 8 "Sorting threads...")
3621     (prog1
3622         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3623       (gnus-message 8 "Sorting threads...done"))))
3624
3625 (defun gnus-sort-articles (articles)
3626   "Sort ARTICLES."
3627   (when gnus-article-sort-functions
3628     (gnus-message 7 "Sorting articles...")
3629     (prog1
3630         (setq gnus-newsgroup-headers
3631               (sort articles (gnus-make-sort-function
3632                               gnus-article-sort-functions)))
3633       (gnus-message 7 "Sorting articles...done"))))
3634
3635 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3636 (defmacro gnus-thread-header (thread)
3637   ;; Return header of first article in THREAD.
3638   ;; Note that THREAD must never, ever be anything else than a variable -
3639   ;; using some other form will lead to serious barfage.
3640   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3641   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3642   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3643         (vector thread) 2))
3644
3645 (defsubst gnus-article-sort-by-number (h1 h2)
3646   "Sort articles by article number."
3647   (< (mail-header-number h1)
3648      (mail-header-number h2)))
3649
3650 (defun gnus-thread-sort-by-number (h1 h2)
3651   "Sort threads by root article number."
3652   (gnus-article-sort-by-number
3653    (gnus-thread-header h1) (gnus-thread-header h2)))
3654
3655 (defsubst gnus-article-sort-by-lines (h1 h2)
3656   "Sort articles by article Lines header."
3657   (< (mail-header-lines h1)
3658      (mail-header-lines h2)))
3659
3660 (defun gnus-thread-sort-by-lines (h1 h2)
3661   "Sort threads by root article Lines header."
3662   (gnus-article-sort-by-lines
3663    (gnus-thread-header h1) (gnus-thread-header h2)))
3664
3665 (defsubst gnus-article-sort-by-chars (h1 h2)
3666   "Sort articles by octet length."
3667   (< (mail-header-chars h1)
3668      (mail-header-chars h2)))
3669
3670 (defun gnus-thread-sort-by-chars (h1 h2)
3671   "Sort threads by root article octet length."
3672   (gnus-article-sort-by-chars
3673    (gnus-thread-header h1) (gnus-thread-header h2)))
3674
3675 (defsubst gnus-article-sort-by-author (h1 h2)
3676   "Sort articles by root author."
3677   (string-lessp
3678    (let ((extract (funcall
3679                    gnus-extract-address-components
3680                    (mail-header-from h1))))
3681      (or (car extract) (cadr extract) ""))
3682    (let ((extract (funcall
3683                    gnus-extract-address-components
3684                    (mail-header-from h2))))
3685      (or (car extract) (cadr extract) ""))))
3686
3687 (defun gnus-thread-sort-by-author (h1 h2)
3688   "Sort threads by root author."
3689   (gnus-article-sort-by-author
3690    (gnus-thread-header h1)  (gnus-thread-header h2)))
3691
3692 (defsubst gnus-article-sort-by-subject (h1 h2)
3693   "Sort articles by root subject."
3694   (string-lessp
3695    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3696    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3697
3698 (defun gnus-thread-sort-by-subject (h1 h2)
3699   "Sort threads by root subject."
3700   (gnus-article-sort-by-subject
3701    (gnus-thread-header h1) (gnus-thread-header h2)))
3702
3703 (defsubst gnus-article-sort-by-date (h1 h2)
3704   "Sort articles by root article date."
3705   (time-less-p
3706    (gnus-date-get-time (mail-header-date h1))
3707    (gnus-date-get-time (mail-header-date h2))))
3708
3709 (defun gnus-thread-sort-by-date (h1 h2)
3710   "Sort threads by root article date."
3711   (gnus-article-sort-by-date
3712    (gnus-thread-header h1) (gnus-thread-header h2)))
3713
3714 (defsubst gnus-article-sort-by-score (h1 h2)
3715   "Sort articles by root article score.
3716 Unscored articles will be counted as having a score of zero."
3717   (> (or (cdr (assq (mail-header-number h1)
3718                     gnus-newsgroup-scored))
3719          gnus-summary-default-score 0)
3720      (or (cdr (assq (mail-header-number h2)
3721                     gnus-newsgroup-scored))
3722          gnus-summary-default-score 0)))
3723
3724 (defun gnus-thread-sort-by-score (h1 h2)
3725   "Sort threads by root article score."
3726   (gnus-article-sort-by-score
3727    (gnus-thread-header h1) (gnus-thread-header h2)))
3728
3729 (defun gnus-thread-sort-by-total-score (h1 h2)
3730   "Sort threads by the sum of all scores in the thread.
3731 Unscored articles will be counted as having a score of zero."
3732   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3733
3734 (defun gnus-thread-total-score (thread)
3735   ;; This function find the total score of THREAD.
3736   (cond ((null thread)
3737          0)
3738         ((consp thread)
3739          (if (stringp (car thread))
3740              (apply gnus-thread-score-function 0
3741                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3742            (gnus-thread-total-score-1 thread)))
3743         (t
3744          (gnus-thread-total-score-1 (list thread)))))
3745
3746 (defun gnus-thread-total-score-1 (root)
3747   ;; This function find the total score of the thread below ROOT.
3748   (setq root (car root))
3749   (apply gnus-thread-score-function
3750          (or (append
3751               (mapcar 'gnus-thread-total-score
3752                       (cdr (gnus-id-to-thread (mail-header-id root))))
3753               (when (> (mail-header-number root) 0)
3754                 (list (or (cdr (assq (mail-header-number root)
3755                                      gnus-newsgroup-scored))
3756                           gnus-summary-default-score 0))))
3757              (list gnus-summary-default-score)
3758              '(0))))
3759
3760 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3761 (defvar gnus-tmp-prev-subject nil)
3762 (defvar gnus-tmp-false-parent nil)
3763 (defvar gnus-tmp-root-expunged nil)
3764 (defvar gnus-tmp-dummy-line nil)
3765
3766 (defvar gnus-tmp-header)
3767 (defun gnus-extra-header (type &optional header)
3768   "Return the extra header of TYPE."
3769   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3770       ""))
3771
3772 (defun gnus-summary-prepare-threads (threads)
3773   "Prepare summary buffer from THREADS and indentation LEVEL.
3774 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3775 or a straight list of headers."
3776   (gnus-message 7 "Generating summary...")
3777
3778   (setq gnus-newsgroup-threads threads)
3779   (beginning-of-line)
3780
3781   (let ((gnus-tmp-level 0)
3782         (default-score (or gnus-summary-default-score 0))
3783         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3784         thread number subject stack state gnus-tmp-gathered beg-match
3785         new-roots gnus-tmp-new-adopts thread-end
3786         gnus-tmp-header gnus-tmp-unread
3787         gnus-tmp-replied gnus-tmp-subject-or-nil
3788         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3789         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3790         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3791
3792     (setq gnus-tmp-prev-subject nil)
3793
3794     (if (vectorp (car threads))
3795         ;; If this is a straight (sic) list of headers, then a
3796         ;; threaded summary display isn't required, so we just create
3797         ;; an unthreaded one.
3798         (gnus-summary-prepare-unthreaded threads)
3799
3800       ;; Do the threaded display.
3801
3802       (while (or threads stack gnus-tmp-new-adopts new-roots)
3803
3804         (if (and (= gnus-tmp-level 0)
3805                  (or (not stack)
3806                      (= (caar stack) 0))
3807                  (not gnus-tmp-false-parent)
3808                  (or gnus-tmp-new-adopts new-roots))
3809             (if gnus-tmp-new-adopts
3810                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3811                       thread (list (car gnus-tmp-new-adopts))
3812                       gnus-tmp-header (caar thread)
3813                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3814               (when new-roots
3815                 (setq thread (list (car new-roots))
3816                       gnus-tmp-header (caar thread)
3817                       new-roots (cdr new-roots))))
3818
3819           (if threads
3820               ;; If there are some threads, we do them before the
3821               ;; threads on the stack.
3822               (setq thread threads
3823                     gnus-tmp-header (caar thread))
3824             ;; There were no current threads, so we pop something off
3825             ;; the stack.
3826             (setq state (car stack)
3827                   gnus-tmp-level (car state)
3828                   thread (cdr state)
3829                   stack (cdr stack)
3830                   gnus-tmp-header (caar thread))))
3831
3832         (setq gnus-tmp-false-parent nil)
3833         (setq gnus-tmp-root-expunged nil)
3834         (setq thread-end nil)
3835
3836         (if (stringp gnus-tmp-header)
3837             ;; The header is a dummy root.
3838             (cond
3839              ((eq gnus-summary-make-false-root 'adopt)
3840               ;; We let the first article adopt the rest.
3841               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3842                                                (cddar thread)))
3843               (setq gnus-tmp-gathered
3844                     (nconc (mapcar
3845                             (lambda (h) (mail-header-number (car h)))
3846                             (cddar thread))
3847                            gnus-tmp-gathered))
3848               (setq thread (cons (list (caar thread)
3849                                        (cadar thread))
3850                                  (cdr thread)))
3851               (setq gnus-tmp-level -1
3852                     gnus-tmp-false-parent t))
3853              ((eq gnus-summary-make-false-root 'empty)
3854               ;; We print adopted articles with empty subject fields.
3855               (setq gnus-tmp-gathered
3856                     (nconc (mapcar
3857                             (lambda (h) (mail-header-number (car h)))
3858                             (cddar thread))
3859                            gnus-tmp-gathered))
3860               (setq gnus-tmp-level -1))
3861              ((eq gnus-summary-make-false-root 'dummy)
3862               ;; We remember that we probably want to output a dummy
3863               ;; root.
3864               (setq gnus-tmp-dummy-line gnus-tmp-header)
3865               (setq gnus-tmp-prev-subject gnus-tmp-header))
3866              (t
3867               ;; We do not make a root for the gathered
3868               ;; sub-threads at all.
3869               (setq gnus-tmp-level -1)))
3870
3871           (setq number (mail-header-number gnus-tmp-header)
3872                 subject (mail-header-subject gnus-tmp-header))
3873
3874           (cond
3875            ;; If the thread has changed subject, we might want to make
3876            ;; this subthread into a root.
3877            ((and (null gnus-thread-ignore-subject)
3878                  (not (zerop gnus-tmp-level))
3879                  gnus-tmp-prev-subject
3880                  (not (inline
3881                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3882             (setq new-roots (nconc new-roots (list (car thread)))
3883                   thread-end t
3884                   gnus-tmp-header nil))
3885            ;; If the article lies outside the current limit,
3886            ;; then we do not display it.
3887            ((not (memq number gnus-newsgroup-limit))
3888             (setq gnus-tmp-gathered
3889                   (nconc (mapcar
3890                           (lambda (h) (mail-header-number (car h)))
3891                           (cdar thread))
3892                          gnus-tmp-gathered))
3893             (setq gnus-tmp-new-adopts (if (cdar thread)
3894                                           (append gnus-tmp-new-adopts
3895                                                   (cdar thread))
3896                                         gnus-tmp-new-adopts)
3897                   thread-end t
3898                   gnus-tmp-header nil)
3899             (when (zerop gnus-tmp-level)
3900               (setq gnus-tmp-root-expunged t)))
3901            ;; Perhaps this article is to be marked as read?
3902            ((and gnus-summary-mark-below
3903                  (< (or (cdr (assq number gnus-newsgroup-scored))
3904                         default-score)
3905                     gnus-summary-mark-below)
3906                  ;; Don't touch sparse articles.
3907                  (not (gnus-summary-article-sparse-p number))
3908                  (not (gnus-summary-article-ancient-p number)))
3909             (setq gnus-newsgroup-unreads
3910                   (delq number gnus-newsgroup-unreads))
3911             (if gnus-newsgroup-auto-expire
3912                 (push number gnus-newsgroup-expirable)
3913               (push (cons number gnus-low-score-mark)
3914                     gnus-newsgroup-reads))))
3915
3916           (when gnus-tmp-header
3917             ;; We may have an old dummy line to output before this
3918             ;; article.
3919             (when (and gnus-tmp-dummy-line
3920                        (gnus-subject-equal
3921                         gnus-tmp-dummy-line
3922                         (mail-header-subject gnus-tmp-header)))
3923               (gnus-summary-insert-dummy-line
3924                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3925               (setq gnus-tmp-dummy-line nil))
3926
3927             ;; Compute the mark.
3928             (setq gnus-tmp-unread (gnus-article-mark number))
3929
3930             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3931                                   gnus-tmp-header gnus-tmp-level)
3932                   gnus-newsgroup-data)
3933
3934             ;; Actually insert the line.
3935             (setq
3936              gnus-tmp-subject-or-nil
3937              (cond
3938               ((and gnus-thread-ignore-subject
3939                     gnus-tmp-prev-subject
3940                     (not (inline (gnus-subject-equal
3941                                   gnus-tmp-prev-subject subject))))
3942                subject)
3943               ((zerop gnus-tmp-level)
3944                (if (and (eq gnus-summary-make-false-root 'empty)
3945                         (memq number gnus-tmp-gathered)
3946                         gnus-tmp-prev-subject
3947                         (inline (gnus-subject-equal
3948                                  gnus-tmp-prev-subject subject)))
3949                    gnus-summary-same-subject
3950                  subject))
3951               (t gnus-summary-same-subject)))
3952             (if (and (eq gnus-summary-make-false-root 'adopt)
3953                      (= gnus-tmp-level 1)
3954                      (memq number gnus-tmp-gathered))
3955                 (setq gnus-tmp-opening-bracket ?\<
3956                       gnus-tmp-closing-bracket ?\>)
3957               (setq gnus-tmp-opening-bracket ?\[
3958                     gnus-tmp-closing-bracket ?\]))
3959             (setq
3960              gnus-tmp-indentation
3961              (aref gnus-thread-indent-array gnus-tmp-level)
3962              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3963              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3964                                 gnus-summary-default-score 0)
3965              gnus-tmp-score-char
3966              (if (or (null gnus-summary-default-score)
3967                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3968                          gnus-summary-zcore-fuzz))
3969                  ?  ;Whitespace
3970                (if (< gnus-tmp-score gnus-summary-default-score)
3971                    gnus-score-below-mark gnus-score-over-mark))
3972              gnus-tmp-replied
3973              (cond ((memq number gnus-newsgroup-processable)
3974                     gnus-process-mark)
3975                    ((memq number gnus-newsgroup-cached)
3976                     gnus-cached-mark)
3977                    ((memq number gnus-newsgroup-replied)
3978                     gnus-replied-mark)
3979                    ((memq number gnus-newsgroup-saved)
3980                     gnus-saved-mark)
3981                    (t gnus-unread-mark))
3982              gnus-tmp-from (mail-header-from gnus-tmp-header)
3983              gnus-tmp-name
3984              (cond
3985               ((string-match "<[^>]+> *$" gnus-tmp-from)
3986                (setq beg-match (match-beginning 0))
3987                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3988                         (substring gnus-tmp-from (1+ (match-beginning 0))
3989                                    (1- (match-end 0))))
3990                    (substring gnus-tmp-from 0 beg-match)))
3991               ((string-match "(.+)" gnus-tmp-from)
3992                (substring gnus-tmp-from
3993                           (1+ (match-beginning 0)) (1- (match-end 0))))
3994               (t gnus-tmp-from)))
3995             (when (string= gnus-tmp-name "")
3996               (setq gnus-tmp-name gnus-tmp-from))
3997             (unless (numberp gnus-tmp-lines)
3998               (setq gnus-tmp-lines 0))
3999             (gnus-put-text-property
4000              (point)
4001              (progn (eval gnus-summary-line-format-spec) (point))
4002              'gnus-number number)
4003             (when gnus-visual-p
4004               (forward-line -1)
4005               (gnus-run-hooks 'gnus-summary-update-hook)
4006               (forward-line 1))
4007
4008             (setq gnus-tmp-prev-subject subject)))
4009
4010         (when (nth 1 thread)
4011           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4012         (incf gnus-tmp-level)
4013         (setq threads (if thread-end nil (cdar thread)))
4014         (unless threads
4015           (setq gnus-tmp-level 0)))))
4016   (gnus-message 7 "Generating summary...done"))
4017
4018 (defun gnus-summary-prepare-unthreaded (headers)
4019   "Generate an unthreaded summary buffer based on HEADERS."
4020   (let (header number mark)
4021
4022     (beginning-of-line)
4023
4024     (while headers
4025       ;; We may have to root out some bad articles...
4026       (when (memq (setq number (mail-header-number
4027                                 (setq header (pop headers))))
4028                   gnus-newsgroup-limit)
4029         ;; Mark article as read when it has a low score.
4030         (when (and gnus-summary-mark-below
4031                    (< (or (cdr (assq number gnus-newsgroup-scored))
4032                           gnus-summary-default-score 0)
4033                       gnus-summary-mark-below)
4034                    (not (gnus-summary-article-ancient-p number)))
4035           (setq gnus-newsgroup-unreads
4036                 (delq number gnus-newsgroup-unreads))
4037           (if gnus-newsgroup-auto-expire
4038               (push number gnus-newsgroup-expirable)
4039             (push (cons number gnus-low-score-mark)
4040                   gnus-newsgroup-reads)))
4041
4042         (setq mark (gnus-article-mark number))
4043         (push (gnus-data-make number mark (1+ (point)) header 0)
4044               gnus-newsgroup-data)
4045         (gnus-summary-insert-line
4046          header 0 number
4047          mark (memq number gnus-newsgroup-replied)
4048          (memq number gnus-newsgroup-expirable)
4049          (mail-header-subject header) nil
4050          (cdr (assq number gnus-newsgroup-scored))
4051          (memq number gnus-newsgroup-processable))))))
4052
4053 (defun gnus-summary-remove-list-identifiers ()
4054   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4055   (let ((regexp (if (stringp gnus-list-identifiers)
4056                     gnus-list-identifiers
4057                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4058     (dolist (header gnus-newsgroup-headers)
4059       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4060                           (mail-header-subject header))
4061         (mail-header-set-subject
4062          header (concat (substring (mail-header-subject header)
4063                                    0 (match-beginning 2))
4064                         (substring (mail-header-subject header)
4065                                    (match-end 2))))))))
4066
4067 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4068   "Select newsgroup GROUP.
4069 If READ-ALL is non-nil, all articles in the group are selected.
4070 If SELECT-ARTICLES, only select those articles from GROUP."
4071   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4072          ;;!!! Dirty hack; should be removed.
4073          (gnus-summary-ignore-duplicates
4074           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4075               t
4076             gnus-summary-ignore-duplicates))
4077          (info (nth 2 entry))
4078          articles fetched-articles cached)
4079
4080     (unless (gnus-check-server
4081              (setq gnus-current-select-method
4082                    (gnus-find-method-for-group group)))
4083       (error "Couldn't open server"))
4084
4085     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4086         (gnus-activate-group group)     ; Or we can activate it...
4087         (progn                          ; Or we bug out.
4088           (when (equal major-mode 'gnus-summary-mode)
4089             (kill-buffer (current-buffer)))
4090           (error "Couldn't request group %s: %s"
4091                  group (gnus-status-message group))))
4092
4093     (unless (gnus-request-group group t)
4094       (when (equal major-mode 'gnus-summary-mode)
4095         (kill-buffer (current-buffer)))
4096       (error "Couldn't request group %s: %s"
4097              group (gnus-status-message group)))
4098
4099     (setq gnus-newsgroup-name group)
4100     (setq gnus-newsgroup-unselected nil)
4101     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4102     (gnus-summary-setup-default-charset)
4103
4104     ;; Adjust and set lists of article marks.
4105     (when info
4106       (gnus-adjust-marked-articles info))
4107
4108     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4109     (when (gnus-virtual-group-p group)
4110       (setq cached gnus-newsgroup-cached))
4111
4112     (setq gnus-newsgroup-unreads
4113           (gnus-set-difference
4114            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4115            gnus-newsgroup-dormant))
4116
4117     (setq gnus-newsgroup-processable nil)
4118
4119     (gnus-update-read-articles group gnus-newsgroup-unreads)
4120
4121     (if (setq articles select-articles)
4122         (setq gnus-newsgroup-unselected
4123               (gnus-sorted-intersection
4124                gnus-newsgroup-unreads
4125                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4126       (setq articles (gnus-articles-to-read group read-all)))
4127
4128     (cond
4129      ((null articles)
4130       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4131       'quit)
4132      ((eq articles 0) nil)
4133      (t
4134       ;; Init the dependencies hash table.
4135       (setq gnus-newsgroup-dependencies
4136             (gnus-make-hashtable (length articles)))
4137       (gnus-set-global-variables)
4138       ;; Retrieve the headers and read them in.
4139       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4140       (setq gnus-newsgroup-headers
4141             (if (eq 'nov
4142                     (setq gnus-headers-retrieved-by
4143                           (gnus-retrieve-headers
4144                            articles gnus-newsgroup-name
4145                            ;; We might want to fetch old headers, but
4146                            ;; not if there is only 1 article.
4147                            (and (or (and
4148                                      (not (eq gnus-fetch-old-headers 'some))
4149                                      (not (numberp gnus-fetch-old-headers)))
4150                                     (> (length articles) 1))
4151                                 gnus-fetch-old-headers))))
4152                 (gnus-get-newsgroup-headers-xover
4153                  articles nil nil gnus-newsgroup-name t)
4154               (gnus-get-newsgroup-headers)))
4155       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4156
4157       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4158       (when cached
4159         (setq gnus-newsgroup-cached cached))
4160
4161       ;; Suppress duplicates?
4162       (when gnus-suppress-duplicates
4163         (gnus-dup-suppress-articles))
4164
4165       ;; Set the initial limit.
4166       (setq gnus-newsgroup-limit (copy-sequence articles))
4167       ;; Remove canceled articles from the list of unread articles.
4168       (setq gnus-newsgroup-unreads
4169             (gnus-set-sorted-intersection
4170              gnus-newsgroup-unreads
4171              (setq fetched-articles
4172                    (mapcar (lambda (headers) (mail-header-number headers))
4173                            gnus-newsgroup-headers))))
4174       ;; Removed marked articles that do not exist.
4175       (gnus-update-missing-marks
4176        (gnus-sorted-complement fetched-articles articles))
4177       ;; We might want to build some more threads first.
4178       (when (and gnus-fetch-old-headers
4179                  (eq gnus-headers-retrieved-by 'nov))
4180         (if (eq gnus-fetch-old-headers 'invisible)
4181             (gnus-build-all-threads)
4182           (gnus-build-old-threads)))
4183       ;; Let the Gnus agent mark articles as read.
4184       (when gnus-agent
4185         (gnus-agent-get-undownloaded-list))
4186       ;; Remove list identifiers from subject
4187       (when gnus-list-identifiers
4188         (gnus-summary-remove-list-identifiers))
4189       ;; Check whether auto-expire is to be done in this group.
4190       (setq gnus-newsgroup-auto-expire
4191             (gnus-group-auto-expirable-p group))
4192       ;; Set up the article buffer now, if necessary.
4193       (unless gnus-single-article-buffer
4194         (gnus-article-setup-buffer))
4195       ;; First and last article in this newsgroup.
4196       (when gnus-newsgroup-headers
4197         (setq gnus-newsgroup-begin
4198               (mail-header-number (car gnus-newsgroup-headers))
4199               gnus-newsgroup-end
4200               (mail-header-number
4201                (gnus-last-element gnus-newsgroup-headers))))
4202       ;; GROUP is successfully selected.
4203       (or gnus-newsgroup-headers t)))))
4204
4205 (defun gnus-articles-to-read (group &optional read-all)
4206   ;; Find out what articles the user wants to read.
4207   (let* ((articles
4208           ;; Select all articles if `read-all' is non-nil, or if there
4209           ;; are no unread articles.
4210           (if (or read-all
4211                   (and (zerop (length gnus-newsgroup-marked))
4212                        (zerop (length gnus-newsgroup-unreads)))
4213                   (eq (gnus-group-find-parameter group 'display)
4214                       'all))
4215               (or
4216                (gnus-uncompress-range (gnus-active group))
4217                (gnus-cache-articles-in-group group))
4218             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4219                           (copy-sequence gnus-newsgroup-unreads))
4220                   '<)))
4221          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4222          (scored (length scored-list))
4223          (number (length articles))
4224          (marked (+ (length gnus-newsgroup-marked)
4225                     (length gnus-newsgroup-dormant)))
4226          (select
4227           (cond
4228            ((numberp read-all)
4229             read-all)
4230            (t
4231             (condition-case ()
4232                 (cond
4233                  ((and (or (<= scored marked) (= scored number))
4234                        (numberp gnus-large-newsgroup)
4235                        (> number gnus-large-newsgroup))
4236                   (let ((input
4237                          (read-string
4238                           (format
4239                            "How many articles from %s (default %d): "
4240                            (gnus-limit-string gnus-newsgroup-name 35)
4241                            number))))
4242                     (if (string-match "^[ \t]*$" input) number input)))
4243                  ((and (> scored marked) (< scored number)
4244                        (> (- scored number) 20))
4245                   (let ((input
4246                          (read-string
4247                           (format "%s %s (%d scored, %d total): "
4248                                   "How many articles from"
4249                                   group scored number))))
4250                     (if (string-match "^[ \t]*$" input)
4251                         number input)))
4252                  (t number))
4253               (quit nil))))))
4254     (setq select (if (stringp select) (string-to-number select) select))
4255     (if (or (null select) (zerop select))
4256         select
4257       (if (and (not (zerop scored)) (<= (abs select) scored))
4258           (progn
4259             (setq articles (sort scored-list '<))
4260             (setq number (length articles)))
4261         (setq articles (copy-sequence articles)))
4262
4263       (when (< (abs select) number)
4264         (if (< select 0)
4265             ;; Select the N oldest articles.
4266             (setcdr (nthcdr (1- (abs select)) articles) nil)
4267           ;; Select the N most recent articles.
4268           (setq articles (nthcdr (- number select) articles))))
4269       (setq gnus-newsgroup-unselected
4270             (gnus-sorted-intersection
4271              gnus-newsgroup-unreads
4272              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4273       articles)))
4274
4275 (defun gnus-killed-articles (killed articles)
4276   (let (out)
4277     (while articles
4278       (when (inline (gnus-member-of-range (car articles) killed))
4279         (push (car articles) out))
4280       (setq articles (cdr articles)))
4281     out))
4282
4283 (defun gnus-uncompress-marks (marks)
4284   "Uncompress the mark ranges in MARKS."
4285   (let ((uncompressed '(score bookmark))
4286         out)
4287     (while marks
4288       (if (memq (caar marks) uncompressed)
4289           (push (car marks) out)
4290         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4291       (setq marks (cdr marks)))
4292     out))
4293
4294 (defun gnus-adjust-marked-articles (info)
4295   "Set all article lists and remove all marks that are no longer valid."
4296   (let* ((marked-lists (gnus-info-marks info))
4297          (active (gnus-active (gnus-info-group info)))
4298          (min (car active))
4299          (max (cdr active))
4300          (types gnus-article-mark-lists)
4301          (uncompressed '(score bookmark killed))
4302          marks var articles article mark)
4303
4304     (while marked-lists
4305       (setq marks (pop marked-lists))
4306       (set (setq var (intern (format "gnus-newsgroup-%s"
4307                                      (car (rassq (setq mark (car marks))
4308                                                  types)))))
4309            (if (memq (car marks) uncompressed) (cdr marks)
4310              (gnus-uncompress-range (cdr marks))))
4311
4312       (setq articles (symbol-value var))
4313
4314       ;; All articles have to be subsets of the active articles.
4315       (cond
4316        ;; Adjust "simple" lists.
4317        ((memq mark '(tick dormant expire reply save))
4318         (while articles
4319           (when (or (< (setq article (pop articles)) min) (> article max))
4320             (set var (delq article (symbol-value var))))))
4321        ;; Adjust assocs.
4322        ((memq mark uncompressed)
4323         (when (not (listp (cdr (symbol-value var))))
4324           (set var (list (symbol-value var))))
4325         (when (not (listp (cdr articles)))
4326           (setq articles (list articles)))
4327         (while articles
4328           (when (or (not (consp (setq article (pop articles))))
4329                     (< (car article) min)
4330                     (> (car article) max))
4331             (set var (delq article (symbol-value var))))))))))
4332
4333 (defun gnus-update-missing-marks (missing)
4334   "Go through the list of MISSING articles and remove them from the mark lists."
4335   (when missing
4336     (let ((types gnus-article-mark-lists)
4337           var m)
4338       ;; Go through all types.
4339       (while types
4340         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4341         (when (symbol-value var)
4342           ;; This list has articles.  So we delete all missing articles
4343           ;; from it.
4344           (setq m missing)
4345           (while m
4346             (set var (delq (pop m) (symbol-value var)))))))))
4347
4348 (defun gnus-update-marks ()
4349   "Enter the various lists of marked articles into the newsgroup info list."
4350   (let ((types gnus-article-mark-lists)
4351         (info (gnus-get-info gnus-newsgroup-name))
4352         (uncompressed '(score bookmark killed))
4353         type list newmarked symbol delta-marks)
4354     (when info
4355       ;; Add all marks lists to the list of marks lists.
4356       (while (setq type (pop types))
4357         (setq list (symbol-value
4358                     (setq symbol
4359                           (intern (format "gnus-newsgroup-%s"
4360                                           (car type))))))
4361
4362         (when list
4363           ;; Get rid of the entries of the articles that have the
4364           ;; default score.
4365           (when (and (eq (cdr type) 'score)
4366                      gnus-save-score
4367                      list)
4368             (let* ((arts list)
4369                    (prev (cons nil list))
4370                    (all prev))
4371               (while arts
4372                 (if (or (not (consp (car arts)))
4373                         (= (cdar arts) gnus-summary-default-score))
4374                     (setcdr prev (cdr arts))
4375                   (setq prev arts))
4376                 (setq arts (cdr arts)))
4377               (setq list (cdr all)))))
4378
4379         (unless (memq (cdr type) uncompressed)
4380           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4381        
4382         (when (gnus-check-backend-function
4383                'request-set-mark gnus-newsgroup-name)
4384           ;; uncompressed:s are not proper flags (they are cons cells)
4385           ;; cache is a internal gnus flag
4386           (unless (memq (cdr type) (cons 'cache uncompressed))
4387             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4388                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4389                    (add (gnus-remove-from-range
4390                          (gnus-copy-sequence list) old)))
4391               (when add
4392                 (push (list add 'add (list (cdr type))) delta-marks))
4393               (when del
4394                 (push (list del 'del (list (cdr type))) delta-marks)))))
4395           
4396         (when list
4397           (push (cons (cdr type) list) newmarked)))
4398
4399       (when delta-marks
4400         (unless (gnus-check-group gnus-newsgroup-name)
4401           (error "Can't open server for %s" gnus-newsgroup-name))
4402         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4403           
4404       ;; Enter these new marks into the info of the group.
4405       (if (nthcdr 3 info)
4406           (setcar (nthcdr 3 info) newmarked)
4407         ;; Add the marks lists to the end of the info.
4408         (when newmarked
4409           (setcdr (nthcdr 2 info) (list newmarked))))
4410
4411       ;; Cut off the end of the info if there's nothing else there.
4412       (let ((i 5))
4413         (while (and (> i 2)
4414                     (not (nth i info)))
4415           (when (nthcdr (decf i) info)
4416             (setcdr (nthcdr i info) nil)))))))
4417
4418 (defun gnus-set-mode-line (where)
4419   "This function sets the mode line of the article or summary buffers.
4420 If WHERE is `summary', the summary mode line format will be used."
4421   ;; Is this mode line one we keep updated?
4422   (when (and (memq where gnus-updated-mode-lines)
4423              (symbol-value
4424               (intern (format "gnus-%s-mode-line-format-spec" where))))
4425     (let (mode-string)
4426       (save-excursion
4427         ;; We evaluate this in the summary buffer since these
4428         ;; variables are buffer-local to that buffer.
4429         (set-buffer gnus-summary-buffer)
4430         ;; We bind all these variables that are used in the `eval' form
4431         ;; below.
4432         (let* ((mformat (symbol-value
4433                          (intern
4434                           (format "gnus-%s-mode-line-format-spec" where))))
4435                (gnus-tmp-group-name gnus-newsgroup-name)
4436                (gnus-tmp-article-number (or gnus-current-article 0))
4437                (gnus-tmp-unread gnus-newsgroup-unreads)
4438                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4439                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4440                (gnus-tmp-unread-and-unselected
4441                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4442                             (zerop gnus-tmp-unselected))
4443                        "")
4444                       ((zerop gnus-tmp-unselected)
4445                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4446                       (t (format "{%d(+%d) more}"
4447                                  gnus-tmp-unread-and-unticked
4448                                  gnus-tmp-unselected))))
4449                (gnus-tmp-subject
4450                 (if (and gnus-current-headers
4451                          (vectorp gnus-current-headers))
4452                     (gnus-mode-string-quote
4453                      (mail-header-subject gnus-current-headers))
4454                   ""))
4455                bufname-length max-len
4456                gnus-tmp-header);; passed as argument to any user-format-funcs
4457           (setq mode-string (eval mformat))
4458           (setq bufname-length (if (string-match "%b" mode-string)
4459                                    (- (length
4460                                        (buffer-name
4461                                         (if (eq where 'summary)
4462                                             nil
4463                                           (get-buffer gnus-article-buffer))))
4464                                       2)
4465                                  0))
4466           (setq max-len (max 4 (if gnus-mode-non-string-length
4467                                    (- (window-width)
4468                                       gnus-mode-non-string-length
4469                                       bufname-length)
4470                                  (length mode-string))))
4471           ;; We might have to chop a bit of the string off...
4472           (when (> (length mode-string) max-len)
4473             (setq mode-string
4474                   (concat (truncate-string-to-width mode-string (- max-len 3))
4475                           "...")))
4476           ;; Pad the mode string a bit.
4477           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4478       ;; Update the mode line.
4479       (setq mode-line-buffer-identification
4480             (gnus-mode-line-buffer-identification (list mode-string)))
4481       (set-buffer-modified-p t))))
4482
4483 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4484   "Go through the HEADERS list and add all Xrefs to a hash table.
4485 The resulting hash table is returned, or nil if no Xrefs were found."
4486   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4487          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4488          (xref-hashtb (gnus-make-hashtable))
4489          start group entry number xrefs header)
4490     (while headers
4491       (setq header (pop headers))
4492       (when (and (setq xrefs (mail-header-xref header))
4493                  (not (memq (setq number (mail-header-number header))
4494                             unreads)))
4495         (setq start 0)
4496         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4497           (setq start (match-end 0))
4498           (setq group (if prefix
4499                           (concat prefix (substring xrefs (match-beginning 1)
4500                                                     (match-end 1)))
4501                         (substring xrefs (match-beginning 1) (match-end 1))))
4502           (setq number
4503                 (string-to-int (substring xrefs (match-beginning 2)
4504                                           (match-end 2))))
4505           (if (setq entry (gnus-gethash group xref-hashtb))
4506               (setcdr entry (cons number (cdr entry)))
4507             (gnus-sethash group (cons number nil) xref-hashtb)))))
4508     (and start xref-hashtb)))
4509
4510 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4511   "Look through all the headers and mark the Xrefs as read."
4512   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4513         name entry info xref-hashtb idlist method nth4)
4514     (save-excursion
4515       (set-buffer gnus-group-buffer)
4516       (when (setq xref-hashtb
4517                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4518         (mapatoms
4519          (lambda (group)
4520            (unless (string= from-newsgroup (setq name (symbol-name group)))
4521              (setq idlist (symbol-value group))
4522              ;; Dead groups are not updated.
4523              (and (prog1
4524                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4525                             info (nth 2 entry))
4526                     (when (stringp (setq nth4 (gnus-info-method info)))
4527                       (setq nth4 (gnus-server-to-method nth4))))
4528                   ;; Only do the xrefs if the group has the same
4529                   ;; select method as the group we have just read.
4530                   (or (gnus-methods-equal-p
4531                        nth4 (gnus-find-method-for-group from-newsgroup))
4532                       virtual
4533                       (equal nth4 (setq method (gnus-find-method-for-group
4534                                                 from-newsgroup)))
4535                       (and (equal (car nth4) (car method))
4536                            (equal (nth 1 nth4) (nth 1 method))))
4537                   gnus-use-cross-reference
4538                   (or (not (eq gnus-use-cross-reference t))
4539                       virtual
4540                       ;; Only do cross-references on subscribed
4541                       ;; groups, if that is what is wanted.
4542                       (<= (gnus-info-level info) gnus-level-subscribed))
4543                   (gnus-group-make-articles-read name idlist))))
4544          xref-hashtb)))))
4545
4546 (defun gnus-compute-read-articles (group articles)
4547   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4548          (info (nth 2 entry))
4549          (active (gnus-active group))
4550          ninfo)
4551     (when entry
4552       ;; First peel off all invalid article numbers.
4553       (when active
4554         (let ((ids articles)
4555               id first)
4556           (while (setq id (pop ids))
4557             (when (and first (> id (cdr active)))
4558               ;; We'll end up in this situation in one particular
4559               ;; obscure situation.  If you re-scan a group and get
4560               ;; a new article that is cross-posted to a different
4561               ;; group that has not been re-scanned, you might get
4562               ;; crossposted article that has a higher number than
4563               ;; Gnus believes possible.  So we re-activate this
4564               ;; group as well.  This might mean doing the
4565               ;; crossposting thingy will *increase* the number
4566               ;; of articles in some groups.  Tsk, tsk.
4567               (setq active (or (gnus-activate-group group) active)))
4568             (when (or (> id (cdr active))
4569                       (< id (car active)))
4570               (setq articles (delq id articles))))))
4571       ;; If the read list is nil, we init it.
4572       (if (and active
4573                (null (gnus-info-read info))
4574                (> (car active) 1))
4575           (setq ninfo (cons 1 (1- (car active))))
4576         (setq ninfo (gnus-info-read info)))
4577       ;; Then we add the read articles to the range.
4578       (gnus-add-to-range
4579        ninfo (setq articles (sort articles '<))))))
4580
4581 (defun gnus-group-make-articles-read (group articles)
4582   "Update the info of GROUP to say that ARTICLES are read."
4583   (let* ((num 0)
4584          (entry (gnus-gethash group gnus-newsrc-hashtb))
4585          (info (nth 2 entry))
4586          (active (gnus-active group))
4587          range)
4588     (when entry
4589       (setq range (gnus-compute-read-articles group articles))
4590       (save-excursion
4591         (set-buffer gnus-group-buffer)
4592         (gnus-undo-register
4593           `(progn
4594              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4595              (gnus-info-set-read ',info ',(gnus-info-read info))
4596              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4597              (gnus-group-update-group ,group t))))
4598       ;; Add the read articles to the range.
4599       (gnus-info-set-read info range)
4600       ;; Then we have to re-compute how many unread
4601       ;; articles there are in this group.
4602       (when active
4603         (cond
4604          ((not range)
4605           (setq num (- (1+ (cdr active)) (car active))))
4606          ((not (listp (cdr range)))
4607           (setq num (- (cdr active) (- (1+ (cdr range))
4608                                        (car range)))))
4609          (t
4610           (while range
4611             (if (numberp (car range))
4612                 (setq num (1+ num))
4613               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4614             (setq range (cdr range)))
4615           (setq num (- (cdr active) num))))
4616         ;; Update the number of unread articles.
4617         (setcar entry num)
4618         ;; Update the group buffer.
4619         (gnus-group-update-group group t)))))
4620
4621 (defvar gnus-newsgroup-none-id 0)
4622
4623 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4624   (let ((cur nntp-server-buffer)
4625         (dependencies
4626          (or dependencies
4627              (save-excursion (set-buffer gnus-summary-buffer)
4628                              gnus-newsgroup-dependencies)))
4629         headers id end ref
4630         (mail-parse-charset gnus-newsgroup-charset)
4631         (mail-parse-ignored-charsets 
4632          (save-excursion (condition-case nil
4633                              (set-buffer gnus-summary-buffer)
4634                            (error))
4635                          gnus-newsgroup-ignored-charsets)))
4636     (save-excursion
4637       (set-buffer nntp-server-buffer)
4638       ;; Translate all TAB characters into SPACE characters.
4639       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4640       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4641       (gnus-run-hooks 'gnus-parse-headers-hook)
4642       (let ((case-fold-search t)
4643             in-reply-to header p lines chars)
4644         (goto-char (point-min))
4645         ;; Search to the beginning of the next header.  Error messages
4646         ;; do not begin with 2 or 3.
4647         (while (re-search-forward "^[23][0-9]+ " nil t)
4648           (setq id nil
4649                 ref nil)
4650           ;; This implementation of this function, with nine
4651           ;; search-forwards instead of the one re-search-forward and
4652           ;; a case (which basically was the old function) is actually
4653           ;; about twice as fast, even though it looks messier.  You
4654           ;; can't have everything, I guess.  Speed and elegance
4655           ;; doesn't always go hand in hand.
4656           (setq
4657            header
4658            (vector
4659             ;; Number.
4660             (prog1
4661                 (read cur)
4662               (end-of-line)
4663               (setq p (point))
4664               (narrow-to-region (point)
4665                                 (or (and (search-forward "\n.\n" nil t)
4666                                          (- (point) 2))
4667                                     (point))))
4668             ;; Subject.
4669             (progn
4670               (goto-char p)
4671               (if (search-forward "\nsubject: " nil t)
4672                   (funcall gnus-decode-encoded-word-function
4673                            (nnheader-header-value))
4674                 "(none)"))
4675             ;; From.
4676             (progn
4677               (goto-char p)
4678               (if (search-forward "\nfrom: " nil t)
4679                   (funcall gnus-decode-encoded-word-function
4680                            (nnheader-header-value))
4681                 "(nobody)"))
4682             ;; Date.
4683             (progn
4684               (goto-char p)
4685               (if (search-forward "\ndate: " nil t)
4686                   (nnheader-header-value) ""))
4687             ;; Message-ID.
4688             (progn
4689               (goto-char p)
4690               (setq id (if (re-search-forward
4691                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4692                            ;; We do it this way to make sure the Message-ID
4693                            ;; is (somewhat) syntactically valid.
4694                            (buffer-substring (match-beginning 1)
4695                                              (match-end 1))
4696                          ;; If there was no message-id, we just fake one
4697                          ;; to make subsequent routines simpler.
4698                          (nnheader-generate-fake-message-id))))
4699             ;; References.
4700             (progn
4701               (goto-char p)
4702               (if (search-forward "\nreferences: " nil t)
4703                   (progn
4704                     (setq end (point))
4705                     (prog1
4706                         (nnheader-header-value)
4707                       (setq ref
4708                             (buffer-substring
4709                              (progn
4710                                (end-of-line)
4711                                (search-backward ">" end t)
4712                                (1+ (point)))
4713                              (progn
4714                                (search-backward "<" end t)
4715                                (point))))))
4716                 ;; Get the references from the in-reply-to header if there
4717                 ;; were no references and the in-reply-to header looks
4718                 ;; promising.
4719                 (if (and (search-forward "\nin-reply-to: " nil t)
4720                          (setq in-reply-to (nnheader-header-value))
4721                          (string-match "<[^>]+>" in-reply-to))
4722                     (let (ref2)
4723                       (setq ref (substring in-reply-to (match-beginning 0)
4724                                            (match-end 0)))
4725                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4726                         (setq ref2 (substring in-reply-to (match-beginning 0)
4727                                               (match-end 0)))
4728                         (when (> (length ref2) (length ref))
4729                           (setq ref ref2)))
4730                       ref)
4731                   (setq ref nil))))
4732             ;; Chars.
4733             (progn
4734               (goto-char p)
4735               (if (search-forward "\nchars: " nil t)
4736                   (if (numberp (setq chars (ignore-errors (read cur))))
4737                       chars 0)
4738                 0))
4739             ;; Lines.
4740             (progn
4741               (goto-char p)
4742               (if (search-forward "\nlines: " nil t)
4743                   (if (numberp (setq lines (ignore-errors (read cur))))
4744                       lines 0)
4745                 0))
4746             ;; Xref.
4747             (progn
4748               (goto-char p)
4749               (and (search-forward "\nxref: " nil t)
4750                    (nnheader-header-value)))
4751             ;; Extra.
4752             (when gnus-extra-headers
4753               (let ((extra gnus-extra-headers)
4754                     out)
4755                 (while extra
4756                   (goto-char p)
4757                   (when (search-forward
4758                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4759                     (push (cons (car extra) (nnheader-header-value))
4760                           out))
4761                   (pop extra))
4762                 out))))
4763           (when (equal id ref)
4764             (setq ref nil))
4765
4766           (when gnus-alter-header-function
4767             (funcall gnus-alter-header-function header)
4768             (setq id (mail-header-id header)
4769                   ref (gnus-parent-id (mail-header-references header))))
4770
4771           (when (setq header
4772                       (gnus-dependencies-add-header
4773                        header dependencies force-new))
4774             (push header headers))
4775           (goto-char (point-max))
4776           (widen))
4777         (nreverse headers)))))
4778
4779 ;; Goes through the xover lines and returns a list of vectors
4780 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4781                                                   force-new dependencies
4782                                                   group also-fetch-heads)
4783   "Parse the news overview data in the server buffer, and return a
4784 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4785   ;; Get the Xref when the users reads the articles since most/some
4786   ;; NNTP servers do not include Xrefs when using XOVER.
4787   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4788   (let ((mail-parse-charset gnus-newsgroup-charset)
4789         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4790         (cur nntp-server-buffer)
4791         (dependencies (or dependencies gnus-newsgroup-dependencies))
4792         number headers header)
4793     (save-excursion
4794       (set-buffer nntp-server-buffer)
4795       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4796       ;; Allow the user to mangle the headers before parsing them.
4797       (gnus-run-hooks 'gnus-parse-headers-hook)
4798       (goto-char (point-min))
4799       (while (not (eobp))
4800         (condition-case ()
4801             (while (and sequence (not (eobp)))
4802               (setq number (read cur))
4803               (while (and sequence
4804                           (< (car sequence) number))
4805                 (setq sequence (cdr sequence)))
4806               (and sequence
4807                    (eq number (car sequence))
4808                    (progn
4809                      (setq sequence (cdr sequence))
4810                      (setq header (inline
4811                                     (gnus-nov-parse-line
4812                                      number dependencies force-new))))
4813                    (push header headers))
4814               (forward-line 1))
4815           (error
4816            (gnus-error 4 "Strange nov line (%d)"
4817                        (count-lines (point-min) (point)))))
4818         (forward-line 1))
4819       ;; A common bug in inn is that if you have posted an article and
4820       ;; then retrieves the active file, it will answer correctly --
4821       ;; the new article is included.  However, a NOV entry for the
4822       ;; article may not have been generated yet, so this may fail.
4823       ;; We work around this problem by retrieving the last few
4824       ;; headers using HEAD.
4825       (if (or (not also-fetch-heads)
4826               (not sequence))
4827           ;; We (probably) got all the headers.
4828           (nreverse headers)
4829         (let ((gnus-nov-is-evil t))
4830           (nconc
4831            (nreverse headers)
4832            (when (gnus-retrieve-headers sequence group)
4833              (gnus-get-newsgroup-headers))))))))
4834
4835 (defun gnus-article-get-xrefs ()
4836   "Fill in the Xref value in `gnus-current-headers', if necessary.
4837 This is meant to be called in `gnus-article-internal-prepare-hook'."
4838   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4839                                  gnus-current-headers)))
4840     (or (not gnus-use-cross-reference)
4841         (not headers)
4842         (and (mail-header-xref headers)
4843              (not (string= (mail-header-xref headers) "")))
4844         (let ((case-fold-search t)
4845               xref)
4846           (save-restriction
4847             (nnheader-narrow-to-headers)
4848             (goto-char (point-min))
4849             (when (or (and (not (eobp))
4850                            (eq (downcase (char-after)) ?x)
4851                            (looking-at "Xref:"))
4852                       (search-forward "\nXref:" nil t))
4853               (goto-char (1+ (match-end 0)))
4854               (setq xref (buffer-substring (point)
4855                                            (progn (end-of-line) (point))))
4856               (mail-header-set-xref headers xref)))))))
4857
4858 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4859   "Find article ID and insert the summary line for that article.
4860 OLD-HEADER can either be a header or a line number to insert
4861 the subject line on."
4862   (let* ((line (and (numberp old-header) old-header))
4863          (old-header (and (vectorp old-header) old-header))
4864          (header (cond ((and old-header use-old-header)
4865                         old-header)
4866                        ((and (numberp id)
4867                              (gnus-number-to-header id))
4868                         (gnus-number-to-header id))
4869                        (t
4870                         (gnus-read-header id))))
4871          (number (and (numberp id) id))
4872          d)
4873     (when header
4874       ;; Rebuild the thread that this article is part of and go to the
4875       ;; article we have fetched.
4876       (when (and (not gnus-show-threads)
4877                  old-header)
4878         (when (and number
4879                    (setq d (gnus-data-find (mail-header-number old-header))))
4880           (goto-char (gnus-data-pos d))
4881           (gnus-data-remove
4882            number
4883            (- (gnus-point-at-bol)
4884               (prog1
4885                   (1+ (gnus-point-at-eol))
4886                 (gnus-delete-line))))))
4887       (when old-header
4888         (mail-header-set-number header (mail-header-number old-header)))
4889       (setq gnus-newsgroup-sparse
4890             (delq (setq number (mail-header-number header))
4891                   gnus-newsgroup-sparse))
4892       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4893       (push number gnus-newsgroup-limit)
4894       (gnus-rebuild-thread (mail-header-id header) line)
4895       (gnus-summary-goto-subject number nil t))
4896     (when (and (numberp number)
4897                (> number 0))
4898       ;; We have to update the boundaries even if we can't fetch the
4899       ;; article if ID is a number -- so that the next `P' or `N'
4900       ;; command will fetch the previous (or next) article even
4901       ;; if the one we tried to fetch this time has been canceled.
4902       (when (> number gnus-newsgroup-end)
4903         (setq gnus-newsgroup-end number))
4904       (when (< number gnus-newsgroup-begin)
4905         (setq gnus-newsgroup-begin number))
4906       (setq gnus-newsgroup-unselected
4907             (delq number gnus-newsgroup-unselected)))
4908     ;; Report back a success?
4909     (and header (mail-header-number header))))
4910
4911 ;;; Process/prefix in the summary buffer
4912
4913 (defun gnus-summary-work-articles (n)
4914   "Return a list of articles to be worked upon.
4915 The prefix argument, the list of process marked articles, and the
4916 current article will be taken into consideration."
4917   (save-excursion
4918     (set-buffer gnus-summary-buffer)
4919     (cond
4920      (n
4921       ;; A numerical prefix has been given.
4922       (setq n (prefix-numeric-value n))
4923       (let ((backward (< n 0))
4924             (n (abs (prefix-numeric-value n)))
4925             articles article)
4926         (save-excursion
4927           (while
4928               (and (> n 0)
4929                    (push (setq article (gnus-summary-article-number))
4930                          articles)
4931                    (if backward
4932                        (gnus-summary-find-prev nil article)
4933                      (gnus-summary-find-next nil article)))
4934             (decf n)))
4935         (nreverse articles)))
4936      ((and (gnus-region-active-p) (mark))
4937       (message "region active")
4938       ;; Work on the region between point and mark.
4939       (let ((max (max (point) (mark)))
4940             articles article)
4941         (save-excursion
4942           (goto-char (min (min (point) (mark))))
4943           (while
4944               (and
4945                (push (setq article (gnus-summary-article-number)) articles)
4946                (gnus-summary-find-next nil article)
4947                (< (point) max)))
4948           (nreverse articles))))
4949      (gnus-newsgroup-processable
4950       ;; There are process-marked articles present.
4951       ;; Save current state.
4952       (gnus-summary-save-process-mark)
4953       ;; Return the list.
4954       (reverse gnus-newsgroup-processable))
4955      (t
4956       ;; Just return the current article.
4957       (list (gnus-summary-article-number))))))
4958
4959 (defmacro gnus-summary-iterate (arg &rest forms)
4960   "Iterate over the process/prefixed articles and do FORMS.
4961 ARG is the interactive prefix given to the command.  FORMS will be
4962 executed with point over the summary line of the articles."
4963   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4964     `(let ((,articles (gnus-summary-work-articles ,arg)))
4965        (while ,articles
4966          (gnus-summary-goto-subject (car ,articles))
4967          ,@forms
4968          (pop ,articles)))))
4969
4970 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4971 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4972
4973 (defun gnus-summary-save-process-mark ()
4974   "Push the current set of process marked articles on the stack."
4975   (interactive)
4976   (push (copy-sequence gnus-newsgroup-processable)
4977         gnus-newsgroup-process-stack))
4978
4979 (defun gnus-summary-kill-process-mark ()
4980   "Push the current set of process marked articles on the stack and unmark."
4981   (interactive)
4982   (gnus-summary-save-process-mark)
4983   (gnus-summary-unmark-all-processable))
4984
4985 (defun gnus-summary-yank-process-mark ()
4986   "Pop the last process mark state off the stack and restore it."
4987   (interactive)
4988   (unless gnus-newsgroup-process-stack
4989     (error "Empty mark stack"))
4990   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4991
4992 (defun gnus-summary-process-mark-set (set)
4993   "Make SET into the current process marked articles."
4994   (gnus-summary-unmark-all-processable)
4995   (while set
4996     (gnus-summary-set-process-mark (pop set))))
4997
4998 ;;; Searching and stuff
4999
5000 (defun gnus-summary-search-group (&optional backward use-level)
5001   "Search for next unread newsgroup.
5002 If optional argument BACKWARD is non-nil, search backward instead."
5003   (save-excursion
5004     (set-buffer gnus-group-buffer)
5005     (when (gnus-group-search-forward
5006            backward nil (if use-level (gnus-group-group-level) nil))
5007       (gnus-group-group-name))))
5008
5009 (defun gnus-summary-best-group (&optional exclude-group)
5010   "Find the name of the best unread group.
5011 If EXCLUDE-GROUP, do not go to this group."
5012   (save-excursion
5013     (set-buffer gnus-group-buffer)
5014     (save-excursion
5015       (gnus-group-best-unread-group exclude-group))))
5016
5017 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5018   (if backward (gnus-summary-find-prev)
5019     (let* ((dummy (gnus-summary-article-intangible-p))
5020            (article (or article (gnus-summary-article-number)))
5021            (arts (gnus-data-find-list article))
5022            result)
5023       (when (and (not dummy)
5024                  (or (not gnus-summary-check-current)
5025                      (not unread)
5026                      (not (gnus-data-unread-p (car arts)))))
5027         (setq arts (cdr arts)))
5028       (when (setq result
5029                   (if unread
5030                       (progn
5031                         (while arts
5032                           (when (or (and undownloaded
5033                                          (eq gnus-undownloaded-mark
5034                                              (gnus-data-mark (car arts))))
5035                                     (gnus-data-unread-p (car arts)))
5036                             (setq result (car arts)
5037                                   arts nil))
5038                           (setq arts (cdr arts)))
5039                         result)
5040                     (car arts)))
5041         (goto-char (gnus-data-pos result))
5042         (gnus-data-number result)))))
5043
5044 (defun gnus-summary-find-prev (&optional unread article)
5045   (let* ((eobp (eobp))
5046          (article (or article (gnus-summary-article-number)))
5047          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5048          result)
5049     (when (and (not eobp)
5050                (or (not gnus-summary-check-current)
5051                    (not unread)
5052                    (not (gnus-data-unread-p (car arts)))))
5053       (setq arts (cdr arts)))
5054     (when (setq result
5055                 (if unread
5056                     (progn
5057                       (while arts
5058                         (when (gnus-data-unread-p (car arts))
5059                           (setq result (car arts)
5060                                 arts nil))
5061                         (setq arts (cdr arts)))
5062                       result)
5063                   (car arts)))
5064       (goto-char (gnus-data-pos result))
5065       (gnus-data-number result))))
5066
5067 (defun gnus-summary-find-subject (subject &optional unread backward article)
5068   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5069          (article (or article (gnus-summary-article-number)))
5070          (articles (gnus-data-list backward))
5071          (arts (gnus-data-find-list article articles))
5072          result)
5073     (when (or (not gnus-summary-check-current)
5074               (not unread)
5075               (not (gnus-data-unread-p (car arts))))
5076       (setq arts (cdr arts)))
5077     (while arts
5078       (and (or (not unread)
5079                (gnus-data-unread-p (car arts)))
5080            (vectorp (gnus-data-header (car arts)))
5081            (gnus-subject-equal
5082             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5083            (setq result (car arts)
5084                  arts nil))
5085       (setq arts (cdr arts)))
5086     (and result
5087          (goto-char (gnus-data-pos result))
5088          (gnus-data-number result))))
5089
5090 (defun gnus-summary-search-forward (&optional unread subject backward)
5091   "Search forward for an article.
5092 If UNREAD, look for unread articles.  If SUBJECT, look for
5093 articles with that subject.  If BACKWARD, search backward instead."
5094   (cond (subject (gnus-summary-find-subject subject unread backward))
5095         (backward (gnus-summary-find-prev unread))
5096         (t (gnus-summary-find-next unread))))
5097
5098 (defun gnus-recenter (&optional n)
5099   "Center point in window and redisplay frame.
5100 Also do horizontal recentering."
5101   (interactive "P")
5102   (when (and gnus-auto-center-summary
5103              (not (eq gnus-auto-center-summary 'vertical)))
5104     (gnus-horizontal-recenter))
5105   (recenter n))
5106
5107 (defun gnus-summary-recenter ()
5108   "Center point in the summary window.
5109 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5110 displayed, no centering will be performed."
5111   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5112   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5113   (let* ((top (cond ((< (window-height) 4) 0)
5114                     ((< (window-height) 7) 1)
5115                     (t (if (numberp gnus-auto-center-summary)
5116                            gnus-auto-center-summary
5117                          2))))
5118          (height (1- (window-height)))
5119          (bottom (save-excursion (goto-char (point-max))
5120                                  (forward-line (- height))
5121                                  (point)))
5122          (window (get-buffer-window (current-buffer))))
5123     ;; The user has to want it.
5124     (when gnus-auto-center-summary
5125       (when (get-buffer-window gnus-article-buffer)
5126         ;; Only do recentering when the article buffer is displayed,
5127         ;; Set the window start to either `bottom', which is the biggest
5128         ;; possible valid number, or the second line from the top,
5129         ;; whichever is the least.
5130         (set-window-start
5131          window (min bottom (save-excursion
5132                               (forward-line (- top)) (point)))
5133          t))
5134       ;; Do horizontal recentering while we're at it.
5135       (when (and (get-buffer-window (current-buffer) t)
5136                  (not (eq gnus-auto-center-summary 'vertical)))
5137         (let ((selected (selected-window)))
5138           (select-window (get-buffer-window (current-buffer) t))
5139           (gnus-summary-position-point)
5140           (gnus-horizontal-recenter)
5141           (select-window selected))))))
5142
5143 (defun gnus-summary-jump-to-group (newsgroup)
5144   "Move point to NEWSGROUP in group mode buffer."
5145   ;; Keep update point of group mode buffer if visible.
5146   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5147       (save-window-excursion
5148         ;; Take care of tree window mode.
5149         (when (get-buffer-window gnus-group-buffer)
5150           (pop-to-buffer gnus-group-buffer))
5151         (gnus-group-jump-to-group newsgroup))
5152     (save-excursion
5153       ;; Take care of tree window mode.
5154       (if (get-buffer-window gnus-group-buffer)
5155           (pop-to-buffer gnus-group-buffer)
5156         (set-buffer gnus-group-buffer))
5157       (gnus-group-jump-to-group newsgroup))))
5158
5159 ;; This function returns a list of article numbers based on the
5160 ;; difference between the ranges of read articles in this group and
5161 ;; the range of active articles.
5162 (defun gnus-list-of-unread-articles (group)
5163   (let* ((read (gnus-info-read (gnus-get-info group)))
5164          (active (or (gnus-active group) (gnus-activate-group group)))
5165          (last (cdr active))
5166          first nlast unread)
5167     ;; If none are read, then all are unread.
5168     (if (not read)
5169         (setq first (car active))
5170       ;; If the range of read articles is a single range, then the
5171       ;; first unread article is the article after the last read
5172       ;; article.  Sounds logical, doesn't it?
5173       (if (and (not (listp (cdr read)))
5174                (or (< (car read) (car active))
5175                    (progn (setq read (list read))
5176                           nil)))
5177           (setq first (max (car active) (1+ (cdr read))))
5178         ;; `read' is a list of ranges.
5179         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5180                                   (caar read)))
5181                   1)
5182           (setq first (car active)))
5183         (while read
5184           (when first
5185             (while (< first nlast)
5186               (push first unread)
5187               (setq first (1+ first))))
5188           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5189           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5190           (setq read (cdr read)))))
5191     ;; And add the last unread articles.
5192     (while (<= first last)
5193       (push first unread)
5194       (setq first (1+ first)))
5195     ;; Return the list of unread articles.
5196     (delq 0 (nreverse unread))))
5197
5198 (defun gnus-list-of-read-articles (group)
5199   "Return a list of unread, unticked and non-dormant articles."
5200   (let* ((info (gnus-get-info group))
5201          (marked (gnus-info-marks info))
5202          (active (gnus-active group)))
5203     (and info active
5204          (gnus-set-difference
5205           (gnus-sorted-complement
5206            (gnus-uncompress-range active)
5207            (gnus-list-of-unread-articles group))
5208           (append
5209            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5210            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5211
5212 ;; Various summary commands
5213
5214 (defun gnus-summary-select-article-buffer ()
5215   "Reconfigure windows to show article buffer."
5216   (interactive)
5217   (if (not (gnus-buffer-live-p gnus-article-buffer))
5218       (error "There is no article buffer for this summary buffer")
5219     (gnus-configure-windows 'article)
5220     (select-window (get-buffer-window gnus-article-buffer))))
5221
5222 (defun gnus-summary-universal-argument (arg)
5223   "Perform any operation on all articles that are process/prefixed."
5224   (interactive "P")
5225   (let ((articles (gnus-summary-work-articles arg))
5226         func article)
5227     (if (eq
5228          (setq
5229           func
5230           (key-binding
5231            (read-key-sequence
5232             (substitute-command-keys
5233              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5234          'undefined)
5235         (gnus-error 1 "Undefined key")
5236       (save-excursion
5237         (while articles
5238           (gnus-summary-goto-subject (setq article (pop articles)))
5239           (let (gnus-newsgroup-processable)
5240             (command-execute func))
5241           (gnus-summary-remove-process-mark article)))))
5242   (gnus-summary-position-point))
5243
5244 (defun gnus-summary-toggle-truncation (&optional arg)
5245   "Toggle truncation of summary lines.
5246 With arg, turn line truncation on iff arg is positive."
5247   (interactive "P")
5248   (setq truncate-lines
5249         (if (null arg) (not truncate-lines)
5250           (> (prefix-numeric-value arg) 0)))
5251   (redraw-display))
5252
5253 (defun gnus-summary-reselect-current-group (&optional all rescan)
5254   "Exit and then reselect the current newsgroup.
5255 The prefix argument ALL means to select all articles."
5256   (interactive "P")
5257   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5258     (error "Ephemeral groups can't be reselected"))
5259   (let ((current-subject (gnus-summary-article-number))
5260         (group gnus-newsgroup-name))
5261     (setq gnus-newsgroup-begin nil)
5262     (gnus-summary-exit)
5263     ;; We have to adjust the point of group mode buffer because
5264     ;; point was moved to the next unread newsgroup by exiting.
5265     (gnus-summary-jump-to-group group)
5266     (when rescan
5267       (save-excursion
5268         (gnus-group-get-new-news-this-group 1)))
5269     (gnus-group-read-group all t)
5270     (gnus-summary-goto-subject current-subject nil t)))
5271
5272 (defun gnus-summary-rescan-group (&optional all)
5273   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5274   (interactive "P")
5275   (gnus-summary-reselect-current-group all t))
5276
5277 (defun gnus-summary-update-info (&optional non-destructive)
5278   (save-excursion
5279     (let ((group gnus-newsgroup-name))
5280       (when group
5281         (when gnus-newsgroup-kill-headers
5282           (setq gnus-newsgroup-killed
5283                 (gnus-compress-sequence
5284                  (nconc
5285                   (gnus-set-sorted-intersection
5286                    (gnus-uncompress-range gnus-newsgroup-killed)
5287                    (setq gnus-newsgroup-unselected
5288                          (sort gnus-newsgroup-unselected '<)))
5289                   (setq gnus-newsgroup-unreads
5290                         (sort gnus-newsgroup-unreads '<)))
5291                  t)))
5292         (unless (listp (cdr gnus-newsgroup-killed))
5293           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5294         (let ((headers gnus-newsgroup-headers))
5295           ;; Set the new ranges of read articles.
5296           (save-excursion
5297             (set-buffer gnus-group-buffer)
5298             (gnus-undo-force-boundary))
5299           (gnus-update-read-articles
5300            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5301           ;; Set the current article marks.
5302           (let ((gnus-newsgroup-scored
5303                  (if (and (not gnus-save-score)
5304                           (not non-destructive))
5305                      nil
5306                    gnus-newsgroup-scored)))
5307             (save-excursion
5308               (gnus-update-marks)))
5309           ;; Do the cross-ref thing.
5310           (when gnus-use-cross-reference
5311             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5312           ;; Do not switch windows but change the buffer to work.
5313           (set-buffer gnus-group-buffer)
5314           (unless (gnus-ephemeral-group-p group)
5315             (gnus-group-update-group group)))))))
5316
5317 (defun gnus-summary-save-newsrc (&optional force)
5318   "Save the current number of read/marked articles in the dribble buffer.
5319 The dribble buffer will then be saved.
5320 If FORCE (the prefix), also save the .newsrc file(s)."
5321   (interactive "P")
5322   (gnus-summary-update-info t)
5323   (if force
5324       (gnus-save-newsrc-file)
5325     (gnus-dribble-save)))
5326
5327 (defun gnus-summary-exit (&optional temporary)
5328   "Exit reading current newsgroup, and then return to group selection mode.
5329 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5330   (interactive)
5331   (gnus-set-global-variables)
5332   (when (gnus-buffer-live-p gnus-article-buffer)
5333     (save-excursion
5334       (set-buffer gnus-article-buffer)
5335       (mm-destroy-parts gnus-article-mime-handles)))
5336   (gnus-kill-save-kill-buffer)
5337   (gnus-async-halt-prefetch)
5338   (let* ((group gnus-newsgroup-name)
5339          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5340          (mode major-mode)
5341          (group-point nil)
5342          (buf (current-buffer)))
5343     (unless quit-config
5344       ;; Do adaptive scoring, and possibly save score files.
5345       (when gnus-newsgroup-adaptive
5346         (gnus-score-adaptive))
5347       (when gnus-use-scoring
5348         (gnus-score-save)))
5349     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5350     ;; If we have several article buffers, we kill them at exit.
5351     (unless gnus-single-article-buffer
5352       (gnus-kill-buffer gnus-original-article-buffer)
5353       (setq gnus-article-current nil))
5354     (when gnus-use-cache
5355       (gnus-cache-possibly-remove-articles)
5356       (gnus-cache-save-buffers))
5357     (gnus-async-prefetch-remove-group group)
5358     (when gnus-suppress-duplicates
5359       (gnus-dup-enter-articles))
5360     (when gnus-use-trees
5361       (gnus-tree-close group))
5362     (when gnus-use-cache
5363       (gnus-cache-write-active))
5364     ;; Remove entries for this group.
5365     (nnmail-purge-split-history (gnus-group-real-name group))
5366     ;; Make all changes in this group permanent.
5367     (unless quit-config
5368       (gnus-run-hooks 'gnus-exit-group-hook)
5369       (gnus-summary-update-info))
5370     (gnus-close-group group)
5371     ;; Make sure where we were, and go to next newsgroup.
5372     (set-buffer gnus-group-buffer)
5373     (unless quit-config
5374       (gnus-group-jump-to-group group))
5375     (gnus-run-hooks 'gnus-summary-exit-hook)
5376     (unless (or quit-config
5377                 ;; If this group has disappeared from the summary
5378                 ;; buffer, don't skip forwards.
5379                 (not (string= group (gnus-group-group-name))))
5380       (gnus-group-next-unread-group 1))
5381     (setq group-point (point))
5382     (if temporary
5383         nil                             ;Nothing to do.
5384       ;; If we have several article buffers, we kill them at exit.
5385       (unless gnus-single-article-buffer
5386         (gnus-kill-buffer gnus-article-buffer)
5387         (gnus-kill-buffer gnus-original-article-buffer)
5388         (setq gnus-article-current nil))
5389       (set-buffer buf)
5390       (if (not gnus-kill-summary-on-exit)
5391           (gnus-deaden-summary)
5392         ;; We set all buffer-local variables to nil.  It is unclear why
5393         ;; this is needed, but if we don't, buffer-local variables are
5394         ;; not garbage-collected, it seems.  This would the lead to en
5395         ;; ever-growing Emacs.
5396         (gnus-summary-clear-local-variables)
5397         (when (get-buffer gnus-article-buffer)
5398           (bury-buffer gnus-article-buffer))
5399         ;; We clear the global counterparts of the buffer-local
5400         ;; variables as well, just to be on the safe side.
5401         (set-buffer gnus-group-buffer)
5402         (gnus-summary-clear-local-variables)
5403         ;; Return to group mode buffer.
5404         (when (eq mode 'gnus-summary-mode)
5405           (gnus-kill-buffer buf)))
5406       (setq gnus-current-select-method gnus-select-method)
5407       (pop-to-buffer gnus-group-buffer)
5408       (if (not quit-config)
5409           (progn
5410             (goto-char group-point)
5411             (gnus-configure-windows 'group 'force))
5412         (gnus-handle-ephemeral-exit quit-config))
5413       ;; Clear the current group name.
5414       (unless quit-config
5415         (setq gnus-newsgroup-name nil)))))
5416
5417 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5418 (defun gnus-summary-exit-no-update (&optional no-questions)
5419   "Quit reading current newsgroup without updating read article info."
5420   (interactive)
5421   (let* ((group gnus-newsgroup-name)
5422          (quit-config (gnus-group-quit-config group)))
5423     (when (or no-questions
5424               gnus-expert-user
5425               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5426       (gnus-async-halt-prefetch)
5427       (mapcar 'funcall
5428               (delq 'gnus-summary-expire-articles
5429                     (copy-sequence gnus-summary-prepare-exit-hook)))
5430       (when (gnus-buffer-live-p gnus-article-buffer)
5431         (save-excursion
5432           (set-buffer gnus-article-buffer)
5433           (mm-destroy-parts gnus-article-mime-handles)))
5434       ;; If we have several article buffers, we kill them at exit.
5435       (unless gnus-single-article-buffer
5436         (gnus-kill-buffer gnus-article-buffer)
5437         (gnus-kill-buffer gnus-original-article-buffer)
5438         (setq gnus-article-current nil))
5439       (if (not gnus-kill-summary-on-exit)
5440           (gnus-deaden-summary)
5441         (gnus-close-group group)
5442         (gnus-summary-clear-local-variables)
5443         (set-buffer gnus-group-buffer)
5444         (gnus-summary-clear-local-variables)
5445         (when (get-buffer gnus-summary-buffer)
5446           (kill-buffer gnus-summary-buffer)))
5447       (unless gnus-single-article-buffer
5448         (setq gnus-article-current nil))
5449       (when gnus-use-trees
5450         (gnus-tree-close group))
5451       (gnus-async-prefetch-remove-group group)
5452       (when (get-buffer gnus-article-buffer)
5453         (bury-buffer gnus-article-buffer))
5454       ;; Return to the group buffer.
5455       (gnus-configure-windows 'group 'force)
5456       ;; Clear the current group name.
5457       (setq gnus-newsgroup-name nil)
5458       (when (equal (gnus-group-group-name) group)
5459         (gnus-group-next-unread-group 1))
5460       (when quit-config
5461         (gnus-handle-ephemeral-exit quit-config)))))
5462
5463 (defun gnus-handle-ephemeral-exit (quit-config)
5464   "Handle movement when leaving an ephemeral group.
5465 The state which existed when entering the ephemeral is reset."
5466   (if (not (buffer-name (car quit-config)))
5467       (gnus-configure-windows 'group 'force)
5468     (set-buffer (car quit-config))
5469     (cond ((eq major-mode 'gnus-summary-mode)
5470            (gnus-set-global-variables))
5471           ((eq major-mode 'gnus-article-mode)
5472            (save-excursion
5473              ;; The `gnus-summary-buffer' variable may point
5474              ;; to the old summary buffer when using a single
5475              ;; article buffer.
5476              (unless (gnus-buffer-live-p gnus-summary-buffer)
5477                (set-buffer gnus-group-buffer))
5478              (set-buffer gnus-summary-buffer)
5479              (gnus-set-global-variables))))
5480     (if (or (eq (cdr quit-config) 'article)
5481             (eq (cdr quit-config) 'pick))
5482         (progn
5483           ;; The current article may be from the ephemeral group
5484           ;; thus it is best that we reload this article
5485           (gnus-summary-show-article)
5486           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5487               (gnus-configure-windows 'pick 'force)
5488             (gnus-configure-windows (cdr quit-config) 'force)))
5489       (gnus-configure-windows (cdr quit-config) 'force))
5490     (when (eq major-mode 'gnus-summary-mode)
5491       (gnus-summary-next-subject 1 nil t)
5492       (gnus-summary-recenter)
5493       (gnus-summary-position-point))))
5494
5495 ;;; Dead summaries.
5496
5497 (defvar gnus-dead-summary-mode-map nil)
5498
5499 (unless gnus-dead-summary-mode-map
5500   (setq gnus-dead-summary-mode-map (make-keymap))
5501   (suppress-keymap gnus-dead-summary-mode-map)
5502   (substitute-key-definition
5503    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5504   (let ((keys '("\C-d" "\r" "\177" [delete])))
5505     (while keys
5506       (define-key gnus-dead-summary-mode-map
5507         (pop keys) 'gnus-summary-wake-up-the-dead))))
5508
5509 (defvar gnus-dead-summary-mode nil
5510   "Minor mode for Gnus summary buffers.")
5511
5512 (defun gnus-dead-summary-mode (&optional arg)
5513   "Minor mode for Gnus summary buffers."
5514   (interactive "P")
5515   (when (eq major-mode 'gnus-summary-mode)
5516     (make-local-variable 'gnus-dead-summary-mode)
5517     (setq gnus-dead-summary-mode
5518           (if (null arg) (not gnus-dead-summary-mode)
5519             (> (prefix-numeric-value arg) 0)))
5520     (when gnus-dead-summary-mode
5521       (gnus-add-minor-mode
5522        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5523
5524 (defun gnus-deaden-summary ()
5525   "Make the current summary buffer into a dead summary buffer."
5526   ;; Kill any previous dead summary buffer.
5527   (when (and gnus-dead-summary
5528              (buffer-name gnus-dead-summary))
5529     (save-excursion
5530       (set-buffer gnus-dead-summary)
5531       (when gnus-dead-summary-mode
5532         (kill-buffer (current-buffer)))))
5533   ;; Make this the current dead summary.
5534   (setq gnus-dead-summary (current-buffer))
5535   (gnus-dead-summary-mode 1)
5536   (let ((name (buffer-name)))
5537     (when (string-match "Summary" name)
5538       (rename-buffer
5539        (concat (substring name 0 (match-beginning 0)) "Dead "
5540                (substring name (match-beginning 0)))
5541        t))))
5542
5543 (defun gnus-kill-or-deaden-summary (buffer)
5544   "Kill or deaden the summary BUFFER."
5545   (save-excursion
5546     (when (and (buffer-name buffer)
5547                (not gnus-single-article-buffer))
5548       (save-excursion
5549         (set-buffer buffer)
5550         (gnus-kill-buffer gnus-article-buffer)
5551         (gnus-kill-buffer gnus-original-article-buffer)))
5552     (cond (gnus-kill-summary-on-exit
5553            (when (and gnus-use-trees
5554                       (gnus-buffer-exists-p buffer))
5555              (save-excursion
5556                (set-buffer buffer)
5557                (gnus-tree-close gnus-newsgroup-name)))
5558            (gnus-kill-buffer buffer))
5559           ((gnus-buffer-exists-p buffer)
5560            (save-excursion
5561              (set-buffer buffer)
5562              (gnus-deaden-summary))))))
5563
5564 (defun gnus-summary-wake-up-the-dead (&rest args)
5565   "Wake up the dead summary buffer."
5566   (interactive)
5567   (gnus-dead-summary-mode -1)
5568   (let ((name (buffer-name)))
5569     (when (string-match "Dead " name)
5570       (rename-buffer
5571        (concat (substring name 0 (match-beginning 0))
5572                (substring name (match-end 0)))
5573        t)))
5574   (gnus-message 3 "This dead summary is now alive again"))
5575
5576 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5577 (defun gnus-summary-fetch-faq (&optional faq-dir)
5578   "Fetch the FAQ for the current group.
5579 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5580 in."
5581   (interactive
5582    (list
5583     (when current-prefix-arg
5584       (completing-read
5585        "Faq dir: " (and (listp gnus-group-faq-directory)
5586                         (mapcar (lambda (file) (list file))
5587                                 gnus-group-faq-directory))))))
5588   (let (gnus-faq-buffer)
5589     (when (setq gnus-faq-buffer
5590                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5591       (gnus-configure-windows 'summary-faq))))
5592
5593 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5594 (defun gnus-summary-describe-group (&optional force)
5595   "Describe the current newsgroup."
5596   (interactive "P")
5597   (gnus-group-describe-group force gnus-newsgroup-name))
5598
5599 (defun gnus-summary-describe-briefly ()
5600   "Describe summary mode commands briefly."
5601   (interactive)
5602   (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")))
5603
5604 ;; Walking around group mode buffer from summary mode.
5605
5606 (defun gnus-summary-next-group (&optional no-article target-group backward)
5607   "Exit current newsgroup and then select next unread newsgroup.
5608 If prefix argument NO-ARTICLE is non-nil, no article is selected
5609 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5610 previous group instead."
5611   (interactive "P")
5612   ;; Stop pre-fetching.
5613   (gnus-async-halt-prefetch)
5614   (let ((current-group gnus-newsgroup-name)
5615         (current-buffer (current-buffer))
5616         entered)
5617     ;; First we semi-exit this group to update Xrefs and all variables.
5618     ;; We can't do a real exit, because the window conf must remain
5619     ;; the same in case the user is prompted for info, and we don't
5620     ;; want the window conf to change before that...
5621     (gnus-summary-exit t)
5622     (while (not entered)
5623       ;; Then we find what group we are supposed to enter.
5624       (set-buffer gnus-group-buffer)
5625       (gnus-group-jump-to-group current-group)
5626       (setq target-group
5627             (or target-group
5628                 (if (eq gnus-keep-same-level 'best)
5629                     (gnus-summary-best-group gnus-newsgroup-name)
5630                   (gnus-summary-search-group backward gnus-keep-same-level))))
5631       (if (not target-group)
5632           ;; There are no further groups, so we return to the group
5633           ;; buffer.
5634           (progn
5635             (gnus-message 5 "Returning to the group buffer")
5636             (setq entered t)
5637             (when (gnus-buffer-live-p current-buffer)
5638               (set-buffer current-buffer)
5639               (gnus-summary-exit))
5640             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5641         ;; We try to enter the target group.
5642         (gnus-group-jump-to-group target-group)
5643         (let ((unreads (gnus-group-group-unread)))
5644           (if (and (or (eq t unreads)
5645                        (and unreads (not (zerop unreads))))
5646                    (gnus-summary-read-group
5647                     target-group nil no-article
5648                     (and (buffer-name current-buffer) current-buffer)
5649                     nil backward))
5650               (setq entered t)
5651             (setq current-group target-group
5652                   target-group nil)))))))
5653
5654 (defun gnus-summary-prev-group (&optional no-article)
5655   "Exit current newsgroup and then select previous unread newsgroup.
5656 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5657   (interactive "P")
5658   (gnus-summary-next-group no-article nil t))
5659
5660 ;; Walking around summary lines.
5661
5662 (defun gnus-summary-first-subject (&optional unread undownloaded)
5663   "Go to the first unread subject.
5664 If UNREAD is non-nil, go to the first unread article.
5665 Returns the article selected or nil if there are no unread articles."
5666   (interactive "P")
5667   (prog1
5668       (cond
5669        ;; Empty summary.
5670        ((null gnus-newsgroup-data)
5671         (gnus-message 3 "No articles in the group")
5672         nil)
5673        ;; Pick the first article.
5674        ((not unread)
5675         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5676         (gnus-data-number (car gnus-newsgroup-data)))
5677        ;; No unread articles.
5678        ((null gnus-newsgroup-unreads)
5679         (gnus-message 3 "No more unread articles")
5680         nil)
5681        ;; Find the first unread article.
5682        (t
5683         (let ((data gnus-newsgroup-data))
5684           (while (and data
5685                       (and (not (and undownloaded
5686                                      (eq gnus-undownloaded-mark
5687                                          (gnus-data-mark (car data)))))
5688                            (not (gnus-data-unread-p (car data)))))
5689             (setq data (cdr data)))
5690           (when data
5691             (goto-char (gnus-data-pos (car data)))
5692             (gnus-data-number (car data))))))
5693     (gnus-summary-position-point)))
5694
5695 (defun gnus-summary-next-subject (n &optional unread dont-display)
5696   "Go to next N'th summary line.
5697 If N is negative, go to the previous N'th subject line.
5698 If UNREAD is non-nil, only unread articles are selected.
5699 The difference between N and the actual number of steps taken is
5700 returned."
5701   (interactive "p")
5702   (let ((backward (< n 0))
5703         (n (abs n)))
5704     (while (and (> n 0)
5705                 (if backward
5706                     (gnus-summary-find-prev unread)
5707                   (gnus-summary-find-next unread)))
5708       (unless (zerop (setq n (1- n)))
5709         (gnus-summary-show-thread)))
5710     (when (/= 0 n)
5711       (gnus-message 7 "No more%s articles"
5712                     (if unread " unread" "")))
5713     (unless dont-display
5714       (gnus-summary-recenter)
5715       (gnus-summary-position-point))
5716     n))
5717
5718 (defun gnus-summary-next-unread-subject (n)
5719   "Go to next N'th unread summary line."
5720   (interactive "p")
5721   (gnus-summary-next-subject n t))
5722
5723 (defun gnus-summary-prev-subject (n &optional unread)
5724   "Go to previous N'th summary line.
5725 If optional argument UNREAD is non-nil, only unread article is selected."
5726   (interactive "p")
5727   (gnus-summary-next-subject (- n) unread))
5728
5729 (defun gnus-summary-prev-unread-subject (n)
5730   "Go to previous N'th unread summary line."
5731   (interactive "p")
5732   (gnus-summary-next-subject (- n) t))
5733
5734 (defun gnus-summary-goto-subject (article &optional force silent)
5735   "Go the subject line of ARTICLE.
5736 If FORCE, also allow jumping to articles not currently shown."
5737   (interactive "nArticle number: ")
5738   (let ((b (point))
5739         (data (gnus-data-find article)))
5740     ;; We read in the article if we have to.
5741     (and (not data)
5742          force
5743          (gnus-summary-insert-subject
5744           article
5745           (if (or (numberp force) (vectorp force)) force)
5746           t)
5747          (setq data (gnus-data-find article)))
5748     (goto-char b)
5749     (if (not data)
5750         (progn
5751           (unless silent
5752             (gnus-message 3 "Can't find article %d" article))
5753           nil)
5754       (goto-char (gnus-data-pos data))
5755       (gnus-summary-position-point)
5756       article)))
5757
5758 ;; Walking around summary lines with displaying articles.
5759
5760 (defun gnus-summary-expand-window (&optional arg)
5761   "Make the summary buffer take up the entire Emacs frame.
5762 Given a prefix, will force an `article' buffer configuration."
5763   (interactive "P")
5764   (if arg
5765       (gnus-configure-windows 'article 'force)
5766     (gnus-configure-windows 'summary 'force)))
5767
5768 (defun gnus-summary-display-article (article &optional all-header)
5769   "Display ARTICLE in article buffer."
5770   (gnus-set-global-variables)
5771   (if (null article)
5772       nil
5773     (prog1
5774         (if gnus-summary-display-article-function
5775             (funcall gnus-summary-display-article-function article all-header)
5776           (gnus-article-prepare article all-header))
5777       (gnus-run-hooks 'gnus-select-article-hook)
5778       (when (and gnus-current-article
5779                  (not (zerop gnus-current-article)))
5780         (gnus-summary-goto-subject gnus-current-article))
5781       (gnus-summary-recenter)
5782       (when (and gnus-use-trees gnus-show-threads)
5783         (gnus-possibly-generate-tree article)
5784         (gnus-highlight-selected-tree article))
5785       ;; Successfully display article.
5786       (gnus-article-set-window-start
5787        (cdr (assq article gnus-newsgroup-bookmarks))))))
5788
5789 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5790   "Select the current article.
5791 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5792 non-nil, the article will be re-fetched even if it already present in
5793 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5794 be displayed."
5795   ;; Make sure we are in the summary buffer to work around bbdb bug.
5796   (unless (eq major-mode 'gnus-summary-mode)
5797     (set-buffer gnus-summary-buffer))
5798   (let ((article (or article (gnus-summary-article-number)))
5799         (all-headers (not (not all-headers))) ;Must be T or NIL.
5800         gnus-summary-display-article-function)
5801     (and (not pseudo)
5802          (gnus-summary-article-pseudo-p article)
5803          (error "This is a pseudo-article"))
5804     (save-excursion
5805       (set-buffer gnus-summary-buffer)
5806       (if (or (and gnus-single-article-buffer
5807                    (or (null gnus-current-article)
5808                        (null gnus-article-current)
5809                        (null (get-buffer gnus-article-buffer))
5810                        (not (eq article (cdr gnus-article-current)))
5811                        (not (equal (car gnus-article-current)
5812                                    gnus-newsgroup-name))))
5813               (and (not gnus-single-article-buffer)
5814                    (or (null gnus-current-article)
5815                        (not (eq gnus-current-article article))))
5816               force)
5817           ;; The requested article is different from the current article.
5818           (progn
5819             (gnus-summary-display-article article all-headers)
5820             (when (or all-headers gnus-show-all-headers)
5821               (gnus-article-show-all-headers))
5822             (gnus-article-set-window-start
5823              (cdr (assq article gnus-newsgroup-bookmarks)))
5824             article)
5825         (when (or all-headers gnus-show-all-headers)
5826           (gnus-article-show-all-headers))
5827         'old))))
5828
5829 (defun gnus-summary-set-current-mark (&optional current-mark)
5830   "Obsolete function."
5831   nil)
5832
5833 (defun gnus-summary-next-article (&optional unread subject backward push)
5834   "Select the next article.
5835 If UNREAD, only unread articles are selected.
5836 If SUBJECT, only articles with SUBJECT are selected.
5837 If BACKWARD, the previous article is selected instead of the next."
5838   (interactive "P")
5839   (cond
5840    ;; Is there such an article?
5841    ((and (gnus-summary-search-forward unread subject backward)
5842          (or (gnus-summary-display-article (gnus-summary-article-number))
5843              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5844     (gnus-summary-position-point))
5845    ;; If not, we try the first unread, if that is wanted.
5846    ((and subject
5847          gnus-auto-select-same
5848          (gnus-summary-first-unread-article))
5849     (gnus-summary-position-point)
5850     (gnus-message 6 "Wrapped"))
5851    ;; Try to get next/previous article not displayed in this group.
5852    ((and gnus-auto-extend-newsgroup
5853          (not unread) (not subject))
5854     (gnus-summary-goto-article
5855      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5856      nil (count-lines (point-min) (point))))
5857    ;; Go to next/previous group.
5858    (t
5859     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5860       (gnus-summary-jump-to-group gnus-newsgroup-name))
5861     (let ((cmd last-command-char)
5862           (point
5863            (save-excursion
5864              (set-buffer gnus-group-buffer)
5865              (point)))
5866           (group
5867            (if (eq gnus-keep-same-level 'best)
5868                (gnus-summary-best-group gnus-newsgroup-name)
5869              (gnus-summary-search-group backward gnus-keep-same-level))))
5870       ;; For some reason, the group window gets selected.  We change
5871       ;; it back.
5872       (select-window (get-buffer-window (current-buffer)))
5873       ;; Select next unread newsgroup automagically.
5874       (cond
5875        ((or (not gnus-auto-select-next)
5876             (not cmd))
5877         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5878        ((or (eq gnus-auto-select-next 'quietly)
5879             (and (eq gnus-auto-select-next 'slightly-quietly)
5880                  push)
5881             (and (eq gnus-auto-select-next 'almost-quietly)
5882                  (gnus-summary-last-article-p)))
5883         ;; Select quietly.
5884         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5885             (gnus-summary-exit)
5886           (gnus-message 7 "No more%s articles (%s)..."
5887                         (if unread " unread" "")
5888                         (if group (concat "selecting " group)
5889                           "exiting"))
5890           (gnus-summary-next-group nil group backward)))
5891        (t
5892         (when (gnus-key-press-event-p last-input-event)
5893           (gnus-summary-walk-group-buffer
5894            gnus-newsgroup-name cmd unread backward point))))))))
5895
5896 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5897   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5898                       (?\C-p (gnus-group-prev-unread-group 1))))
5899         (cursor-in-echo-area t)
5900         keve key group ended)
5901     (save-excursion
5902       (set-buffer gnus-group-buffer)
5903       (goto-char start)
5904       (setq group
5905             (if (eq gnus-keep-same-level 'best)
5906                 (gnus-summary-best-group gnus-newsgroup-name)
5907               (gnus-summary-search-group backward gnus-keep-same-level))))
5908     (while (not ended)
5909       (gnus-message
5910        5 "No more%s articles%s" (if unread " unread" "")
5911        (if (and group
5912                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5913            (format " (Type %s for %s [%s])"
5914                    (single-key-description cmd) group
5915                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5916          (format " (Type %s to exit %s)"
5917                  (single-key-description cmd)
5918                  gnus-newsgroup-name)))
5919       ;; Confirm auto selection.
5920       (setq key (car (setq keve (gnus-read-event-char))))
5921       (setq ended t)
5922       (cond
5923        ((assq key keystrokes)
5924         (let ((obuf (current-buffer)))
5925           (switch-to-buffer gnus-group-buffer)
5926           (when group
5927             (gnus-group-jump-to-group group))
5928           (eval (cadr (assq key keystrokes)))
5929           (setq group (gnus-group-group-name))
5930           (switch-to-buffer obuf))
5931         (setq ended nil))
5932        ((equal key cmd)
5933         (if (or (not group)
5934                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5935             (gnus-summary-exit)
5936           (gnus-summary-next-group nil group backward)))
5937        (t
5938         (push (cdr keve) unread-command-events))))))
5939
5940 (defun gnus-summary-next-unread-article ()
5941   "Select unread article after current one."
5942   (interactive)
5943   (gnus-summary-next-article
5944    (or (not (eq gnus-summary-goto-unread 'never))
5945        (gnus-summary-last-article-p (gnus-summary-article-number)))
5946    (and gnus-auto-select-same
5947         (gnus-summary-article-subject))))
5948
5949 (defun gnus-summary-prev-article (&optional unread subject)
5950   "Select the article after the current one.
5951 If UNREAD is non-nil, only unread articles are selected."
5952   (interactive "P")
5953   (gnus-summary-next-article unread subject t))
5954
5955 (defun gnus-summary-prev-unread-article ()
5956   "Select unread article before current one."
5957   (interactive)
5958   (gnus-summary-prev-article
5959    (or (not (eq gnus-summary-goto-unread 'never))
5960        (gnus-summary-first-article-p (gnus-summary-article-number)))
5961    (and gnus-auto-select-same
5962         (gnus-summary-article-subject))))
5963
5964 (defun gnus-summary-next-page (&optional lines circular)
5965   "Show next page of the selected article.
5966 If at the end of the current article, select the next article.
5967 LINES says how many lines should be scrolled up.
5968
5969 If CIRCULAR is non-nil, go to the start of the article instead of
5970 selecting the next article when reaching the end of the current
5971 article."
5972   (interactive "P")
5973   (setq gnus-summary-buffer (current-buffer))
5974   (gnus-set-global-variables)
5975   (let ((article (gnus-summary-article-number))
5976         (article-window (get-buffer-window gnus-article-buffer t))
5977         endp)
5978     ;; If the buffer is empty, we have no article.
5979     (unless article
5980       (error "No article to select"))
5981     (gnus-configure-windows 'article)
5982     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5983         (if (and (eq gnus-summary-goto-unread 'never)
5984                  (not (gnus-summary-last-article-p article)))
5985             (gnus-summary-next-article)
5986           (gnus-summary-next-unread-article))
5987       (if (or (null gnus-current-article)
5988               (null gnus-article-current)
5989               (/= article (cdr gnus-article-current))
5990               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5991           ;; Selected subject is different from current article's.
5992           (gnus-summary-display-article article)
5993         (when article-window
5994           (gnus-eval-in-buffer-window gnus-article-buffer
5995             (setq endp (gnus-article-next-page lines)))
5996           (when endp
5997             (cond (circular
5998                    (gnus-summary-beginning-of-article))
5999                   (lines
6000                    (gnus-message 3 "End of message"))
6001                   ((null lines)
6002                    (if (and (eq gnus-summary-goto-unread 'never)
6003                             (not (gnus-summary-last-article-p article)))
6004                        (gnus-summary-next-article)
6005                      (gnus-summary-next-unread-article))))))))
6006     (gnus-summary-recenter)
6007     (gnus-summary-position-point)))
6008
6009 (defun gnus-summary-prev-page (&optional lines move)
6010   "Show previous page of selected article.
6011 Argument LINES specifies lines to be scrolled down.
6012 If MOVE, move to the previous unread article if point is at
6013 the beginning of the buffer."
6014   (interactive "P")
6015   (let ((article (gnus-summary-article-number))
6016         (article-window (get-buffer-window gnus-article-buffer t))
6017         endp)
6018     (gnus-configure-windows 'article)
6019     (if (or (null gnus-current-article)
6020             (null gnus-article-current)
6021             (/= article (cdr gnus-article-current))
6022             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6023         ;; Selected subject is different from current article's.
6024         (gnus-summary-display-article article)
6025       (gnus-summary-recenter)
6026       (when article-window
6027         (gnus-eval-in-buffer-window gnus-article-buffer
6028           (setq endp (gnus-article-prev-page lines)))
6029         (when (and move endp)
6030           (cond (lines
6031                  (gnus-message 3 "Beginning of message"))
6032                 ((null lines)
6033                  (if (and (eq gnus-summary-goto-unread 'never)
6034                           (not (gnus-summary-first-article-p article)))
6035                      (gnus-summary-prev-article)
6036                    (gnus-summary-prev-unread-article))))))))
6037   (gnus-summary-position-point))
6038
6039 (defun gnus-summary-prev-page-or-article (&optional lines)
6040   "Show previous page of selected article.
6041 Argument LINES specifies lines to be scrolled down.
6042 If at the beginning of the article, go to the next article."
6043   (interactive "P")
6044   (gnus-summary-prev-page lines t))
6045
6046 (defun gnus-summary-scroll-up (lines)
6047   "Scroll up (or down) one line current article.
6048 Argument LINES specifies lines to be scrolled up (or down if negative)."
6049   (interactive "p")
6050   (gnus-configure-windows 'article)
6051   (gnus-summary-show-thread)
6052   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6053     (gnus-eval-in-buffer-window gnus-article-buffer
6054       (cond ((> lines 0)
6055              (when (gnus-article-next-page lines)
6056                (gnus-message 3 "End of message")))
6057             ((< lines 0)
6058              (gnus-article-prev-page (- lines))))))
6059   (gnus-summary-recenter)
6060   (gnus-summary-position-point))
6061
6062 (defun gnus-summary-scroll-down (lines)
6063   "Scroll down (or up) one line current article.
6064 Argument LINES specifies lines to be scrolled down (or up if negative)."
6065   (interactive "p")
6066   (gnus-summary-scroll-up (- lines)))
6067
6068 (defun gnus-summary-next-same-subject ()
6069   "Select next article which has the same subject as current one."
6070   (interactive)
6071   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6072
6073 (defun gnus-summary-prev-same-subject ()
6074   "Select previous article which has the same subject as current one."
6075   (interactive)
6076   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6077
6078 (defun gnus-summary-next-unread-same-subject ()
6079   "Select next unread article which has the same subject as current one."
6080   (interactive)
6081   (gnus-summary-next-article t (gnus-summary-article-subject)))
6082
6083 (defun gnus-summary-prev-unread-same-subject ()
6084   "Select previous unread article which has the same subject as current one."
6085   (interactive)
6086   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6087
6088 (defun gnus-summary-first-unread-article ()
6089   "Select the first unread article.
6090 Return nil if there are no unread articles."
6091   (interactive)
6092   (prog1
6093       (when (gnus-summary-first-subject t)
6094         (gnus-summary-show-thread)
6095         (gnus-summary-first-subject t)
6096         (gnus-summary-display-article (gnus-summary-article-number)))
6097     (gnus-summary-position-point)))
6098
6099 (defun gnus-summary-first-unread-subject ()
6100   "Place the point on the subject line of the first unread article.
6101 Return nil if there are no unread articles."
6102   (interactive)
6103   (prog1
6104       (when (gnus-summary-first-subject t)
6105         (gnus-summary-show-thread)
6106         (gnus-summary-first-subject t))
6107     (gnus-summary-position-point)))
6108
6109 (defun gnus-summary-first-article ()
6110   "Select the first article.
6111 Return nil if there are no articles."
6112   (interactive)
6113   (prog1
6114       (when (gnus-summary-first-subject)
6115         (gnus-summary-show-thread)
6116         (gnus-summary-first-subject)
6117         (gnus-summary-display-article (gnus-summary-article-number)))
6118     (gnus-summary-position-point)))
6119
6120 (defun gnus-summary-best-unread-article ()
6121   "Select the unread article with the highest score."
6122   (interactive)
6123   (let ((best -1000000)
6124         (data gnus-newsgroup-data)
6125         article score)
6126     (while data
6127       (and (gnus-data-unread-p (car data))
6128            (> (setq score
6129                     (gnus-summary-article-score (gnus-data-number (car data))))
6130               best)
6131            (setq best score
6132                  article (gnus-data-number (car data))))
6133       (setq data (cdr data)))
6134     (prog1
6135         (if article
6136             (gnus-summary-goto-article article)
6137           (error "No unread articles"))
6138       (gnus-summary-position-point))))
6139
6140 (defun gnus-summary-last-subject ()
6141   "Go to the last displayed subject line in the group."
6142   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6143     (when article
6144       (gnus-summary-goto-subject article))))
6145
6146 (defun gnus-summary-goto-article (article &optional all-headers force)
6147   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6148 If ALL-HEADERS is non-nil, no header lines are hidden.
6149 If FORCE, go to the article even if it isn't displayed.  If FORCE
6150 is a number, it is the line the article is to be displayed on."
6151   (interactive
6152    (list
6153     (completing-read
6154      "Article number or Message-ID: "
6155      (mapcar (lambda (number) (list (int-to-string number)))
6156              gnus-newsgroup-limit))
6157     current-prefix-arg
6158     t))
6159   (prog1
6160       (if (and (stringp article)
6161                (string-match "@" article))
6162           (gnus-summary-refer-article article)
6163         (when (stringp article)
6164           (setq article (string-to-number article)))
6165         (if (gnus-summary-goto-subject article force)
6166             (gnus-summary-display-article article all-headers)
6167           (gnus-message 4 "Couldn't go to article %s" article) nil))
6168     (gnus-summary-position-point)))
6169
6170 (defun gnus-summary-goto-last-article ()
6171   "Go to the previously read article."
6172   (interactive)
6173   (prog1
6174       (when gnus-last-article
6175         (gnus-summary-goto-article gnus-last-article nil t))
6176     (gnus-summary-position-point)))
6177
6178 (defun gnus-summary-pop-article (number)
6179   "Pop one article off the history and go to the previous.
6180 NUMBER articles will be popped off."
6181   (interactive "p")
6182   (let (to)
6183     (setq gnus-newsgroup-history
6184           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6185     (if to
6186         (gnus-summary-goto-article (car to) nil t)
6187       (error "Article history empty")))
6188   (gnus-summary-position-point))
6189
6190 ;; Summary commands and functions for limiting the summary buffer.
6191
6192 (defun gnus-summary-limit-to-articles (n)
6193   "Limit the summary buffer to the next N articles.
6194 If not given a prefix, use the process marked articles instead."
6195   (interactive "P")
6196   (prog1
6197       (let ((articles (gnus-summary-work-articles n)))
6198         (setq gnus-newsgroup-processable nil)
6199         (gnus-summary-limit articles))
6200     (gnus-summary-position-point)))
6201
6202 (defun gnus-summary-pop-limit (&optional total)
6203   "Restore the previous limit.
6204 If given a prefix, remove all limits."
6205   (interactive "P")
6206   (when total
6207     (setq gnus-newsgroup-limits
6208           (list (mapcar (lambda (h) (mail-header-number h))
6209                         gnus-newsgroup-headers))))
6210   (unless gnus-newsgroup-limits
6211     (error "No limit to pop"))
6212   (prog1
6213       (gnus-summary-limit nil 'pop)
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-limit-to-subject (subject &optional header)
6217   "Limit the summary buffer to articles that have subjects that match a regexp."
6218   (interactive "sLimit to subject (regexp): ")
6219   (unless header
6220     (setq header "subject"))
6221   (when (not (equal "" subject))
6222     (prog1
6223         (let ((articles (gnus-summary-find-matching
6224                          (or header "subject") subject 'all)))
6225           (unless articles
6226             (error "Found no matches for \"%s\"" subject))
6227           (gnus-summary-limit articles))
6228       (gnus-summary-position-point))))
6229
6230 (defun gnus-summary-limit-to-author (from)
6231   "Limit the summary buffer to articles that have authors that match a regexp."
6232   (interactive "sLimit to author (regexp): ")
6233   (gnus-summary-limit-to-subject from "from"))
6234
6235 (defun gnus-summary-limit-to-age (age &optional younger-p)
6236   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6237 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6238 articles that are younger than AGE days."
6239   (interactive
6240    (let ((younger current-prefix-arg)
6241          (days-got nil)
6242          days)
6243      (while (not days-got)
6244        (setq days (if younger
6245                       (read-string "Limit to articles within (in days): ")
6246                     (read-string "Limit to articles old than (in days): ")))
6247        (when (> (length days) 0)
6248          (setq days (read days)))
6249        (if (numberp days)
6250            (setq days-got t)
6251          (message "Please enter a number.")
6252          (sleep-for 1)))
6253      (list days younger)))
6254   (prog1
6255       (let ((data gnus-newsgroup-data)
6256             (cutoff (days-to-time age))
6257             articles d date is-younger)
6258         (while (setq d (pop data))
6259           (when (and (vectorp (gnus-data-header d))
6260                      (setq date (mail-header-date (gnus-data-header d))))
6261             (setq is-younger (time-less-p
6262                               (time-since (condition-case ()
6263                                               (date-to-time date)
6264                                             (error '(0 0))))
6265                               cutoff))
6266             (when (if younger-p
6267                       is-younger
6268                     (not is-younger))
6269               (push (gnus-data-number d) articles))))
6270         (gnus-summary-limit (nreverse articles)))
6271     (gnus-summary-position-point)))
6272
6273 (defun gnus-summary-limit-to-extra (header regexp)
6274   "Limit the summary buffer to articles that match an 'extra' header."
6275   (interactive
6276    (let ((header
6277           (intern
6278            (gnus-completing-read
6279             (symbol-name (car gnus-extra-headers))      
6280             "Limit extra header:"       
6281             (mapcar (lambda (x) 
6282                       (cons (symbol-name x) x))
6283                     gnus-extra-headers)
6284             nil                 
6285             t))))
6286      (list header
6287            (read-string (format "Limit to header %s (regexp): " header)))))
6288   (when (not (equal "" regexp))
6289     (prog1
6290         (let ((articles (gnus-summary-find-matching
6291                          (cons 'extra header) regexp 'all)))
6292           (unless articles
6293             (error "Found no matches for \"%s\"" regexp))
6294           (gnus-summary-limit articles))
6295       (gnus-summary-position-point))))
6296
6297 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6298 (make-obsolete
6299  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6300
6301 (defun gnus-summary-limit-to-unread (&optional all)
6302   "Limit the summary buffer to articles that are not marked as read.
6303 If ALL is non-nil, limit strictly to unread articles."
6304   (interactive "P")
6305   (if all
6306       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6307     (gnus-summary-limit-to-marks
6308      ;; Concat all the marks that say that an article is read and have
6309      ;; those removed.
6310      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6311            gnus-killed-mark gnus-kill-file-mark
6312            gnus-low-score-mark gnus-expirable-mark
6313            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6314            gnus-duplicate-mark gnus-souped-mark)
6315      'reverse)))
6316
6317 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6318 (make-obsolete 'gnus-summary-delete-marked-with
6319                'gnus-summary-limit-exlude-marks)
6320
6321 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6322   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6323 If REVERSE, limit the summary buffer to articles that are marked
6324 with MARKS.  MARKS can either be a string of marks or a list of marks.
6325 Returns how many articles were removed."
6326   (interactive "sMarks: ")
6327   (gnus-summary-limit-to-marks marks t))
6328
6329 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6330   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6331 If REVERSE (the prefix), limit the summary buffer to articles that are
6332 not marked with MARKS.  MARKS can either be a string of marks or a
6333 list of marks.
6334 Returns how many articles were removed."
6335   (interactive "sMarks: \nP")
6336   (prog1
6337       (let ((data gnus-newsgroup-data)
6338             (marks (if (listp marks) marks
6339                      (append marks nil))) ; Transform to list.
6340             articles)
6341         (while data
6342           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6343                   (memq (gnus-data-mark (car data)) marks))
6344             (push (gnus-data-number (car data)) articles))
6345           (setq data (cdr data)))
6346         (gnus-summary-limit articles))
6347     (gnus-summary-position-point)))
6348
6349 (defun gnus-summary-limit-to-score (&optional score)
6350   "Limit to articles with score at or above SCORE."
6351   (interactive "P")
6352   (setq score (if score
6353                   (prefix-numeric-value score)
6354                 (or gnus-summary-default-score 0)))
6355   (let ((data gnus-newsgroup-data)
6356         articles)
6357     (while data
6358       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6359                 score)
6360         (push (gnus-data-number (car data)) articles))
6361       (setq data (cdr data)))
6362     (prog1
6363         (gnus-summary-limit articles)
6364       (gnus-summary-position-point))))
6365
6366 (defun gnus-summary-limit-include-thread (id)
6367   "Display all the hidden articles that in the current thread."
6368   (interactive (list (mail-header-id (gnus-summary-article-header))))
6369   (let ((articles (gnus-articles-in-thread
6370                    (gnus-id-to-thread (gnus-root-id id)))))
6371     (prog1
6372         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6373       (gnus-summary-position-point))))
6374
6375 (defun gnus-summary-limit-include-dormant ()
6376   "Display all the hidden articles that are marked as dormant.
6377 Note that this command only works on a subset of the articles currently
6378 fetched for this group."
6379   (interactive)
6380   (unless gnus-newsgroup-dormant
6381     (error "There are no dormant articles in this group"))
6382   (prog1
6383       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6384     (gnus-summary-position-point)))
6385
6386 (defun gnus-summary-limit-exclude-dormant ()
6387   "Hide all dormant articles."
6388   (interactive)
6389   (prog1
6390       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6391     (gnus-summary-position-point)))
6392
6393 (defun gnus-summary-limit-exclude-childless-dormant ()
6394   "Hide all dormant articles that have no children."
6395   (interactive)
6396   (let ((data (gnus-data-list t))
6397         articles d children)
6398     ;; Find all articles that are either not dormant or have
6399     ;; children.
6400     (while (setq d (pop data))
6401       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6402                 (and (setq children
6403                            (gnus-article-children (gnus-data-number d)))
6404                      (let (found)
6405                        (while children
6406                          (when (memq (car children) articles)
6407                            (setq children nil
6408                                  found t))
6409                          (pop children))
6410                        found)))
6411         (push (gnus-data-number d) articles)))
6412     ;; Do the limiting.
6413     (prog1
6414         (gnus-summary-limit articles)
6415       (gnus-summary-position-point))))
6416
6417 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6418   "Mark all unread excluded articles as read.
6419 If ALL, mark even excluded ticked and dormants as read."
6420   (interactive "P")
6421   (let ((articles (gnus-sorted-complement
6422                    (sort
6423                     (mapcar (lambda (h) (mail-header-number h))
6424                             gnus-newsgroup-headers)
6425                     '<)
6426                    (sort gnus-newsgroup-limit '<)))
6427         article)
6428     (setq gnus-newsgroup-unreads
6429           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6430     (if all
6431         (setq gnus-newsgroup-dormant nil
6432               gnus-newsgroup-marked nil
6433               gnus-newsgroup-reads
6434               (nconc
6435                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6436                gnus-newsgroup-reads))
6437       (while (setq article (pop articles))
6438         (unless (or (memq article gnus-newsgroup-dormant)
6439                     (memq article gnus-newsgroup-marked))
6440           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6441
6442 (defun gnus-summary-limit (articles &optional pop)
6443   (if pop
6444       ;; We pop the previous limit off the stack and use that.
6445       (setq articles (car gnus-newsgroup-limits)
6446             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6447     ;; We use the new limit, so we push the old limit on the stack.
6448     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6449   ;; Set the limit.
6450   (setq gnus-newsgroup-limit articles)
6451   (let ((total (length gnus-newsgroup-data))
6452         (data (gnus-data-find-list (gnus-summary-article-number)))
6453         (gnus-summary-mark-below nil)   ; Inhibit this.
6454         found)
6455     ;; This will do all the work of generating the new summary buffer
6456     ;; according to the new limit.
6457     (gnus-summary-prepare)
6458     ;; Hide any threads, possibly.
6459     (and gnus-show-threads
6460          gnus-thread-hide-subtree
6461          (gnus-summary-hide-all-threads))
6462     ;; Try to return to the article you were at, or one in the
6463     ;; neighborhood.
6464     (when data
6465       ;; We try to find some article after the current one.
6466       (while data
6467         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6468           (setq data nil
6469                 found t))
6470         (setq data (cdr data))))
6471     (unless found
6472       ;; If there is no data, that means that we were after the last
6473       ;; article.  The same goes when we can't find any articles
6474       ;; after the current one.
6475       (goto-char (point-max))
6476       (gnus-summary-find-prev))
6477     (gnus-set-mode-line 'summary)
6478     ;; We return how many articles were removed from the summary
6479     ;; buffer as a result of the new limit.
6480     (- total (length gnus-newsgroup-data))))
6481
6482 (defsubst gnus-invisible-cut-children (threads)
6483   (let ((num 0))
6484     (while threads
6485       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6486         (incf num))
6487       (pop threads))
6488     (< num 2)))
6489
6490 (defsubst gnus-cut-thread (thread)
6491   "Go forwards in the thread until we find an article that we want to display."
6492   (when (or (eq gnus-fetch-old-headers 'some)
6493             (eq gnus-fetch-old-headers 'invisible)
6494             (numberp gnus-fetch-old-headers)
6495             (eq gnus-build-sparse-threads 'some)
6496             (eq gnus-build-sparse-threads 'more))
6497     ;; Deal with old-fetched headers and sparse threads.
6498     (while (and
6499             thread
6500             (or
6501              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6502              (gnus-summary-article-ancient-p
6503               (mail-header-number (car thread))))
6504             (if (or (<= (length (cdr thread)) 1)
6505                     (eq gnus-fetch-old-headers 'invisible))
6506                 (setq gnus-newsgroup-limit
6507                       (delq (mail-header-number (car thread))
6508                             gnus-newsgroup-limit)
6509                       thread (cadr thread))
6510               (when (gnus-invisible-cut-children (cdr thread))
6511                 (let ((th (cdr thread)))
6512                   (while th
6513                     (if (memq (mail-header-number (caar th))
6514                               gnus-newsgroup-limit)
6515                         (setq thread (car th)
6516                               th nil)
6517                       (setq th (cdr th))))))))))
6518   thread)
6519
6520 (defun gnus-cut-threads (threads)
6521   "Cut off all uninteresting articles from the beginning of threads."
6522   (when (or (eq gnus-fetch-old-headers 'some)
6523             (eq gnus-fetch-old-headers 'invisible)
6524             (numberp gnus-fetch-old-headers)
6525             (eq gnus-build-sparse-threads 'some)
6526             (eq gnus-build-sparse-threads 'more))
6527     (let ((th threads))
6528       (while th
6529         (setcar th (gnus-cut-thread (car th)))
6530         (setq th (cdr th)))))
6531   ;; Remove nixed out threads.
6532   (delq nil threads))
6533
6534 (defun gnus-summary-initial-limit (&optional show-if-empty)
6535   "Figure out what the initial limit is supposed to be on group entry.
6536 This entails weeding out unwanted dormants, low-scored articles,
6537 fetch-old-headers verbiage, and so on."
6538   ;; Most groups have nothing to remove.
6539   (if (or gnus-inhibit-limiting
6540           (and (null gnus-newsgroup-dormant)
6541                (not (eq gnus-fetch-old-headers 'some))
6542                (not (numberp gnus-fetch-old-headers))
6543                (not (eq gnus-fetch-old-headers 'invisible))
6544                (null gnus-summary-expunge-below)
6545                (not (eq gnus-build-sparse-threads 'some))
6546                (not (eq gnus-build-sparse-threads 'more))
6547                (null gnus-thread-expunge-below)
6548                (not gnus-use-nocem)))
6549       ()                                ; Do nothing.
6550     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6551     (setq gnus-newsgroup-limit nil)
6552     (mapatoms
6553      (lambda (node)
6554        (unless (car (symbol-value node))
6555          ;; These threads have no parents -- they are roots.
6556          (let ((nodes (cdr (symbol-value node)))
6557                thread)
6558            (while nodes
6559              (if (and gnus-thread-expunge-below
6560                       (< (gnus-thread-total-score (car nodes))
6561                          gnus-thread-expunge-below))
6562                  (gnus-expunge-thread (pop nodes))
6563                (setq thread (pop nodes))
6564                (gnus-summary-limit-children thread))))))
6565      gnus-newsgroup-dependencies)
6566     ;; If this limitation resulted in an empty group, we might
6567     ;; pop the previous limit and use it instead.
6568     (when (and (not gnus-newsgroup-limit)
6569                show-if-empty)
6570       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6571     gnus-newsgroup-limit))
6572
6573 (defun gnus-summary-limit-children (thread)
6574   "Return 1 if this subthread is visible and 0 if it is not."
6575   ;; First we get the number of visible children to this thread.  This
6576   ;; is done by recursing down the thread using this function, so this
6577   ;; will really go down to a leaf article first, before slowly
6578   ;; working its way up towards the root.
6579   (when thread
6580     (let ((children
6581            (if (cdr thread)
6582                (apply '+ (mapcar 'gnus-summary-limit-children
6583                                  (cdr thread)))
6584              0))
6585           (number (mail-header-number (car thread)))
6586           score)
6587       (if (and
6588            (not (memq number gnus-newsgroup-marked))
6589            (or
6590             ;; If this article is dormant and has absolutely no visible
6591             ;; children, then this article isn't visible.
6592             (and (memq number gnus-newsgroup-dormant)
6593                  (zerop children))
6594             ;; If this is "fetch-old-headered" and there is no
6595             ;; visible children, then we don't want this article.
6596             (and (or (eq gnus-fetch-old-headers 'some)
6597                      (numberp gnus-fetch-old-headers))
6598                  (gnus-summary-article-ancient-p number)
6599                  (zerop children))
6600             ;; If this is "fetch-old-headered" and `invisible', then
6601             ;; we don't want this article.
6602             (and (eq gnus-fetch-old-headers 'invisible)
6603                  (gnus-summary-article-ancient-p number))
6604             ;; If this is a sparsely inserted article with no children,
6605             ;; we don't want it.
6606             (and (eq gnus-build-sparse-threads 'some)
6607                  (gnus-summary-article-sparse-p number)
6608                  (zerop children))
6609             ;; If we use expunging, and this article is really
6610             ;; low-scored, then we don't want this article.
6611             (when (and gnus-summary-expunge-below
6612                        (< (setq score
6613                                 (or (cdr (assq number gnus-newsgroup-scored))
6614                                     gnus-summary-default-score))
6615                           gnus-summary-expunge-below))
6616               ;; We increase the expunge-tally here, but that has
6617               ;; nothing to do with the limits, really.
6618               (incf gnus-newsgroup-expunged-tally)
6619               ;; We also mark as read here, if that's wanted.
6620               (when (and gnus-summary-mark-below
6621                          (< score gnus-summary-mark-below))
6622                 (setq gnus-newsgroup-unreads
6623                       (delq number gnus-newsgroup-unreads))
6624                 (if gnus-newsgroup-auto-expire
6625                     (push number gnus-newsgroup-expirable)
6626                   (push (cons number gnus-low-score-mark)
6627                         gnus-newsgroup-reads)))
6628               t)
6629             ;; Check NoCeM things.
6630             (if (and gnus-use-nocem
6631                      (gnus-nocem-unwanted-article-p
6632                       (mail-header-id (car thread))))
6633                 (progn
6634                   (setq gnus-newsgroup-unreads
6635                         (delq number gnus-newsgroup-unreads))
6636                   t))))
6637           ;; Nope, invisible article.
6638           0
6639         ;; Ok, this article is to be visible, so we add it to the limit
6640         ;; and return 1.
6641         (push number gnus-newsgroup-limit)
6642         1))))
6643
6644 (defun gnus-expunge-thread (thread)
6645   "Mark all articles in THREAD as read."
6646   (let* ((number (mail-header-number (car thread))))
6647     (incf gnus-newsgroup-expunged-tally)
6648     ;; We also mark as read here, if that's wanted.
6649     (setq gnus-newsgroup-unreads
6650           (delq number gnus-newsgroup-unreads))
6651     (if gnus-newsgroup-auto-expire
6652         (push number gnus-newsgroup-expirable)
6653       (push (cons number gnus-low-score-mark)
6654             gnus-newsgroup-reads)))
6655   ;; Go recursively through all subthreads.
6656   (mapcar 'gnus-expunge-thread (cdr thread)))
6657
6658 ;; Summary article oriented commands
6659
6660 (defun gnus-summary-refer-parent-article (n)
6661   "Refer parent article N times.
6662 If N is negative, go to ancestor -N instead.
6663 The difference between N and the number of articles fetched is returned."
6664   (interactive "p")
6665   (let ((skip 1)
6666         error header ref)
6667     (when (not (natnump n))
6668       (setq skip (abs n)
6669             n 1))
6670     (while (and (> n 0)
6671                 (not error))
6672       (setq header (gnus-summary-article-header))
6673       (if (and (eq (mail-header-number header)
6674                    (cdr gnus-article-current))
6675                (equal gnus-newsgroup-name
6676                       (car gnus-article-current)))
6677           ;; If we try to find the parent of the currently
6678           ;; displayed article, then we take a look at the actual
6679           ;; References header, since this is slightly more
6680           ;; reliable than the References field we got from the
6681           ;; server.
6682           (save-excursion
6683             (set-buffer gnus-original-article-buffer)
6684             (nnheader-narrow-to-headers)
6685             (unless (setq ref (message-fetch-field "references"))
6686               (setq ref (message-fetch-field "in-reply-to")))
6687             (widen))
6688         (setq ref
6689               ;; It's not the current article, so we take a bet on
6690               ;; the value we got from the server.
6691               (mail-header-references header)))
6692       (if (and ref
6693                (not (equal ref "")))
6694           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6695             (gnus-message 1 "Couldn't find parent"))
6696         (gnus-message 1 "No references in article %d"
6697                       (gnus-summary-article-number))
6698         (setq error t))
6699       (decf n))
6700     (gnus-summary-position-point)
6701     n))
6702
6703 (defun gnus-summary-refer-references ()
6704   "Fetch all articles mentioned in the References header.
6705 Return the number of articles fetched."
6706   (interactive)
6707   (let ((ref (mail-header-references (gnus-summary-article-header)))
6708         (current (gnus-summary-article-number))
6709         (n 0))
6710     (if (or (not ref)
6711             (equal ref ""))
6712         (error "No References in the current article")
6713       ;; For each Message-ID in the References header...
6714       (while (string-match "<[^>]*>" ref)
6715         (incf n)
6716         ;; ... fetch that article.
6717         (gnus-summary-refer-article
6718          (prog1 (match-string 0 ref)
6719            (setq ref (substring ref (match-end 0))))))
6720       (gnus-summary-goto-subject current)
6721       (gnus-summary-position-point)
6722       n)))
6723
6724 (defun gnus-summary-refer-thread (&optional limit)
6725   "Fetch all articles in the current thread.
6726 If LIMIT (the numerical prefix), fetch that many old headers instead
6727 of what's specified by the `gnus-refer-thread-limit' variable."
6728   (interactive "P")
6729   (let ((id (mail-header-id (gnus-summary-article-header)))
6730         (limit (if limit (prefix-numeric-value limit)
6731                  gnus-refer-thread-limit)))
6732     ;; We want to fetch LIMIT *old* headers, but we also have to
6733     ;; re-fetch all the headers in the current buffer, because many of
6734     ;; them may be undisplayed.  So we adjust LIMIT.
6735     (when (numberp limit)
6736       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6737     (unless (eq gnus-fetch-old-headers 'invisible)
6738       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6739       ;; Retrieve the headers and read them in.
6740       (if (eq (gnus-retrieve-headers
6741                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6742               'nov)
6743           (gnus-build-all-threads)
6744         (error "Can't fetch thread from backends that don't support NOV"))
6745       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6746     (gnus-summary-limit-include-thread id)))
6747
6748 (defun gnus-summary-refer-article (message-id)
6749   "Fetch an article specified by MESSAGE-ID."
6750   (interactive "sMessage-ID: ")
6751   (when (and (stringp message-id)
6752              (not (zerop (length message-id))))
6753     ;; Construct the correct Message-ID if necessary.
6754     ;; Suggested by tale@pawl.rpi.edu.
6755     (unless (string-match "^<" message-id)
6756       (setq message-id (concat "<" message-id)))
6757     (unless (string-match ">$" message-id)
6758       (setq message-id (concat message-id ">")))
6759     (let* ((header (gnus-id-to-header message-id))
6760            (sparse (and header
6761                         (gnus-summary-article-sparse-p
6762                          (mail-header-number header))
6763                         (memq (mail-header-number header)
6764                               gnus-newsgroup-limit)))
6765            number)
6766       (cond
6767        ;; If the article is present in the buffer we just go to it.
6768        ((and header
6769              (or (not (gnus-summary-article-sparse-p
6770                        (mail-header-number header)))
6771                  sparse))
6772         (prog1
6773             (gnus-summary-goto-article
6774              (mail-header-number header) nil t)
6775           (when sparse
6776             (gnus-summary-update-article (mail-header-number header)))))
6777        (t
6778         ;; We fetch the article.
6779         (catch 'found
6780           (dolist (gnus-override-method (gnus-refer-article-methods))
6781             (gnus-check-server gnus-override-method)
6782             ;; Fetch the header, and display the article.
6783             (when (setq number (gnus-summary-insert-subject message-id))
6784               (gnus-summary-select-article nil nil nil number)
6785               (throw 'found t)))
6786           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6787
6788 (defun gnus-refer-article-methods ()
6789   "Return a list of referrable methods."
6790   (cond
6791    ;; No method, so we default to current and native.
6792    ((null gnus-refer-article-method)
6793     (list gnus-current-select-method gnus-select-method))
6794    ;; Current.
6795    ((eq 'current gnus-refer-article-method)
6796     (list gnus-current-select-method))
6797    ;; List of select methods.
6798    ((not (stringp (cadr gnus-refer-article-method)))
6799     (let (out)
6800       (dolist (method gnus-refer-article-method)
6801         (push (if (eq 'current method)
6802                   gnus-current-select-method
6803                 method)
6804               out))
6805       (nreverse out)))
6806    ;; One single select method.
6807    (t
6808     (list gnus-refer-article-method))))
6809
6810 (defun gnus-summary-edit-parameters ()
6811   "Edit the group parameters of the current group."
6812   (interactive)
6813   (gnus-group-edit-group gnus-newsgroup-name 'params))
6814
6815 (defun gnus-summary-customize-parameters ()
6816   "Customize the group parameters of the current group."
6817   (interactive)
6818   (gnus-group-customize gnus-newsgroup-name))
6819
6820 (defun gnus-summary-enter-digest-group (&optional force)
6821   "Enter an nndoc group based on the current article.
6822 If FORCE, force a digest interpretation.  If not, try
6823 to guess what the document format is."
6824   (interactive "P")
6825   (let ((conf gnus-current-window-configuration))
6826     (save-excursion
6827       (gnus-summary-select-article))
6828     (setq gnus-current-window-configuration conf)
6829     (let* ((name (format "%s-%d"
6830                          (gnus-group-prefixed-name
6831                           gnus-newsgroup-name (list 'nndoc ""))
6832                          (save-excursion
6833                            (set-buffer gnus-summary-buffer)
6834                            gnus-current-article)))
6835            (ogroup gnus-newsgroup-name)
6836            (params (append (gnus-info-params (gnus-get-info ogroup))
6837                            (list (cons 'to-group ogroup))
6838                            (list (cons 'save-article-group ogroup))))
6839            (case-fold-search t)
6840            (buf (current-buffer))
6841            dig to-address)
6842       (save-excursion
6843         (set-buffer gnus-original-article-buffer)
6844         ;; Have the digest group inherit the main mail address of
6845         ;; the parent article.
6846         (when (setq to-address (or (message-fetch-field "reply-to")
6847                                    (message-fetch-field "from")))
6848           (setq params (append (list (cons 'to-address to-address)))))
6849         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6850         (insert-buffer-substring gnus-original-article-buffer)
6851         ;; Remove lines that may lead nndoc to misinterpret the
6852         ;; document type.
6853         (narrow-to-region
6854          (goto-char (point-min))
6855          (or (search-forward "\n\n" nil t) (point)))
6856         (goto-char (point-min))
6857         (delete-matching-lines "^Path:\\|^From ")
6858         (widen))
6859       (unwind-protect
6860           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6861                     (gnus-newsgroup-ephemeral-ignored-charsets
6862                      gnus-newsgroup-ignored-charsets))
6863                 (gnus-group-read-ephemeral-group
6864                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6865                               (nndoc-article-type
6866                                ,(if force 'mbox 'guess))) t))
6867               ;; Make all postings to this group go to the parent group.
6868               (nconc (gnus-info-params (gnus-get-info name))
6869                      params)
6870             ;; Couldn't select this doc group.
6871             (switch-to-buffer buf)
6872             (gnus-set-global-variables)
6873             (gnus-configure-windows 'summary)
6874             (gnus-message 3 "Article couldn't be entered?"))
6875         (kill-buffer dig)))))
6876
6877 (defun gnus-summary-read-document (n)
6878   "Open a new group based on the current article(s).
6879 This will allow you to read digests and other similar
6880 documents as newsgroups.
6881 Obeys the standard process/prefix convention."
6882   (interactive "P")
6883   (let* ((articles (gnus-summary-work-articles n))
6884          (ogroup gnus-newsgroup-name)
6885          (params (append (gnus-info-params (gnus-get-info ogroup))
6886                          (list (cons 'to-group ogroup))))
6887          article group egroup groups vgroup)
6888     (while (setq article (pop articles))
6889       (setq group (format "%s-%d" gnus-newsgroup-name article))
6890       (gnus-summary-remove-process-mark article)
6891       (when (gnus-summary-display-article article)
6892         (save-excursion
6893           (with-temp-buffer
6894             (insert-buffer-substring gnus-original-article-buffer)
6895             ;; Remove some headers that may lead nndoc to make
6896             ;; the wrong guess.
6897             (message-narrow-to-head)
6898             (goto-char (point-min))
6899             (delete-matching-lines "^\\(Path\\):\\|^From ")
6900             (widen)
6901             (if (setq egroup
6902                       (gnus-group-read-ephemeral-group
6903                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6904                                      (nndoc-article-type guess))
6905                        t nil t))
6906                 (progn
6907                   ;; Make all postings to this group go to the parent group.
6908                   (nconc (gnus-info-params (gnus-get-info egroup))
6909                          params)
6910                   (push egroup groups))
6911               ;; Couldn't select this doc group.
6912               (gnus-error 3 "Article couldn't be entered"))))))
6913     ;; Now we have selected all the documents.
6914     (cond
6915      ((not groups)
6916       (error "None of the articles could be interpreted as documents"))
6917      ((gnus-group-read-ephemeral-group
6918        (setq vgroup (format
6919                      "nnvirtual:%s-%s" gnus-newsgroup-name
6920                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6921        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6922        t
6923        (cons (current-buffer) 'summary)))
6924      (t
6925       (error "Couldn't select virtual nndoc group")))))
6926
6927 (defun gnus-summary-isearch-article (&optional regexp-p)
6928   "Do incremental search forward on the current article.
6929 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6930   (interactive "P")
6931   (gnus-summary-select-article)
6932   (gnus-configure-windows 'article)
6933   (gnus-eval-in-buffer-window gnus-article-buffer
6934     (save-restriction
6935       (widen)
6936       (isearch-forward regexp-p))))
6937
6938 (defun gnus-summary-search-article-forward (regexp &optional backward)
6939   "Search for an article containing REGEXP forward.
6940 If BACKWARD, search backward instead."
6941   (interactive
6942    (list (read-string
6943           (format "Search article %s (regexp%s): "
6944                   (if current-prefix-arg "backward" "forward")
6945                   (if gnus-last-search-regexp
6946                       (concat ", default " gnus-last-search-regexp)
6947                     "")))
6948          current-prefix-arg))
6949   (if (string-equal regexp "")
6950       (setq regexp (or gnus-last-search-regexp ""))
6951     (setq gnus-last-search-regexp regexp))
6952   (if (gnus-summary-search-article regexp backward)
6953       (gnus-summary-show-thread)
6954     (error "Search failed: \"%s\"" regexp)))
6955
6956 (defun gnus-summary-search-article-backward (regexp)
6957   "Search for an article containing REGEXP backward."
6958   (interactive
6959    (list (read-string
6960           (format "Search article backward (regexp%s): "
6961                   (if gnus-last-search-regexp
6962                       (concat ", default " gnus-last-search-regexp)
6963                     "")))))
6964   (gnus-summary-search-article-forward regexp 'backward))
6965
6966 (defun gnus-summary-search-article (regexp &optional backward)
6967   "Search for an article containing REGEXP.
6968 Optional argument BACKWARD means do search for backward.
6969 `gnus-select-article-hook' is not called during the search."
6970   ;; We have to require this here to make sure that the following
6971   ;; dynamic binding isn't shadowed by autoloading.
6972   (require 'gnus-async)
6973   (require 'gnus-art)
6974   (let ((gnus-select-article-hook nil)  ;Disable hook.
6975         (gnus-article-prepare-hook nil)
6976         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6977         (gnus-use-article-prefetch nil)
6978         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6979         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6980         (sum (current-buffer))
6981         (gnus-display-mime-function nil)
6982         (found nil)
6983         point)
6984     (gnus-save-hidden-threads
6985       (gnus-summary-select-article)
6986       (set-buffer gnus-article-buffer)
6987       (goto-char (window-point (get-buffer-window (current-buffer))))
6988       (when backward
6989         (forward-line -1))
6990       (while (not found)
6991         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6992         (if (if backward
6993                 (re-search-backward regexp nil t)
6994               (re-search-forward regexp nil t))
6995             ;; We found the regexp.
6996             (progn
6997               (setq found 'found)
6998               (beginning-of-line)
6999               (set-window-start
7000                (get-buffer-window (current-buffer))
7001                (point))
7002               (forward-line 1)
7003               (set-window-point
7004                (get-buffer-window (current-buffer))
7005                (point))
7006               (set-buffer sum)
7007               (setq point (point)))
7008           ;; We didn't find it, so we go to the next article.
7009           (set-buffer sum)
7010           (setq found 'not)
7011           (while (eq found 'not)
7012             (if (not (if backward (gnus-summary-find-prev)
7013                        (gnus-summary-find-next)))
7014                 ;; No more articles.
7015                 (setq found t)
7016               ;; Select the next article and adjust point.
7017               (unless (gnus-summary-article-sparse-p
7018                        (gnus-summary-article-number))
7019                 (setq found nil)
7020                 (gnus-summary-select-article)
7021                 (set-buffer gnus-article-buffer)
7022                 (widen)
7023                 (goto-char (if backward (point-max) (point-min))))))))
7024       (gnus-message 7 ""))
7025     ;; Return whether we found the regexp.
7026     (when (eq found 'found)
7027       (goto-char point)
7028       (gnus-summary-show-thread)
7029       (gnus-summary-goto-subject gnus-current-article)
7030       (gnus-summary-position-point)
7031       t)))
7032
7033 (defun gnus-summary-find-matching (header regexp &optional backward unread
7034                                           not-case-fold)
7035   "Return a list of all articles that match REGEXP on HEADER.
7036 The search stars on the current article and goes forwards unless
7037 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7038 If UNREAD is non-nil, only unread articles will
7039 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7040 in the comparisons."
7041   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7042                 (gnus-data-find-list
7043                  (gnus-summary-article-number) (gnus-data-list backward))))
7044         (case-fold-search (not not-case-fold))
7045         articles d func)
7046     (if (consp header)
7047         (if (eq (car header) 'extra)
7048             (setq func
7049                   `(lambda (h)
7050                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7051                          "")))
7052           (error "%s is an invalid header" header))
7053       (unless (fboundp (intern (concat "mail-header-" header)))
7054         (error "%s is not a valid header" header))
7055       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7056     (while data
7057       (setq d (car data))
7058       (and (or (not unread)             ; We want all articles...
7059                (gnus-data-unread-p d))  ; Or just unreads.
7060            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7061            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7062            (push (gnus-data-number d) articles)) ; Success!
7063       (setq data (cdr data)))
7064     (nreverse articles)))
7065
7066 (defun gnus-summary-execute-command (header regexp command &optional backward)
7067   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7068 If HEADER is an empty string (or nil), the match is done on the entire
7069 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7070   (interactive
7071    (list (let ((completion-ignore-case t))
7072            (completing-read
7073             "Header name: "
7074             (mapcar (lambda (string) (list string))
7075                     '("Number" "Subject" "From" "Lines" "Date"
7076                       "Message-ID" "Xref" "References" "Body"))
7077             nil 'require-match))
7078          (read-string "Regexp: ")
7079          (read-key-sequence "Command: ")
7080          current-prefix-arg))
7081   (when (equal header "Body")
7082     (setq header ""))
7083   ;; Hidden thread subtrees must be searched as well.
7084   (gnus-summary-show-all-threads)
7085   ;; We don't want to change current point nor window configuration.
7086   (save-excursion
7087     (save-window-excursion
7088       (gnus-message 6 "Executing %s..." (key-description command))
7089       ;; We'd like to execute COMMAND interactively so as to give arguments.
7090       (gnus-execute header regexp
7091                     `(call-interactively ',(key-binding command))
7092                     backward)
7093       (gnus-message 6 "Executing %s...done" (key-description command)))))
7094
7095 (defun gnus-summary-beginning-of-article ()
7096   "Scroll the article back to the beginning."
7097   (interactive)
7098   (gnus-summary-select-article)
7099   (gnus-configure-windows 'article)
7100   (gnus-eval-in-buffer-window gnus-article-buffer
7101     (widen)
7102     (goto-char (point-min))
7103     (when gnus-page-broken
7104       (gnus-narrow-to-page))))
7105
7106 (defun gnus-summary-end-of-article ()
7107   "Scroll to the end of the article."
7108   (interactive)
7109   (gnus-summary-select-article)
7110   (gnus-configure-windows 'article)
7111   (gnus-eval-in-buffer-window gnus-article-buffer
7112     (widen)
7113     (goto-char (point-max))
7114     (recenter -3)
7115     (when gnus-page-broken
7116       (gnus-narrow-to-page))))
7117
7118 (defun gnus-summary-print-article (&optional filename n)
7119   "Generate and print a PostScript image of the N next (mail) articles.
7120
7121 If N is negative, print the N previous articles.  If N is nil and articles
7122 have been marked with the process mark, print these instead.
7123
7124 If the optional first argument FILENAME is nil, send the image to the
7125 printer.  If FILENAME is a string, save the PostScript image in a file with
7126 that name.  If FILENAME is a number, prompt the user for the name of the file
7127 to save in."
7128   (interactive (list (ps-print-preprint current-prefix-arg)
7129                      current-prefix-arg))
7130   (dolist (article (gnus-summary-work-articles n))
7131     (gnus-summary-select-article nil nil 'pseudo article)
7132     (gnus-eval-in-buffer-window gnus-article-buffer
7133       (let ((buffer (generate-new-buffer " *print*")))
7134         (unwind-protect
7135             (progn
7136               (copy-to-buffer buffer (point-min) (point-max))
7137               (set-buffer buffer)
7138               (gnus-article-delete-invisible-text)
7139               (let ((ps-left-header
7140                      (list
7141                       (concat "("
7142                               (mail-header-subject gnus-current-headers) ")")
7143                       (concat "("
7144                               (mail-header-from gnus-current-headers) ")")))
7145                     (ps-right-header
7146                      (list
7147                       "/pagenumberstring load"
7148                       (concat "("
7149                               (mail-header-date gnus-current-headers) ")"))))
7150                 (gnus-run-hooks 'gnus-ps-print-hook)
7151                 (save-excursion
7152                   (ps-print-buffer-with-faces filename))))
7153           (kill-buffer buffer))))))
7154
7155 (defun gnus-summary-show-article (&optional arg)
7156   "Force re-fetching of the current article.
7157 If ARG (the prefix) is a number, show the article with the charset 
7158 defined in `gnus-summary-show-article-charset-alist', or the charset
7159 inputed.
7160 If ARG (the prefix) is non-nil and not a number, show the raw article 
7161 without any article massaging functions being run."
7162   (interactive "P")
7163   (cond 
7164    ((numberp arg)
7165     (let ((gnus-newsgroup-charset 
7166            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7167                (read-coding-system "Charset: ")))
7168           (gnus-newsgroup-ignored-charsets 'gnus-all))
7169       (gnus-summary-select-article nil 'force)))
7170    ((not arg)
7171     ;; Select the article the normal way.
7172     (gnus-summary-select-article nil 'force))
7173    (t
7174     ;; We have to require this here to make sure that the following
7175     ;; dynamic binding isn't shadowed by autoloading.
7176     (require 'gnus-async)
7177     (require 'gnus-art)
7178     ;; Bind the article treatment functions to nil.
7179     (let ((gnus-have-all-headers t)
7180           gnus-article-prepare-hook
7181           gnus-article-decode-hook
7182           gnus-display-mime-function
7183           gnus-break-pages)
7184       ;; Destroy any MIME parts.
7185       (when (gnus-buffer-live-p gnus-article-buffer)
7186         (save-excursion
7187           (set-buffer gnus-article-buffer)
7188           (mm-destroy-parts gnus-article-mime-handles)))
7189       (gnus-summary-select-article nil 'force))))
7190   (gnus-summary-goto-subject gnus-current-article)
7191   (gnus-summary-position-point))
7192
7193 (defun gnus-summary-verbose-headers (&optional arg)
7194   "Toggle permanent full header display.
7195 If ARG is a positive number, turn header display on.
7196 If ARG is a negative number, turn header display off."
7197   (interactive "P")
7198   (setq gnus-show-all-headers
7199         (cond ((or (not (numberp arg))
7200                    (zerop arg))
7201                (not gnus-show-all-headers))
7202               ((natnump arg)
7203                t)))
7204   (gnus-summary-show-article))
7205
7206 (defun gnus-summary-toggle-header (&optional arg)
7207   "Show the headers if they are hidden, or hide them if they are shown.
7208 If ARG is a positive number, show the entire header.
7209 If ARG is a negative number, hide the unwanted header lines."
7210   (interactive "P")
7211   (save-excursion
7212     (set-buffer gnus-article-buffer)
7213     (save-restriction
7214       (let* ((buffer-read-only nil)
7215              (inhibit-point-motion-hooks t)
7216              hidden e)
7217         (setq hidden
7218               (if (numberp arg)
7219                   (>= arg 0)
7220                 (save-restriction 
7221                   (article-narrow-to-head)
7222                   (gnus-article-hidden-text-p 'headers))))
7223         (goto-char (point-min))
7224         (when (search-forward "\n\n" nil t)
7225           (delete-region (point-min) (1- (point))))
7226         (goto-char (point-min))
7227         (save-excursion
7228           (set-buffer gnus-original-article-buffer)
7229           (goto-char (point-min))
7230           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7231         (insert-buffer-substring gnus-original-article-buffer 1 e)
7232         (save-restriction
7233           (narrow-to-region (point-min) (point))
7234           (article-decode-encoded-words)
7235           (if  hidden
7236               (let ((gnus-treat-hide-headers nil)
7237                     (gnus-treat-hide-boring-headers nil))
7238                 (gnus-treat-article 'head))
7239             (gnus-treat-article 'head)))))))
7240
7241 (defun gnus-summary-show-all-headers ()
7242   "Make all header lines visible."
7243   (interactive)
7244   (gnus-article-show-all-headers))
7245
7246 (defun gnus-summary-caesar-message (&optional arg)
7247   "Caesar rotate the current article by 13.
7248 The numerical prefix specifies how many places to rotate each letter
7249 forward."
7250   (interactive "P")
7251   (gnus-summary-select-article)
7252   (let ((mail-header-separator ""))
7253     (gnus-eval-in-buffer-window gnus-article-buffer
7254       (save-restriction
7255         (widen)
7256         (let ((start (window-start))
7257               buffer-read-only)
7258           (message-caesar-buffer-body arg)
7259           (set-window-start (get-buffer-window (current-buffer)) start))))))
7260
7261 (defun gnus-summary-stop-page-breaking ()
7262   "Stop page breaking in the current article."
7263   (interactive)
7264   (gnus-summary-select-article)
7265   (gnus-eval-in-buffer-window gnus-article-buffer
7266     (widen)
7267     (when (gnus-visual-p 'page-marker)
7268       (let ((buffer-read-only nil))
7269         (gnus-remove-text-with-property 'gnus-prev)
7270         (gnus-remove-text-with-property 'gnus-next))
7271       (setq gnus-page-broken nil))))
7272
7273 (defun gnus-summary-move-article (&optional n to-newsgroup
7274                                             select-method action)
7275   "Move the current article to a different newsgroup.
7276 If N is a positive number, move the N next articles.
7277 If N is a negative number, move the N previous articles.
7278 If N is nil and any articles have been marked with the process mark,
7279 move those articles instead.
7280 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7281 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7282 re-spool using this method.
7283
7284 For this function to work, both the current newsgroup and the
7285 newsgroup that you want to move to have to support the `request-move'
7286 and `request-accept' functions.
7287
7288 ACTION can be either `move' (the default), `crosspost' or `copy'."
7289   (interactive "P")
7290   (unless action
7291     (setq action 'move))
7292   ;; Disable marking as read.
7293   (let (gnus-mark-article-hook)
7294     (save-window-excursion
7295       (gnus-summary-select-article)))
7296   ;; Check whether the source group supports the required functions.
7297   (cond ((and (eq action 'move)
7298               (not (gnus-check-backend-function
7299                     'request-move-article gnus-newsgroup-name)))
7300          (error "The current group does not support article moving"))
7301         ((and (eq action 'crosspost)
7302               (not (gnus-check-backend-function
7303                     'request-replace-article gnus-newsgroup-name)))
7304          (error "The current group does not support article editing")))
7305   (let ((articles (gnus-summary-work-articles n))
7306         (prefix (if (gnus-check-backend-function
7307                     'request-move-article gnus-newsgroup-name)
7308                     (gnus-group-real-prefix gnus-newsgroup-name)
7309                   ""))
7310         (names '((move "Move" "Moving")
7311                  (copy "Copy" "Copying")
7312                  (crosspost "Crosspost" "Crossposting")))
7313         (copy-buf (save-excursion
7314                     (nnheader-set-temp-buffer " *copy article*")))
7315         art-group to-method new-xref article to-groups)
7316     (unless (assq action names)
7317       (error "Unknown action %s" action))
7318     ;; Read the newsgroup name.
7319     (when (and (not to-newsgroup)
7320                (not select-method))
7321       (setq to-newsgroup
7322             (gnus-read-move-group-name
7323              (cadr (assq action names))
7324              (symbol-value (intern (format "gnus-current-%s-group" action)))
7325              articles prefix))
7326       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7327     (setq to-method (or select-method
7328                         (gnus-group-name-to-method to-newsgroup)))
7329     ;; Check the method we are to move this article to...
7330     (unless (gnus-check-backend-function
7331              'request-accept-article (car to-method))
7332       (error "%s does not support article copying" (car to-method)))
7333     (unless (gnus-check-server to-method)
7334       (error "Can't open server %s" (car to-method)))
7335     (gnus-message 6 "%s to %s: %s..."
7336                   (caddr (assq action names))
7337                   (or (car select-method) to-newsgroup) articles)
7338     (while articles
7339       (setq article (pop articles))
7340       (setq
7341        art-group
7342        (cond
7343         ;; Move the article.
7344         ((eq action 'move)
7345          ;; Remove this article from future suppression.
7346          (gnus-dup-unsuppress-article article)
7347          (gnus-request-move-article
7348           article                       ; Article to move
7349           gnus-newsgroup-name           ; From newsgroup
7350           (nth 1 (gnus-find-method-for-group
7351                   gnus-newsgroup-name)) ; Server
7352           (list 'gnus-request-accept-article
7353                 to-newsgroup (list 'quote select-method)
7354                 (not articles) t)       ; Accept form
7355           (not articles)))              ; Only save nov last time
7356         ;; Copy the article.
7357         ((eq action 'copy)
7358          (save-excursion
7359            (set-buffer copy-buf)
7360            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7361              (gnus-request-accept-article
7362               to-newsgroup select-method (not articles) t))))
7363         ;; Crosspost the article.
7364         ((eq action 'crosspost)
7365          (let ((xref (message-tokenize-header
7366                       (mail-header-xref (gnus-summary-article-header article))
7367                       " ")))
7368            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7369                                   ":" article))
7370            (unless xref
7371              (setq xref (list (system-name))))
7372            (setq new-xref
7373                  (concat
7374                   (mapconcat 'identity
7375                              (delete "Xref:" (delete new-xref xref))
7376                              " ")
7377                   " " new-xref))
7378            (save-excursion
7379              (set-buffer copy-buf)
7380              ;; First put the article in the destination group.
7381              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7382              (when (consp (setq art-group
7383                                 (gnus-request-accept-article
7384                                  to-newsgroup select-method (not articles))))
7385                (setq new-xref (concat new-xref " " (car art-group)
7386                                       ":" (cdr art-group)))
7387                ;; Now we have the new Xrefs header, so we insert
7388                ;; it and replace the new article.
7389                (nnheader-replace-header "Xref" new-xref)
7390                (gnus-request-replace-article
7391                 (cdr art-group) to-newsgroup (current-buffer))
7392                art-group))))))
7393       (cond
7394        ((not art-group)
7395         (gnus-message 1 "Couldn't %s article %s: %s"
7396                       (cadr (assq action names)) article
7397                       (nnheader-get-report (car to-method))))
7398        ((and (eq art-group 'junk)
7399              (eq action 'move))
7400         (gnus-summary-mark-article article gnus-canceled-mark)
7401         (gnus-message 4 "Deleted article %s" article))
7402        (t
7403         (let* ((pto-group (gnus-group-prefixed-name
7404                            (car art-group) to-method))
7405                (entry
7406                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7407                (info (nth 2 entry))
7408                (to-group (gnus-info-group info))
7409                to-marks)
7410           ;; Update the group that has been moved to.
7411           (when (and info
7412                      (memq action '(move copy)))
7413             (unless (member to-group to-groups)
7414               (push to-group to-groups))
7415
7416             (unless (memq article gnus-newsgroup-unreads)
7417               (push 'read to-marks)
7418               (gnus-info-set-read
7419                info (gnus-add-to-range (gnus-info-read info)
7420                                        (list (cdr art-group)))))
7421
7422             ;; See whether the article is to be put in the cache.
7423             (let ((marks gnus-article-mark-lists)
7424                   (to-article (cdr art-group)))
7425
7426               ;; Enter the article into the cache in the new group,
7427               ;; if that is required.
7428               (when gnus-use-cache
7429                 (gnus-cache-possibly-enter-article
7430                  to-group to-article
7431                  (memq article gnus-newsgroup-marked)
7432                  (memq article gnus-newsgroup-dormant)
7433                  (memq article gnus-newsgroup-unreads)))
7434
7435               (when gnus-preserve-marks 
7436                 ;; Copy any marks over to the new group.
7437                 (when (and (equal to-group gnus-newsgroup-name)
7438                            (not (memq article gnus-newsgroup-unreads)))
7439                   ;; Mark this article as read in this group.
7440                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7441                   (setcdr (gnus-active to-group) to-article)
7442                   (setcdr gnus-newsgroup-active to-article))
7443
7444                 (while marks
7445                   (when (memq article (symbol-value
7446                                        (intern (format "gnus-newsgroup-%s"
7447                                                        (caar marks)))))
7448                     (push (cdar marks) to-marks)
7449                     ;; If the other group is the same as this group,
7450                     ;; then we have to add the mark to the list.
7451                     (when (equal to-group gnus-newsgroup-name)
7452                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7453                            (cons to-article
7454                                  (symbol-value
7455                                   (intern (format "gnus-newsgroup-%s"
7456                                                   (caar marks)))))))
7457                     ;; Copy the marks to other group.
7458                     (gnus-add-marked-articles
7459                      to-group (cdar marks) (list to-article) info))
7460                   (setq marks (cdr marks)))
7461
7462                 (gnus-request-set-mark to-group (list (list (list to-article)
7463                                                             'set
7464                                                             to-marks))))
7465
7466               (gnus-dribble-enter
7467                (concat "(gnus-group-set-info '"
7468                        (gnus-prin1-to-string (gnus-get-info to-group))
7469                        ")"))))
7470
7471           ;; Update the Xref header in this article to point to
7472           ;; the new crossposted article we have just created.
7473           (when (eq action 'crosspost)
7474             (save-excursion
7475               (set-buffer copy-buf)
7476               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7477               (nnheader-replace-header "Xref" new-xref)
7478               (gnus-request-replace-article
7479                article gnus-newsgroup-name (current-buffer)))))
7480
7481         ;;;!!!Why is this necessary?
7482         (set-buffer gnus-summary-buffer)
7483
7484         (gnus-summary-goto-subject article)
7485         (when (eq action 'move)
7486           (gnus-summary-mark-article article gnus-canceled-mark))))
7487       (gnus-summary-remove-process-mark article))
7488     ;; Re-activate all groups that have been moved to.
7489     (while to-groups
7490       (save-excursion
7491         (set-buffer gnus-group-buffer)
7492         (when (gnus-group-goto-group (car to-groups) t)
7493           (gnus-group-get-new-news-this-group 1 t))
7494         (pop to-groups)))
7495
7496     (gnus-kill-buffer copy-buf)
7497     (gnus-summary-position-point)
7498     (gnus-set-mode-line 'summary)))
7499
7500 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7501   "Move the current article to a different newsgroup.
7502 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7503 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7504 re-spool using this method."
7505   (interactive "P")
7506   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7507
7508 (defun gnus-summary-crosspost-article (&optional n)
7509   "Crosspost the current article to some other group."
7510   (interactive "P")
7511   (gnus-summary-move-article n nil nil 'crosspost))
7512
7513 (defcustom gnus-summary-respool-default-method nil
7514   "Default method for respooling an article.
7515 If nil, use to the current newsgroup method."
7516   :type '(choice (gnus-select-method :value (nnml ""))
7517                  (const nil))
7518   :group 'gnus-summary-mail)
7519
7520 (defun gnus-summary-respool-article (&optional n method)
7521   "Respool the current article.
7522 The article will be squeezed through the mail spooling process again,
7523 which means that it will be put in some mail newsgroup or other
7524 depending on `nnmail-split-methods'.
7525 If N is a positive number, respool the N next articles.
7526 If N is a negative number, respool the N previous articles.
7527 If N is nil and any articles have been marked with the process mark,
7528 respool those articles instead.
7529
7530 Respooling can be done both from mail groups and \"real\" newsgroups.
7531 In the former case, the articles in question will be moved from the
7532 current group into whatever groups they are destined to.  In the
7533 latter case, they will be copied into the relevant groups."
7534   (interactive
7535    (list current-prefix-arg
7536          (let* ((methods (gnus-methods-using 'respool))
7537                 (methname
7538                  (symbol-name (or gnus-summary-respool-default-method
7539                                   (car (gnus-find-method-for-group
7540                                         gnus-newsgroup-name)))))
7541                 (method
7542                  (gnus-completing-read
7543                   methname "What backend do you want to use when respooling?"
7544                   methods nil t nil 'gnus-mail-method-history))
7545                 ms)
7546            (cond
7547             ((zerop (length (setq ms (gnus-servers-using-backend
7548                                       (intern method)))))
7549              (list (intern method) ""))
7550             ((= 1 (length ms))
7551              (car ms))
7552             (t
7553              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7554                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7555                            ms-alist))))))))
7556   (unless method
7557     (error "No method given for respooling"))
7558   (if (assoc (symbol-name
7559               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7560              (gnus-methods-using 'respool))
7561       (gnus-summary-move-article n nil method)
7562     (gnus-summary-copy-article n nil method)))
7563
7564 (defun gnus-summary-import-article (file)
7565   "Import an arbitrary file into a mail newsgroup."
7566   (interactive "fImport file: ")
7567   (let ((group gnus-newsgroup-name)
7568         (now (current-time))
7569         atts lines)
7570     (unless (gnus-check-backend-function 'request-accept-article group)
7571       (error "%s does not support article importing" group))
7572     (or (file-readable-p file)
7573         (not (file-regular-p file))
7574         (error "Can't read %s" file))
7575     (save-excursion
7576       (set-buffer (gnus-get-buffer-create " *import file*"))
7577       (erase-buffer)
7578       (insert-file-contents file)
7579       (goto-char (point-min))
7580       (unless (nnheader-article-p)
7581         ;; This doesn't look like an article, so we fudge some headers.
7582         (setq atts (file-attributes file)
7583               lines (count-lines (point-min) (point-max)))
7584         (insert "From: " (read-string "From: ") "\n"
7585                 "Subject: " (read-string "Subject: ") "\n"
7586                 "Date: " (message-make-date (nth 5 atts))
7587                 "\n"
7588                 "Message-ID: " (message-make-message-id) "\n"
7589                 "Lines: " (int-to-string lines) "\n"
7590                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7591       (gnus-request-accept-article group nil t)
7592       (kill-buffer (current-buffer)))))
7593
7594 (defun gnus-summary-article-posted-p ()
7595   "Say whether the current (mail) article is available from news as well.
7596 This will be the case if the article has both been mailed and posted."
7597   (interactive)
7598   (let ((id (mail-header-references (gnus-summary-article-header)))
7599         (gnus-override-method (car (gnus-refer-article-methods))))
7600     (if (gnus-request-head id "")
7601         (gnus-message 2 "The current message was found on %s"
7602                       gnus-override-method)
7603       (gnus-message 2 "The current message couldn't be found on %s"
7604                     gnus-override-method)
7605       nil)))
7606
7607 (defun gnus-summary-expire-articles (&optional now)
7608   "Expire all articles that are marked as expirable in the current group."
7609   (interactive)
7610   (when (gnus-check-backend-function
7611          'request-expire-articles gnus-newsgroup-name)
7612     ;; This backend supports expiry.
7613     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7614            (expirable (if total
7615                           (progn
7616                             ;; We need to update the info for
7617                             ;; this group for `gnus-list-of-read-articles'
7618                             ;; to give us the right answer.
7619                             (gnus-run-hooks 'gnus-exit-group-hook)
7620                             (gnus-summary-update-info)
7621                             (gnus-list-of-read-articles gnus-newsgroup-name))
7622                         (setq gnus-newsgroup-expirable
7623                               (sort gnus-newsgroup-expirable '<))))
7624            (expiry-wait (if now 'immediate
7625                           (gnus-group-find-parameter
7626                            gnus-newsgroup-name 'expiry-wait)))
7627            es)
7628       (when expirable
7629         ;; There are expirable articles in this group, so we run them
7630         ;; through the expiry process.
7631         (gnus-message 6 "Expiring articles...")
7632         (unless (gnus-check-group gnus-newsgroup-name)
7633           (error "Can't open server for %s" gnus-newsgroup-name))
7634         ;; The list of articles that weren't expired is returned.
7635         (save-excursion
7636           (if expiry-wait
7637               (let ((nnmail-expiry-wait-function nil)
7638                     (nnmail-expiry-wait expiry-wait))
7639                 (setq es (gnus-request-expire-articles
7640                           expirable gnus-newsgroup-name)))
7641             (setq es (gnus-request-expire-articles
7642                       expirable gnus-newsgroup-name))))
7643         (unless total
7644           (setq gnus-newsgroup-expirable es))
7645         ;; We go through the old list of expirable, and mark all
7646         ;; really expired articles as nonexistent.
7647         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7648           (let ((gnus-use-cache nil))
7649             (while expirable
7650               (unless (memq (car expirable) es)
7651                 (when (gnus-data-find (car expirable))
7652                   (gnus-summary-mark-article
7653                    (car expirable) gnus-canceled-mark)))
7654               (setq expirable (cdr expirable)))))
7655         (gnus-message 6 "Expiring articles...done")))))
7656
7657 (defun gnus-summary-expire-articles-now ()
7658   "Expunge all expirable articles in the current group.
7659 This means that *all* articles that are marked as expirable will be
7660 deleted forever, right now."
7661   (interactive)
7662   (or gnus-expert-user
7663       (gnus-yes-or-no-p
7664        "Are you really, really, really sure you want to delete all these messages? ")
7665       (error "Phew!"))
7666   (gnus-summary-expire-articles t))
7667
7668 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7669 (defun gnus-summary-delete-article (&optional n)
7670   "Delete the N next (mail) articles.
7671 This command actually deletes articles.  This is not a marking
7672 command.  The article will disappear forever from your life, never to
7673 return.
7674 If N is negative, delete backwards.
7675 If N is nil and articles have been marked with the process mark,
7676 delete these instead."
7677   (interactive "P")
7678   (unless (gnus-check-backend-function 'request-expire-articles
7679                                        gnus-newsgroup-name)
7680     (error "The current newsgroup does not support article deletion"))
7681   ;; Compute the list of articles to delete.
7682   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7683         not-deleted)
7684     (if (and gnus-novice-user
7685              (not (gnus-yes-or-no-p
7686                    (format "Do you really want to delete %s forever? "
7687                            (if (> (length articles) 1)
7688                                (format "these %s articles" (length articles))
7689                              "this article")))))
7690         ()
7691       ;; Delete the articles.
7692       (setq not-deleted (gnus-request-expire-articles
7693                          articles gnus-newsgroup-name 'force))
7694       (while articles
7695         (gnus-summary-remove-process-mark (car articles))
7696         ;; The backend might not have been able to delete the article
7697         ;; after all.
7698         (unless (memq (car articles) not-deleted)
7699           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7700         (setq articles (cdr articles)))
7701       (when not-deleted
7702         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7703     (gnus-summary-position-point)
7704     (gnus-set-mode-line 'summary)
7705     not-deleted))
7706
7707 (defun gnus-summary-edit-article (&optional force)
7708   "Edit the current article.
7709 This will have permanent effect only in mail groups.
7710 If FORCE is non-nil, allow editing of articles even in read-only
7711 groups."
7712   (interactive "P")
7713   (save-excursion
7714     (set-buffer gnus-summary-buffer)
7715     (let ((mail-parse-charset gnus-newsgroup-charset)
7716           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7717       (gnus-set-global-variables)
7718       (when (and (not force)
7719                  (gnus-group-read-only-p))
7720         (error "The current newsgroup does not support article editing"))
7721       (gnus-summary-show-article t)
7722       (gnus-article-edit-article
7723        'mime-to-mml
7724        `(lambda (no-highlight)
7725           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7726                 (mail-parse-ignored-charsets 
7727                  ',gnus-newsgroup-ignored-charsets))
7728             (mml-to-mime)
7729             (gnus-summary-edit-article-done
7730              ,(or (mail-header-references gnus-current-headers) "")
7731              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7732
7733 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7734
7735 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7736                                                  no-highlight)
7737   "Make edits to the current article permanent."
7738   (interactive)
7739   ;; Replace the article.
7740   (let ((buf (current-buffer)))
7741     (with-temp-buffer
7742       (insert-buffer-substring buf)
7743       (if (and (not read-only)
7744                (not (gnus-request-replace-article
7745                      (cdr gnus-article-current) (car gnus-article-current)
7746                      (current-buffer) t)))
7747           (error "Couldn't replace article")
7748         ;; Update the summary buffer.
7749         (if (and references
7750                  (equal (message-tokenize-header references " ")
7751                         (message-tokenize-header
7752                          (or (message-fetch-field "references") "") " ")))
7753             ;; We only have to update this line.
7754             (save-excursion
7755               (save-restriction
7756                 (message-narrow-to-head)
7757                 (let ((head (buffer-string))
7758                       header)
7759                   (with-temp-buffer
7760                     (insert (format "211 %d Article retrieved.\n"
7761                                     (cdr gnus-article-current)))
7762                     (insert head)
7763                     (insert ".\n")
7764                     (let ((nntp-server-buffer (current-buffer)))
7765                       (setq header (car (gnus-get-newsgroup-headers
7766                                          (save-excursion
7767                                            (set-buffer gnus-summary-buffer)
7768                                            gnus-newsgroup-dependencies)
7769                                          t))))
7770                     (save-excursion
7771                       (set-buffer gnus-summary-buffer)
7772                       (gnus-data-set-header
7773                        (gnus-data-find (cdr gnus-article-current))
7774                        header)
7775                       (gnus-summary-update-article-line
7776                        (cdr gnus-article-current) header))))))
7777           ;; Update threads.
7778           (set-buffer (or buffer gnus-summary-buffer))
7779           (gnus-summary-update-article (cdr gnus-article-current)))
7780         ;; Prettify the article buffer again.
7781         (unless no-highlight
7782           (save-excursion
7783             (set-buffer gnus-article-buffer)
7784             ;;;!!! Fix this -- article should be rehighlighted.
7785             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7786             (set-buffer gnus-original-article-buffer)
7787             (gnus-request-article
7788              (cdr gnus-article-current)
7789              (car gnus-article-current) (current-buffer))))
7790         ;; Prettify the summary buffer line.
7791         (when (gnus-visual-p 'summary-highlight 'highlight)
7792           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7793
7794 (defun gnus-summary-edit-wash (key)
7795   "Perform editing command KEY in the article buffer."
7796   (interactive
7797    (list
7798     (progn
7799       (message "%s" (concat (this-command-keys) "- "))
7800       (read-char))))
7801   (message "")
7802   (gnus-summary-edit-article)
7803   (execute-kbd-macro (concat (this-command-keys) key))
7804   (gnus-article-edit-done))
7805
7806 ;;; Respooling
7807
7808 (defun gnus-summary-respool-query (&optional silent trace)
7809   "Query where the respool algorithm would put this article."
7810   (interactive)
7811   (let (gnus-mark-article-hook)
7812     (gnus-summary-select-article)
7813     (save-excursion
7814       (set-buffer gnus-original-article-buffer)
7815       (save-restriction
7816         (message-narrow-to-head)
7817         (let ((groups (nnmail-article-group 'identity trace)))
7818           (unless silent
7819             (if groups
7820                 (message "This message would go to %s"
7821                          (mapconcat 'car groups ", "))
7822               (message "This message would go to no groups"))
7823             groups))))))
7824
7825 (defun gnus-summary-respool-trace ()
7826   "Trace where the respool algorithm would put this article.
7827 Display a buffer showing all fancy splitting patterns which matched."
7828   (interactive)
7829   (gnus-summary-respool-query nil t))
7830
7831 ;; Summary marking commands.
7832
7833 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7834   "Mark articles which has the same subject as read, and then select the next.
7835 If UNMARK is positive, remove any kind of mark.
7836 If UNMARK is negative, tick articles."
7837   (interactive "P")
7838   (when unmark
7839     (setq unmark (prefix-numeric-value unmark)))
7840   (let ((count
7841          (gnus-summary-mark-same-subject
7842           (gnus-summary-article-subject) unmark)))
7843     ;; Select next unread article.  If auto-select-same mode, should
7844     ;; select the first unread article.
7845     (gnus-summary-next-article t (and gnus-auto-select-same
7846                                       (gnus-summary-article-subject)))
7847     (gnus-message 7 "%d article%s marked as %s"
7848                   count (if (= count 1) " is" "s are")
7849                   (if unmark "unread" "read"))))
7850
7851 (defun gnus-summary-kill-same-subject (&optional unmark)
7852   "Mark articles which has the same subject as read.
7853 If UNMARK is positive, remove any kind of mark.
7854 If UNMARK is negative, tick articles."
7855   (interactive "P")
7856   (when unmark
7857     (setq unmark (prefix-numeric-value unmark)))
7858   (let ((count
7859          (gnus-summary-mark-same-subject
7860           (gnus-summary-article-subject) unmark)))
7861     ;; If marked as read, go to next unread subject.
7862     (when (null unmark)
7863       ;; Go to next unread subject.
7864       (gnus-summary-next-subject 1 t))
7865     (gnus-message 7 "%d articles are marked as %s"
7866                   count (if unmark "unread" "read"))))
7867
7868 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7869   "Mark articles with same SUBJECT as read, and return marked number.
7870 If optional argument UNMARK is positive, remove any kinds of marks.
7871 If optional argument UNMARK is negative, mark articles as unread instead."
7872   (let ((count 1))
7873     (save-excursion
7874       (cond
7875        ((null unmark)                   ; Mark as read.
7876         (while (and
7877                 (progn
7878                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7879                   (gnus-summary-show-thread) t)
7880                 (gnus-summary-find-subject subject))
7881           (setq count (1+ count))))
7882        ((> unmark 0)                    ; Tick.
7883         (while (and
7884                 (progn
7885                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7886                   (gnus-summary-show-thread) t)
7887                 (gnus-summary-find-subject subject))
7888           (setq count (1+ count))))
7889        (t                               ; Mark as unread.
7890         (while (and
7891                 (progn
7892                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7893                   (gnus-summary-show-thread) t)
7894                 (gnus-summary-find-subject subject))
7895           (setq count (1+ count)))))
7896       (gnus-set-mode-line 'summary)
7897       ;; Return the number of marked articles.
7898       count)))
7899
7900 (defun gnus-summary-mark-as-processable (n &optional unmark)
7901   "Set the process mark on the next N articles.
7902 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7903 the process mark instead.  The difference between N and the actual
7904 number of articles marked is returned."
7905   (interactive "p")
7906   (let ((backward (< n 0))
7907         (n (abs n)))
7908     (while (and
7909             (> n 0)
7910             (if unmark
7911                 (gnus-summary-remove-process-mark
7912                  (gnus-summary-article-number))
7913               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7914             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7915       (setq n (1- n)))
7916     (when (/= 0 n)
7917       (gnus-message 7 "No more articles"))
7918     (gnus-summary-recenter)
7919     (gnus-summary-position-point)
7920     n))
7921
7922 (defun gnus-summary-unmark-as-processable (n)
7923   "Remove the process mark from the next N articles.
7924 If N is negative, unmark backward instead.  The difference between N and
7925 the actual number of articles unmarked is returned."
7926   (interactive "p")
7927   (gnus-summary-mark-as-processable n t))
7928
7929 (defun gnus-summary-unmark-all-processable ()
7930   "Remove the process mark from all articles."
7931   (interactive)
7932   (save-excursion
7933     (while gnus-newsgroup-processable
7934       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7935   (gnus-summary-position-point))
7936
7937 (defun gnus-summary-mark-as-expirable (n)
7938   "Mark N articles forward as expirable.
7939 If N is negative, mark backward instead.  The difference between N and
7940 the actual number of articles marked is returned."
7941   (interactive "p")
7942   (gnus-summary-mark-forward n gnus-expirable-mark))
7943
7944 (defun gnus-summary-mark-article-as-replied (article)
7945   "Mark ARTICLE replied and update the summary line."
7946   (push article gnus-newsgroup-replied)
7947   (let ((buffer-read-only nil))
7948     (when (gnus-summary-goto-subject article nil t)
7949       (gnus-summary-update-secondary-mark article))))
7950
7951 (defun gnus-summary-set-bookmark (article)
7952   "Set a bookmark in current article."
7953   (interactive (list (gnus-summary-article-number)))
7954   (when (or (not (get-buffer gnus-article-buffer))
7955             (not gnus-current-article)
7956             (not gnus-article-current)
7957             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7958     (error "No current article selected"))
7959   ;; Remove old bookmark, if one exists.
7960   (let ((old (assq article gnus-newsgroup-bookmarks)))
7961     (when old
7962       (setq gnus-newsgroup-bookmarks
7963             (delq old gnus-newsgroup-bookmarks))))
7964   ;; Set the new bookmark, which is on the form
7965   ;; (article-number . line-number-in-body).
7966   (push
7967    (cons article
7968          (save-excursion
7969            (set-buffer gnus-article-buffer)
7970            (count-lines
7971             (min (point)
7972                  (save-excursion
7973                    (goto-char (point-min))
7974                    (search-forward "\n\n" nil t)
7975                    (point)))
7976             (point))))
7977    gnus-newsgroup-bookmarks)
7978   (gnus-message 6 "A bookmark has been added to the current article."))
7979
7980 (defun gnus-summary-remove-bookmark (article)
7981   "Remove the bookmark from the current article."
7982   (interactive (list (gnus-summary-article-number)))
7983   ;; Remove old bookmark, if one exists.
7984   (let ((old (assq article gnus-newsgroup-bookmarks)))
7985     (if old
7986         (progn
7987           (setq gnus-newsgroup-bookmarks
7988                 (delq old gnus-newsgroup-bookmarks))
7989           (gnus-message 6 "Removed bookmark."))
7990       (gnus-message 6 "No bookmark in current article."))))
7991
7992 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7993 (defun gnus-summary-mark-as-dormant (n)
7994   "Mark N articles forward as dormant.
7995 If N is negative, mark backward instead.  The difference between N and
7996 the actual number of articles marked is returned."
7997   (interactive "p")
7998   (gnus-summary-mark-forward n gnus-dormant-mark))
7999
8000 (defun gnus-summary-set-process-mark (article)
8001   "Set the process mark on ARTICLE and update the summary line."
8002   (setq gnus-newsgroup-processable
8003         (cons article
8004               (delq article gnus-newsgroup-processable)))
8005   (when (gnus-summary-goto-subject article)
8006     (gnus-summary-show-thread)
8007     (gnus-summary-goto-subject article)
8008     (gnus-summary-update-secondary-mark article)))
8009
8010 (defun gnus-summary-remove-process-mark (article)
8011   "Remove the process mark from ARTICLE and update the summary line."
8012   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8013   (when (gnus-summary-goto-subject article)
8014     (gnus-summary-show-thread)
8015     (gnus-summary-goto-subject article)
8016     (gnus-summary-update-secondary-mark article)))
8017
8018 (defun gnus-summary-set-saved-mark (article)
8019   "Set the process mark on ARTICLE and update the summary line."
8020   (push article gnus-newsgroup-saved)
8021   (when (gnus-summary-goto-subject article)
8022     (gnus-summary-update-secondary-mark article)))
8023
8024 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8025   "Mark N articles as read forwards.
8026 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8027 The difference between N and the actual number of articles marked is
8028 returned.
8029 Iff NO-EXPIRE, auto-expiry will be inhibited."
8030   (interactive "p")
8031   (gnus-summary-show-thread)
8032   (let ((backward (< n 0))
8033         (gnus-summary-goto-unread
8034          (and gnus-summary-goto-unread
8035               (not (eq gnus-summary-goto-unread 'never))
8036               (not (memq mark (list gnus-unread-mark
8037                                     gnus-ticked-mark gnus-dormant-mark)))))
8038         (n (abs n))
8039         (mark (or mark gnus-del-mark)))
8040     (while (and (> n 0)
8041                 (gnus-summary-mark-article nil mark no-expire)
8042                 (zerop (gnus-summary-next-subject
8043                         (if backward -1 1)
8044                         (and gnus-summary-goto-unread
8045                              (not (eq gnus-summary-goto-unread 'never)))
8046                         t)))
8047       (setq n (1- n)))
8048     (when (/= 0 n)
8049       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8050     (gnus-summary-recenter)
8051     (gnus-summary-position-point)
8052     (gnus-set-mode-line 'summary)
8053     n))
8054
8055 (defun gnus-summary-mark-article-as-read (mark)
8056   "Mark the current article quickly as read with MARK."
8057   (let ((article (gnus-summary-article-number)))
8058     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8059     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8060     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8061     (push (cons article mark) gnus-newsgroup-reads)
8062     ;; Possibly remove from cache, if that is used.
8063     (when gnus-use-cache
8064       (gnus-cache-enter-remove-article article))
8065     ;; Allow the backend to change the mark.
8066     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8067     ;; Check for auto-expiry.
8068     (when (and gnus-newsgroup-auto-expire
8069                (memq mark gnus-auto-expirable-marks))
8070       (setq mark gnus-expirable-mark)
8071       ;; Let the backend know about the mark change.
8072       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8073       (push article gnus-newsgroup-expirable))
8074     ;; Set the mark in the buffer.
8075     (gnus-summary-update-mark mark 'unread)
8076     t))
8077
8078 (defun gnus-summary-mark-article-as-unread (mark)
8079   "Mark the current article quickly as unread with MARK."
8080   (let* ((article (gnus-summary-article-number))
8081          (old-mark (gnus-summary-article-mark article)))
8082     ;; Allow the backend to change the mark.
8083     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8084     (if (eq mark old-mark)
8085         t
8086       (if (<= article 0)
8087           (progn
8088             (gnus-error 1 "Can't mark negative article numbers")
8089             nil)
8090         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8091         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8092         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8093         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8094         (cond ((= mark gnus-ticked-mark)
8095                (push article gnus-newsgroup-marked))
8096               ((= mark gnus-dormant-mark)
8097                (push article gnus-newsgroup-dormant))
8098               (t
8099                (push article gnus-newsgroup-unreads)))
8100         (gnus-pull article gnus-newsgroup-reads)
8101
8102         ;; See whether the article is to be put in the cache.
8103         (and gnus-use-cache
8104              (vectorp (gnus-summary-article-header article))
8105              (save-excursion
8106                (gnus-cache-possibly-enter-article
8107                 gnus-newsgroup-name article
8108                 (= mark gnus-ticked-mark)
8109                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8110
8111         ;; Fix the mark.
8112         (gnus-summary-update-mark mark 'unread)
8113         t))))
8114
8115 (defun gnus-summary-mark-article (&optional article mark no-expire)
8116   "Mark ARTICLE with MARK.  MARK can be any character.
8117 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8118 `??' (dormant) and `?E' (expirable).
8119 If MARK is nil, then the default character `?r' is used.
8120 If ARTICLE is nil, then the article on the current line will be
8121 marked.
8122 Iff NO-EXPIRE, auto-expiry will be inhibited."
8123   ;; The mark might be a string.
8124   (when (stringp mark)
8125     (setq mark (aref mark 0)))
8126   ;; If no mark is given, then we check auto-expiring.
8127   (when (null mark)
8128     (setq mark gnus-del-mark))
8129   (when (and (not no-expire)
8130              gnus-newsgroup-auto-expire
8131              (memq mark gnus-auto-expirable-marks))
8132     (setq mark gnus-expirable-mark))
8133   (let ((article (or article (gnus-summary-article-number)))
8134         (old-mark (gnus-summary-article-mark article)))
8135     ;; Allow the backend to change the mark.
8136     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8137     (if (eq mark old-mark)
8138         t
8139       (unless article
8140         (error "No article on current line"))
8141       (if (not (if (or (= mark gnus-unread-mark)
8142                        (= mark gnus-ticked-mark)
8143                        (= mark gnus-dormant-mark))
8144                    (gnus-mark-article-as-unread article mark)
8145                  (gnus-mark-article-as-read article mark)))
8146           t
8147         ;; See whether the article is to be put in the cache.
8148         (and gnus-use-cache
8149              (not (= mark gnus-canceled-mark))
8150              (vectorp (gnus-summary-article-header article))
8151              (save-excursion
8152                (gnus-cache-possibly-enter-article
8153                 gnus-newsgroup-name article
8154                 (= mark gnus-ticked-mark)
8155                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8156
8157         (when (gnus-summary-goto-subject article nil t)
8158           (let ((buffer-read-only nil))
8159             (gnus-summary-show-thread)
8160             ;; Fix the mark.
8161             (gnus-summary-update-mark mark 'unread)
8162             t))))))
8163
8164 (defun gnus-summary-update-secondary-mark (article)
8165   "Update the secondary (read, process, cache) mark."
8166   (gnus-summary-update-mark
8167    (cond ((memq article gnus-newsgroup-processable)
8168           gnus-process-mark)
8169          ((memq article gnus-newsgroup-cached)
8170           gnus-cached-mark)
8171          ((memq article gnus-newsgroup-replied)
8172           gnus-replied-mark)
8173          ((memq article gnus-newsgroup-saved)
8174           gnus-saved-mark)
8175          (t gnus-unread-mark))
8176    'replied)
8177   (when (gnus-visual-p 'summary-highlight 'highlight)
8178     (gnus-run-hooks 'gnus-summary-update-hook))
8179   t)
8180
8181 (defun gnus-summary-update-mark (mark type)
8182   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8183         (buffer-read-only nil))
8184     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8185     (when forward
8186       (when (looking-at "\r")
8187         (incf forward))
8188       (when (<= (+ forward (point)) (point-max))
8189         ;; Go to the right position on the line.
8190         (goto-char (+ forward (point)))
8191         ;; Replace the old mark with the new mark.
8192         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8193         ;; Optionally update the marks by some user rule.
8194         (when (eq type 'unread)
8195           (gnus-data-set-mark
8196            (gnus-data-find (gnus-summary-article-number)) mark)
8197           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8198
8199 (defun gnus-mark-article-as-read (article &optional mark)
8200   "Enter ARTICLE in the pertinent lists and remove it from others."
8201   ;; Make the article expirable.
8202   (let ((mark (or mark gnus-del-mark)))
8203     (if (= mark gnus-expirable-mark)
8204         (push article gnus-newsgroup-expirable)
8205       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8206     ;; Remove from unread and marked lists.
8207     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8208     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8209     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8210     (push (cons article mark) gnus-newsgroup-reads)
8211     ;; Possibly remove from cache, if that is used.
8212     (when gnus-use-cache
8213       (gnus-cache-enter-remove-article article))
8214     t))
8215
8216 (defun gnus-mark-article-as-unread (article &optional mark)
8217   "Enter ARTICLE in the pertinent lists and remove it from others."
8218   (let ((mark (or mark gnus-ticked-mark)))
8219     (if (<= article 0)
8220         (progn
8221           (gnus-error 1 "Can't mark negative article numbers")
8222           nil)
8223       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8224             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8225             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8226             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8227
8228       ;; Unsuppress duplicates?
8229       (when gnus-suppress-duplicates
8230         (gnus-dup-unsuppress-article article))
8231
8232       (cond ((= mark gnus-ticked-mark)
8233              (push article gnus-newsgroup-marked))
8234             ((= mark gnus-dormant-mark)
8235              (push article gnus-newsgroup-dormant))
8236             (t
8237              (push article gnus-newsgroup-unreads)))
8238       (gnus-pull article gnus-newsgroup-reads)
8239       t)))
8240
8241 (defalias 'gnus-summary-mark-as-unread-forward
8242   'gnus-summary-tick-article-forward)
8243 (make-obsolete 'gnus-summary-mark-as-unread-forward
8244                'gnus-summary-tick-article-forward)
8245 (defun gnus-summary-tick-article-forward (n)
8246   "Tick N articles forwards.
8247 If N is negative, tick backwards instead.
8248 The difference between N and the number of articles ticked is returned."
8249   (interactive "p")
8250   (gnus-summary-mark-forward n gnus-ticked-mark))
8251
8252 (defalias 'gnus-summary-mark-as-unread-backward
8253   'gnus-summary-tick-article-backward)
8254 (make-obsolete 'gnus-summary-mark-as-unread-backward
8255                'gnus-summary-tick-article-backward)
8256 (defun gnus-summary-tick-article-backward (n)
8257   "Tick N articles backwards.
8258 The difference between N and the number of articles ticked is returned."
8259   (interactive "p")
8260   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8261
8262 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8263 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8264 (defun gnus-summary-tick-article (&optional article clear-mark)
8265   "Mark current article as unread.
8266 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8267 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8268   (interactive)
8269   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8270                                        gnus-ticked-mark)))
8271
8272 (defun gnus-summary-mark-as-read-forward (n)
8273   "Mark N articles as read forwards.
8274 If N is negative, mark backwards instead.
8275 The difference between N and the actual number of articles marked is
8276 returned."
8277   (interactive "p")
8278   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8279
8280 (defun gnus-summary-mark-as-read-backward (n)
8281   "Mark the N articles as read backwards.
8282 The difference between N and the actual number of articles marked is
8283 returned."
8284   (interactive "p")
8285   (gnus-summary-mark-forward
8286    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8287
8288 (defun gnus-summary-mark-as-read (&optional article mark)
8289   "Mark current article as read.
8290 ARTICLE specifies the article to be marked as read.
8291 MARK specifies a string to be inserted at the beginning of the line."
8292   (gnus-summary-mark-article article mark))
8293
8294 (defun gnus-summary-clear-mark-forward (n)
8295   "Clear marks from N articles forward.
8296 If N is negative, clear backward instead.
8297 The difference between N and the number of marks cleared is returned."
8298   (interactive "p")
8299   (gnus-summary-mark-forward n gnus-unread-mark))
8300
8301 (defun gnus-summary-clear-mark-backward (n)
8302   "Clear marks from N articles backward.
8303 The difference between N and the number of marks cleared is returned."
8304   (interactive "p")
8305   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8306
8307 (defun gnus-summary-mark-unread-as-read ()
8308   "Intended to be used by `gnus-summary-mark-article-hook'."
8309   (when (memq gnus-current-article gnus-newsgroup-unreads)
8310     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8311
8312 (defun gnus-summary-mark-read-and-unread-as-read ()
8313   "Intended to be used by `gnus-summary-mark-article-hook'."
8314   (let ((mark (gnus-summary-article-mark)))
8315     (when (or (gnus-unread-mark-p mark)
8316               (gnus-read-mark-p mark))
8317       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8318
8319 (defun gnus-summary-mark-region-as-read (point mark all)
8320   "Mark all unread articles between point and mark as read.
8321 If given a prefix, mark all articles between point and mark as read,
8322 even ticked and dormant ones."
8323   (interactive "r\nP")
8324   (save-excursion
8325     (let (article)
8326       (goto-char point)
8327       (beginning-of-line)
8328       (while (and
8329               (< (point) mark)
8330               (progn
8331                 (when (or all
8332                           (memq (setq article (gnus-summary-article-number))
8333                                 gnus-newsgroup-unreads))
8334                   (gnus-summary-mark-article article gnus-del-mark))
8335                 t)
8336               (gnus-summary-find-next))))))
8337
8338 (defun gnus-summary-mark-below (score mark)
8339   "Mark articles with score less than SCORE with MARK."
8340   (interactive "P\ncMark: ")
8341   (setq score (if score
8342                   (prefix-numeric-value score)
8343                 (or gnus-summary-default-score 0)))
8344   (save-excursion
8345     (set-buffer gnus-summary-buffer)
8346     (goto-char (point-min))
8347     (while
8348         (progn
8349           (and (< (gnus-summary-article-score) score)
8350                (gnus-summary-mark-article nil mark))
8351           (gnus-summary-find-next)))))
8352
8353 (defun gnus-summary-kill-below (&optional score)
8354   "Mark articles with score below SCORE as read."
8355   (interactive "P")
8356   (gnus-summary-mark-below score gnus-killed-mark))
8357
8358 (defun gnus-summary-clear-above (&optional score)
8359   "Clear all marks from articles with score above SCORE."
8360   (interactive "P")
8361   (gnus-summary-mark-above score gnus-unread-mark))
8362
8363 (defun gnus-summary-tick-above (&optional score)
8364   "Tick all articles with score above SCORE."
8365   (interactive "P")
8366   (gnus-summary-mark-above score gnus-ticked-mark))
8367
8368 (defun gnus-summary-mark-above (score mark)
8369   "Mark articles with score over SCORE with MARK."
8370   (interactive "P\ncMark: ")
8371   (setq score (if score
8372                   (prefix-numeric-value score)
8373                 (or gnus-summary-default-score 0)))
8374   (save-excursion
8375     (set-buffer gnus-summary-buffer)
8376     (goto-char (point-min))
8377     (while (and (progn
8378                   (when (> (gnus-summary-article-score) score)
8379                     (gnus-summary-mark-article nil mark))
8380                   t)
8381                 (gnus-summary-find-next)))))
8382
8383 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8384 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8385 (defun gnus-summary-limit-include-expunged (&optional no-error)
8386   "Display all the hidden articles that were expunged for low scores."
8387   (interactive)
8388   (let ((buffer-read-only nil))
8389     (let ((scored gnus-newsgroup-scored)
8390           headers h)
8391       (while scored
8392         (unless (gnus-summary-goto-subject (caar scored))
8393           (and (setq h (gnus-summary-article-header (caar scored)))
8394                (< (cdar scored) gnus-summary-expunge-below)
8395                (push h headers)))
8396         (setq scored (cdr scored)))
8397       (if (not headers)
8398           (when (not no-error)
8399             (error "No expunged articles hidden"))
8400         (goto-char (point-min))
8401         (gnus-summary-prepare-unthreaded (nreverse headers))
8402         (goto-char (point-min))
8403         (gnus-summary-position-point)
8404         t))))
8405
8406 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8407   "Mark all unread articles in this newsgroup as read.
8408 If prefix argument ALL is non-nil, ticked and dormant articles will
8409 also be marked as read.
8410 If QUIETLY is non-nil, no questions will be asked.
8411 If TO-HERE is non-nil, it should be a point in the buffer.  All
8412 articles before this point will be marked as read.
8413 Note that this function will only catch up the unread article
8414 in the current summary buffer limitation.
8415 The number of articles marked as read is returned."
8416   (interactive "P")
8417   (prog1
8418       (save-excursion
8419         (when (or quietly
8420                   (not gnus-interactive-catchup) ;Without confirmation?
8421                   gnus-expert-user
8422                   (gnus-y-or-n-p
8423                    (if all
8424                        "Mark absolutely all articles as read? "
8425                      "Mark all unread articles as read? ")))
8426           (if (and not-mark
8427                    (not gnus-newsgroup-adaptive)
8428                    (not gnus-newsgroup-auto-expire)
8429                    (not gnus-suppress-duplicates)
8430                    (or (not gnus-use-cache)
8431                        (eq gnus-use-cache 'passive)))
8432               (progn
8433                 (when all
8434                   (setq gnus-newsgroup-marked nil
8435                         gnus-newsgroup-dormant nil))
8436                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8437             ;; We actually mark all articles as canceled, which we
8438             ;; have to do when using auto-expiry or adaptive scoring.
8439             (gnus-summary-show-all-threads)
8440             (when (gnus-summary-first-subject (not all) t)
8441               (while (and
8442                       (if to-here (< (point) to-here) t)
8443                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8444                       (gnus-summary-find-next (not all) nil nil t))))
8445             (gnus-set-mode-line 'summary))
8446           t))
8447     (gnus-summary-position-point)))
8448
8449 (defun gnus-summary-catchup-to-here (&optional all)
8450   "Mark all unticked articles before the current one as read.
8451 If ALL is non-nil, also mark ticked and dormant articles as read."
8452   (interactive "P")
8453   (save-excursion
8454     (gnus-save-hidden-threads
8455       (let ((beg (point)))
8456         ;; We check that there are unread articles.
8457         (when (or all (gnus-summary-find-prev))
8458           (gnus-summary-catchup all t beg)))))
8459   (gnus-summary-position-point))
8460
8461 (defun gnus-summary-catchup-all (&optional quietly)
8462   "Mark all articles in this newsgroup as read."
8463   (interactive "P")
8464   (gnus-summary-catchup t quietly))
8465
8466 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8467   "Mark all unread articles in this group as read, then exit.
8468 If prefix argument ALL is non-nil, all articles are marked as read."
8469   (interactive "P")
8470   (when (gnus-summary-catchup all quietly nil 'fast)
8471     ;; Select next newsgroup or exit.
8472     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8473              (eq gnus-auto-select-next 'quietly))
8474         (gnus-summary-next-group nil)
8475       (gnus-summary-exit))))
8476
8477 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8478   "Mark all articles in this newsgroup as read, and then exit."
8479   (interactive "P")
8480   (gnus-summary-catchup-and-exit t quietly))
8481
8482 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8483   "Mark all articles in this group as read and select the next group.
8484 If given a prefix, mark all articles, unread as well as ticked, as
8485 read."
8486   (interactive "P")
8487   (save-excursion
8488     (gnus-summary-catchup all))
8489   (gnus-summary-next-group))
8490
8491 ;; Thread-based commands.
8492
8493 (defun gnus-summary-articles-in-thread (&optional article)
8494   "Return a list of all articles in the current thread.
8495 If ARTICLE is non-nil, return all articles in the thread that starts
8496 with that article."
8497   (let* ((article (or article (gnus-summary-article-number)))
8498          (data (gnus-data-find-list article))
8499          (top-level (gnus-data-level (car data)))
8500          (top-subject
8501           (cond ((null gnus-thread-operation-ignore-subject)
8502                  (gnus-simplify-subject-re
8503                   (mail-header-subject (gnus-data-header (car data)))))
8504                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8505                  (gnus-simplify-subject-fuzzy
8506                   (mail-header-subject (gnus-data-header (car data)))))
8507                 (t nil)))
8508          (end-point (save-excursion
8509                       (if (gnus-summary-go-to-next-thread)
8510                           (point) (point-max))))
8511          articles)
8512     (while (and data
8513                 (< (gnus-data-pos (car data)) end-point))
8514       (when (or (not top-subject)
8515                 (string= top-subject
8516                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8517                              (gnus-simplify-subject-fuzzy
8518                               (mail-header-subject
8519                                (gnus-data-header (car data))))
8520                            (gnus-simplify-subject-re
8521                             (mail-header-subject
8522                              (gnus-data-header (car data)))))))
8523         (push (gnus-data-number (car data)) articles))
8524       (unless (and (setq data (cdr data))
8525                    (> (gnus-data-level (car data)) top-level))
8526         (setq data nil)))
8527     ;; Return the list of articles.
8528     (nreverse articles)))
8529
8530 (defun gnus-summary-rethread-current ()
8531   "Rethread the thread the current article is part of."
8532   (interactive)
8533   (let* ((gnus-show-threads t)
8534          (article (gnus-summary-article-number))
8535          (id (mail-header-id (gnus-summary-article-header)))
8536          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8537     (unless id
8538       (error "No article on the current line"))
8539     (gnus-rebuild-thread id)
8540     (gnus-summary-goto-subject article)))
8541
8542 (defun gnus-summary-reparent-thread ()
8543   "Make the current article child of the marked (or previous) article.
8544
8545 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8546 is non-nil or the Subject: of both articles are the same."
8547   (interactive)
8548   (unless (not (gnus-group-read-only-p))
8549     (error "The current newsgroup does not support article editing"))
8550   (unless (<= (length gnus-newsgroup-processable) 1)
8551     (error "No more than one article may be marked"))
8552   (save-window-excursion
8553     (let ((gnus-article-buffer " *reparent*")
8554           (current-article (gnus-summary-article-number))
8555           ;; First grab the marked article, otherwise one line up.
8556           (parent-article (if (not (null gnus-newsgroup-processable))
8557                               (car gnus-newsgroup-processable)
8558                             (save-excursion
8559                               (if (eq (forward-line -1) 0)
8560                                   (gnus-summary-article-number)
8561                                 (error "Beginning of summary buffer"))))))
8562       (unless (not (eq current-article parent-article))
8563         (error "An article may not be self-referential"))
8564       (let ((message-id (mail-header-id
8565                          (gnus-summary-article-header parent-article))))
8566         (unless (and message-id (not (equal message-id "")))
8567           (error "No message-id in desired parent"))
8568         (gnus-with-article current-article
8569           (goto-char (point-min))
8570           (if (re-search-forward "^References: " nil t)
8571               (progn
8572                 (re-search-forward "^[^ \t]" nil t)
8573                 (forward-line -1)
8574                 (end-of-line)
8575                 (insert " " message-id))
8576             (insert "References: " message-id "\n")))
8577         (set-buffer gnus-summary-buffer)
8578         (gnus-summary-unmark-all-processable)
8579         (gnus-summary-update-article current-article)
8580         (gnus-summary-rethread-current)
8581         (gnus-message 3 "Article %d is now the child of article %d"
8582                       current-article parent-article)))))
8583
8584 (defun gnus-summary-toggle-threads (&optional arg)
8585   "Toggle showing conversation threads.
8586 If ARG is positive number, turn showing conversation threads on."
8587   (interactive "P")
8588   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8589     (setq gnus-show-threads
8590           (if (null arg) (not gnus-show-threads)
8591             (> (prefix-numeric-value arg) 0)))
8592     (gnus-summary-prepare)
8593     (gnus-summary-goto-subject current)
8594     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8595     (gnus-summary-position-point)))
8596
8597 (defun gnus-summary-show-all-threads ()
8598   "Show all threads."
8599   (interactive)
8600   (save-excursion
8601     (let ((buffer-read-only nil))
8602       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8603   (gnus-summary-position-point))
8604
8605 (defun gnus-summary-show-thread ()
8606   "Show thread subtrees.
8607 Returns nil if no thread was there to be shown."
8608   (interactive)
8609   (let ((buffer-read-only nil)
8610         (orig (point))
8611         ;; first goto end then to beg, to have point at beg after let
8612         (end (progn (end-of-line) (point)))
8613         (beg (progn (beginning-of-line) (point))))
8614     (prog1
8615         ;; Any hidden lines here?
8616         (search-forward "\r" end t)
8617       (subst-char-in-region beg end ?\^M ?\n t)
8618       (goto-char orig)
8619       (gnus-summary-position-point))))
8620
8621 (defun gnus-summary-hide-all-threads ()
8622   "Hide all thread subtrees."
8623   (interactive)
8624   (save-excursion
8625     (goto-char (point-min))
8626     (gnus-summary-hide-thread)
8627     (while (zerop (gnus-summary-next-thread 1 t))
8628       (gnus-summary-hide-thread)))
8629   (gnus-summary-position-point))
8630
8631 (defun gnus-summary-hide-thread ()
8632   "Hide thread subtrees.
8633 Returns nil if no threads were there to be hidden."
8634   (interactive)
8635   (let ((buffer-read-only nil)
8636         (start (point))
8637         (article (gnus-summary-article-number)))
8638     (goto-char start)
8639     ;; Go forward until either the buffer ends or the subthread
8640     ;; ends.
8641     (when (and (not (eobp))
8642                (or (zerop (gnus-summary-next-thread 1 t))
8643                    (goto-char (point-max))))
8644       (prog1
8645           (if (and (> (point) start)
8646                    (search-backward "\n" start t))
8647               (progn
8648                 (subst-char-in-region start (point) ?\n ?\^M)
8649                 (gnus-summary-goto-subject article))
8650             (goto-char start)
8651             nil)))))
8652
8653 (defun gnus-summary-go-to-next-thread (&optional previous)
8654   "Go to the same level (or less) next thread.
8655 If PREVIOUS is non-nil, go to previous thread instead.
8656 Return the article number moved to, or nil if moving was impossible."
8657   (let ((level (gnus-summary-thread-level))
8658         (way (if previous -1 1))
8659         (beg (point)))
8660     (forward-line way)
8661     (while (and (not (eobp))
8662                 (< level (gnus-summary-thread-level)))
8663       (forward-line way))
8664     (if (eobp)
8665         (progn
8666           (goto-char beg)
8667           nil)
8668       (setq beg (point))
8669       (prog1
8670           (gnus-summary-article-number)
8671         (goto-char beg)))))
8672
8673 (defun gnus-summary-next-thread (n &optional silent)
8674   "Go to the same level next N'th thread.
8675 If N is negative, search backward instead.
8676 Returns the difference between N and the number of skips actually
8677 done.
8678
8679 If SILENT, don't output messages."
8680   (interactive "p")
8681   (let ((backward (< n 0))
8682         (n (abs n)))
8683     (while (and (> n 0)
8684                 (gnus-summary-go-to-next-thread backward))
8685       (decf n))
8686     (unless silent
8687       (gnus-summary-position-point))
8688     (when (and (not silent) (/= 0 n))
8689       (gnus-message 7 "No more threads"))
8690     n))
8691
8692 (defun gnus-summary-prev-thread (n)
8693   "Go to the same level previous N'th thread.
8694 Returns the difference between N and the number of skips actually
8695 done."
8696   (interactive "p")
8697   (gnus-summary-next-thread (- n)))
8698
8699 (defun gnus-summary-go-down-thread ()
8700   "Go down one level in the current thread."
8701   (let ((children (gnus-summary-article-children)))
8702     (when children
8703       (gnus-summary-goto-subject (car children)))))
8704
8705 (defun gnus-summary-go-up-thread ()
8706   "Go up one level in the current thread."
8707   (let ((parent (gnus-summary-article-parent)))
8708     (when parent
8709       (gnus-summary-goto-subject parent))))
8710
8711 (defun gnus-summary-down-thread (n)
8712   "Go down thread N steps.
8713 If N is negative, go up instead.
8714 Returns the difference between N and how many steps down that were
8715 taken."
8716   (interactive "p")
8717   (let ((up (< n 0))
8718         (n (abs n)))
8719     (while (and (> n 0)
8720                 (if up (gnus-summary-go-up-thread)
8721                   (gnus-summary-go-down-thread)))
8722       (setq n (1- n)))
8723     (gnus-summary-position-point)
8724     (when (/= 0 n)
8725       (gnus-message 7 "Can't go further"))
8726     n))
8727
8728 (defun gnus-summary-up-thread (n)
8729   "Go up thread N steps.
8730 If N is negative, go up instead.
8731 Returns the difference between N and how many steps down that were
8732 taken."
8733   (interactive "p")
8734   (gnus-summary-down-thread (- n)))
8735
8736 (defun gnus-summary-top-thread ()
8737   "Go to the top of the thread."
8738   (interactive)
8739   (while (gnus-summary-go-up-thread))
8740   (gnus-summary-article-number))
8741
8742 (defun gnus-summary-kill-thread (&optional unmark)
8743   "Mark articles under current thread as read.
8744 If the prefix argument is positive, remove any kinds of marks.
8745 If the prefix argument is negative, tick articles instead."
8746   (interactive "P")
8747   (when unmark
8748     (setq unmark (prefix-numeric-value unmark)))
8749   (let ((articles (gnus-summary-articles-in-thread)))
8750     (save-excursion
8751       ;; Expand the thread.
8752       (gnus-summary-show-thread)
8753       ;; Mark all the articles.
8754       (while articles
8755         (gnus-summary-goto-subject (car articles))
8756         (cond ((null unmark)
8757                (gnus-summary-mark-article-as-read gnus-killed-mark))
8758               ((> unmark 0)
8759                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8760               (t
8761                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8762         (setq articles (cdr articles))))
8763     ;; Hide killed subtrees.
8764     (and (null unmark)
8765          gnus-thread-hide-killed
8766          (gnus-summary-hide-thread))
8767     ;; If marked as read, go to next unread subject.
8768     (when (null unmark)
8769       ;; Go to next unread subject.
8770       (gnus-summary-next-subject 1 t)))
8771   (gnus-set-mode-line 'summary))
8772
8773 ;; Summary sorting commands
8774
8775 (defun gnus-summary-sort-by-number (&optional reverse)
8776   "Sort the summary buffer by article number.
8777 Argument REVERSE means reverse order."
8778   (interactive "P")
8779   (gnus-summary-sort 'number reverse))
8780
8781 (defun gnus-summary-sort-by-author (&optional reverse)
8782   "Sort the summary buffer by author name alphabetically.
8783 If case-fold-search is non-nil, case of letters is ignored.
8784 Argument REVERSE means reverse order."
8785   (interactive "P")
8786   (gnus-summary-sort 'author reverse))
8787
8788 (defun gnus-summary-sort-by-subject (&optional reverse)
8789   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8790 If case-fold-search is non-nil, case of letters is ignored.
8791 Argument REVERSE means reverse order."
8792   (interactive "P")
8793   (gnus-summary-sort 'subject reverse))
8794
8795 (defun gnus-summary-sort-by-date (&optional reverse)
8796   "Sort the summary buffer by date.
8797 Argument REVERSE means reverse order."
8798   (interactive "P")
8799   (gnus-summary-sort 'date reverse))
8800
8801 (defun gnus-summary-sort-by-score (&optional reverse)
8802   "Sort the summary buffer by score.
8803 Argument REVERSE means reverse order."
8804   (interactive "P")
8805   (gnus-summary-sort 'score reverse))
8806
8807 (defun gnus-summary-sort-by-lines (&optional reverse)
8808   "Sort the summary buffer by the number of lines.
8809 Argument REVERSE means reverse order."
8810   (interactive "P")
8811   (gnus-summary-sort 'lines reverse))
8812
8813 (defun gnus-summary-sort-by-chars (&optional reverse)
8814   "Sort the summary buffer by article length.
8815 Argument REVERSE means reverse order."
8816   (interactive "P")
8817   (gnus-summary-sort 'chars reverse))   
8818
8819 (defun gnus-summary-sort (predicate reverse)
8820   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8821   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8822          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8823          (gnus-thread-sort-functions
8824           (if (not reverse)
8825               thread
8826             `(lambda (t1 t2)
8827                (,thread t2 t1))))
8828          (gnus-article-sort-functions
8829           (if (not reverse)
8830               article
8831             `(lambda (t1 t2)
8832                (,article t2 t1))))
8833          (buffer-read-only)
8834          (gnus-summary-prepare-hook nil))
8835     ;; We do the sorting by regenerating the threads.
8836     (gnus-summary-prepare)
8837     ;; Hide subthreads if needed.
8838     (when (and gnus-show-threads gnus-thread-hide-subtree)
8839       (gnus-summary-hide-all-threads))))
8840
8841 ;; Summary saving commands.
8842
8843 (defun gnus-summary-save-article (&optional n not-saved)
8844   "Save the current article using the default saver function.
8845 If N is a positive number, save the N next articles.
8846 If N is a negative number, save the N previous articles.
8847 If N is nil and any articles have been marked with the process mark,
8848 save those articles instead.
8849 The variable `gnus-default-article-saver' specifies the saver function."
8850   (interactive "P")
8851   (let* ((articles (gnus-summary-work-articles n))
8852          (save-buffer (save-excursion
8853                         (nnheader-set-temp-buffer " *Gnus Save*")))
8854          (num (length articles))
8855          header file)
8856     (dolist (article articles)
8857       (setq header (gnus-summary-article-header article))
8858       (if (not (vectorp header))
8859           ;; This is a pseudo-article.
8860           (if (assq 'name header)
8861               (gnus-copy-file (cdr (assq 'name header)))
8862             (gnus-message 1 "Article %d is unsaveable" article))
8863         ;; This is a real article.
8864         (save-window-excursion
8865           (gnus-summary-select-article t nil nil article))
8866         (save-excursion
8867           (set-buffer save-buffer)
8868           (erase-buffer)
8869           (insert-buffer-substring gnus-original-article-buffer))
8870         (setq file (gnus-article-save save-buffer file num))
8871         (gnus-summary-remove-process-mark article)
8872         (unless not-saved
8873           (gnus-summary-set-saved-mark article))))
8874     (gnus-kill-buffer save-buffer)
8875     (gnus-summary-position-point)
8876     (gnus-set-mode-line 'summary)
8877     n))
8878
8879 (defun gnus-summary-pipe-output (&optional arg)
8880   "Pipe the current article to a subprocess.
8881 If N is a positive number, pipe the N next articles.
8882 If N is a negative number, pipe the N previous articles.
8883 If N is nil and any articles have been marked with the process mark,
8884 pipe those articles instead."
8885   (interactive "P")
8886   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8887     (gnus-summary-save-article arg t))
8888   (gnus-configure-windows 'pipe))
8889
8890 (defun gnus-summary-save-article-mail (&optional arg)
8891   "Append the current article to an mail file.
8892 If N is a positive number, save the N next articles.
8893 If N is a negative number, save the N previous articles.
8894 If N is nil and any articles have been marked with the process mark,
8895 save those articles instead."
8896   (interactive "P")
8897   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8898     (gnus-summary-save-article arg)))
8899
8900 (defun gnus-summary-save-article-rmail (&optional arg)
8901   "Append the current article to an rmail file.
8902 If N is a positive number, save the N next articles.
8903 If N is a negative number, save the N previous articles.
8904 If N is nil and any articles have been marked with the process mark,
8905 save those articles instead."
8906   (interactive "P")
8907   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8908     (gnus-summary-save-article arg)))
8909
8910 (defun gnus-summary-save-article-file (&optional arg)
8911   "Append the current article to a file.
8912 If N is a positive number, save the N next articles.
8913 If N is a negative number, save the N previous articles.
8914 If N is nil and any articles have been marked with the process mark,
8915 save those articles instead."
8916   (interactive "P")
8917   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8918     (gnus-summary-save-article arg)))
8919
8920 (defun gnus-summary-write-article-file (&optional arg)
8921   "Write the current article to a file, deleting the previous file.
8922 If N is a positive number, save the N next articles.
8923 If N is a negative number, save the N previous articles.
8924 If N is nil and any articles have been marked with the process mark,
8925 save those articles instead."
8926   (interactive "P")
8927   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8928     (gnus-summary-save-article arg)))
8929
8930 (defun gnus-summary-save-article-body-file (&optional arg)
8931   "Append the current article body to a file.
8932 If N is a positive number, save the N next articles.
8933 If N is a negative number, save the N previous articles.
8934 If N is nil and any articles have been marked with the process mark,
8935 save those articles instead."
8936   (interactive "P")
8937   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8938     (gnus-summary-save-article arg)))
8939
8940 (defun gnus-summary-pipe-message (program)
8941   "Pipe the current article through PROGRAM."
8942   (interactive "sProgram: ")
8943   (gnus-summary-select-article)
8944   (let ((mail-header-separator ""))
8945     (gnus-eval-in-buffer-window gnus-article-buffer
8946       (save-restriction
8947         (widen)
8948         (let ((start (window-start))
8949               buffer-read-only)
8950           (message-pipe-buffer-body program)
8951           (set-window-start (get-buffer-window (current-buffer)) start))))))
8952
8953 (defun gnus-get-split-value (methods)
8954   "Return a value based on the split METHODS."
8955   (let (split-name method result match)
8956     (when methods
8957       (save-excursion
8958         (set-buffer gnus-original-article-buffer)
8959         (save-restriction
8960           (nnheader-narrow-to-headers)
8961           (while methods
8962             (goto-char (point-min))
8963             (setq method (pop methods))
8964             (setq match (car method))
8965             (when (cond
8966                    ((stringp match)
8967                     ;; Regular expression.
8968                     (ignore-errors
8969                       (re-search-forward match nil t)))
8970                    ((gnus-functionp match)
8971                     ;; Function.
8972                     (save-restriction
8973                       (widen)
8974                       (setq result (funcall match gnus-newsgroup-name))))
8975                    ((consp match)
8976                     ;; Form.
8977                     (save-restriction
8978                       (widen)
8979                       (setq result (eval match)))))
8980               (setq split-name (append (cdr method) split-name))
8981               (cond ((stringp result)
8982                      (push (expand-file-name
8983                             result gnus-article-save-directory)
8984                            split-name))
8985                     ((consp result)
8986                      (setq split-name (append result split-name)))))))))
8987     (nreverse split-name)))
8988
8989 (defun gnus-valid-move-group-p (group)
8990   (and (boundp group)
8991        (symbol-name group)
8992        (symbol-value group)
8993        (gnus-get-function (gnus-find-method-for-group
8994                            (symbol-name group)) 'request-accept-article t)))
8995
8996 (defun gnus-read-move-group-name (prompt default articles prefix)
8997   "Read a group name."
8998   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8999          (minibuffer-confirm-incomplete nil) ; XEmacs
9000          (prom
9001           (format "%s %s to:"
9002                   prompt
9003                   (if (> (length articles) 1)
9004                       (format "these %d articles" (length articles))
9005                     "this article")))
9006          (to-newsgroup
9007           (cond
9008            ((null split-name)
9009             (gnus-completing-read default prom
9010                                   gnus-active-hashtb
9011                                   'gnus-valid-move-group-p
9012                                   nil prefix
9013                                   'gnus-group-history))
9014            ((= 1 (length split-name))
9015             (gnus-completing-read (car split-name) prom
9016                                   gnus-active-hashtb
9017                                   'gnus-valid-move-group-p
9018                                   nil nil
9019                                   'gnus-group-history))
9020            (t
9021             (gnus-completing-read nil prom
9022                                   (mapcar (lambda (el) (list el))
9023                                           (nreverse split-name))
9024                                   nil nil nil
9025                                   'gnus-group-history)))))
9026     (when to-newsgroup
9027       (if (or (string= to-newsgroup "")
9028               (string= to-newsgroup prefix))
9029           (setq to-newsgroup default))
9030       (unless to-newsgroup
9031         (error "No group name entered"))
9032       (or (gnus-active to-newsgroup)
9033           (gnus-activate-group to-newsgroup)
9034           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9035                                      to-newsgroup))
9036               (or (and (gnus-request-create-group
9037                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9038                        (gnus-activate-group
9039                         to-newsgroup nil nil
9040                         (gnus-group-name-to-method to-newsgroup))
9041                        (gnus-subscribe-group to-newsgroup))
9042                   (error "Couldn't create group %s" to-newsgroup)))
9043           (error "No such group: %s" to-newsgroup)))
9044     to-newsgroup))
9045
9046 (defun gnus-summary-save-parts (type dir n reverse)
9047   "Save parts matching TYPE to DIR.
9048 If REVERSE, save parts that do not match TYPE."
9049   (interactive
9050    (list (read-string "Save parts of type: " "image/.*")
9051          (read-file-name "Save to directory: " t nil t)
9052          current-prefix-arg))
9053   (gnus-summary-iterate n
9054     (let ((gnus-display-mime-function nil)
9055           (gnus-inhibit-treatment t))
9056       (gnus-summary-select-article))
9057     (save-excursion
9058       (set-buffer gnus-article-buffer)
9059       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9060         (when handles
9061           (gnus-summary-save-parts-1 type dir handles reverse)
9062           (mm-destroy-parts handles))))))
9063
9064 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9065   (if (stringp (car handle))
9066       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9067               (cdr handle))
9068     (when (if reverse
9069               (not (string-match type (mm-handle-media-type handle)))
9070             (string-match type (mm-handle-media-type handle)))
9071       (let ((file (expand-file-name
9072                    (file-name-nondirectory
9073                     (or
9074                      (mail-content-type-get
9075                       (mm-handle-disposition handle) 'filename)
9076                      (concat gnus-newsgroup-name "." gnus-current-article)))
9077                    dir)))
9078         (unless (file-exists-p file)
9079           (mm-save-part-to-file handle file))))))
9080
9081 ;; Summary extract commands
9082
9083 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9084   (let ((buffer-read-only nil)
9085         (article (gnus-summary-article-number))
9086         after-article b e)
9087     (unless (gnus-summary-goto-subject article)
9088       (error "No such article: %d" article))
9089     (gnus-summary-position-point)
9090     ;; If all commands are to be bunched up on one line, we collect
9091     ;; them here.
9092     (unless gnus-view-pseudos-separately
9093       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9094             files action)
9095         (while ps
9096           (setq action (cdr (assq 'action (car ps))))
9097           (setq files (list (cdr (assq 'name (car ps)))))
9098           (while (and ps (cdr ps)
9099                       (string= (or action "1")
9100                                (or (cdr (assq 'action (cadr ps))) "2")))
9101             (push (cdr (assq 'name (cadr ps))) files)
9102             (setcdr ps (cddr ps)))
9103           (when files
9104             (when (not (string-match "%s" action))
9105               (push " " files))
9106             (push " " files)
9107             (when (assq 'execute (car ps))
9108               (setcdr (assq 'execute (car ps))
9109                       (funcall (if (string-match "%s" action)
9110                                    'format 'concat)
9111                                action
9112                                (mapconcat
9113                                 (lambda (f)
9114                                   (if (equal f " ")
9115                                       f
9116                                     (mm-quote-arg f)))
9117                                 files " ")))))
9118           (setq ps (cdr ps)))))
9119     (if (and gnus-view-pseudos (not not-view))
9120         (while pslist
9121           (when (assq 'execute (car pslist))
9122             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9123                                   (eq gnus-view-pseudos 'not-confirm)))
9124           (setq pslist (cdr pslist)))
9125       (save-excursion
9126         (while pslist
9127           (setq after-article (or (cdr (assq 'article (car pslist)))
9128                                   (gnus-summary-article-number)))
9129           (gnus-summary-goto-subject after-article)
9130           (forward-line 1)
9131           (setq b (point))
9132           (insert "    " (file-name-nondirectory
9133                           (cdr (assq 'name (car pslist))))
9134                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9135           (setq e (point))
9136           (forward-line -1)             ; back to `b'
9137           (gnus-add-text-properties
9138            b (1- e) (list 'gnus-number gnus-reffed-article-number
9139                           gnus-mouse-face-prop gnus-mouse-face))
9140           (gnus-data-enter
9141            after-article gnus-reffed-article-number
9142            gnus-unread-mark b (car pslist) 0 (- e b))
9143           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9144           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9145           (setq pslist (cdr pslist)))))))
9146
9147 (defun gnus-pseudos< (p1 p2)
9148   (let ((c1 (cdr (assq 'action p1)))
9149         (c2 (cdr (assq 'action p2))))
9150     (and c1 c2 (string< c1 c2))))
9151
9152 (defun gnus-request-pseudo-article (props)
9153   (cond ((assq 'execute props)
9154          (gnus-execute-command (cdr (assq 'execute props)))))
9155   (let ((gnus-current-article (gnus-summary-article-number)))
9156     (gnus-run-hooks 'gnus-mark-article-hook)))
9157
9158 (defun gnus-execute-command (command &optional automatic)
9159   (save-excursion
9160     (gnus-article-setup-buffer)
9161     (set-buffer gnus-article-buffer)
9162     (setq buffer-read-only nil)
9163     (let ((command (if automatic command
9164                      (read-string "Command: " (cons command 0)))))
9165       (erase-buffer)
9166       (insert "$ " command "\n\n")
9167       (if gnus-view-pseudo-asynchronously
9168           (start-process "gnus-execute" (current-buffer) shell-file-name
9169                          shell-command-switch command)
9170         (call-process shell-file-name nil t nil
9171                       shell-command-switch command)))))
9172
9173 ;; Summary kill commands.
9174
9175 (defun gnus-summary-edit-global-kill (article)
9176   "Edit the \"global\" kill file."
9177   (interactive (list (gnus-summary-article-number)))
9178   (gnus-group-edit-global-kill article))
9179
9180 (defun gnus-summary-edit-local-kill ()
9181   "Edit a local kill file applied to the current newsgroup."
9182   (interactive)
9183   (setq gnus-current-headers (gnus-summary-article-header))
9184   (gnus-group-edit-local-kill
9185    (gnus-summary-article-number) gnus-newsgroup-name))
9186
9187 ;;; Header reading.
9188
9189 (defun gnus-read-header (id &optional header)
9190   "Read the headers of article ID and enter them into the Gnus system."
9191   (let ((group gnus-newsgroup-name)
9192         (gnus-override-method
9193          (or
9194           gnus-override-method
9195           (and (gnus-news-group-p gnus-newsgroup-name)
9196                (car (gnus-refer-article-methods)))))
9197         where)
9198     ;; First we check to see whether the header in question is already
9199     ;; fetched.
9200     (if (stringp id)
9201         ;; This is a Message-ID.
9202         (setq header (or header (gnus-id-to-header id)))
9203       ;; This is an article number.
9204       (setq header (or header (gnus-summary-article-header id))))
9205     (if (and header
9206              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9207         ;; We have found the header.
9208         header
9209       ;; If this is a sparse article, we have to nix out its
9210       ;; previous entry in the thread hashtb.
9211       (when (and header
9212                  (gnus-summary-article-sparse-p (mail-header-number header)))
9213         (let* ((parent (gnus-parent-id (mail-header-references header)))
9214                (thread (and parent (gnus-id-to-thread parent))))
9215           (when thread
9216             (delq (assq header thread) thread))))
9217       ;; We have to really fetch the header to this article.
9218       (save-excursion
9219         (set-buffer nntp-server-buffer)
9220         (when (setq where (gnus-request-head id group))
9221           (nnheader-fold-continuation-lines)
9222           (goto-char (point-max))
9223           (insert ".\n")
9224           (goto-char (point-min))
9225           (insert "211 ")
9226           (princ (cond
9227                   ((numberp id) id)
9228                   ((cdr where) (cdr where))
9229                   (header (mail-header-number header))
9230                   (t gnus-reffed-article-number))
9231                  (current-buffer))
9232           (insert " Article retrieved.\n"))
9233         (if (or (not where)
9234                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9235             ()                          ; Malformed head.
9236           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9237             (when (and (stringp id)
9238                        (not (string= (gnus-group-real-name group)
9239                                      (car where))))
9240               ;; If we fetched by Message-ID and the article came
9241               ;; from a different group, we fudge some bogus article
9242               ;; numbers for this article.
9243               (mail-header-set-number header gnus-reffed-article-number))
9244             (save-excursion
9245               (set-buffer gnus-summary-buffer)
9246               (decf gnus-reffed-article-number)
9247               (gnus-remove-header (mail-header-number header))
9248               (push header gnus-newsgroup-headers)
9249               (setq gnus-current-headers header)
9250               (push (mail-header-number header) gnus-newsgroup-limit)))
9251           header)))))
9252
9253 (defun gnus-remove-header (number)
9254   "Remove header NUMBER from `gnus-newsgroup-headers'."
9255   (if (and gnus-newsgroup-headers
9256            (= number (mail-header-number (car gnus-newsgroup-headers))))
9257       (pop gnus-newsgroup-headers)
9258     (let ((headers gnus-newsgroup-headers))
9259       (while (and (cdr headers)
9260                   (not (= number (mail-header-number (cadr headers)))))
9261         (pop headers))
9262       (when (cdr headers)
9263         (setcdr headers (cddr headers))))))
9264
9265 ;;;
9266 ;;; summary highlights
9267 ;;;
9268
9269 (defun gnus-highlight-selected-summary ()
9270   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9271   ;; Highlight selected article in summary buffer
9272   (when gnus-summary-selected-face
9273     (save-excursion
9274       (let* ((beg (progn (beginning-of-line) (point)))
9275              (end (progn (end-of-line) (point)))
9276              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9277              (from (if (get-text-property beg gnus-mouse-face-prop)
9278                        beg
9279                      (or (next-single-property-change
9280                           beg gnus-mouse-face-prop nil end)
9281                          beg)))
9282              (to
9283               (if (= from end)
9284                   (- from 2)
9285                 (or (next-single-property-change
9286                      from gnus-mouse-face-prop nil end)
9287                     end))))
9288         ;; If no mouse-face prop on line we will have to = from = end,
9289         ;; so we highlight the entire line instead.
9290         (when (= (+ to 2) from)
9291           (setq from beg)
9292           (setq to end))
9293         (if gnus-newsgroup-selected-overlay
9294             ;; Move old overlay.
9295             (gnus-move-overlay
9296              gnus-newsgroup-selected-overlay from to (current-buffer))
9297           ;; Create new overlay.
9298           (gnus-overlay-put
9299            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9300            'face gnus-summary-selected-face))))))
9301
9302 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9303 (defun gnus-summary-highlight-line ()
9304   "Highlight current line according to `gnus-summary-highlight'."
9305   (let* ((list gnus-summary-highlight)
9306          (p (point))
9307          (end (progn (end-of-line) (point)))
9308          ;; now find out where the line starts and leave point there.
9309          (beg (progn (beginning-of-line) (point)))
9310          (article (gnus-summary-article-number))
9311          (score (or (cdr (assq (or article gnus-current-article)
9312                                gnus-newsgroup-scored))
9313                     gnus-summary-default-score 0))
9314          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9315          (inhibit-read-only t))
9316     ;; Eval the cars of the lists until we find a match.
9317     (let ((default gnus-summary-default-score))
9318       (while (and list
9319                   (not (eval (caar list))))
9320         (setq list (cdr list))))
9321     (let ((face (cdar list)))
9322       (unless (eq face (get-text-property beg 'face))
9323         (gnus-put-text-property-excluding-characters-with-faces
9324          beg end 'face
9325          (setq face (if (boundp face) (symbol-value face) face)))
9326         (when gnus-summary-highlight-line-function
9327           (funcall gnus-summary-highlight-line-function article face))))
9328     (goto-char p)))
9329
9330 (defun gnus-update-read-articles (group unread &optional compute)
9331   "Update the list of read articles in GROUP."
9332   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9333          (entry (gnus-gethash group gnus-newsrc-hashtb))
9334          (info (nth 2 entry))
9335          (prev 1)
9336          (unread (sort (copy-sequence unread) '<))
9337          read)
9338     (if (or (not info) (not active))
9339         ;; There is no info on this group if it was, in fact,
9340         ;; killed.  Gnus stores no information on killed groups, so
9341         ;; there's nothing to be done.
9342         ;; One could store the information somewhere temporarily,
9343         ;; perhaps...  Hmmm...
9344         ()
9345       ;; Remove any negative articles numbers.
9346       (while (and unread (< (car unread) 0))
9347         (setq unread (cdr unread)))
9348       ;; Remove any expired article numbers
9349       (while (and unread (< (car unread) (car active)))
9350         (setq unread (cdr unread)))
9351       ;; Compute the ranges of read articles by looking at the list of
9352       ;; unread articles.
9353       (while unread
9354         (when (/= (car unread) prev)
9355           (push (if (= prev (1- (car unread))) prev
9356                   (cons prev (1- (car unread))))
9357                 read))
9358         (setq prev (1+ (car unread)))
9359         (setq unread (cdr unread)))
9360       (when (<= prev (cdr active))
9361         (push (cons prev (cdr active)) read))
9362       (setq read (if (> (length read) 1) (nreverse read) read))
9363       (if compute
9364           read
9365         (save-excursion
9366           (set-buffer gnus-group-buffer)
9367           (gnus-undo-register
9368             `(progn
9369                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9370                (gnus-info-set-read ',info ',(gnus-info-read info))
9371                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9372                (gnus-group-update-group ,group t))))
9373         ;; Propagate the read marks to the backend.
9374         (if (gnus-check-backend-function 'request-set-mark group)
9375             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9376                   (add (gnus-remove-from-range read (gnus-info-read info))))
9377               (when (or add del)
9378                 (unless (gnus-check-group group)
9379                   (error "Can't open server for %s" group))
9380                 (gnus-request-set-mark
9381                  group (delq nil (list (if add (list add 'add '(read)))
9382                                        (if del (list del 'del '(read)))))))))
9383         ;; Enter this list into the group info.
9384         (gnus-info-set-read info read)
9385         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9386         (gnus-get-unread-articles-in-group info (gnus-active group))
9387         t))))
9388
9389 (defun gnus-offer-save-summaries ()
9390   "Offer to save all active summary buffers."
9391   (save-excursion
9392     (let ((buflist (buffer-list))
9393           buffers bufname)
9394       ;; Go through all buffers and find all summaries.
9395       (while buflist
9396         (and (setq bufname (buffer-name (car buflist)))
9397              (string-match "Summary" bufname)
9398              (save-excursion
9399                (set-buffer bufname)
9400                ;; We check that this is, indeed, a summary buffer.
9401                (and (eq major-mode 'gnus-summary-mode)
9402                     ;; Also make sure this isn't bogus.
9403                     gnus-newsgroup-prepared
9404                     ;; Also make sure that this isn't a dead summary buffer.
9405                     (not gnus-dead-summary-mode)))
9406              (push bufname buffers))
9407         (setq buflist (cdr buflist)))
9408       ;; Go through all these summary buffers and offer to save them.
9409       (when buffers
9410         (map-y-or-n-p
9411          "Update summary buffer %s? "
9412          (lambda (buf)
9413            (switch-to-buffer buf)
9414            (gnus-summary-exit))
9415          buffers)))))
9416
9417 (defun gnus-summary-setup-default-charset ()
9418   "Setup newsgroup default charset."
9419   (if (equal gnus-newsgroup-name "nndraft:drafts")
9420       (setq gnus-newsgroup-charset nil)
9421     (let* ((name (and gnus-newsgroup-name
9422                       (gnus-group-real-name gnus-newsgroup-name)))
9423            (ignored-charsets 
9424             (or gnus-newsgroup-ephemeral-ignored-charsets
9425                 (append
9426                  (and gnus-newsgroup-name
9427                       (or (gnus-group-find-parameter gnus-newsgroup-name
9428                                                      'ignored-charsets t)
9429                           (let ((alist gnus-group-ignored-charsets-alist)
9430                                 elem (charsets nil))
9431                             (while (setq elem (pop alist))
9432                               (when (and name
9433                                          (string-match (car elem) name))
9434                                 (setq alist nil
9435                                       charsets (cdr elem))))
9436                             charsets)))
9437                  gnus-newsgroup-ignored-charsets))))
9438       (setq gnus-newsgroup-charset
9439             (or gnus-newsgroup-ephemeral-charset
9440                 (and gnus-newsgroup-name
9441                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9442                          (let ((alist gnus-group-charset-alist)
9443                                elem charset)
9444                            (while (setq elem (pop alist))
9445                              (when (and name
9446                                         (string-match (car elem) name))
9447                                (setq alist nil
9448                                      charset (cadr elem))))
9449                            charset)))
9450                 gnus-default-charset))
9451       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9452            ignored-charsets))))
9453
9454 ;;;
9455 ;;; Mime Commands
9456 ;;;
9457
9458 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9459   "Display the current article buffer fully MIME-buttonized.
9460 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9461 treated as multipart/mixed."
9462   (interactive "P")
9463   (require 'gnus-art)
9464   (let ((gnus-unbuttonized-mime-types nil)
9465         (gnus-mime-display-multipart-as-mixed show-all-parts))
9466     (gnus-summary-show-article)))
9467
9468 (defun gnus-summary-repair-multipart (article)
9469   "Add a Content-Type header to a multipart article without one."
9470   (interactive (list (gnus-summary-article-number)))
9471   (gnus-with-article article
9472     (message-narrow-to-head)
9473     (goto-char (point-max))
9474     (widen)
9475     (when (search-forward "\n--" nil t)
9476       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9477         (message-narrow-to-head)
9478         (message-remove-header "Mime-Version")
9479         (message-remove-header "Content-Type")
9480         (goto-char (point-max))
9481         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9482                         separator))
9483         (insert "Mime-Version: 1.0\n")
9484         (widen))))
9485   (let (gnus-mark-article-hook)
9486     (gnus-summary-select-article t t nil article)))
9487
9488 (defun gnus-summary-toggle-display-buttonized ()
9489   "Toggle the buttonizing of the article buffer."
9490   (interactive)
9491   (require 'gnus-art)
9492   (if (setq gnus-inhibit-mime-unbuttonizing
9493             (not gnus-inhibit-mime-unbuttonizing))
9494       (let ((gnus-unbuttonized-mime-types nil))
9495         (gnus-summary-show-article))
9496     (gnus-summary-show-article)))
9497
9498 ;;;
9499 ;;; with article
9500 ;;;
9501
9502 (defmacro gnus-with-article (article &rest forms)
9503   "Select ARTICLE and perform FORMS in the original article buffer.
9504 Then replace the article with the result."
9505   `(progn
9506      ;; We don't want the article to be marked as read.
9507      (let (gnus-mark-article-hook)
9508        (gnus-summary-select-article t t nil ,article))
9509      (set-buffer gnus-original-article-buffer)
9510      ,@forms
9511      (if (not (gnus-check-backend-function
9512                'request-replace-article (car gnus-article-current)))
9513          (gnus-message 5 "Read-only group; not replacing")
9514        (unless (gnus-request-replace-article
9515                 ,article (car gnus-article-current)
9516                 (current-buffer) t)
9517          (error "Couldn't replace article")))
9518      ;; The cache and backlog have to be flushed somewhat.
9519      (when gnus-keep-backlog
9520        (gnus-backlog-remove-article
9521         (car gnus-article-current) (cdr gnus-article-current)))
9522      (when gnus-use-cache
9523        (gnus-cache-update-article
9524         (car gnus-article-current) (cdr gnus-article-current)))))
9525
9526 (put 'gnus-with-article 'lisp-indent-function 1)
9527 (put 'gnus-with-article 'edebug-form-spec '(form body))
9528
9529 ;;;
9530 ;;; Generic summary marking commands
9531 ;;;
9532
9533 (defvar gnus-summary-marking-alist
9534   '((read gnus-del-mark "d")
9535     (unread gnus-unread-mark "u")
9536     (ticked gnus-ticked-mark "!")
9537     (dormant gnus-dormant-mark "?")
9538     (expirable gnus-expirable-mark "e"))
9539   "An alist of names/marks/keystrokes.")
9540
9541 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9542 (defvar gnus-summary-mark-map)
9543
9544 (defun gnus-summary-make-all-marking-commands ()
9545   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9546   (dolist (elem gnus-summary-marking-alist)
9547     (apply 'gnus-summary-make-marking-command elem)))
9548
9549 (defun gnus-summary-make-marking-command (name mark keystroke)
9550   (let ((map (make-sparse-keymap)))
9551     (define-key gnus-summary-generic-mark-map keystroke map)
9552     (dolist (lway `((next "next" next nil "n")
9553                     (next-unread "next unread" next t "N")
9554                     (prev "previous" prev nil "p")
9555                     (prev-unread "previous unread" prev t "P")
9556                     (nomove "" nil nil ,keystroke)))
9557       (let ((func (gnus-summary-make-marking-command-1
9558                    mark (car lway) lway name)))
9559         (setq func (eval func))
9560         (define-key map (nth 4 lway) func)))))
9561       
9562 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9563   `(defun ,(intern
9564             (format "gnus-summary-put-mark-as-%s%s"
9565                     name (if (eq way 'nomove)
9566                              ""
9567                            (concat "-" (symbol-name way)))))
9568      (n)
9569      ,(format
9570        "Mark the current article as %s%s.
9571 If N, the prefix, then repeat N times.
9572 If N is negative, move in reverse order.
9573 The difference between N and the actual number of articles marked is
9574 returned."
9575        name (cadr lway))
9576      (interactive "p")
9577      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9578     
9579 (defun gnus-summary-generic-mark (n mark move unread)
9580   "Mark N articles with MARK."
9581   (unless (eq major-mode 'gnus-summary-mode)
9582     (error "This command can only be used in the summary buffer"))
9583   (gnus-summary-show-thread)
9584   (let ((nummove
9585          (cond
9586           ((eq move 'next) 1)
9587           ((eq move 'prev) -1)
9588           (t 0))))
9589     (if (zerop nummove)
9590         (setq n 1)
9591       (when (< n 0)
9592         (setq n (abs n)
9593               nummove (* -1 nummove))))
9594     (while (and (> n 0)
9595                 (gnus-summary-mark-article nil mark)
9596                 (zerop (gnus-summary-next-subject nummove unread t)))
9597       (setq n (1- n)))
9598     (when (/= 0 n)
9599       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9600     (gnus-summary-recenter)
9601     (gnus-summary-position-point)
9602     (gnus-set-mode-line 'summary)
9603     n))
9604
9605 (gnus-summary-make-all-marking-commands)
9606
9607 (gnus-ems-redefine)
9608
9609 (provide 'gnus-sum)
9610
9611 (run-hooks 'gnus-sum-load-hook)
9612
9613 ;;; gnus-sum.el ends here