ae959cf682a10dbc75388c7ab9e3d2f862cabd1e
[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 (not quitting) summary mode."
700   :group 'gnus-various
701   :type 'hook)
702
703 (defcustom gnus-summary-update-hook
704   (list 'gnus-summary-highlight-line)
705   "*A hook called when a summary line is changed.
706 The hook will not be called if `gnus-visual' is nil.
707
708 The default function `gnus-summary-highlight-line' will
709 highlight the line according to the `gnus-summary-highlight'
710 variable."
711   :group 'gnus-summary-visual
712   :type 'hook)
713
714 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
715   "*A hook called when an article is selected for the first time.
716 The hook is intended to mark an article as read (or unread)
717 automatically when it is selected."
718   :group 'gnus-summary-choose
719   :type 'hook)
720
721 (defcustom gnus-group-no-more-groups-hook nil
722   "*A hook run when returning to group mode having no more (unread) groups."
723   :group 'gnus-group-select
724   :type 'hook)
725
726 (defcustom gnus-ps-print-hook nil
727   "*A hook run before ps-printing something from Gnus."
728   :group 'gnus-summary
729   :type 'hook)
730
731 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
732   "Face used for highlighting the current article in the summary buffer."
733   :group 'gnus-summary-visual
734   :type 'face)
735
736 (defcustom gnus-summary-highlight
737   '(((= mark gnus-canceled-mark)
738      . gnus-summary-cancelled-face)
739     ((and (> score default)
740           (or (= mark gnus-dormant-mark)
741               (= mark gnus-ticked-mark)))
742      . gnus-summary-high-ticked-face)
743     ((and (< score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-low-ticked-face)
747     ((or (= mark gnus-dormant-mark)
748          (= mark gnus-ticked-mark))
749      . gnus-summary-normal-ticked-face)
750     ((and (> score default) (= mark gnus-ancient-mark))
751      . gnus-summary-high-ancient-face)
752     ((and (< score default) (= mark gnus-ancient-mark))
753      . gnus-summary-low-ancient-face)
754     ((= mark gnus-ancient-mark)
755      . gnus-summary-normal-ancient-face)
756     ((and (> score default) (= mark gnus-unread-mark))
757      . gnus-summary-high-unread-face)
758     ((and (< score default) (= mark gnus-unread-mark))
759      . gnus-summary-low-unread-face)
760     ((= mark gnus-unread-mark)
761      . gnus-summary-normal-unread-face)
762     ((and (> score default) (memq mark (list gnus-downloadable-mark
763                                              gnus-undownloaded-mark)))
764      . gnus-summary-high-unread-face)
765     ((and (< score default) (memq mark (list gnus-downloadable-mark
766                                              gnus-undownloaded-mark)))
767      . gnus-summary-low-unread-face)
768     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
769      . gnus-summary-normal-unread-face)
770     ((> score default)
771      . gnus-summary-high-read-face)
772     ((< score default)
773      . gnus-summary-low-read-face)
774     (t
775      . gnus-summary-normal-read-face))
776   "*Controls the highlighting of summary buffer lines.
777
778 A list of (FORM . FACE) pairs.  When deciding how a a particular
779 summary line should be displayed, each form is evaluated.  The content
780 of the face field after the first true form is used.  You can change
781 how those summary lines are displayed, by editing the face field.
782
783 You can use the following variables in the FORM field.
784
785 score:   The articles score
786 default: The default article score.
787 below:   The score below which articles are automatically marked as read.
788 mark:    The articles mark."
789   :group 'gnus-summary-visual
790   :type '(repeat (cons (sexp :tag "Form" nil)
791                        face)))
792
793 (defcustom gnus-alter-header-function nil
794   "Function called to allow alteration of article header structures.
795 The function is called with one parameter, the article header vector,
796 which it may alter in any way.")
797
798 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
799   "Variable that says which function should be used to decode a string with encoded words.")
800
801 (defcustom gnus-extra-headers nil
802   "*Extra headers to parse."
803   :group 'gnus-summary
804   :type '(repeat symbol))
805
806 (defcustom gnus-ignored-from-addresses
807   (and user-mail-address (regexp-quote user-mail-address))
808   "*Regexp of From headers that may be suppressed in favor of To headers."
809   :group 'gnus-summary
810   :type 'regexp)
811
812 (defcustom gnus-group-charset-alist
813   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
814     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
815     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
816     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
817     ("^relcom\\>" koi8-r)
818     ("^fido7\\>" koi8-r)
819     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
820     ("^israel\\>" iso-8859-1)
821     ("^han\\>" euc-kr)
822     ("^alt.chinese.text.big5\\>" chinese-big5)
823     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
824     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
825     (".*" iso-8859-1))
826   "Alist of regexps (to match group names) and default charsets to be used when reading."
827   :type '(repeat (list (regexp :tag "Group")
828                        (symbol :tag "Charset")))
829   :group 'gnus-charset)
830
831 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
832   "List of charsets that should be ignored.
833 When these charsets are used in the \"charset\" parameter, the
834 default charset will be used instead."
835   :type '(repeat symbol)
836   :group 'gnus-charset)
837
838 (defcustom gnus-group-ignored-charsets-alist 
839   '(("alt\\.chinese\\.text" iso-8859-1))
840   "Alist of regexps (to match group names) and charsets that should be ignored.
841 When these charsets are used in the \"charset\" parameter, the
842 default charset will be used instead."
843   :type '(repeat (cons (regexp :tag "Group")
844                        (repeat symbol)))
845   :group 'gnus-charset)
846
847 (defcustom gnus-group-highlight-words-alist nil
848   "Alist of group regexps and highlight regexps.
849 This variable uses the same syntax as `gnus-emphasis-alist'."
850   :type '(repeat (cons (regexp :tag "Group")
851                        (repeat (list (regexp :tag "Highlight regexp")
852                                      (number :tag "Group for entire word" 0)
853                                      (number :tag "Group for displayed part" 0)
854                                      (symbol :tag "Face" 
855                                              gnus-emphasis-highlight-words)))))
856   :group 'gnus-summary-visual)
857
858 (defcustom gnus-summary-show-article-charset-alist
859   nil
860   "Alist of number and charset.
861 The article will be shown with the charset corresponding to the
862 numbered argument.
863 For example: ((1 . cn-gb-2312) (2 . big5))."
864   :type '(repeat (cons (number :tag "Argument" 1)
865                        (symbol :tag "Charset")))
866   :group 'gnus-charset)
867
868 (defcustom gnus-preserve-marks t
869   "Whether marks are preserved when moving, copying and respooling messages."
870   :type 'boolean
871   :group 'gnus-summary-marks)
872
873 ;;; Internal variables
874
875 (defvar gnus-article-mime-handles nil)
876 (defvar gnus-article-decoded-p nil)
877 (defvar gnus-scores-exclude-files nil)
878 (defvar gnus-page-broken nil)
879 (defvar gnus-inhibit-mime-unbuttonizing nil)
880
881 (defvar gnus-original-article nil)
882 (defvar gnus-article-internal-prepare-hook nil)
883 (defvar gnus-newsgroup-process-stack nil)
884
885 (defvar gnus-thread-indent-array nil)
886 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
887 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
888   "Function called to sort the articles within a thread after it has been gathered together.")
889
890 ;; Avoid highlighting in kill files.
891 (defvar gnus-summary-inhibit-highlight nil)
892 (defvar gnus-newsgroup-selected-overlay nil)
893 (defvar gnus-inhibit-limiting nil)
894 (defvar gnus-newsgroup-adaptive-score-file nil)
895 (defvar gnus-current-score-file nil)
896 (defvar gnus-current-move-group nil)
897 (defvar gnus-current-copy-group nil)
898 (defvar gnus-current-crosspost-group nil)
899
900 (defvar gnus-newsgroup-dependencies nil)
901 (defvar gnus-newsgroup-adaptive nil)
902 (defvar gnus-summary-display-article-function nil)
903 (defvar gnus-summary-highlight-line-function nil
904   "Function called after highlighting a summary line.")
905
906 (defvar gnus-summary-line-format-alist
907   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
908     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
909     (?s gnus-tmp-subject-or-nil ?s)
910     (?n gnus-tmp-name ?s)
911     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
912         ?s)
913     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
914             gnus-tmp-from) ?s)
915     (?F gnus-tmp-from ?s)
916     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
917     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
918     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
919     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
920     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
921     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
922     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
923     (?L gnus-tmp-lines ?d)
924     (?I gnus-tmp-indentation ?s)
925     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
926     (?R gnus-tmp-replied ?c)
927     (?\[ gnus-tmp-opening-bracket ?c)
928     (?\] gnus-tmp-closing-bracket ?c)
929     (?\> (make-string gnus-tmp-level ? ) ?s)
930     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
931     (?i gnus-tmp-score ?d)
932     (?z gnus-tmp-score-char ?c)
933     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
934     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
935     (?U gnus-tmp-unread ?c)
936     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
937     (?t (gnus-summary-number-of-articles-in-thread
938          (and (boundp 'thread) (car thread)) gnus-tmp-level)
939         ?d)
940     (?e (gnus-summary-number-of-articles-in-thread
941          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
942         ?c)
943     (?u gnus-tmp-user-defined ?s)
944     (?P (gnus-pick-line-number) ?d))
945   "An alist of format specifications that can appear in summary lines,
946 and what variables they correspond with, along with the type of the
947 variable (string, integer, character, etc).")
948
949 (defvar gnus-summary-dummy-line-format-alist
950   `((?S gnus-tmp-subject ?s)
951     (?N gnus-tmp-number ?d)
952     (?u gnus-tmp-user-defined ?s)))
953
954 (defvar gnus-summary-mode-line-format-alist
955   `((?G gnus-tmp-group-name ?s)
956     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
957     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
958     (?A gnus-tmp-article-number ?d)
959     (?Z gnus-tmp-unread-and-unselected ?s)
960     (?V gnus-version ?s)
961     (?U gnus-tmp-unread-and-unticked ?d)
962     (?S gnus-tmp-subject ?s)
963     (?e gnus-tmp-unselected ?d)
964     (?u gnus-tmp-user-defined ?s)
965     (?d (length gnus-newsgroup-dormant) ?d)
966     (?t (length gnus-newsgroup-marked) ?d)
967     (?r (length gnus-newsgroup-reads) ?d)
968     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
969     (?E gnus-newsgroup-expunged-tally ?d)
970     (?s (gnus-current-score-file-nondirectory) ?s)))
971
972 (defvar gnus-last-search-regexp nil
973   "Default regexp for article search command.")
974
975 (defvar gnus-last-shell-command nil
976   "Default shell command on article.")
977
978 (defvar gnus-newsgroup-begin nil)
979 (defvar gnus-newsgroup-end nil)
980 (defvar gnus-newsgroup-last-rmail nil)
981 (defvar gnus-newsgroup-last-mail nil)
982 (defvar gnus-newsgroup-last-folder nil)
983 (defvar gnus-newsgroup-last-file nil)
984 (defvar gnus-newsgroup-auto-expire nil)
985 (defvar gnus-newsgroup-active nil)
986
987 (defvar gnus-newsgroup-data nil)
988 (defvar gnus-newsgroup-data-reverse nil)
989 (defvar gnus-newsgroup-limit nil)
990 (defvar gnus-newsgroup-limits nil)
991
992 (defvar gnus-newsgroup-unreads nil
993   "List of unread articles in the current newsgroup.")
994
995 (defvar gnus-newsgroup-unselected nil
996   "List of unselected unread articles in the current newsgroup.")
997
998 (defvar gnus-newsgroup-reads nil
999   "Alist of read articles and article marks in the current newsgroup.")
1000
1001 (defvar gnus-newsgroup-expunged-tally nil)
1002
1003 (defvar gnus-newsgroup-marked nil
1004   "List of ticked articles in the current newsgroup (a subset of unread art).")
1005
1006 (defvar gnus-newsgroup-killed nil
1007   "List of ranges of articles that have been through the scoring process.")
1008
1009 (defvar gnus-newsgroup-cached nil
1010   "List of articles that come from the article cache.")
1011
1012 (defvar gnus-newsgroup-saved nil
1013   "List of articles that have been saved.")
1014
1015 (defvar gnus-newsgroup-kill-headers nil)
1016
1017 (defvar gnus-newsgroup-replied nil
1018   "List of articles that have been replied to in the current newsgroup.")
1019
1020 (defvar gnus-newsgroup-expirable nil
1021   "List of articles in the current newsgroup that can be expired.")
1022
1023 (defvar gnus-newsgroup-processable nil
1024   "List of articles in the current newsgroup that can be processed.")
1025
1026 (defvar gnus-newsgroup-downloadable nil
1027   "List of articles in the current newsgroup that can be processed.")
1028
1029 (defvar gnus-newsgroup-undownloaded nil
1030   "List of articles in the current newsgroup that haven't been downloaded..")
1031
1032 (defvar gnus-newsgroup-unsendable nil
1033   "List of articles in the current newsgroup that won't be sent.")
1034
1035 (defvar gnus-newsgroup-bookmarks nil
1036   "List of articles in the current newsgroup that have bookmarks.")
1037
1038 (defvar gnus-newsgroup-dormant nil
1039   "List of dormant articles in the current newsgroup.")
1040
1041 (defvar gnus-newsgroup-scored nil
1042   "List of scored articles in the current newsgroup.")
1043
1044 (defvar gnus-newsgroup-headers nil
1045   "List of article headers in the current newsgroup.")
1046
1047 (defvar gnus-newsgroup-threads nil)
1048
1049 (defvar gnus-newsgroup-prepared nil
1050   "Whether the current group has been prepared properly.")
1051
1052 (defvar gnus-newsgroup-ancient nil
1053   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1054
1055 (defvar gnus-newsgroup-sparse nil)
1056
1057 (defvar gnus-current-article nil)
1058 (defvar gnus-article-current nil)
1059 (defvar gnus-current-headers nil)
1060 (defvar gnus-have-all-headers nil)
1061 (defvar gnus-last-article nil)
1062 (defvar gnus-newsgroup-history nil)
1063 (defvar gnus-newsgroup-charset nil)
1064 (defvar gnus-newsgroup-ephemeral-charset nil)
1065 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1066
1067 (defconst gnus-summary-local-variables
1068   '(gnus-newsgroup-name
1069     gnus-newsgroup-begin gnus-newsgroup-end
1070     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1071     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1072     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1073     gnus-newsgroup-unselected gnus-newsgroup-marked
1074     gnus-newsgroup-reads gnus-newsgroup-saved
1075     gnus-newsgroup-replied gnus-newsgroup-expirable
1076     gnus-newsgroup-processable gnus-newsgroup-killed
1077     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1078     gnus-newsgroup-unsendable
1079     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1080     gnus-newsgroup-headers gnus-newsgroup-threads
1081     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1082     gnus-current-article gnus-current-headers gnus-have-all-headers
1083     gnus-last-article gnus-article-internal-prepare-hook
1084     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1085     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1086     gnus-thread-expunge-below
1087     gnus-score-alist gnus-current-score-file
1088     (gnus-summary-expunge-below . global)
1089     (gnus-summary-mark-below . global)
1090     gnus-newsgroup-active gnus-scores-exclude-files
1091     gnus-newsgroup-history gnus-newsgroup-ancient
1092     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1093     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1094     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1095     (gnus-newsgroup-expunged-tally . 0)
1096     gnus-cache-removable-articles gnus-newsgroup-cached
1097     gnus-newsgroup-data gnus-newsgroup-data-reverse
1098     gnus-newsgroup-limit gnus-newsgroup-limits
1099     gnus-newsgroup-charset)
1100   "Variables that are buffer-local to the summary buffers.")
1101
1102 ;; Byte-compiler warning.
1103 (defvar gnus-article-mode-map)
1104
1105 ;; MIME stuff.
1106
1107 (defvar gnus-decode-encoded-word-methods
1108   '(mail-decode-encoded-word-string)
1109   "List of methods used to decode encoded words.
1110
1111 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1112 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1113 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1114 whose names match REGEXP.
1115
1116 For example:
1117 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1118  mail-decode-encoded-word-string
1119  (\"chinese\" . rfc1843-decode-string))
1120 ")
1121
1122 (defvar gnus-decode-encoded-word-methods-cache nil)
1123
1124 (defun gnus-multi-decode-encoded-word-string (string)
1125   "Apply the functions from `gnus-encoded-word-methods' that match."
1126   (unless (and gnus-decode-encoded-word-methods-cache
1127                (eq gnus-newsgroup-name
1128                    (car gnus-decode-encoded-word-methods-cache)))
1129     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1130     (mapcar (lambda (x)
1131               (if (symbolp x)
1132                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1133                 (if (and gnus-newsgroup-name
1134                          (string-match (car x) gnus-newsgroup-name))
1135                     (nconc gnus-decode-encoded-word-methods-cache
1136                            (list (cdr x))))))
1137           gnus-decode-encoded-word-methods))
1138   (let ((xlist gnus-decode-encoded-word-methods-cache))
1139     (pop xlist)
1140     (while xlist
1141       (setq string (funcall (pop xlist) string))))
1142   string)
1143
1144 ;; Subject simplification.
1145
1146 (defun gnus-simplify-whitespace (str)
1147   "Remove excessive whitespace."
1148   (let ((mystr str))
1149     ;; Multiple spaces.
1150     (while (string-match "[ \t][ \t]+" mystr)
1151       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1152                           " "
1153                           (substring mystr (match-end 0)))))
1154     ;; Leading spaces.
1155     (when (string-match "^[ \t]+" mystr)
1156       (setq mystr (substring mystr (match-end 0))))
1157     ;; Trailing spaces.
1158     (when (string-match "[ \t]+$" mystr)
1159       (setq mystr (substring mystr 0 (match-beginning 0))))
1160     mystr))
1161
1162 (defsubst gnus-simplify-subject-re (subject)
1163   "Remove \"Re:\" from subject lines."
1164   (if (string-match "^[Rr][Ee]: *" subject)
1165       (substring subject (match-end 0))
1166     subject))
1167
1168 (defun gnus-simplify-subject (subject &optional re-only)
1169   "Remove `Re:' and words in parentheses.
1170 If RE-ONLY is non-nil, strip leading `Re:'s only."
1171   (let ((case-fold-search t))           ;Ignore case.
1172     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1173     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1174       (setq subject (substring subject (match-end 0))))
1175     ;; Remove uninteresting prefixes.
1176     (when (and (not re-only)
1177                gnus-simplify-ignored-prefixes
1178                (string-match gnus-simplify-ignored-prefixes subject))
1179       (setq subject (substring subject (match-end 0))))
1180     ;; Remove words in parentheses from end.
1181     (unless re-only
1182       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1183         (setq subject (substring subject 0 (match-beginning 0)))))
1184     ;; Return subject string.
1185     subject))
1186
1187 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1188 ;; all whitespace.
1189 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1190   (goto-char (point-min))
1191   (while (re-search-forward regexp nil t)
1192     (replace-match (or newtext ""))))
1193
1194 (defun gnus-simplify-buffer-fuzzy ()
1195   "Simplify string in the buffer fuzzily.
1196 The string in the accessible portion of the current buffer is simplified.
1197 It is assumed to be a single-line subject.
1198 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1199 matter is removed.  Additional things can be deleted by setting
1200 gnus-simplify-subject-fuzzy-regexp."
1201   (let ((case-fold-search t)
1202         (modified-tick))
1203     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1204
1205     (while (not (eq modified-tick (buffer-modified-tick)))
1206       (setq modified-tick (buffer-modified-tick))
1207       (cond
1208        ((listp gnus-simplify-subject-fuzzy-regexp)
1209         (mapcar 'gnus-simplify-buffer-fuzzy-step
1210                 gnus-simplify-subject-fuzzy-regexp))
1211        (gnus-simplify-subject-fuzzy-regexp
1212         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1213       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1214       (gnus-simplify-buffer-fuzzy-step
1215        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1216       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1217
1218     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1219     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1220     (gnus-simplify-buffer-fuzzy-step " $")
1221     (gnus-simplify-buffer-fuzzy-step "^ +")))
1222
1223 (defun gnus-simplify-subject-fuzzy (subject)
1224   "Simplify a subject string fuzzily.
1225 See `gnus-simplify-buffer-fuzzy' for details."
1226   (save-excursion
1227     (gnus-set-work-buffer)
1228     (let ((case-fold-search t))
1229       ;; Remove uninteresting prefixes.
1230       (when (and gnus-simplify-ignored-prefixes
1231                  (string-match gnus-simplify-ignored-prefixes subject))
1232         (setq subject (substring subject (match-end 0))))
1233       (insert subject)
1234       (inline (gnus-simplify-buffer-fuzzy))
1235       (buffer-string))))
1236
1237 (defsubst gnus-simplify-subject-fully (subject)
1238   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1239   (cond
1240    (gnus-simplify-subject-functions
1241     (gnus-map-function gnus-simplify-subject-functions subject))
1242    ((null gnus-summary-gather-subject-limit)
1243     (gnus-simplify-subject-re subject))
1244    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1245     (gnus-simplify-subject-fuzzy subject))
1246    ((numberp gnus-summary-gather-subject-limit)
1247     (gnus-limit-string (gnus-simplify-subject-re subject)
1248                        gnus-summary-gather-subject-limit))
1249    (t
1250     subject)))
1251
1252 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1253   "Check whether two subjects are equal.
1254 If optional argument simple-first is t, first argument is already
1255 simplified."
1256   (cond
1257    ((null simple-first)
1258     (equal (gnus-simplify-subject-fully s1)
1259            (gnus-simplify-subject-fully s2)))
1260    (t
1261     (equal s1
1262            (gnus-simplify-subject-fully s2)))))
1263
1264 (defun gnus-summary-bubble-group ()
1265   "Increase the score of the current group.
1266 This is a handy function to add to `gnus-summary-exit-hook' to
1267 increase the score of each group you read."
1268   (gnus-group-add-score gnus-newsgroup-name))
1269
1270 \f
1271 ;;;
1272 ;;; Gnus summary mode
1273 ;;;
1274
1275 (put 'gnus-summary-mode 'mode-class 'special)
1276
1277 (when t
1278   ;; Non-orthogonal keys
1279
1280   (gnus-define-keys gnus-summary-mode-map
1281     " " gnus-summary-next-page
1282     "\177" gnus-summary-prev-page
1283     [delete] gnus-summary-prev-page
1284     [backspace] gnus-summary-prev-page
1285     "\r" gnus-summary-scroll-up
1286     "\M-\r" gnus-summary-scroll-down
1287     "n" gnus-summary-next-unread-article
1288     "p" gnus-summary-prev-unread-article
1289     "N" gnus-summary-next-article
1290     "P" gnus-summary-prev-article
1291     "\M-\C-n" gnus-summary-next-same-subject
1292     "\M-\C-p" gnus-summary-prev-same-subject
1293     "\M-n" gnus-summary-next-unread-subject
1294     "\M-p" gnus-summary-prev-unread-subject
1295     "." gnus-summary-first-unread-article
1296     "," gnus-summary-best-unread-article
1297     "\M-s" gnus-summary-search-article-forward
1298     "\M-r" gnus-summary-search-article-backward
1299     "<" gnus-summary-beginning-of-article
1300     ">" gnus-summary-end-of-article
1301     "j" gnus-summary-goto-article
1302     "^" gnus-summary-refer-parent-article
1303     "\M-^" gnus-summary-refer-article
1304     "u" gnus-summary-tick-article-forward
1305     "!" gnus-summary-tick-article-forward
1306     "U" gnus-summary-tick-article-backward
1307     "d" gnus-summary-mark-as-read-forward
1308     "D" gnus-summary-mark-as-read-backward
1309     "E" gnus-summary-mark-as-expirable
1310     "\M-u" gnus-summary-clear-mark-forward
1311     "\M-U" gnus-summary-clear-mark-backward
1312     "k" gnus-summary-kill-same-subject-and-select
1313     "\C-k" gnus-summary-kill-same-subject
1314     "\M-\C-k" gnus-summary-kill-thread
1315     "\M-\C-l" gnus-summary-lower-thread
1316     "e" gnus-summary-edit-article
1317     "#" gnus-summary-mark-as-processable
1318     "\M-#" gnus-summary-unmark-as-processable
1319     "\M-\C-t" gnus-summary-toggle-threads
1320     "\M-\C-s" gnus-summary-show-thread
1321     "\M-\C-h" gnus-summary-hide-thread
1322     "\M-\C-f" gnus-summary-next-thread
1323     "\M-\C-b" gnus-summary-prev-thread
1324     "\M-\C-u" gnus-summary-up-thread
1325     "\M-\C-d" gnus-summary-down-thread
1326     "&" gnus-summary-execute-command
1327     "c" gnus-summary-catchup-and-exit
1328     "\C-w" gnus-summary-mark-region-as-read
1329     "\C-t" gnus-summary-toggle-truncation
1330     "?" gnus-summary-mark-as-dormant
1331     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1332     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1333     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1334     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1335     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1336     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1337     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1338     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1339     "=" gnus-summary-expand-window
1340     "\C-x\C-s" gnus-summary-reselect-current-group
1341     "\M-g" gnus-summary-rescan-group
1342     "w" gnus-summary-stop-page-breaking
1343     "\C-c\C-r" gnus-summary-caesar-message
1344     "f" gnus-summary-followup
1345     "F" gnus-summary-followup-with-original
1346     "C" gnus-summary-cancel-article
1347     "r" gnus-summary-reply
1348     "R" gnus-summary-reply-with-original
1349     "\C-c\C-f" gnus-summary-mail-forward
1350     "o" gnus-summary-save-article
1351     "\C-o" gnus-summary-save-article-mail
1352     "|" gnus-summary-pipe-output
1353     "\M-k" gnus-summary-edit-local-kill
1354     "\M-K" gnus-summary-edit-global-kill
1355     ;; "V" gnus-version
1356     "\C-c\C-d" gnus-summary-describe-group
1357     "q" gnus-summary-exit
1358     "Q" gnus-summary-exit-no-update
1359     "\C-c\C-i" gnus-info-find-node
1360     gnus-mouse-2 gnus-mouse-pick-article
1361     "m" gnus-summary-mail-other-window
1362     "a" gnus-summary-post-news
1363     "x" gnus-summary-limit-to-unread
1364     "s" gnus-summary-isearch-article
1365     "t" gnus-summary-toggle-header
1366     "g" gnus-summary-show-article
1367     "l" gnus-summary-goto-last-article
1368     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1369     "\C-d" gnus-summary-enter-digest-group
1370     "\M-\C-d" gnus-summary-read-document
1371     "\M-\C-e" gnus-summary-edit-parameters
1372     "\M-\C-a" gnus-summary-customize-parameters
1373     "\C-c\C-b" gnus-bug
1374     "*" gnus-cache-enter-article
1375     "\M-*" gnus-cache-remove-article
1376     "\M-&" gnus-summary-universal-argument
1377     "\C-l" gnus-recenter
1378     "I" gnus-summary-increase-score
1379     "L" gnus-summary-lower-score
1380     "\M-i" gnus-symbolic-argument
1381     "h" gnus-summary-select-article-buffer
1382
1383     "b" gnus-article-view-part
1384     "\M-t" gnus-summary-toggle-display-buttonized
1385
1386     "V" gnus-summary-score-map
1387     "X" gnus-uu-extract-map
1388     "S" gnus-summary-send-map)
1389
1390   ;; Sort of orthogonal keymap
1391   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1392     "t" gnus-summary-tick-article-forward
1393     "!" gnus-summary-tick-article-forward
1394     "d" gnus-summary-mark-as-read-forward
1395     "r" gnus-summary-mark-as-read-forward
1396     "c" gnus-summary-clear-mark-forward
1397     " " gnus-summary-clear-mark-forward
1398     "e" gnus-summary-mark-as-expirable
1399     "x" gnus-summary-mark-as-expirable
1400     "?" gnus-summary-mark-as-dormant
1401     "b" gnus-summary-set-bookmark
1402     "B" gnus-summary-remove-bookmark
1403     "#" gnus-summary-mark-as-processable
1404     "\M-#" gnus-summary-unmark-as-processable
1405     "S" gnus-summary-limit-include-expunged
1406     "C" gnus-summary-catchup
1407     "H" gnus-summary-catchup-to-here
1408     "\C-c" gnus-summary-catchup-all
1409     "k" gnus-summary-kill-same-subject-and-select
1410     "K" gnus-summary-kill-same-subject
1411     "P" gnus-uu-mark-map)
1412
1413   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1414     "c" gnus-summary-clear-above
1415     "u" gnus-summary-tick-above
1416     "m" gnus-summary-mark-above
1417     "k" gnus-summary-kill-below)
1418
1419   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1420     "/" gnus-summary-limit-to-subject
1421     "n" gnus-summary-limit-to-articles
1422     "w" gnus-summary-pop-limit
1423     "s" gnus-summary-limit-to-subject
1424     "a" gnus-summary-limit-to-author
1425     "u" gnus-summary-limit-to-unread
1426     "m" gnus-summary-limit-to-marks
1427     "M" gnus-summary-limit-exclude-marks
1428     "v" gnus-summary-limit-to-score
1429     "*" gnus-summary-limit-include-cached
1430     "D" gnus-summary-limit-include-dormant
1431     "T" gnus-summary-limit-include-thread
1432     "d" gnus-summary-limit-exclude-dormant
1433     "t" gnus-summary-limit-to-age
1434     "x" gnus-summary-limit-to-extra 
1435     "E" gnus-summary-limit-include-expunged
1436     "c" gnus-summary-limit-exclude-childless-dormant
1437     "C" gnus-summary-limit-mark-excluded-as-read)
1438
1439   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1440     "n" gnus-summary-next-unread-article
1441     "p" gnus-summary-prev-unread-article
1442     "N" gnus-summary-next-article
1443     "P" gnus-summary-prev-article
1444     "\C-n" gnus-summary-next-same-subject
1445     "\C-p" gnus-summary-prev-same-subject
1446     "\M-n" gnus-summary-next-unread-subject
1447     "\M-p" gnus-summary-prev-unread-subject
1448     "f" gnus-summary-first-unread-article
1449     "b" gnus-summary-best-unread-article
1450     "j" gnus-summary-goto-article
1451     "g" gnus-summary-goto-subject
1452     "l" gnus-summary-goto-last-article
1453     "o" gnus-summary-pop-article)
1454
1455   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1456     "k" gnus-summary-kill-thread
1457     "l" gnus-summary-lower-thread
1458     "i" gnus-summary-raise-thread
1459     "T" gnus-summary-toggle-threads
1460     "t" gnus-summary-rethread-current
1461     "^" gnus-summary-reparent-thread
1462     "s" gnus-summary-show-thread
1463     "S" gnus-summary-show-all-threads
1464     "h" gnus-summary-hide-thread
1465     "H" gnus-summary-hide-all-threads
1466     "n" gnus-summary-next-thread
1467     "p" gnus-summary-prev-thread
1468     "u" gnus-summary-up-thread
1469     "o" gnus-summary-top-thread
1470     "d" gnus-summary-down-thread
1471     "#" gnus-uu-mark-thread
1472     "\M-#" gnus-uu-unmark-thread)
1473
1474   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1475     "g" gnus-summary-prepare
1476     "c" gnus-summary-insert-cached-articles)
1477
1478   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1479     "c" gnus-summary-catchup-and-exit
1480     "C" gnus-summary-catchup-all-and-exit
1481     "E" gnus-summary-exit-no-update
1482     "Q" gnus-summary-exit
1483     "Z" gnus-summary-exit
1484     "n" gnus-summary-catchup-and-goto-next-group
1485     "R" gnus-summary-reselect-current-group
1486     "G" gnus-summary-rescan-group
1487     "N" gnus-summary-next-group
1488     "s" gnus-summary-save-newsrc
1489     "P" gnus-summary-prev-group)
1490
1491   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1492     " " gnus-summary-next-page
1493     "n" gnus-summary-next-page
1494     "\177" gnus-summary-prev-page
1495     [delete] gnus-summary-prev-page
1496     "p" gnus-summary-prev-page
1497     "\r" gnus-summary-scroll-up
1498     "\M-\r" gnus-summary-scroll-down
1499     "<" gnus-summary-beginning-of-article
1500     ">" gnus-summary-end-of-article
1501     "b" gnus-summary-beginning-of-article
1502     "e" gnus-summary-end-of-article
1503     "^" gnus-summary-refer-parent-article
1504     "r" gnus-summary-refer-parent-article
1505     "D" gnus-summary-enter-digest-group
1506     "R" gnus-summary-refer-references
1507     "T" gnus-summary-refer-thread
1508     "g" gnus-summary-show-article
1509     "s" gnus-summary-isearch-article
1510     "P" gnus-summary-print-article
1511     "t" gnus-article-babel)
1512
1513   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1514     "b" gnus-article-add-buttons
1515     "B" gnus-article-add-buttons-to-head
1516     "o" gnus-article-treat-overstrike
1517     "e" gnus-article-emphasize
1518     "w" gnus-article-fill-cited-article
1519     "Q" gnus-article-fill-long-lines
1520     "C" gnus-article-capitalize-sentences
1521     "c" gnus-article-remove-cr
1522     "q" gnus-article-de-quoted-unreadable
1523     "f" gnus-article-display-x-face
1524     "l" gnus-summary-stop-page-breaking
1525     "r" gnus-summary-caesar-message
1526     "t" gnus-article-hide-headers
1527     "v" gnus-summary-verbose-headers
1528     "H" gnus-article-strip-headers-in-body
1529     "d" gnus-article-treat-dumbquotes)
1530
1531   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1532     "a" gnus-article-hide
1533     "h" gnus-article-hide-headers
1534     "b" gnus-article-hide-boring-headers
1535     "s" gnus-article-hide-signature
1536     "c" gnus-article-hide-citation
1537     "C" gnus-article-hide-citation-in-followups
1538     "l" gnus-article-hide-list-identifiers
1539     "p" gnus-article-hide-pgp
1540     "B" gnus-article-strip-banner
1541     "P" gnus-article-hide-pem
1542     "\C-c" gnus-article-hide-citation-maybe)
1543
1544   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1545     "a" gnus-article-highlight
1546     "h" gnus-article-highlight-headers
1547     "c" gnus-article-highlight-citation
1548     "s" gnus-article-highlight-signature)
1549
1550   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1551     "w" gnus-article-decode-mime-words
1552     "c" gnus-article-decode-charset
1553     "v" gnus-mime-view-all-parts
1554     "b" gnus-article-view-part)
1555
1556   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1557     "z" gnus-article-date-ut
1558     "u" gnus-article-date-ut
1559     "l" gnus-article-date-local
1560     "e" gnus-article-date-lapsed
1561     "o" gnus-article-date-original
1562     "i" gnus-article-date-iso8601
1563     "s" gnus-article-date-user)
1564
1565   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1566     "t" gnus-article-remove-trailing-blank-lines
1567     "l" gnus-article-strip-leading-blank-lines
1568     "m" gnus-article-strip-multiple-blank-lines
1569     "a" gnus-article-strip-blank-lines
1570     "A" gnus-article-strip-all-blank-lines
1571     "s" gnus-article-strip-leading-space
1572     "e" gnus-article-strip-trailing-space)
1573
1574   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1575     "v" gnus-version
1576     "f" gnus-summary-fetch-faq
1577     "d" gnus-summary-describe-group
1578     "h" gnus-summary-describe-briefly
1579     "i" gnus-info-find-node)
1580
1581   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1582     "e" gnus-summary-expire-articles
1583     "\M-\C-e" gnus-summary-expire-articles-now
1584     "\177" gnus-summary-delete-article
1585     [delete] gnus-summary-delete-article
1586     [backspace] gnus-summary-delete-article
1587     "m" gnus-summary-move-article
1588     "r" gnus-summary-respool-article
1589     "w" gnus-summary-edit-article
1590     "c" gnus-summary-copy-article
1591     "B" gnus-summary-crosspost-article
1592     "q" gnus-summary-respool-query
1593     "t" gnus-summary-respool-trace
1594     "i" gnus-summary-import-article
1595     "p" gnus-summary-article-posted-p)
1596
1597   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1598     "o" gnus-summary-save-article
1599     "m" gnus-summary-save-article-mail
1600     "F" gnus-summary-write-article-file
1601     "r" gnus-summary-save-article-rmail
1602     "f" gnus-summary-save-article-file
1603     "b" gnus-summary-save-article-body-file
1604     "h" gnus-summary-save-article-folder
1605     "v" gnus-summary-save-article-vm
1606     "p" gnus-summary-pipe-output
1607     "s" gnus-soup-add-article)
1608
1609   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1610     "b" gnus-summary-display-buttonized
1611     "m" gnus-summary-repair-multipart
1612     "v" gnus-article-view-part
1613     "o" gnus-article-save-part
1614     "c" gnus-article-copy-part
1615     "e" gnus-article-externalize-part
1616     "i" gnus-article-inline-part
1617     "|" gnus-article-pipe-part))
1618
1619 (defun gnus-summary-make-menu-bar ()
1620   (gnus-turn-off-edit-menu 'summary)
1621
1622   (unless (boundp 'gnus-summary-misc-menu)
1623
1624     (easy-menu-define
1625      gnus-summary-kill-menu gnus-summary-mode-map ""
1626      (cons
1627       "Score"
1628       (nconc
1629        (list
1630         ["Enter score..." gnus-summary-score-entry t]
1631         ["Customize" gnus-score-customize t])
1632        (gnus-make-score-map 'increase)
1633        (gnus-make-score-map 'lower)
1634        '(("Mark"
1635           ["Kill below" gnus-summary-kill-below t]
1636           ["Mark above" gnus-summary-mark-above t]
1637           ["Tick above" gnus-summary-tick-above t]
1638           ["Clear above" gnus-summary-clear-above t])
1639          ["Current score" gnus-summary-current-score t]
1640          ["Set score" gnus-summary-set-score t]
1641          ["Switch current score file..." gnus-score-change-score-file t]
1642          ["Set mark below..." gnus-score-set-mark-below t]
1643          ["Set expunge below..." gnus-score-set-expunge-below t]
1644          ["Edit current score file" gnus-score-edit-current-scores t]
1645          ["Edit score file" gnus-score-edit-file t]
1646          ["Trace score" gnus-score-find-trace t]
1647          ["Find words" gnus-score-find-favourite-words t]
1648          ["Rescore buffer" gnus-summary-rescore t]
1649          ["Increase score..." gnus-summary-increase-score t]
1650          ["Lower score..." gnus-summary-lower-score t]))))
1651
1652     ;; Define both the Article menu in the summary buffer and the equivalent
1653     ;; Commands menu in the article buffer here for consistency.
1654     (let ((innards
1655            '(("Hide"
1656               ["All" gnus-article-hide t]
1657               ["Headers" gnus-article-hide-headers t]
1658               ["Signature" gnus-article-hide-signature t]
1659               ["Citation" gnus-article-hide-citation t]
1660               ["List identifiers" gnus-article-hide-list-identifiers t]
1661               ["PGP" gnus-article-hide-pgp t]
1662               ["Banner" gnus-article-strip-banner t]
1663               ["Boring headers" gnus-article-hide-boring-headers t])
1664              ("Highlight"
1665               ["All" gnus-article-highlight t]
1666               ["Headers" gnus-article-highlight-headers t]
1667               ["Signature" gnus-article-highlight-signature t]
1668               ["Citation" gnus-article-highlight-citation t])
1669              ("MIME"
1670               ["Words" gnus-article-decode-mime-words t]
1671               ["Charset" gnus-article-decode-charset t]
1672               ["QP" gnus-article-de-quoted-unreadable t]
1673               ["View all" gnus-mime-view-all-parts t])
1674              ("Date"
1675               ["Local" gnus-article-date-local t]
1676               ["ISO8601" gnus-article-date-iso8601 t]
1677               ["UT" gnus-article-date-ut t]
1678               ["Original" gnus-article-date-original t]
1679               ["Lapsed" gnus-article-date-lapsed t]
1680               ["User-defined" gnus-article-date-user t])
1681              ("Washing"
1682               ("Remove Blanks"
1683                ["Leading" gnus-article-strip-leading-blank-lines t]
1684                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1685                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1686                ["All of the above" gnus-article-strip-blank-lines t]
1687                ["All" gnus-article-strip-all-blank-lines t]
1688                ["Leading space" gnus-article-strip-leading-space t]
1689                ["Trailing space" gnus-article-strip-trailing-space t])
1690               ["Overstrike" gnus-article-treat-overstrike t]
1691               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1692               ["Emphasis" gnus-article-emphasize t]
1693               ["Word wrap" gnus-article-fill-cited-article t]
1694               ["Fill long lines" gnus-article-fill-long-lines t]
1695               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1696               ["CR" gnus-article-remove-cr t]
1697               ["Show X-Face" gnus-article-display-x-face t]
1698               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1699               ["Rot 13" gnus-summary-caesar-message t]
1700               ["Unix pipe" gnus-summary-pipe-message t]
1701               ["Add buttons" gnus-article-add-buttons t]
1702               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1703               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1704               ["Verbose header" gnus-summary-verbose-headers t]
1705               ["Toggle header" gnus-summary-toggle-header t]
1706               ["HZ" gnus-article-decode-HZ t])
1707              ("Output"
1708               ["Save in default format" gnus-summary-save-article t]
1709               ["Save in file" gnus-summary-save-article-file t]
1710               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1711               ["Save in MH folder" gnus-summary-save-article-folder t]
1712               ["Save in VM folder" gnus-summary-save-article-vm t]
1713               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1714               ["Save body in file" gnus-summary-save-article-body-file t]
1715               ["Pipe through a filter" gnus-summary-pipe-output t]
1716               ["Add to SOUP packet" gnus-soup-add-article t]
1717               ["Print" gnus-summary-print-article t])
1718              ("Backend"
1719               ["Respool article..." gnus-summary-respool-article t]
1720               ["Move article..." gnus-summary-move-article
1721                (gnus-check-backend-function
1722                 'request-move-article gnus-newsgroup-name)]
1723               ["Copy article..." gnus-summary-copy-article t]
1724               ["Crosspost article..." gnus-summary-crosspost-article
1725                (gnus-check-backend-function
1726                 'request-replace-article gnus-newsgroup-name)]
1727               ["Import file..." gnus-summary-import-article t]
1728               ["Check if posted" gnus-summary-article-posted-p t]
1729               ["Edit article" gnus-summary-edit-article
1730                (not (gnus-group-read-only-p))]
1731               ["Delete article" gnus-summary-delete-article
1732                (gnus-check-backend-function
1733                 'request-expire-articles gnus-newsgroup-name)]
1734               ["Query respool" gnus-summary-respool-query t]
1735               ["Trace respool" gnus-summary-respool-trace t]
1736               ["Delete expirable articles" gnus-summary-expire-articles-now
1737                (gnus-check-backend-function
1738                 'request-expire-articles gnus-newsgroup-name)])
1739              ("Extract"
1740               ["Uudecode" gnus-uu-decode-uu t]
1741               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1742               ["Unshar" gnus-uu-decode-unshar t]
1743               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1744               ["Save" gnus-uu-decode-save t]
1745               ["Binhex" gnus-uu-decode-binhex t]
1746               ["Postscript" gnus-uu-decode-postscript t])
1747              ("Cache"
1748               ["Enter article" gnus-cache-enter-article t]
1749               ["Remove article" gnus-cache-remove-article t])
1750              ["Translate" gnus-article-babel t]
1751              ["Select article buffer" gnus-summary-select-article-buffer t]
1752              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1753              ["Isearch article..." gnus-summary-isearch-article t]
1754              ["Beginning of the article" gnus-summary-beginning-of-article t]
1755              ["End of the article" gnus-summary-end-of-article t]
1756              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1757              ["Fetch referenced articles" gnus-summary-refer-references t]
1758              ["Fetch current thread" gnus-summary-refer-thread t]
1759              ["Fetch article with id..." gnus-summary-refer-article t]
1760              ["Redisplay" gnus-summary-show-article t])))
1761       (easy-menu-define
1762        gnus-summary-article-menu gnus-summary-mode-map ""
1763        (cons "Article" innards))
1764
1765       (easy-menu-define
1766        gnus-article-commands-menu gnus-article-mode-map ""
1767        (cons "Commands" innards)))
1768
1769     (easy-menu-define
1770      gnus-summary-thread-menu gnus-summary-mode-map ""
1771      '("Threads"
1772        ["Toggle threading" gnus-summary-toggle-threads t]
1773        ["Hide threads" gnus-summary-hide-all-threads t]
1774        ["Show threads" gnus-summary-show-all-threads t]
1775        ["Hide thread" gnus-summary-hide-thread t]
1776        ["Show thread" gnus-summary-show-thread t]
1777        ["Go to next thread" gnus-summary-next-thread t]
1778        ["Go to previous thread" gnus-summary-prev-thread t]
1779        ["Go down thread" gnus-summary-down-thread t]
1780        ["Go up thread" gnus-summary-up-thread t]
1781        ["Top of thread" gnus-summary-top-thread t]
1782        ["Mark thread as read" gnus-summary-kill-thread t]
1783        ["Lower thread score" gnus-summary-lower-thread t]
1784        ["Raise thread score" gnus-summary-raise-thread t]
1785        ["Rethread current" gnus-summary-rethread-current t]))
1786
1787     (easy-menu-define
1788      gnus-summary-post-menu gnus-summary-mode-map ""
1789      '("Post"
1790        ["Post an article" gnus-summary-post-news t]
1791        ["Followup" gnus-summary-followup t]
1792        ["Followup and yank" gnus-summary-followup-with-original t]
1793        ["Supersede article" gnus-summary-supersede-article t]
1794        ["Cancel article" gnus-summary-cancel-article t]
1795        ["Reply" gnus-summary-reply t]
1796        ["Reply and yank" gnus-summary-reply-with-original t]
1797        ["Wide reply" gnus-summary-wide-reply t]
1798        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1799        ["Mail forward" gnus-summary-mail-forward t]
1800        ["Post forward" gnus-summary-post-forward t]
1801        ["Digest and mail" gnus-uu-digest-mail-forward t]
1802        ["Digest and post" gnus-uu-digest-post-forward t]
1803        ["Resend message" gnus-summary-resend-message t]
1804        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1805        ["Send a mail" gnus-summary-mail-other-window t]
1806        ["Uuencode and post" gnus-uu-post-news t]
1807        ["Followup via news" gnus-summary-followup-to-mail t]
1808        ["Followup via news and yank"
1809         gnus-summary-followup-to-mail-with-original t]
1810        ;;("Draft"
1811        ;;["Send" gnus-summary-send-draft t]
1812        ;;["Send bounced" gnus-resend-bounced-mail t])
1813        ))
1814
1815     (easy-menu-define
1816      gnus-summary-misc-menu gnus-summary-mode-map ""
1817      '("Misc"
1818        ("Mark Read"
1819         ["Mark as read" gnus-summary-mark-as-read-forward t]
1820         ["Mark same subject and select"
1821          gnus-summary-kill-same-subject-and-select t]
1822         ["Mark same subject" gnus-summary-kill-same-subject t]
1823         ["Catchup" gnus-summary-catchup t]
1824         ["Catchup all" gnus-summary-catchup-all t]
1825         ["Catchup to here" gnus-summary-catchup-to-here t]
1826         ["Catchup region" gnus-summary-mark-region-as-read t]
1827         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1828        ("Mark Various"
1829         ["Tick" gnus-summary-tick-article-forward t]
1830         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1831         ["Remove marks" gnus-summary-clear-mark-forward t]
1832         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1833         ["Set bookmark" gnus-summary-set-bookmark t]
1834         ["Remove bookmark" gnus-summary-remove-bookmark t])
1835        ("Mark Limit"
1836         ["Marks..." gnus-summary-limit-to-marks t]
1837         ["Subject..." gnus-summary-limit-to-subject t]
1838         ["Author..." gnus-summary-limit-to-author t]
1839         ["Age..." gnus-summary-limit-to-age t]
1840         ["Extra..." gnus-summary-limit-to-extra t]
1841         ["Score" gnus-summary-limit-to-score t]
1842         ["Unread" gnus-summary-limit-to-unread t]
1843         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1844         ["Articles" gnus-summary-limit-to-articles t]
1845         ["Pop limit" gnus-summary-pop-limit t]
1846         ["Show dormant" gnus-summary-limit-include-dormant t]
1847         ["Hide childless dormant"
1848          gnus-summary-limit-exclude-childless-dormant t]
1849         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1850         ["Hide marked" gnus-summary-limit-exclude-marks t]
1851         ["Show expunged" gnus-summary-show-all-expunged t])
1852        ("Process Mark"
1853         ["Set mark" gnus-summary-mark-as-processable t]
1854         ["Remove mark" gnus-summary-unmark-as-processable t]
1855         ["Remove all marks" gnus-summary-unmark-all-processable t]
1856         ["Mark above" gnus-uu-mark-over t]
1857         ["Mark series" gnus-uu-mark-series t]
1858         ["Mark region" gnus-uu-mark-region t]
1859         ["Unmark region" gnus-uu-unmark-region t]
1860         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1861         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1862         ["Mark all" gnus-uu-mark-all t]
1863         ["Mark buffer" gnus-uu-mark-buffer t]
1864         ["Mark sparse" gnus-uu-mark-sparse t]
1865         ["Mark thread" gnus-uu-mark-thread t]
1866         ["Unmark thread" gnus-uu-unmark-thread t]
1867         ("Process Mark Sets"
1868          ["Kill" gnus-summary-kill-process-mark t]
1869          ["Yank" gnus-summary-yank-process-mark
1870           gnus-newsgroup-process-stack]
1871          ["Save" gnus-summary-save-process-mark t]))
1872        ("Scroll article"
1873         ["Page forward" gnus-summary-next-page t]
1874         ["Page backward" gnus-summary-prev-page t]
1875         ["Line forward" gnus-summary-scroll-up t])
1876        ("Move"
1877         ["Next unread article" gnus-summary-next-unread-article t]
1878         ["Previous unread article" gnus-summary-prev-unread-article t]
1879         ["Next article" gnus-summary-next-article t]
1880         ["Previous article" gnus-summary-prev-article t]
1881         ["Next unread subject" gnus-summary-next-unread-subject t]
1882         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1883         ["Next article same subject" gnus-summary-next-same-subject t]
1884         ["Previous article same subject" gnus-summary-prev-same-subject t]
1885         ["First unread article" gnus-summary-first-unread-article t]
1886         ["Best unread article" gnus-summary-best-unread-article t]
1887         ["Go to subject number..." gnus-summary-goto-subject t]
1888         ["Go to article number..." gnus-summary-goto-article t]
1889         ["Go to the last article" gnus-summary-goto-last-article t]
1890         ["Pop article off history" gnus-summary-pop-article t])
1891        ("Sort"
1892         ["Sort by number" gnus-summary-sort-by-number t]
1893         ["Sort by author" gnus-summary-sort-by-author t]
1894         ["Sort by subject" gnus-summary-sort-by-subject t]
1895         ["Sort by date" gnus-summary-sort-by-date t]
1896         ["Sort by score" gnus-summary-sort-by-score t]
1897         ["Sort by lines" gnus-summary-sort-by-lines t]
1898         ["Sort by characters" gnus-summary-sort-by-chars t])
1899        ("Help"
1900         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1901         ["Describe group" gnus-summary-describe-group t]
1902         ["Read manual" gnus-info-find-node t])
1903        ("Modes"
1904         ["Pick and read" gnus-pick-mode t]
1905         ["Binary" gnus-binary-mode t])
1906        ("Regeneration"
1907         ["Regenerate" gnus-summary-prepare t]
1908         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1909         ["Toggle threading" gnus-summary-toggle-threads t])
1910        ["Filter articles..." gnus-summary-execute-command t]
1911        ["Run command on subjects..." gnus-summary-universal-argument t]
1912        ["Search articles forward..." gnus-summary-search-article-forward t]
1913        ["Search articles backward..." gnus-summary-search-article-backward t]
1914        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1915        ["Expand window" gnus-summary-expand-window t]
1916        ["Expire expirable articles" gnus-summary-expire-articles
1917         (gnus-check-backend-function
1918          'request-expire-articles gnus-newsgroup-name)]
1919        ["Edit local kill file" gnus-summary-edit-local-kill t]
1920        ["Edit main kill file" gnus-summary-edit-global-kill t]
1921        ["Edit group parameters" gnus-summary-edit-parameters t]
1922        ["Customize group parameters" gnus-summary-customize-parameters t]
1923        ["Send a bug report" gnus-bug t]
1924        ("Exit"
1925         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1926         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1927         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1928         ["Exit group" gnus-summary-exit t]
1929         ["Exit group without updating" gnus-summary-exit-no-update t]
1930         ["Exit and goto next group" gnus-summary-next-group t]
1931         ["Exit and goto prev group" gnus-summary-prev-group t]
1932         ["Reselect group" gnus-summary-reselect-current-group t]
1933         ["Rescan group" gnus-summary-rescan-group t]
1934         ["Update dribble" gnus-summary-save-newsrc t])))
1935
1936     (gnus-run-hooks 'gnus-summary-menu-hook)))
1937
1938 (defun gnus-score-set-default (var value)
1939   "A version of set that updates the GNU Emacs menu-bar."
1940   (set var value)
1941   ;; It is the message that forces the active status to be updated.
1942   (message ""))
1943
1944 (defun gnus-make-score-map (type)
1945   "Make a summary score map of type TYPE."
1946   (if t
1947       nil
1948     (let ((headers '(("author" "from" string)
1949                      ("subject" "subject" string)
1950                      ("article body" "body" string)
1951                      ("article head" "head" string)
1952                      ("xref" "xref" string)
1953                      ("extra header" "extra" string)
1954                      ("lines" "lines" number)
1955                      ("followups to author" "followup" string)))
1956           (types '((number ("less than" <)
1957                            ("greater than" >)
1958                            ("equal" =))
1959                    (string ("substring" s)
1960                            ("exact string" e)
1961                            ("fuzzy string" f)
1962                            ("regexp" r))))
1963           (perms '(("temporary" (current-time-string))
1964                    ("permanent" nil)
1965                    ("immediate" now)))
1966           header)
1967       (list
1968        (apply
1969         'nconc
1970         (list
1971          (if (eq type 'lower)
1972              "Lower score"
1973            "Increase score"))
1974         (let (outh)
1975           (while headers
1976             (setq header (car headers))
1977             (setq outh
1978                   (cons
1979                    (apply
1980                     'nconc
1981                     (list (car header))
1982                     (let ((ts (cdr (assoc (nth 2 header) types)))
1983                           outt)
1984                       (while ts
1985                         (setq outt
1986                               (cons
1987                                (apply
1988                                 'nconc
1989                                 (list (caar ts))
1990                                 (let ((ps perms)
1991                                       outp)
1992                                   (while ps
1993                                     (setq outp
1994                                           (cons
1995                                            (vector
1996                                             (caar ps)
1997                                             (list
1998                                              'gnus-summary-score-entry
1999                                              (nth 1 header)
2000                                              (if (or (string= (nth 1 header)
2001                                                               "head")
2002                                                      (string= (nth 1 header)
2003                                                               "body"))
2004                                                  ""
2005                                                (list 'gnus-summary-header
2006                                                      (nth 1 header)))
2007                                              (list 'quote (nth 1 (car ts)))
2008                                              (list 'gnus-score-delta-default
2009                                                    nil)
2010                                              (nth 1 (car ps))
2011                                              t)
2012                                             t)
2013                                            outp))
2014                                     (setq ps (cdr ps)))
2015                                   (list (nreverse outp))))
2016                                outt))
2017                         (setq ts (cdr ts)))
2018                       (list (nreverse outt))))
2019                    outh))
2020             (setq headers (cdr headers)))
2021           (list (nreverse outh))))))))
2022
2023 \f
2024
2025 (defun gnus-summary-mode (&optional group)
2026   "Major mode for reading articles.
2027
2028 All normal editing commands are switched off.
2029 \\<gnus-summary-mode-map>
2030 Each line in this buffer represents one article.  To read an
2031 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2032 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2033 respectively.
2034
2035 You can also post articles and send mail from this buffer.  To
2036 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2037 of an article, type `\\[gnus-summary-reply]'.
2038
2039 There are approx. one gazillion commands you can execute in this
2040 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2041
2042 The following commands are available:
2043
2044 \\{gnus-summary-mode-map}"
2045   (interactive)
2046   (when (gnus-visual-p 'summary-menu 'menu)
2047     (gnus-summary-make-menu-bar))
2048   (kill-all-local-variables)
2049   (gnus-summary-make-local-variables)
2050   (gnus-make-thread-indent-array)
2051   (gnus-simplify-mode-line)
2052   (setq major-mode 'gnus-summary-mode)
2053   (setq mode-name "Summary")
2054   (make-local-variable 'minor-mode-alist)
2055   (use-local-map gnus-summary-mode-map)
2056   (buffer-disable-undo)
2057   (setq buffer-read-only t)             ;Disable modification
2058   (setq truncate-lines t)
2059   (setq selective-display t)
2060   (setq selective-display-ellipses t)   ;Display `...'
2061   (gnus-summary-set-display-table)
2062   (gnus-set-default-directory)
2063   (setq gnus-newsgroup-name group)
2064   (make-local-variable 'gnus-summary-line-format)
2065   (make-local-variable 'gnus-summary-line-format-spec)
2066   (make-local-variable 'gnus-summary-dummy-line-format)
2067   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2068   (make-local-variable 'gnus-summary-mark-positions)
2069   (make-local-hook 'pre-command-hook)
2070   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2071   (gnus-run-hooks 'gnus-summary-mode-hook)
2072   (mm-enable-multibyte)
2073   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2074   (gnus-update-summary-mark-positions))
2075
2076 (defun gnus-summary-make-local-variables ()
2077   "Make all the local summary buffer variables."
2078   (let (global)
2079     (dolist (local gnus-summary-local-variables)
2080       (if (consp local)
2081           (progn
2082             (if (eq (cdr local) 'global)
2083                 ;; Copy the global value of the variable.
2084                 (setq global (symbol-value (car local)))
2085               ;; Use the value from the list.
2086               (setq global (eval (cdr local))))
2087             (set (make-local-variable (car local)) global))
2088         ;; Simple nil-valued local variable.
2089         (set (make-local-variable local) nil)))))
2090
2091 (defun gnus-summary-clear-local-variables ()
2092   (let ((locals gnus-summary-local-variables))
2093     (while locals
2094       (if (consp (car locals))
2095           (and (vectorp (caar locals))
2096                (set (caar locals) nil))
2097         (and (vectorp (car locals))
2098              (set (car locals) nil)))
2099       (setq locals (cdr locals)))))
2100
2101 ;; Summary data functions.
2102
2103 (defmacro gnus-data-number (data)
2104   `(car ,data))
2105
2106 (defmacro gnus-data-set-number (data number)
2107   `(setcar ,data ,number))
2108
2109 (defmacro gnus-data-mark (data)
2110   `(nth 1 ,data))
2111
2112 (defmacro gnus-data-set-mark (data mark)
2113   `(setcar (nthcdr 1 ,data) ,mark))
2114
2115 (defmacro gnus-data-pos (data)
2116   `(nth 2 ,data))
2117
2118 (defmacro gnus-data-set-pos (data pos)
2119   `(setcar (nthcdr 2 ,data) ,pos))
2120
2121 (defmacro gnus-data-header (data)
2122   `(nth 3 ,data))
2123
2124 (defmacro gnus-data-set-header (data header)
2125   `(setf (nth 3 ,data) ,header))
2126
2127 (defmacro gnus-data-level (data)
2128   `(nth 4 ,data))
2129
2130 (defmacro gnus-data-unread-p (data)
2131   `(= (nth 1 ,data) gnus-unread-mark))
2132
2133 (defmacro gnus-data-read-p (data)
2134   `(/= (nth 1 ,data) gnus-unread-mark))
2135
2136 (defmacro gnus-data-pseudo-p (data)
2137   `(consp (nth 3 ,data)))
2138
2139 (defmacro gnus-data-find (number)
2140   `(assq ,number gnus-newsgroup-data))
2141
2142 (defmacro gnus-data-find-list (number &optional data)
2143   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2144      (memq (assq ,number bdata)
2145            bdata)))
2146
2147 (defmacro gnus-data-make (number mark pos header level)
2148   `(list ,number ,mark ,pos ,header ,level))
2149
2150 (defun gnus-data-enter (after-article number mark pos header level offset)
2151   (let ((data (gnus-data-find-list after-article)))
2152     (unless data
2153       (error "No such article: %d" after-article))
2154     (setcdr data (cons (gnus-data-make number mark pos header level)
2155                        (cdr data)))
2156     (setq gnus-newsgroup-data-reverse nil)
2157     (gnus-data-update-list (cddr data) offset)))
2158
2159 (defun gnus-data-enter-list (after-article list &optional offset)
2160   (when list
2161     (let ((data (and after-article (gnus-data-find-list after-article)))
2162           (ilist list))
2163       (if (not (or data
2164                    after-article))
2165           (let ((odata gnus-newsgroup-data))
2166             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2167             (when offset
2168               (gnus-data-update-list odata offset)))
2169         ;; Find the last element in the list to be spliced into the main
2170         ;; list.
2171         (while (cdr list)
2172           (setq list (cdr list)))
2173         (if (not data)
2174             (progn
2175               (setcdr list gnus-newsgroup-data)
2176               (setq gnus-newsgroup-data ilist)
2177               (when offset
2178                 (gnus-data-update-list (cdr list) offset)))
2179           (setcdr list (cdr data))
2180           (setcdr data ilist)
2181           (when offset
2182             (gnus-data-update-list (cdr list) offset))))
2183       (setq gnus-newsgroup-data-reverse nil))))
2184
2185 (defun gnus-data-remove (article &optional offset)
2186   (let ((data gnus-newsgroup-data))
2187     (if (= (gnus-data-number (car data)) article)
2188         (progn
2189           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2190                 gnus-newsgroup-data-reverse nil)
2191           (when offset
2192             (gnus-data-update-list gnus-newsgroup-data offset)))
2193       (while (cdr data)
2194         (when (= (gnus-data-number (cadr data)) article)
2195           (setcdr data (cddr data))
2196           (when offset
2197             (gnus-data-update-list (cdr data) offset))
2198           (setq data nil
2199                 gnus-newsgroup-data-reverse nil))
2200         (setq data (cdr data))))))
2201
2202 (defmacro gnus-data-list (backward)
2203   `(if ,backward
2204        (or gnus-newsgroup-data-reverse
2205            (setq gnus-newsgroup-data-reverse
2206                  (reverse gnus-newsgroup-data)))
2207      gnus-newsgroup-data))
2208
2209 (defun gnus-data-update-list (data offset)
2210   "Add OFFSET to the POS of all data entries in DATA."
2211   (setq gnus-newsgroup-data-reverse nil)
2212   (while data
2213     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2214     (setq data (cdr data))))
2215
2216 (defun gnus-summary-article-pseudo-p (article)
2217   "Say whether this article is a pseudo article or not."
2218   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2219
2220 (defmacro gnus-summary-article-sparse-p (article)
2221   "Say whether this article is a sparse article or not."
2222   `(memq ,article gnus-newsgroup-sparse))
2223
2224 (defmacro gnus-summary-article-ancient-p (article)
2225   "Say whether this article is a sparse article or not."
2226   `(memq ,article gnus-newsgroup-ancient))
2227
2228 (defun gnus-article-parent-p (number)
2229   "Say whether this article is a parent or not."
2230   (let ((data (gnus-data-find-list number)))
2231     (and (cdr data)                     ; There has to be an article after...
2232          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2233             (gnus-data-level (nth 1 data))))))
2234
2235 (defun gnus-article-children (number)
2236   "Return a list of all children to NUMBER."
2237   (let* ((data (gnus-data-find-list number))
2238          (level (gnus-data-level (car data)))
2239          children)
2240     (setq data (cdr data))
2241     (while (and data
2242                 (= (gnus-data-level (car data)) (1+ level)))
2243       (push (gnus-data-number (car data)) children)
2244       (setq data (cdr data)))
2245     children))
2246
2247 (defmacro gnus-summary-skip-intangible ()
2248   "If the current article is intangible, then jump to a different article."
2249   '(let ((to (get-text-property (point) 'gnus-intangible)))
2250      (and to (gnus-summary-goto-subject to))))
2251
2252 (defmacro gnus-summary-article-intangible-p ()
2253   "Say whether this article is intangible or not."
2254   '(get-text-property (point) 'gnus-intangible))
2255
2256 (defun gnus-article-read-p (article)
2257   "Say whether ARTICLE is read or not."
2258   (not (or (memq article gnus-newsgroup-marked)
2259            (memq article gnus-newsgroup-unreads)
2260            (memq article gnus-newsgroup-unselected)
2261            (memq article gnus-newsgroup-dormant))))
2262
2263 ;; Some summary mode macros.
2264
2265 (defmacro gnus-summary-article-number ()
2266   "The article number of the article on the current line.
2267 If there isn's an article number here, then we return the current
2268 article number."
2269   '(progn
2270      (gnus-summary-skip-intangible)
2271      (or (get-text-property (point) 'gnus-number)
2272          (gnus-summary-last-subject))))
2273
2274 (defmacro gnus-summary-article-header (&optional number)
2275   "Return the header of article NUMBER."
2276   `(gnus-data-header (gnus-data-find
2277                       ,(or number '(gnus-summary-article-number)))))
2278
2279 (defmacro gnus-summary-thread-level (&optional number)
2280   "Return the level of thread that starts with article NUMBER."
2281   `(if (and (eq gnus-summary-make-false-root 'dummy)
2282             (get-text-property (point) 'gnus-intangible))
2283        0
2284      (gnus-data-level (gnus-data-find
2285                        ,(or number '(gnus-summary-article-number))))))
2286
2287 (defmacro gnus-summary-article-mark (&optional number)
2288   "Return the mark of article NUMBER."
2289   `(gnus-data-mark (gnus-data-find
2290                     ,(or number '(gnus-summary-article-number)))))
2291
2292 (defmacro gnus-summary-article-pos (&optional number)
2293   "Return the position of the line of article NUMBER."
2294   `(gnus-data-pos (gnus-data-find
2295                    ,(or number '(gnus-summary-article-number)))))
2296
2297 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2298 (defmacro gnus-summary-article-subject (&optional number)
2299   "Return current subject string or nil if nothing."
2300   `(let ((headers
2301           ,(if number
2302                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2303              '(gnus-data-header (assq (gnus-summary-article-number)
2304                                       gnus-newsgroup-data)))))
2305      (and headers
2306           (vectorp headers)
2307           (mail-header-subject headers))))
2308
2309 (defmacro gnus-summary-article-score (&optional number)
2310   "Return current article score."
2311   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2312                   gnus-newsgroup-scored))
2313        gnus-summary-default-score 0))
2314
2315 (defun gnus-summary-article-children (&optional number)
2316   "Return a list of article numbers that are children of article NUMBER."
2317   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2318          (level (gnus-data-level (car data)))
2319          l children)
2320     (while (and (setq data (cdr data))
2321                 (> (setq l (gnus-data-level (car data))) level))
2322       (and (= (1+ level) l)
2323            (push (gnus-data-number (car data))
2324                  children)))
2325     (nreverse children)))
2326
2327 (defun gnus-summary-article-parent (&optional number)
2328   "Return the article number of the parent of article NUMBER."
2329   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2330                                     (gnus-data-list t)))
2331          (level (gnus-data-level (car data))))
2332     (if (zerop level)
2333         ()                              ; This is a root.
2334       ;; We search until we find an article with a level less than
2335       ;; this one.  That function has to be the parent.
2336       (while (and (setq data (cdr data))
2337                   (not (< (gnus-data-level (car data)) level))))
2338       (and data (gnus-data-number (car data))))))
2339
2340 (defun gnus-unread-mark-p (mark)
2341   "Say whether MARK is the unread mark."
2342   (= mark gnus-unread-mark))
2343
2344 (defun gnus-read-mark-p (mark)
2345   "Say whether MARK is one of the marks that mark as read.
2346 This is all marks except unread, ticked, dormant, and expirable."
2347   (not (or (= mark gnus-unread-mark)
2348            (= mark gnus-ticked-mark)
2349            (= mark gnus-dormant-mark)
2350            (= mark gnus-expirable-mark))))
2351
2352 (defmacro gnus-article-mark (number)
2353   "Return the MARK of article NUMBER.
2354 This macro should only be used when computing the mark the \"first\"
2355 time; i.e., when generating the summary lines.  After that,
2356 `gnus-summary-article-mark' should be used to examine the
2357 marks of articles."
2358   `(cond
2359     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2360     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2361     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2362     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2363     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2364     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2365     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2366     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2367            gnus-ancient-mark))))
2368
2369 ;; Saving hidden threads.
2370
2371 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2372 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2373
2374 (defmacro gnus-save-hidden-threads (&rest forms)
2375   "Save hidden threads, eval FORMS, and restore the hidden threads."
2376   (let ((config (make-symbol "config")))
2377     `(let ((,config (gnus-hidden-threads-configuration)))
2378        (unwind-protect
2379            (save-excursion
2380              ,@forms)
2381          (gnus-restore-hidden-threads-configuration ,config)))))
2382
2383 (defun gnus-data-compute-positions ()
2384   "Compute the positions of all articles."
2385   (setq gnus-newsgroup-data-reverse nil)
2386   (let ((data gnus-newsgroup-data))
2387     (save-excursion
2388       (gnus-save-hidden-threads
2389         (gnus-summary-show-all-threads)
2390         (goto-char (point-min))
2391         (while data
2392           (while (get-text-property (point) 'gnus-intangible)
2393             (forward-line 1))
2394           (gnus-data-set-pos (car data) (+ (point) 3))
2395           (setq data (cdr data))
2396           (forward-line 1))))))
2397
2398 (defun gnus-hidden-threads-configuration ()
2399   "Return the current hidden threads configuration."
2400   (save-excursion
2401     (let (config)
2402       (goto-char (point-min))
2403       (while (search-forward "\r" nil t)
2404         (push (1- (point)) config))
2405       config)))
2406
2407 (defun gnus-restore-hidden-threads-configuration (config)
2408   "Restore hidden threads configuration from CONFIG."
2409   (let (point buffer-read-only)
2410     (while (setq point (pop config))
2411       (when (and (< point (point-max))
2412                  (goto-char point)
2413                  (eq (char-after) ?\n))
2414         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2415
2416 ;; Various summary mode internalish functions.
2417
2418 (defun gnus-mouse-pick-article (e)
2419   (interactive "e")
2420   (mouse-set-point e)
2421   (gnus-summary-next-page nil t))
2422
2423 (defun gnus-summary-set-display-table ()
2424   ;; Change the display table.  Odd characters have a tendency to mess
2425   ;; up nicely formatted displays - we make all possible glyphs
2426   ;; display only a single character.
2427
2428   ;; We start from the standard display table, if any.
2429   (let ((table (or (copy-sequence standard-display-table)
2430                    (make-display-table)))
2431         (i 32))
2432     ;; Nix out all the control chars...
2433     (while (>= (setq i (1- i)) 0)
2434       (aset table i [??]))
2435     ;; ... but not newline and cr, of course.  (cr is necessary for the
2436     ;; selective display).
2437     (aset table ?\n nil)
2438     (aset table ?\r nil)
2439     ;; We keep TAB as well.
2440     (aset table ?\t nil)
2441     ;; We nix out any glyphs over 126 that are not set already.
2442     (let ((i 256))
2443       (while (>= (setq i (1- i)) 127)
2444         ;; Only modify if the entry is nil.
2445         (unless (aref table i)
2446           (aset table i [??]))))
2447     (setq buffer-display-table table)))
2448
2449 (defun gnus-summary-setup-buffer (group)
2450   "Initialize summary buffer."
2451   (let ((buffer (concat "*Summary " group "*")))
2452     (if (get-buffer buffer)
2453         (progn
2454           (set-buffer buffer)
2455           (setq gnus-summary-buffer (current-buffer))
2456           (not gnus-newsgroup-prepared))
2457       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2458       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2459       (gnus-summary-mode group)
2460       (when gnus-carpal
2461         (gnus-carpal-setup-buffer 'summary))
2462       (unless gnus-single-article-buffer
2463         (make-local-variable 'gnus-article-buffer)
2464         (make-local-variable 'gnus-article-current)
2465         (make-local-variable 'gnus-original-article-buffer))
2466       (setq gnus-newsgroup-name group)
2467       t)))
2468
2469 (defun gnus-set-global-variables ()
2470   ;; Set the global equivalents of the summary buffer-local variables
2471   ;; to the latest values they had.  These reflect the summary buffer
2472   ;; that was in action when the last article was fetched.
2473   (when (eq major-mode 'gnus-summary-mode)
2474     (setq gnus-summary-buffer (current-buffer))
2475     (let ((name gnus-newsgroup-name)
2476           (marked gnus-newsgroup-marked)
2477           (unread gnus-newsgroup-unreads)
2478           (headers gnus-current-headers)
2479           (data gnus-newsgroup-data)
2480           (summary gnus-summary-buffer)
2481           (article-buffer gnus-article-buffer)
2482           (original gnus-original-article-buffer)
2483           (gac gnus-article-current)
2484           (reffed gnus-reffed-article-number)
2485           (score-file gnus-current-score-file)
2486           (default-charset gnus-newsgroup-charset))
2487       (save-excursion
2488         (set-buffer gnus-group-buffer)
2489         (setq gnus-newsgroup-name name
2490               gnus-newsgroup-marked marked
2491               gnus-newsgroup-unreads unread
2492               gnus-current-headers headers
2493               gnus-newsgroup-data data
2494               gnus-article-current gac
2495               gnus-summary-buffer summary
2496               gnus-article-buffer article-buffer
2497               gnus-original-article-buffer original
2498               gnus-reffed-article-number reffed
2499               gnus-current-score-file score-file
2500               gnus-newsgroup-charset default-charset)
2501         ;; The article buffer also has local variables.
2502         (when (gnus-buffer-live-p gnus-article-buffer)
2503           (set-buffer gnus-article-buffer)
2504           (setq gnus-summary-buffer summary))))))
2505
2506 (defun gnus-summary-article-unread-p (article)
2507   "Say whether ARTICLE is unread or not."
2508   (memq article gnus-newsgroup-unreads))
2509
2510 (defun gnus-summary-first-article-p (&optional article)
2511   "Return whether ARTICLE is the first article in the buffer."
2512   (if (not (setq article (or article (gnus-summary-article-number))))
2513       nil
2514     (eq article (caar gnus-newsgroup-data))))
2515
2516 (defun gnus-summary-last-article-p (&optional article)
2517   "Return whether ARTICLE is the last article in the buffer."
2518   (if (not (setq article (or article (gnus-summary-article-number))))
2519       ;; All non-existent numbers are the last article.  :-)
2520       t
2521     (not (cdr (gnus-data-find-list article)))))
2522
2523 (defun gnus-make-thread-indent-array ()
2524   (let ((n 200))
2525     (unless (and gnus-thread-indent-array
2526                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2527       (setq gnus-thread-indent-array (make-vector 201 "")
2528             gnus-thread-indent-array-level gnus-thread-indent-level)
2529       (while (>= n 0)
2530         (aset gnus-thread-indent-array n
2531               (make-string (* n gnus-thread-indent-level) ? ))
2532         (setq n (1- n))))))
2533
2534 (defun gnus-update-summary-mark-positions ()
2535   "Compute where the summary marks are to go."
2536   (save-excursion
2537     (when (gnus-buffer-exists-p gnus-summary-buffer)
2538       (set-buffer gnus-summary-buffer))
2539     (let ((gnus-replied-mark 129)
2540           (gnus-score-below-mark 130)
2541           (gnus-score-over-mark 130)
2542           (gnus-download-mark 131)
2543           (spec gnus-summary-line-format-spec)
2544           gnus-visual pos)
2545       (save-excursion
2546         (gnus-set-work-buffer)
2547         (let ((gnus-summary-line-format-spec spec)
2548               (gnus-newsgroup-downloadable '((0 . t))))
2549           (gnus-summary-insert-line
2550            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2551           (goto-char (point-min))
2552           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2553                                              (- (point) 2)))))
2554           (goto-char (point-min))
2555           (push (cons 'replied (and (search-forward "\201" nil t)
2556                                     (- (point) 2)))
2557                 pos)
2558           (goto-char (point-min))
2559           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2560                 pos)
2561           (goto-char (point-min))
2562           (push (cons 'download
2563                       (and (search-forward "\203" nil t) (- (point) 2)))
2564                 pos)))
2565       (setq gnus-summary-mark-positions pos))))
2566
2567 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2568   "Insert a dummy root in the summary buffer."
2569   (beginning-of-line)
2570   (gnus-add-text-properties
2571    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2572    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2573
2574 (defun gnus-summary-from-or-to-or-newsgroups (header)
2575   (let ((to (cdr (assq 'To (mail-header-extra header))))
2576         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2577         (mail-parse-charset gnus-newsgroup-charset)
2578         (mail-parse-ignored-charsets 
2579          (save-excursion (set-buffer gnus-summary-buffer)
2580                          gnus-newsgroup-ignored-charsets)))
2581     (cond
2582      ((and to
2583            gnus-ignored-from-addresses
2584            (string-match gnus-ignored-from-addresses
2585                          (mail-header-from header)))
2586       (concat "-> "
2587               (or (car (funcall gnus-extract-address-components
2588                                 (funcall
2589                                  gnus-decode-encoded-word-function to)))
2590                   (funcall gnus-decode-encoded-word-function to))))
2591      ((and newsgroups
2592            gnus-ignored-from-addresses
2593            (string-match gnus-ignored-from-addresses
2594                          (mail-header-from header)))
2595       (concat "=> " newsgroups))
2596      (t
2597       (or (car (funcall gnus-extract-address-components
2598                         (mail-header-from header)))
2599           (mail-header-from header))))))
2600
2601 (defun gnus-summary-insert-line (gnus-tmp-header
2602                                  gnus-tmp-level gnus-tmp-current
2603                                  gnus-tmp-unread gnus-tmp-replied
2604                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2605                                  &optional gnus-tmp-dummy gnus-tmp-score
2606                                  gnus-tmp-process)
2607   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2608          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2609          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2610          (gnus-tmp-score-char
2611           (if (or (null gnus-summary-default-score)
2612                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2613                       gnus-summary-zcore-fuzz))
2614               ?  ;Whitespace
2615             (if (< gnus-tmp-score gnus-summary-default-score)
2616                 gnus-score-below-mark gnus-score-over-mark)))
2617          (gnus-tmp-replied
2618           (cond (gnus-tmp-process gnus-process-mark)
2619                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2620                  gnus-cached-mark)
2621                 (gnus-tmp-replied gnus-replied-mark)
2622                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2623                  gnus-saved-mark)
2624                 (t gnus-unread-mark)))
2625          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2626          (gnus-tmp-name
2627           (cond
2628            ((string-match "<[^>]+> *$" gnus-tmp-from)
2629             (let ((beg (match-beginning 0)))
2630               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2631                        (substring gnus-tmp-from (1+ (match-beginning 0))
2632                                   (1- (match-end 0))))
2633                   (substring gnus-tmp-from 0 beg))))
2634            ((string-match "(.+)" gnus-tmp-from)
2635             (substring gnus-tmp-from
2636                        (1+ (match-beginning 0)) (1- (match-end 0))))
2637            (t gnus-tmp-from)))
2638          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2639          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2640          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2641          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2642          (buffer-read-only nil))
2643     (when (string= gnus-tmp-name "")
2644       (setq gnus-tmp-name gnus-tmp-from))
2645     (unless (numberp gnus-tmp-lines)
2646       (setq gnus-tmp-lines 0))
2647     (gnus-put-text-property
2648      (point)
2649      (progn (eval gnus-summary-line-format-spec) (point))
2650      'gnus-number gnus-tmp-number)
2651     (when (gnus-visual-p 'summary-highlight 'highlight)
2652       (forward-line -1)
2653       (gnus-run-hooks 'gnus-summary-update-hook)
2654       (forward-line 1))))
2655
2656 (defun gnus-summary-update-line (&optional dont-update)
2657   ;; Update summary line after change.
2658   (when (and gnus-summary-default-score
2659              (not gnus-summary-inhibit-highlight))
2660     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2661            (article (gnus-summary-article-number))
2662            (score (gnus-summary-article-score article)))
2663       (unless dont-update
2664         (if (and gnus-summary-mark-below
2665                  (< (gnus-summary-article-score)
2666                     gnus-summary-mark-below))
2667             ;; This article has a low score, so we mark it as read.
2668             (when (memq article gnus-newsgroup-unreads)
2669               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2670           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2671             ;; This article was previously marked as read on account
2672             ;; of a low score, but now it has risen, so we mark it as
2673             ;; unread.
2674             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2675         (gnus-summary-update-mark
2676          (if (or (null gnus-summary-default-score)
2677                  (<= (abs (- score gnus-summary-default-score))
2678                      gnus-summary-zcore-fuzz))
2679              ?  ;Whitespace
2680            (if (< score gnus-summary-default-score)
2681                gnus-score-below-mark gnus-score-over-mark))
2682          'score))
2683       ;; Do visual highlighting.
2684       (when (gnus-visual-p 'summary-highlight 'highlight)
2685         (gnus-run-hooks 'gnus-summary-update-hook)))))
2686
2687 (defvar gnus-tmp-new-adopts nil)
2688
2689 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2690   "Return the number of articles in THREAD.
2691 This may be 0 in some cases -- if none of the articles in
2692 the thread are to be displayed."
2693   (let* ((number
2694           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2695           (cond
2696            ((not (listp thread))
2697             1)
2698            ((and (consp thread) (cdr thread))
2699             (apply
2700              '+ 1 (mapcar
2701                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2702            ((null thread)
2703             1)
2704            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2705             1)
2706            (t 0))))
2707     (when (and level (zerop level) gnus-tmp-new-adopts)
2708       (incf number
2709             (apply '+ (mapcar
2710                        'gnus-summary-number-of-articles-in-thread
2711                        gnus-tmp-new-adopts))))
2712     (if char
2713         (if (> number 1) gnus-not-empty-thread-mark
2714           gnus-empty-thread-mark)
2715       number)))
2716
2717 (defun gnus-summary-set-local-parameters (group)
2718   "Go through the local params of GROUP and set all variable specs in that list."
2719   (let ((params (gnus-group-find-parameter group))
2720         elem)
2721     (while params
2722       (setq elem (car params)
2723             params (cdr params))
2724       (and (consp elem)                 ; Has to be a cons.
2725            (consp (cdr elem))           ; The cdr has to be a list.
2726            (symbolp (car elem))         ; Has to be a symbol in there.
2727            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2728            (ignore-errors               ; So we set it.
2729              (make-local-variable (car elem))
2730              (set (car elem) (eval (nth 1 elem))))))))
2731
2732 (defun gnus-summary-read-group (group &optional show-all no-article
2733                                       kill-buffer no-display backward
2734                                       select-articles)
2735   "Start reading news in newsgroup GROUP.
2736 If SHOW-ALL is non-nil, already read articles are also listed.
2737 If NO-ARTICLE is non-nil, no article is selected initially.
2738 If NO-DISPLAY, don't generate a summary buffer."
2739   (let (result)
2740     (while (and group
2741                 (null (setq result
2742                             (let ((gnus-auto-select-next nil))
2743                               (or (gnus-summary-read-group-1
2744                                    group show-all no-article
2745                                    kill-buffer no-display
2746                                    select-articles)
2747                                   (setq show-all nil
2748                                         select-articles nil)))))
2749                 (eq gnus-auto-select-next 'quietly))
2750       (set-buffer gnus-group-buffer)
2751       ;; The entry function called above goes to the next
2752       ;; group automatically, so we go two groups back
2753       ;; if we are searching for the previous group.
2754       (when backward
2755         (gnus-group-prev-unread-group 2))
2756       (if (not (equal group (gnus-group-group-name)))
2757           (setq group (gnus-group-group-name))
2758         (setq group nil)))
2759     result))
2760
2761 (defun gnus-summary-read-group-1 (group show-all no-article
2762                                         kill-buffer no-display
2763                                         &optional select-articles)
2764   ;; Killed foreign groups can't be entered.
2765   (when (and (not (gnus-group-native-p group))
2766              (not (gnus-gethash group gnus-newsrc-hashtb)))
2767     (error "Dead non-native groups can't be entered"))
2768   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2769   (let* ((new-group (gnus-summary-setup-buffer group))
2770          (quit-config (gnus-group-quit-config group))
2771          (did-select (and new-group (gnus-select-newsgroup
2772                                      group show-all select-articles))))
2773     (cond
2774      ;; This summary buffer exists already, so we just select it.
2775      ((not new-group)
2776       (gnus-set-global-variables)
2777       (when kill-buffer
2778         (gnus-kill-or-deaden-summary kill-buffer))
2779       (gnus-configure-windows 'summary 'force)
2780       (gnus-set-mode-line 'summary)
2781       (gnus-summary-position-point)
2782       (message "")
2783       t)
2784      ;; We couldn't select this group.
2785      ((null did-select)
2786       (when (and (eq major-mode 'gnus-summary-mode)
2787                  (not (equal (current-buffer) kill-buffer)))
2788         (kill-buffer (current-buffer))
2789         (if (not quit-config)
2790             (progn
2791               ;; Update the info -- marks might need to be removed,
2792               ;; for instance.
2793               (gnus-summary-update-info)
2794               (set-buffer gnus-group-buffer)
2795               (gnus-group-jump-to-group group)
2796               (gnus-group-next-unread-group 1))
2797           (gnus-handle-ephemeral-exit quit-config)))
2798       (gnus-message 3 "Can't select group")
2799       nil)
2800      ;; The user did a `C-g' while prompting for number of articles,
2801      ;; so we exit this group.
2802      ((eq did-select 'quit)
2803       (and (eq major-mode 'gnus-summary-mode)
2804            (not (equal (current-buffer) kill-buffer))
2805            (kill-buffer (current-buffer)))
2806       (when kill-buffer
2807         (gnus-kill-or-deaden-summary kill-buffer))
2808       (if (not quit-config)
2809           (progn
2810             (set-buffer gnus-group-buffer)
2811             (gnus-group-jump-to-group group)
2812             (gnus-group-next-unread-group 1)
2813             (gnus-configure-windows 'group 'force))
2814         (gnus-handle-ephemeral-exit quit-config))
2815       ;; Finally signal the quit.
2816       (signal 'quit nil))
2817      ;; The group was successfully selected.
2818      (t
2819       (gnus-set-global-variables)
2820       ;; Save the active value in effect when the group was entered.
2821       (setq gnus-newsgroup-active
2822             (gnus-copy-sequence
2823              (gnus-active gnus-newsgroup-name)))
2824       ;; You can change the summary buffer in some way with this hook.
2825       (gnus-run-hooks 'gnus-select-group-hook)
2826       ;; Set any local variables in the group parameters.
2827       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2828       (gnus-update-format-specifications
2829        nil 'summary 'summary-mode 'summary-dummy)
2830       (gnus-update-summary-mark-positions)
2831       ;; Do score processing.
2832       (when gnus-use-scoring
2833         (gnus-possibly-score-headers))
2834       ;; Check whether to fill in the gaps in the threads.
2835       (when gnus-build-sparse-threads
2836         (gnus-build-sparse-threads))
2837       ;; Find the initial limit.
2838       (if gnus-show-threads
2839           (if show-all
2840               (let ((gnus-newsgroup-dormant nil))
2841                 (gnus-summary-initial-limit show-all))
2842             (gnus-summary-initial-limit show-all))
2843         ;; When untreaded, all articles are always shown.
2844         (setq gnus-newsgroup-limit
2845               (mapcar
2846                (lambda (header) (mail-header-number header))
2847                gnus-newsgroup-headers)))
2848       ;; Generate the summary buffer.
2849       (unless no-display
2850         (gnus-summary-prepare))
2851       (when gnus-use-trees
2852         (gnus-tree-open group)
2853         (setq gnus-summary-highlight-line-function
2854               'gnus-tree-highlight-article))
2855       ;; If the summary buffer is empty, but there are some low-scored
2856       ;; articles or some excluded dormants, we include these in the
2857       ;; buffer.
2858       (when (and (zerop (buffer-size))
2859                  (not no-display))
2860         (cond (gnus-newsgroup-dormant
2861                (gnus-summary-limit-include-dormant))
2862               ((and gnus-newsgroup-scored show-all)
2863                (gnus-summary-limit-include-expunged t))))
2864       ;; Function `gnus-apply-kill-file' must be called in this hook.
2865       (gnus-run-hooks 'gnus-apply-kill-hook)
2866       (if (and (zerop (buffer-size))
2867                (not no-display))
2868           (progn
2869             ;; This newsgroup is empty.
2870             (gnus-summary-catchup-and-exit nil t)
2871             (gnus-message 6 "No unread news")
2872             (when kill-buffer
2873               (gnus-kill-or-deaden-summary kill-buffer))
2874             ;; Return nil from this function.
2875             nil)
2876         ;; Hide conversation thread subtrees.  We cannot do this in
2877         ;; gnus-summary-prepare-hook since kill processing may not
2878         ;; work with hidden articles.
2879         (and gnus-show-threads
2880              gnus-thread-hide-subtree
2881              (gnus-summary-hide-all-threads))
2882         (when kill-buffer
2883           (gnus-kill-or-deaden-summary kill-buffer))
2884         ;; Show first unread article if requested.
2885         (if (and (not no-article)
2886                  (not no-display)
2887                  gnus-newsgroup-unreads
2888                  gnus-auto-select-first)
2889             (progn
2890               (gnus-configure-windows 'summary)
2891               (cond
2892                ((eq gnus-auto-select-first 'best)
2893                 (gnus-summary-best-unread-article))
2894                ((eq gnus-auto-select-first t)
2895                 (gnus-summary-first-unread-article))
2896                ((gnus-functionp gnus-auto-select-first)
2897                 (funcall gnus-auto-select-first))))
2898           ;; Don't select any articles, just move point to the first
2899           ;; article in the group.
2900           (goto-char (point-min))
2901           (gnus-summary-position-point)
2902           (gnus-configure-windows 'summary 'force)
2903           (gnus-set-mode-line 'summary))
2904         (when (get-buffer-window gnus-group-buffer t)
2905           ;; Gotta use windows, because recenter does weird stuff if
2906           ;; the current buffer ain't the displayed window.
2907           (let ((owin (selected-window)))
2908             (select-window (get-buffer-window gnus-group-buffer t))
2909             (when (gnus-group-goto-group group)
2910               (recenter))
2911             (select-window owin)))
2912         ;; Mark this buffer as "prepared".
2913         (setq gnus-newsgroup-prepared t)
2914         (gnus-run-hooks 'gnus-summary-prepared-hook)
2915         t)))))
2916
2917 (defun gnus-summary-prepare ()
2918   "Generate the summary buffer."
2919   (interactive)
2920   (let ((buffer-read-only nil))
2921     (erase-buffer)
2922     (setq gnus-newsgroup-data nil
2923           gnus-newsgroup-data-reverse nil)
2924     (gnus-run-hooks 'gnus-summary-generate-hook)
2925     ;; Generate the buffer, either with threads or without.
2926     (when gnus-newsgroup-headers
2927       (gnus-summary-prepare-threads
2928        (if gnus-show-threads
2929            (gnus-sort-gathered-threads
2930             (funcall gnus-summary-thread-gathering-function
2931                      (gnus-sort-threads
2932                       (gnus-cut-threads (gnus-make-threads)))))
2933          ;; Unthreaded display.
2934          (gnus-sort-articles gnus-newsgroup-headers))))
2935     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2936     ;; Call hooks for modifying summary buffer.
2937     (goto-char (point-min))
2938     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2939
2940 (defsubst gnus-general-simplify-subject (subject)
2941   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2942   (setq subject
2943         (cond
2944          ;; Truncate the subject.
2945          (gnus-simplify-subject-functions
2946           (gnus-map-function gnus-simplify-subject-functions subject))
2947          ((numberp gnus-summary-gather-subject-limit)
2948           (setq subject (gnus-simplify-subject-re subject))
2949           (if (> (length subject) gnus-summary-gather-subject-limit)
2950               (substring subject 0 gnus-summary-gather-subject-limit)
2951             subject))
2952          ;; Fuzzily simplify it.
2953          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2954           (gnus-simplify-subject-fuzzy subject))
2955          ;; Just remove the leading "Re:".
2956          (t
2957           (gnus-simplify-subject-re subject))))
2958
2959   (if (and gnus-summary-gather-exclude-subject
2960            (string-match gnus-summary-gather-exclude-subject subject))
2961       nil                               ; This article shouldn't be gathered
2962     subject))
2963
2964 (defun gnus-summary-simplify-subject-query ()
2965   "Query where the respool algorithm would put this article."
2966   (interactive)
2967   (gnus-summary-select-article)
2968   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2969
2970 (defun gnus-gather-threads-by-subject (threads)
2971   "Gather threads by looking at Subject headers."
2972   (if (not gnus-summary-make-false-root)
2973       threads
2974     (let ((hashtb (gnus-make-hashtable 1024))
2975           (prev threads)
2976           (result threads)
2977           subject hthread whole-subject)
2978       (while threads
2979         (setq subject (gnus-general-simplify-subject
2980                        (setq whole-subject (mail-header-subject
2981                                             (caar threads)))))
2982         (when subject
2983           (if (setq hthread (gnus-gethash subject hashtb))
2984               (progn
2985                 ;; We enter a dummy root into the thread, if we
2986                 ;; haven't done that already.
2987                 (unless (stringp (caar hthread))
2988                   (setcar hthread (list whole-subject (car hthread))))
2989                 ;; We add this new gathered thread to this gathered
2990                 ;; thread.
2991                 (setcdr (car hthread)
2992                         (nconc (cdar hthread) (list (car threads))))
2993                 ;; Remove it from the list of threads.
2994                 (setcdr prev (cdr threads))
2995                 (setq threads prev))
2996             ;; Enter this thread into the hash table.
2997             (gnus-sethash subject threads hashtb)))
2998         (setq prev threads)
2999         (setq threads (cdr threads)))
3000       result)))
3001
3002 (defun gnus-gather-threads-by-references (threads)
3003   "Gather threads by looking at References headers."
3004   (let ((idhashtb (gnus-make-hashtable 1024))
3005         (thhashtb (gnus-make-hashtable 1024))
3006         (prev threads)
3007         (result threads)
3008         ids references id gthread gid entered ref)
3009     (while threads
3010       (when (setq references (mail-header-references (caar threads)))
3011         (setq id (mail-header-id (caar threads))
3012               ids (gnus-split-references references)
3013               entered nil)
3014         (while (setq ref (pop ids))
3015           (setq ids (delete ref ids))
3016           (if (not (setq gid (gnus-gethash ref idhashtb)))
3017               (progn
3018                 (gnus-sethash ref id idhashtb)
3019                 (gnus-sethash id threads thhashtb))
3020             (setq gthread (gnus-gethash gid thhashtb))
3021             (unless entered
3022               ;; We enter a dummy root into the thread, if we
3023               ;; haven't done that already.
3024               (unless (stringp (caar gthread))
3025                 (setcar gthread (list (mail-header-subject (caar gthread))
3026                                       (car gthread))))
3027               ;; We add this new gathered thread to this gathered
3028               ;; thread.
3029               (setcdr (car gthread)
3030                       (nconc (cdar gthread) (list (car threads)))))
3031             ;; Add it into the thread hash table.
3032             (gnus-sethash id gthread thhashtb)
3033             (setq entered t)
3034             ;; Remove it from the list of threads.
3035             (setcdr prev (cdr threads))
3036             (setq threads prev))))
3037       (setq prev threads)
3038       (setq threads (cdr threads)))
3039     result))
3040
3041 (defun gnus-sort-gathered-threads (threads)
3042   "Sort subtreads inside each gathered thread by article number."
3043   (let ((result threads))
3044     (while threads
3045       (when (stringp (caar threads))
3046         (setcdr (car threads)
3047                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3048       (setq threads (cdr threads)))
3049     result))
3050
3051 (defun gnus-thread-loop-p (root thread)
3052   "Say whether ROOT is in THREAD."
3053   (let ((stack (list thread))
3054         (infloop 0)
3055         th)
3056     (while (setq thread (pop stack))
3057       (setq th (cdr thread))
3058       (while (and th
3059                   (not (eq (caar th) root)))
3060         (pop th))
3061       (if th
3062           ;; We have found a loop.
3063           (let (ref-dep)
3064             (setcdr thread (delq (car th) (cdr thread)))
3065             (if (boundp (setq ref-dep (intern "none"
3066                                               gnus-newsgroup-dependencies)))
3067                 (setcdr (symbol-value ref-dep)
3068                         (nconc (cdr (symbol-value ref-dep))
3069                                (list (car th))))
3070               (set ref-dep (list nil (car th))))
3071             (setq infloop 1
3072                   stack nil))
3073         ;; Push all the subthreads onto the stack.
3074         (push (cdr thread) stack)))
3075     infloop))
3076
3077 (defun gnus-make-threads ()
3078   "Go through the dependency hashtb and find the roots.  Return all threads."
3079   (let (threads)
3080     (while (catch 'infloop
3081              (mapatoms
3082               (lambda (refs)
3083                 ;; Deal with self-referencing References loops.
3084                 (when (and (car (symbol-value refs))
3085                            (not (zerop
3086                                  (apply
3087                                   '+
3088                                   (mapcar
3089                                    (lambda (thread)
3090                                      (gnus-thread-loop-p
3091                                       (car (symbol-value refs)) thread))
3092                                    (cdr (symbol-value refs)))))))
3093                   (setq threads nil)
3094                   (throw 'infloop t))
3095                 (unless (car (symbol-value refs))
3096                   ;; These threads do not refer back to any other articles,
3097                   ;; so they're roots.
3098                   (setq threads (append (cdr (symbol-value refs)) threads))))
3099               gnus-newsgroup-dependencies)))
3100     threads))
3101
3102 ;; Build the thread tree.
3103 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3104   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3105
3106 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3107 if it was already present.
3108
3109 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3110 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3111 Message-IDs will be renamed be renamed to a unique Message-ID before
3112 being entered.
3113
3114 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3115   (let* ((id (mail-header-id header))
3116          (id-dep (and id (intern id dependencies)))
3117          ref ref-dep ref-header)
3118     ;; Enter this `header' in the `dependencies' table.
3119     (cond
3120      ((not id-dep)
3121       (setq header nil))
3122      ;; The first two cases do the normal part: enter a new `header'
3123      ;; in the `dependencies' table.
3124      ((not (boundp id-dep))
3125       (set id-dep (list header)))
3126      ((null (car (symbol-value id-dep)))
3127       (setcar (symbol-value id-dep) header))
3128
3129      ;; From here the `header' was already present in the
3130      ;; `dependencies' table.
3131      (force-new
3132       ;; Overrides an existing entry;
3133       ;; just set the header part of the entry.
3134       (setcar (symbol-value id-dep) header))
3135
3136      ;; Renames the existing `header' to a unique Message-ID.
3137      ((not gnus-summary-ignore-duplicates)
3138       ;; An article with this Message-ID has already been seen.
3139       ;; We rename the Message-ID.
3140       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3141            (list header))
3142       (mail-header-set-id header id))
3143
3144      ;; The last case ignores an existing entry, except it adds any
3145      ;; additional Xrefs (in case the two articles came from different
3146      ;; servers.
3147      ;; Also sets `header' to `nil' meaning that the `dependencies'
3148      ;; table was *not* modified.
3149      (t
3150       (mail-header-set-xref
3151        (car (symbol-value id-dep))
3152        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3153                    "")
3154                (or (mail-header-xref header) "")))
3155       (setq header nil)))
3156
3157     (when header
3158       ;; First check if that we are not creating a References loop.
3159       (setq ref (gnus-parent-id (mail-header-references header)))
3160       (while (and ref
3161                   (setq ref-dep (intern-soft ref dependencies))
3162                   (boundp ref-dep)
3163                   (setq ref-header (car (symbol-value ref-dep))))
3164         (if (string= id ref)
3165             ;; Yuk!  This is a reference loop.  Make the article be a
3166             ;; root article.
3167             (progn
3168               (mail-header-set-references (car (symbol-value id-dep)) "none")
3169               (setq ref nil))
3170           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3171       (setq ref (gnus-parent-id (mail-header-references header)))
3172       (setq ref-dep (intern (or ref "none") dependencies))
3173       (if (boundp ref-dep)
3174           (setcdr (symbol-value ref-dep)
3175                   (nconc (cdr (symbol-value ref-dep))
3176                          (list (symbol-value id-dep))))
3177         (set ref-dep (list nil (symbol-value id-dep)))))
3178     header))
3179
3180 (defun gnus-build-sparse-threads ()
3181   (let ((headers gnus-newsgroup-headers)
3182         (mail-parse-charset gnus-newsgroup-charset)
3183         (gnus-summary-ignore-duplicates t)
3184         header references generation relations
3185         subject child end new-child date)
3186     ;; First we create an alist of generations/relations, where
3187     ;; generations is how much we trust the relation, and the relation
3188     ;; is parent/child.
3189     (gnus-message 7 "Making sparse threads...")
3190     (save-excursion
3191       (nnheader-set-temp-buffer " *gnus sparse threads*")
3192       (while (setq header (pop headers))
3193         (when (and (setq references (mail-header-references header))
3194                    (not (string= references "")))
3195           (insert references)
3196           (setq child (mail-header-id header)
3197                 subject (mail-header-subject header)
3198                 date (mail-header-date header)
3199                 generation 0)
3200           (while (search-backward ">" nil t)
3201             (setq end (1+ (point)))
3202             (when (search-backward "<" nil t)
3203               (setq new-child (buffer-substring (point) end))
3204               (push (list (incf generation)
3205                           child (setq child new-child)
3206                           subject date)
3207                     relations)))
3208           (when child
3209             (push (list (1+ generation) child nil subject) relations))
3210           (erase-buffer)))
3211       (kill-buffer (current-buffer)))
3212     ;; Sort over trustworthiness.
3213     (mapcar
3214      (lambda (relation)
3215        (when (gnus-dependencies-add-header
3216               (make-full-mail-header
3217                gnus-reffed-article-number
3218                (nth 3 relation) "" (or (nth 4 relation) "")
3219                (nth 1 relation)
3220                (or (nth 2 relation) "") 0 0 "")
3221               gnus-newsgroup-dependencies nil)
3222          (push gnus-reffed-article-number gnus-newsgroup-limit)
3223          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3224          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3225                gnus-newsgroup-reads)
3226          (decf gnus-reffed-article-number)))
3227      (sort relations 'car-less-than-car))
3228     (gnus-message 7 "Making sparse threads...done")))
3229
3230 (defun gnus-build-old-threads ()
3231   ;; Look at all the articles that refer back to old articles, and
3232   ;; fetch the headers for the articles that aren't there.  This will
3233   ;; build complete threads - if the roots haven't been expired by the
3234   ;; server, that is.
3235   (let ((mail-parse-charset gnus-newsgroup-charset)
3236         id heads)
3237     (mapatoms
3238      (lambda (refs)
3239        (when (not (car (symbol-value refs)))
3240          (setq heads (cdr (symbol-value refs)))
3241          (while heads
3242            (if (memq (mail-header-number (caar heads))
3243                      gnus-newsgroup-dormant)
3244                (setq heads (cdr heads))
3245              (setq id (symbol-name refs))
3246              (while (and (setq id (gnus-build-get-header id))
3247                          (not (car (gnus-id-to-thread id)))))
3248              (setq heads nil)))))
3249      gnus-newsgroup-dependencies)))
3250
3251 ;; This function has to be called with point after the article number
3252 ;; on the beginning of the line.
3253 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3254   (let ((eol (gnus-point-at-eol))
3255         (buffer (current-buffer))
3256         header)
3257
3258     ;; overview: [num subject from date id refs chars lines misc]
3259     (unwind-protect
3260         (progn
3261           (narrow-to-region (point) eol)
3262           (unless (eobp)
3263             (forward-char))
3264
3265           (setq header
3266                 (make-full-mail-header
3267                  number                 ; number
3268                  (funcall gnus-decode-encoded-word-function
3269                           (nnheader-nov-field)) ; subject
3270                  (funcall gnus-decode-encoded-word-function
3271                           (nnheader-nov-field)) ; from
3272                  (nnheader-nov-field)   ; date
3273                  (nnheader-nov-read-message-id) ; id
3274                  (nnheader-nov-field)   ; refs
3275                  (nnheader-nov-read-integer) ; chars
3276                  (nnheader-nov-read-integer) ; lines
3277                  (unless (eobp)
3278                    (nnheader-nov-field)) ; misc
3279                  (nnheader-nov-parse-extra)))) ; extra
3280
3281       (widen))
3282
3283     (when gnus-alter-header-function
3284       (funcall gnus-alter-header-function header))
3285     (gnus-dependencies-add-header header dependencies force-new)))
3286
3287 (defun gnus-build-get-header (id)
3288   ;; Look through the buffer of NOV lines and find the header to
3289   ;; ID.  Enter this line into the dependencies hash table, and return
3290   ;; the id of the parent article (if any).
3291   (let ((deps gnus-newsgroup-dependencies)
3292         found header)
3293     (prog1
3294         (save-excursion
3295           (set-buffer nntp-server-buffer)
3296           (let ((case-fold-search nil))
3297             (goto-char (point-min))
3298             (while (and (not found)
3299                         (search-forward id nil t))
3300               (beginning-of-line)
3301               (setq found (looking-at
3302                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3303                                    (regexp-quote id))))
3304               (or found (beginning-of-line 2)))
3305             (when found
3306               (beginning-of-line)
3307               (and
3308                (setq header (gnus-nov-parse-line
3309                              (read (current-buffer)) deps))
3310                (gnus-parent-id (mail-header-references header))))))
3311       (when header
3312         (let ((number (mail-header-number header)))
3313           (push number gnus-newsgroup-limit)
3314           (push header gnus-newsgroup-headers)
3315           (if (memq number gnus-newsgroup-unselected)
3316               (progn
3317                 (push number gnus-newsgroup-unreads)
3318                 (setq gnus-newsgroup-unselected
3319                       (delq number gnus-newsgroup-unselected)))
3320             (push number gnus-newsgroup-ancient)))))))
3321
3322 (defun gnus-build-all-threads ()
3323   "Read all the headers."
3324   (let ((gnus-summary-ignore-duplicates t)
3325         (mail-parse-charset gnus-newsgroup-charset)
3326         (dependencies gnus-newsgroup-dependencies)
3327         header article)
3328     (save-excursion
3329       (set-buffer nntp-server-buffer)
3330       (let ((case-fold-search nil))
3331         (goto-char (point-min))
3332         (while (not (eobp))
3333           (ignore-errors
3334             (setq article (read (current-buffer))
3335                   header (gnus-nov-parse-line article dependencies)))
3336           (when header
3337             (save-excursion
3338               (set-buffer gnus-summary-buffer)
3339               (push header gnus-newsgroup-headers)
3340               (if (memq (setq article (mail-header-number header))
3341                         gnus-newsgroup-unselected)
3342                   (progn
3343                     (push article gnus-newsgroup-unreads)
3344                     (setq gnus-newsgroup-unselected
3345                           (delq article gnus-newsgroup-unselected)))
3346                 (push article gnus-newsgroup-ancient)))
3347             (forward-line 1)))))))
3348
3349 (defun gnus-summary-update-article-line (article header)
3350   "Update the line for ARTICLE using HEADERS."
3351   (let* ((id (mail-header-id header))
3352          (thread (gnus-id-to-thread id)))
3353     (unless thread
3354       (error "Article in no thread"))
3355     ;; Update the thread.
3356     (setcar thread header)
3357     (gnus-summary-goto-subject article)
3358     (let* ((datal (gnus-data-find-list article))
3359            (data (car datal))
3360            (length (when (cdr datal)
3361                      (- (gnus-data-pos data)
3362                         (gnus-data-pos (cadr datal)))))
3363            (buffer-read-only nil)
3364            (level (gnus-summary-thread-level)))
3365       (gnus-delete-line)
3366       (gnus-summary-insert-line
3367        header level nil (gnus-article-mark article)
3368        (memq article gnus-newsgroup-replied)
3369        (memq article gnus-newsgroup-expirable)
3370        ;; Only insert the Subject string when it's different
3371        ;; from the previous Subject string.
3372        (if (gnus-subject-equal
3373             (condition-case ()
3374                 (mail-header-subject
3375                  (gnus-data-header
3376                   (cadr
3377                    (gnus-data-find-list
3378                     article
3379                     (gnus-data-list t)))))
3380               ;; Error on the side of excessive subjects.
3381               (error ""))
3382             (mail-header-subject header))
3383            ""
3384          (mail-header-subject header))
3385        nil (cdr (assq article gnus-newsgroup-scored))
3386        (memq article gnus-newsgroup-processable))
3387       (when length
3388         (gnus-data-update-list
3389          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3390
3391 (defun gnus-summary-update-article (article &optional iheader)
3392   "Update ARTICLE in the summary buffer."
3393   (set-buffer gnus-summary-buffer)
3394   (let* ((header (gnus-summary-article-header article))
3395          (id (mail-header-id header))
3396          (data (gnus-data-find article))
3397          (thread (gnus-id-to-thread id))
3398          (references (mail-header-references header))
3399          (parent
3400           (gnus-id-to-thread
3401            (or (gnus-parent-id
3402                 (when (and references
3403                            (not (equal "" references)))
3404                   references))
3405                "none")))
3406          (buffer-read-only nil)
3407          (old (car thread)))
3408     (when thread
3409       (unless iheader
3410         (setcar thread nil)
3411         (when parent
3412           (delq thread parent)))
3413       (if (gnus-summary-insert-subject id header)
3414           ;; Set the (possibly) new article number in the data structure.
3415           (gnus-data-set-number data (gnus-id-to-article id))
3416         (setcar thread old)
3417         nil))))
3418
3419 (defun gnus-rebuild-thread (id &optional line)
3420   "Rebuild the thread containing ID.
3421 If LINE, insert the rebuilt thread starting on line LINE."
3422   (let ((buffer-read-only nil)
3423         old-pos current thread data)
3424     (if (not gnus-show-threads)
3425         (setq thread (list (car (gnus-id-to-thread id))))
3426       ;; Get the thread this article is part of.
3427       (setq thread (gnus-remove-thread id)))
3428     (setq old-pos (gnus-point-at-bol))
3429     (setq current (save-excursion
3430                     (and (zerop (forward-line -1))
3431                          (gnus-summary-article-number))))
3432     ;; If this is a gathered thread, we have to go some re-gathering.
3433     (when (stringp (car thread))
3434       (let ((subject (car thread))
3435             roots thr)
3436         (setq thread (cdr thread))
3437         (while thread
3438           (unless (memq (setq thr (gnus-id-to-thread
3439                                    (gnus-root-id
3440                                     (mail-header-id (caar thread)))))
3441                         roots)
3442             (push thr roots))
3443           (setq thread (cdr thread)))
3444         ;; We now have all (unique) roots.
3445         (if (= (length roots) 1)
3446             ;; All the loose roots are now one solid root.
3447             (setq thread (car roots))
3448           (setq thread (cons subject (gnus-sort-threads roots))))))
3449     (let (threads)
3450       ;; We then insert this thread into the summary buffer.
3451       (when line
3452         (goto-char (point-min))
3453         (forward-line (1- line)))
3454       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3455         (if gnus-show-threads
3456             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3457           (gnus-summary-prepare-unthreaded thread))
3458         (setq data (nreverse gnus-newsgroup-data))
3459         (setq threads gnus-newsgroup-threads))
3460       ;; We splice the new data into the data structure.
3461       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3462       ;;!!! then we want to insert at the beginning of the buffer.
3463       ;;!!! That happens to be true with Gnus now, but that may
3464       ;;!!! change in the future.  Perhaps.
3465       (gnus-data-enter-list
3466        (if line nil current) data (- (point) old-pos))
3467       (setq gnus-newsgroup-threads
3468             (nconc threads gnus-newsgroup-threads))
3469       (gnus-data-compute-positions))))
3470
3471 (defun gnus-number-to-header (number)
3472   "Return the header for article NUMBER."
3473   (let ((headers gnus-newsgroup-headers))
3474     (while (and headers
3475                 (not (= number (mail-header-number (car headers)))))
3476       (pop headers))
3477     (when headers
3478       (car headers))))
3479
3480 (defun gnus-parent-headers (in-headers &optional generation)
3481   "Return the headers of the GENERATIONeth parent of HEADERS."
3482   (unless generation
3483     (setq generation 1))
3484   (let ((parent t)
3485         (headers in-headers)
3486         references)
3487     (while (and parent
3488                 (not (zerop generation))
3489                 (setq references (mail-header-references headers)))
3490       (setq headers (if (and references
3491                              (setq parent (gnus-parent-id references)))
3492                         (car (gnus-id-to-thread parent))
3493                       nil))
3494       (decf generation))
3495     (and (not (eq headers in-headers))
3496          headers)))
3497
3498 (defun gnus-id-to-thread (id)
3499   "Return the (sub-)thread where ID appears."
3500   (gnus-gethash id gnus-newsgroup-dependencies))
3501
3502 (defun gnus-id-to-article (id)
3503   "Return the article number of ID."
3504   (let ((thread (gnus-id-to-thread id)))
3505     (when (and thread
3506                (car thread))
3507       (mail-header-number (car thread)))))
3508
3509 (defun gnus-id-to-header (id)
3510   "Return the article headers of ID."
3511   (car (gnus-id-to-thread id)))
3512
3513 (defun gnus-article-displayed-root-p (article)
3514   "Say whether ARTICLE is a root(ish) article."
3515   (let ((level (gnus-summary-thread-level article))
3516         (refs (mail-header-references  (gnus-summary-article-header article)))
3517         particle)
3518     (cond
3519      ((null level) nil)
3520      ((zerop level) t)
3521      ((null refs) t)
3522      ((null (gnus-parent-id refs)) t)
3523      ((and (= 1 level)
3524            (null (setq particle (gnus-id-to-article
3525                                  (gnus-parent-id refs))))
3526            (null (gnus-summary-thread-level particle)))))))
3527
3528 (defun gnus-root-id (id)
3529   "Return the id of the root of the thread where ID appears."
3530   (let (last-id prev)
3531     (while (and id (setq prev (car (gnus-id-to-thread id))))
3532       (setq last-id id
3533             id (gnus-parent-id (mail-header-references prev))))
3534     last-id))
3535
3536 (defun gnus-articles-in-thread (thread)
3537   "Return the list of articles in THREAD."
3538   (cons (mail-header-number (car thread))
3539         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3540
3541 (defun gnus-remove-thread (id &optional dont-remove)
3542   "Remove the thread that has ID in it."
3543   (let (headers thread last-id)
3544     ;; First go up in this thread until we find the root.
3545     (setq last-id (gnus-root-id id)
3546           headers (message-flatten-list (gnus-id-to-thread last-id)))
3547     ;; We have now found the real root of this thread.  It might have
3548     ;; been gathered into some loose thread, so we have to search
3549     ;; through the threads to find the thread we wanted.
3550     (let ((threads gnus-newsgroup-threads)
3551           sub)
3552       (while threads
3553         (setq sub (car threads))
3554         (if (stringp (car sub))
3555             ;; This is a gathered thread, so we look at the roots
3556             ;; below it to find whether this article is in this
3557             ;; gathered root.
3558             (progn
3559               (setq sub (cdr sub))
3560               (while sub
3561                 (when (member (caar sub) headers)
3562                   (setq thread (car threads)
3563                         threads nil
3564                         sub nil))
3565                 (setq sub (cdr sub))))
3566           ;; It's an ordinary thread, so we check it.
3567           (when (eq (car sub) (car headers))
3568             (setq thread sub
3569                   threads nil)))
3570         (setq threads (cdr threads)))
3571       ;; If this article is in no thread, then it's a root.
3572       (if thread
3573           (unless dont-remove
3574             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3575         (setq thread (gnus-id-to-thread last-id)))
3576       (when thread
3577         (prog1
3578             thread                      ; We return this thread.
3579           (unless dont-remove
3580             (if (stringp (car thread))
3581                 (progn
3582                   ;; If we use dummy roots, then we have to remove the
3583                   ;; dummy root as well.
3584                   (when (eq gnus-summary-make-false-root 'dummy)
3585                     ;; We go to the dummy root by going to
3586                     ;; the first sub-"thread", and then one line up.
3587                     (gnus-summary-goto-article
3588                      (mail-header-number (caadr thread)))
3589                     (forward-line -1)
3590                     (gnus-delete-line)
3591                     (gnus-data-compute-positions))
3592                   (setq thread (cdr thread))
3593                   (while thread
3594                     (gnus-remove-thread-1 (car thread))
3595                     (setq thread (cdr thread))))
3596               (gnus-summary-show-all-threads)
3597               (gnus-remove-thread-1 thread))))))))
3598
3599 (defun gnus-remove-thread-1 (thread)
3600   "Remove the thread THREAD recursively."
3601   (let ((number (mail-header-number (pop thread)))
3602         d)
3603     (setq thread (reverse thread))
3604     (while thread
3605       (gnus-remove-thread-1 (pop thread)))
3606     (when (setq d (gnus-data-find number))
3607       (goto-char (gnus-data-pos d))
3608       (gnus-data-remove
3609        number
3610        (- (gnus-point-at-bol)
3611           (prog1
3612               (1+ (gnus-point-at-eol))
3613             (gnus-delete-line)))))))
3614
3615 (defun gnus-sort-threads (threads)
3616   "Sort THREADS."
3617   (if (not gnus-thread-sort-functions)
3618       threads
3619     (gnus-message 8 "Sorting threads...")
3620     (prog1
3621         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3622       (gnus-message 8 "Sorting threads...done"))))
3623
3624 (defun gnus-sort-articles (articles)
3625   "Sort ARTICLES."
3626   (when gnus-article-sort-functions
3627     (gnus-message 7 "Sorting articles...")
3628     (prog1
3629         (setq gnus-newsgroup-headers
3630               (sort articles (gnus-make-sort-function
3631                               gnus-article-sort-functions)))
3632       (gnus-message 7 "Sorting articles...done"))))
3633
3634 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3635 (defmacro gnus-thread-header (thread)
3636   ;; Return header of first article in THREAD.
3637   ;; Note that THREAD must never, ever be anything else than a variable -
3638   ;; using some other form will lead to serious barfage.
3639   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3640   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3641   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3642         (vector thread) 2))
3643
3644 (defsubst gnus-article-sort-by-number (h1 h2)
3645   "Sort articles by article number."
3646   (< (mail-header-number h1)
3647      (mail-header-number h2)))
3648
3649 (defun gnus-thread-sort-by-number (h1 h2)
3650   "Sort threads by root article number."
3651   (gnus-article-sort-by-number
3652    (gnus-thread-header h1) (gnus-thread-header h2)))
3653
3654 (defsubst gnus-article-sort-by-lines (h1 h2)
3655   "Sort articles by article Lines header."
3656   (< (mail-header-lines h1)
3657      (mail-header-lines h2)))
3658
3659 (defun gnus-thread-sort-by-lines (h1 h2)
3660   "Sort threads by root article Lines header."
3661   (gnus-article-sort-by-lines
3662    (gnus-thread-header h1) (gnus-thread-header h2)))
3663
3664 (defsubst gnus-article-sort-by-chars (h1 h2)
3665   "Sort articles by octet length."
3666   (< (mail-header-chars h1)
3667      (mail-header-chars h2)))
3668
3669 (defun gnus-thread-sort-by-chars (h1 h2)
3670   "Sort threads by root article octet length."
3671   (gnus-article-sort-by-chars
3672    (gnus-thread-header h1) (gnus-thread-header h2)))
3673
3674 (defsubst gnus-article-sort-by-author (h1 h2)
3675   "Sort articles by root author."
3676   (string-lessp
3677    (let ((extract (funcall
3678                    gnus-extract-address-components
3679                    (mail-header-from h1))))
3680      (or (car extract) (cadr extract) ""))
3681    (let ((extract (funcall
3682                    gnus-extract-address-components
3683                    (mail-header-from h2))))
3684      (or (car extract) (cadr extract) ""))))
3685
3686 (defun gnus-thread-sort-by-author (h1 h2)
3687   "Sort threads by root author."
3688   (gnus-article-sort-by-author
3689    (gnus-thread-header h1)  (gnus-thread-header h2)))
3690
3691 (defsubst gnus-article-sort-by-subject (h1 h2)
3692   "Sort articles by root subject."
3693   (string-lessp
3694    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3695    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3696
3697 (defun gnus-thread-sort-by-subject (h1 h2)
3698   "Sort threads by root subject."
3699   (gnus-article-sort-by-subject
3700    (gnus-thread-header h1) (gnus-thread-header h2)))
3701
3702 (defsubst gnus-article-sort-by-date (h1 h2)
3703   "Sort articles by root article date."
3704   (time-less-p
3705    (gnus-date-get-time (mail-header-date h1))
3706    (gnus-date-get-time (mail-header-date h2))))
3707
3708 (defun gnus-thread-sort-by-date (h1 h2)
3709   "Sort threads by root article date."
3710   (gnus-article-sort-by-date
3711    (gnus-thread-header h1) (gnus-thread-header h2)))
3712
3713 (defsubst gnus-article-sort-by-score (h1 h2)
3714   "Sort articles by root article score.
3715 Unscored articles will be counted as having a score of zero."
3716   (> (or (cdr (assq (mail-header-number h1)
3717                     gnus-newsgroup-scored))
3718          gnus-summary-default-score 0)
3719      (or (cdr (assq (mail-header-number h2)
3720                     gnus-newsgroup-scored))
3721          gnus-summary-default-score 0)))
3722
3723 (defun gnus-thread-sort-by-score (h1 h2)
3724   "Sort threads by root article score."
3725   (gnus-article-sort-by-score
3726    (gnus-thread-header h1) (gnus-thread-header h2)))
3727
3728 (defun gnus-thread-sort-by-total-score (h1 h2)
3729   "Sort threads by the sum of all scores in the thread.
3730 Unscored articles will be counted as having a score of zero."
3731   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3732
3733 (defun gnus-thread-total-score (thread)
3734   ;; This function find the total score of THREAD.
3735   (cond ((null thread)
3736          0)
3737         ((consp thread)
3738          (if (stringp (car thread))
3739              (apply gnus-thread-score-function 0
3740                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3741            (gnus-thread-total-score-1 thread)))
3742         (t
3743          (gnus-thread-total-score-1 (list thread)))))
3744
3745 (defun gnus-thread-total-score-1 (root)
3746   ;; This function find the total score of the thread below ROOT.
3747   (setq root (car root))
3748   (apply gnus-thread-score-function
3749          (or (append
3750               (mapcar 'gnus-thread-total-score
3751                       (cdr (gnus-id-to-thread (mail-header-id root))))
3752               (when (> (mail-header-number root) 0)
3753                 (list (or (cdr (assq (mail-header-number root)
3754                                      gnus-newsgroup-scored))
3755                           gnus-summary-default-score 0))))
3756              (list gnus-summary-default-score)
3757              '(0))))
3758
3759 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3760 (defvar gnus-tmp-prev-subject nil)
3761 (defvar gnus-tmp-false-parent nil)
3762 (defvar gnus-tmp-root-expunged nil)
3763 (defvar gnus-tmp-dummy-line nil)
3764
3765 (defvar gnus-tmp-header)
3766 (defun gnus-extra-header (type &optional header)
3767   "Return the extra header of TYPE."
3768   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3769       ""))
3770
3771 (defun gnus-summary-prepare-threads (threads)
3772   "Prepare summary buffer from THREADS and indentation LEVEL.
3773 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3774 or a straight list of headers."
3775   (gnus-message 7 "Generating summary...")
3776
3777   (setq gnus-newsgroup-threads threads)
3778   (beginning-of-line)
3779
3780   (let ((gnus-tmp-level 0)
3781         (default-score (or gnus-summary-default-score 0))
3782         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3783         thread number subject stack state gnus-tmp-gathered beg-match
3784         new-roots gnus-tmp-new-adopts thread-end
3785         gnus-tmp-header gnus-tmp-unread
3786         gnus-tmp-replied gnus-tmp-subject-or-nil
3787         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3788         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3789         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3790
3791     (setq gnus-tmp-prev-subject nil)
3792
3793     (if (vectorp (car threads))
3794         ;; If this is a straight (sic) list of headers, then a
3795         ;; threaded summary display isn't required, so we just create
3796         ;; an unthreaded one.
3797         (gnus-summary-prepare-unthreaded threads)
3798
3799       ;; Do the threaded display.
3800
3801       (while (or threads stack gnus-tmp-new-adopts new-roots)
3802
3803         (if (and (= gnus-tmp-level 0)
3804                  (or (not stack)
3805                      (= (caar stack) 0))
3806                  (not gnus-tmp-false-parent)
3807                  (or gnus-tmp-new-adopts new-roots))
3808             (if gnus-tmp-new-adopts
3809                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3810                       thread (list (car gnus-tmp-new-adopts))
3811                       gnus-tmp-header (caar thread)
3812                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3813               (when new-roots
3814                 (setq thread (list (car new-roots))
3815                       gnus-tmp-header (caar thread)
3816                       new-roots (cdr new-roots))))
3817
3818           (if threads
3819               ;; If there are some threads, we do them before the
3820               ;; threads on the stack.
3821               (setq thread threads
3822                     gnus-tmp-header (caar thread))
3823             ;; There were no current threads, so we pop something off
3824             ;; the stack.
3825             (setq state (car stack)
3826                   gnus-tmp-level (car state)
3827                   thread (cdr state)
3828                   stack (cdr stack)
3829                   gnus-tmp-header (caar thread))))
3830
3831         (setq gnus-tmp-false-parent nil)
3832         (setq gnus-tmp-root-expunged nil)
3833         (setq thread-end nil)
3834
3835         (if (stringp gnus-tmp-header)
3836             ;; The header is a dummy root.
3837             (cond
3838              ((eq gnus-summary-make-false-root 'adopt)
3839               ;; We let the first article adopt the rest.
3840               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3841                                                (cddar thread)))
3842               (setq gnus-tmp-gathered
3843                     (nconc (mapcar
3844                             (lambda (h) (mail-header-number (car h)))
3845                             (cddar thread))
3846                            gnus-tmp-gathered))
3847               (setq thread (cons (list (caar thread)
3848                                        (cadar thread))
3849                                  (cdr thread)))
3850               (setq gnus-tmp-level -1
3851                     gnus-tmp-false-parent t))
3852              ((eq gnus-summary-make-false-root 'empty)
3853               ;; We print adopted articles with empty subject fields.
3854               (setq gnus-tmp-gathered
3855                     (nconc (mapcar
3856                             (lambda (h) (mail-header-number (car h)))
3857                             (cddar thread))
3858                            gnus-tmp-gathered))
3859               (setq gnus-tmp-level -1))
3860              ((eq gnus-summary-make-false-root 'dummy)
3861               ;; We remember that we probably want to output a dummy
3862               ;; root.
3863               (setq gnus-tmp-dummy-line gnus-tmp-header)
3864               (setq gnus-tmp-prev-subject gnus-tmp-header))
3865              (t
3866               ;; We do not make a root for the gathered
3867               ;; sub-threads at all.
3868               (setq gnus-tmp-level -1)))
3869
3870           (setq number (mail-header-number gnus-tmp-header)
3871                 subject (mail-header-subject gnus-tmp-header))
3872
3873           (cond
3874            ;; If the thread has changed subject, we might want to make
3875            ;; this subthread into a root.
3876            ((and (null gnus-thread-ignore-subject)
3877                  (not (zerop gnus-tmp-level))
3878                  gnus-tmp-prev-subject
3879                  (not (inline
3880                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3881             (setq new-roots (nconc new-roots (list (car thread)))
3882                   thread-end t
3883                   gnus-tmp-header nil))
3884            ;; If the article lies outside the current limit,
3885            ;; then we do not display it.
3886            ((not (memq number gnus-newsgroup-limit))
3887             (setq gnus-tmp-gathered
3888                   (nconc (mapcar
3889                           (lambda (h) (mail-header-number (car h)))
3890                           (cdar thread))
3891                          gnus-tmp-gathered))
3892             (setq gnus-tmp-new-adopts (if (cdar thread)
3893                                           (append gnus-tmp-new-adopts
3894                                                   (cdar thread))
3895                                         gnus-tmp-new-adopts)
3896                   thread-end t
3897                   gnus-tmp-header nil)
3898             (when (zerop gnus-tmp-level)
3899               (setq gnus-tmp-root-expunged t)))
3900            ;; Perhaps this article is to be marked as read?
3901            ((and gnus-summary-mark-below
3902                  (< (or (cdr (assq number gnus-newsgroup-scored))
3903                         default-score)
3904                     gnus-summary-mark-below)
3905                  ;; Don't touch sparse articles.
3906                  (not (gnus-summary-article-sparse-p number))
3907                  (not (gnus-summary-article-ancient-p number)))
3908             (setq gnus-newsgroup-unreads
3909                   (delq number gnus-newsgroup-unreads))
3910             (if gnus-newsgroup-auto-expire
3911                 (push number gnus-newsgroup-expirable)
3912               (push (cons number gnus-low-score-mark)
3913                     gnus-newsgroup-reads))))
3914
3915           (when gnus-tmp-header
3916             ;; We may have an old dummy line to output before this
3917             ;; article.
3918             (when (and gnus-tmp-dummy-line
3919                        (gnus-subject-equal
3920                         gnus-tmp-dummy-line
3921                         (mail-header-subject gnus-tmp-header)))
3922               (gnus-summary-insert-dummy-line
3923                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3924               (setq gnus-tmp-dummy-line nil))
3925
3926             ;; Compute the mark.
3927             (setq gnus-tmp-unread (gnus-article-mark number))
3928
3929             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3930                                   gnus-tmp-header gnus-tmp-level)
3931                   gnus-newsgroup-data)
3932
3933             ;; Actually insert the line.
3934             (setq
3935              gnus-tmp-subject-or-nil
3936              (cond
3937               ((and gnus-thread-ignore-subject
3938                     gnus-tmp-prev-subject
3939                     (not (inline (gnus-subject-equal
3940                                   gnus-tmp-prev-subject subject))))
3941                subject)
3942               ((zerop gnus-tmp-level)
3943                (if (and (eq gnus-summary-make-false-root 'empty)
3944                         (memq number gnus-tmp-gathered)
3945                         gnus-tmp-prev-subject
3946                         (inline (gnus-subject-equal
3947                                  gnus-tmp-prev-subject subject)))
3948                    gnus-summary-same-subject
3949                  subject))
3950               (t gnus-summary-same-subject)))
3951             (if (and (eq gnus-summary-make-false-root 'adopt)
3952                      (= gnus-tmp-level 1)
3953                      (memq number gnus-tmp-gathered))
3954                 (setq gnus-tmp-opening-bracket ?\<
3955                       gnus-tmp-closing-bracket ?\>)
3956               (setq gnus-tmp-opening-bracket ?\[
3957                     gnus-tmp-closing-bracket ?\]))
3958             (setq
3959              gnus-tmp-indentation
3960              (aref gnus-thread-indent-array gnus-tmp-level)
3961              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3962              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3963                                 gnus-summary-default-score 0)
3964              gnus-tmp-score-char
3965              (if (or (null gnus-summary-default-score)
3966                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3967                          gnus-summary-zcore-fuzz))
3968                  ?  ;Whitespace
3969                (if (< gnus-tmp-score gnus-summary-default-score)
3970                    gnus-score-below-mark gnus-score-over-mark))
3971              gnus-tmp-replied
3972              (cond ((memq number gnus-newsgroup-processable)
3973                     gnus-process-mark)
3974                    ((memq number gnus-newsgroup-cached)
3975                     gnus-cached-mark)
3976                    ((memq number gnus-newsgroup-replied)
3977                     gnus-replied-mark)
3978                    ((memq number gnus-newsgroup-saved)
3979                     gnus-saved-mark)
3980                    (t gnus-unread-mark))
3981              gnus-tmp-from (mail-header-from gnus-tmp-header)
3982              gnus-tmp-name
3983              (cond
3984               ((string-match "<[^>]+> *$" gnus-tmp-from)
3985                (setq beg-match (match-beginning 0))
3986                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3987                         (substring gnus-tmp-from (1+ (match-beginning 0))
3988                                    (1- (match-end 0))))
3989                    (substring gnus-tmp-from 0 beg-match)))
3990               ((string-match "(.+)" gnus-tmp-from)
3991                (substring gnus-tmp-from
3992                           (1+ (match-beginning 0)) (1- (match-end 0))))
3993               (t gnus-tmp-from)))
3994             (when (string= gnus-tmp-name "")
3995               (setq gnus-tmp-name gnus-tmp-from))
3996             (unless (numberp gnus-tmp-lines)
3997               (setq gnus-tmp-lines 0))
3998             (gnus-put-text-property
3999              (point)
4000              (progn (eval gnus-summary-line-format-spec) (point))
4001              'gnus-number number)
4002             (when gnus-visual-p
4003               (forward-line -1)
4004               (gnus-run-hooks 'gnus-summary-update-hook)
4005               (forward-line 1))
4006
4007             (setq gnus-tmp-prev-subject subject)))
4008
4009         (when (nth 1 thread)
4010           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4011         (incf gnus-tmp-level)
4012         (setq threads (if thread-end nil (cdar thread)))
4013         (unless threads
4014           (setq gnus-tmp-level 0)))))
4015   (gnus-message 7 "Generating summary...done"))
4016
4017 (defun gnus-summary-prepare-unthreaded (headers)
4018   "Generate an unthreaded summary buffer based on HEADERS."
4019   (let (header number mark)
4020
4021     (beginning-of-line)
4022
4023     (while headers
4024       ;; We may have to root out some bad articles...
4025       (when (memq (setq number (mail-header-number
4026                                 (setq header (pop headers))))
4027                   gnus-newsgroup-limit)
4028         ;; Mark article as read when it has a low score.
4029         (when (and gnus-summary-mark-below
4030                    (< (or (cdr (assq number gnus-newsgroup-scored))
4031                           gnus-summary-default-score 0)
4032                       gnus-summary-mark-below)
4033                    (not (gnus-summary-article-ancient-p number)))
4034           (setq gnus-newsgroup-unreads
4035                 (delq number gnus-newsgroup-unreads))
4036           (if gnus-newsgroup-auto-expire
4037               (push number gnus-newsgroup-expirable)
4038             (push (cons number gnus-low-score-mark)
4039                   gnus-newsgroup-reads)))
4040
4041         (setq mark (gnus-article-mark number))
4042         (push (gnus-data-make number mark (1+ (point)) header 0)
4043               gnus-newsgroup-data)
4044         (gnus-summary-insert-line
4045          header 0 number
4046          mark (memq number gnus-newsgroup-replied)
4047          (memq number gnus-newsgroup-expirable)
4048          (mail-header-subject header) nil
4049          (cdr (assq number gnus-newsgroup-scored))
4050          (memq number gnus-newsgroup-processable))))))
4051
4052 (defun gnus-summary-remove-list-identifiers ()
4053   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4054   (let ((regexp (if (stringp gnus-list-identifiers)
4055                     gnus-list-identifiers
4056                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4057     (dolist (header gnus-newsgroup-headers)
4058       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4059                           (mail-header-subject header))
4060         (mail-header-set-subject
4061          header (concat (substring (mail-header-subject header)
4062                                    0 (match-beginning 2))
4063                         (substring (mail-header-subject header)
4064                                    (match-end 2))))))))
4065
4066 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4067   "Select newsgroup GROUP.
4068 If READ-ALL is non-nil, all articles in the group are selected.
4069 If SELECT-ARTICLES, only select those articles from GROUP."
4070   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4071          ;;!!! Dirty hack; should be removed.
4072          (gnus-summary-ignore-duplicates
4073           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4074               t
4075             gnus-summary-ignore-duplicates))
4076          (info (nth 2 entry))
4077          articles fetched-articles cached)
4078
4079     (unless (gnus-check-server
4080              (setq gnus-current-select-method
4081                    (gnus-find-method-for-group group)))
4082       (error "Couldn't open server"))
4083
4084     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4085         (gnus-activate-group group)     ; Or we can activate it...
4086         (progn                          ; Or we bug out.
4087           (when (equal major-mode 'gnus-summary-mode)
4088             (kill-buffer (current-buffer)))
4089           (error "Couldn't request group %s: %s"
4090                  group (gnus-status-message group))))
4091
4092     (unless (gnus-request-group group t)
4093       (when (equal major-mode 'gnus-summary-mode)
4094         (kill-buffer (current-buffer)))
4095       (error "Couldn't request group %s: %s"
4096              group (gnus-status-message group)))
4097
4098     (setq gnus-newsgroup-name group)
4099     (setq gnus-newsgroup-unselected nil)
4100     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4101     (gnus-summary-setup-default-charset)
4102
4103     ;; Adjust and set lists of article marks.
4104     (when info
4105       (gnus-adjust-marked-articles info))
4106
4107     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4108     (when (gnus-virtual-group-p group)
4109       (setq cached gnus-newsgroup-cached))
4110
4111     (setq gnus-newsgroup-unreads
4112           (gnus-set-difference
4113            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4114            gnus-newsgroup-dormant))
4115
4116     (setq gnus-newsgroup-processable nil)
4117
4118     (gnus-update-read-articles group gnus-newsgroup-unreads)
4119
4120     (if (setq articles select-articles)
4121         (setq gnus-newsgroup-unselected
4122               (gnus-sorted-intersection
4123                gnus-newsgroup-unreads
4124                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4125       (setq articles (gnus-articles-to-read group read-all)))
4126
4127     (cond
4128      ((null articles)
4129       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4130       'quit)
4131      ((eq articles 0) nil)
4132      (t
4133       ;; Init the dependencies hash table.
4134       (setq gnus-newsgroup-dependencies
4135             (gnus-make-hashtable (length articles)))
4136       (gnus-set-global-variables)
4137       ;; Retrieve the headers and read them in.
4138       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4139       (setq gnus-newsgroup-headers
4140             (if (eq 'nov
4141                     (setq gnus-headers-retrieved-by
4142                           (gnus-retrieve-headers
4143                            articles gnus-newsgroup-name
4144                            ;; We might want to fetch old headers, but
4145                            ;; not if there is only 1 article.
4146                            (and (or (and
4147                                      (not (eq gnus-fetch-old-headers 'some))
4148                                      (not (numberp gnus-fetch-old-headers)))
4149                                     (> (length articles) 1))
4150                                 gnus-fetch-old-headers))))
4151                 (gnus-get-newsgroup-headers-xover
4152                  articles nil nil gnus-newsgroup-name t)
4153               (gnus-get-newsgroup-headers)))
4154       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4155
4156       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4157       (when cached
4158         (setq gnus-newsgroup-cached cached))
4159
4160       ;; Suppress duplicates?
4161       (when gnus-suppress-duplicates
4162         (gnus-dup-suppress-articles))
4163
4164       ;; Set the initial limit.
4165       (setq gnus-newsgroup-limit (copy-sequence articles))
4166       ;; Remove canceled articles from the list of unread articles.
4167       (setq gnus-newsgroup-unreads
4168             (gnus-set-sorted-intersection
4169              gnus-newsgroup-unreads
4170              (setq fetched-articles
4171                    (mapcar (lambda (headers) (mail-header-number headers))
4172                            gnus-newsgroup-headers))))
4173       ;; Removed marked articles that do not exist.
4174       (gnus-update-missing-marks
4175        (gnus-sorted-complement fetched-articles articles))
4176       ;; We might want to build some more threads first.
4177       (when (and gnus-fetch-old-headers
4178                  (eq gnus-headers-retrieved-by 'nov))
4179         (if (eq gnus-fetch-old-headers 'invisible)
4180             (gnus-build-all-threads)
4181           (gnus-build-old-threads)))
4182       ;; Let the Gnus agent mark articles as read.
4183       (when gnus-agent
4184         (gnus-agent-get-undownloaded-list))
4185       ;; Remove list identifiers from subject
4186       (when gnus-list-identifiers
4187         (gnus-summary-remove-list-identifiers))
4188       ;; Check whether auto-expire is to be done in this group.
4189       (setq gnus-newsgroup-auto-expire
4190             (gnus-group-auto-expirable-p group))
4191       ;; Set up the article buffer now, if necessary.
4192       (unless gnus-single-article-buffer
4193         (gnus-article-setup-buffer))
4194       ;; First and last article in this newsgroup.
4195       (when gnus-newsgroup-headers
4196         (setq gnus-newsgroup-begin
4197               (mail-header-number (car gnus-newsgroup-headers))
4198               gnus-newsgroup-end
4199               (mail-header-number
4200                (gnus-last-element gnus-newsgroup-headers))))
4201       ;; GROUP is successfully selected.
4202       (or gnus-newsgroup-headers t)))))
4203
4204 (defun gnus-articles-to-read (group &optional read-all)
4205   ;; Find out what articles the user wants to read.
4206   (let* ((articles
4207           ;; Select all articles if `read-all' is non-nil, or if there
4208           ;; are no unread articles.
4209           (if (or read-all
4210                   (and (zerop (length gnus-newsgroup-marked))
4211                        (zerop (length gnus-newsgroup-unreads)))
4212                   (eq (gnus-group-find-parameter group 'display)
4213                       'all))
4214               (or
4215                (gnus-uncompress-range (gnus-active group))
4216                (gnus-cache-articles-in-group group))
4217             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4218                           (copy-sequence gnus-newsgroup-unreads))
4219                   '<)))
4220          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4221          (scored (length scored-list))
4222          (number (length articles))
4223          (marked (+ (length gnus-newsgroup-marked)
4224                     (length gnus-newsgroup-dormant)))
4225          (select
4226           (cond
4227            ((numberp read-all)
4228             read-all)
4229            (t
4230             (condition-case ()
4231                 (cond
4232                  ((and (or (<= scored marked) (= scored number))
4233                        (numberp gnus-large-newsgroup)
4234                        (> number gnus-large-newsgroup))
4235                   (let ((input
4236                          (read-string
4237                           (format
4238                            "How many articles from %s (default %d): "
4239                            (gnus-limit-string gnus-newsgroup-name 35)
4240                            number))))
4241                     (if (string-match "^[ \t]*$" input) number input)))
4242                  ((and (> scored marked) (< scored number)
4243                        (> (- scored number) 20))
4244                   (let ((input
4245                          (read-string
4246                           (format "%s %s (%d scored, %d total): "
4247                                   "How many articles from"
4248                                   group scored number))))
4249                     (if (string-match "^[ \t]*$" input)
4250                         number input)))
4251                  (t number))
4252               (quit nil))))))
4253     (setq select (if (stringp select) (string-to-number select) select))
4254     (if (or (null select) (zerop select))
4255         select
4256       (if (and (not (zerop scored)) (<= (abs select) scored))
4257           (progn
4258             (setq articles (sort scored-list '<))
4259             (setq number (length articles)))
4260         (setq articles (copy-sequence articles)))
4261
4262       (when (< (abs select) number)
4263         (if (< select 0)
4264             ;; Select the N oldest articles.
4265             (setcdr (nthcdr (1- (abs select)) articles) nil)
4266           ;; Select the N most recent articles.
4267           (setq articles (nthcdr (- number select) articles))))
4268       (setq gnus-newsgroup-unselected
4269             (gnus-sorted-intersection
4270              gnus-newsgroup-unreads
4271              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4272       articles)))
4273
4274 (defun gnus-killed-articles (killed articles)
4275   (let (out)
4276     (while articles
4277       (when (inline (gnus-member-of-range (car articles) killed))
4278         (push (car articles) out))
4279       (setq articles (cdr articles)))
4280     out))
4281
4282 (defun gnus-uncompress-marks (marks)
4283   "Uncompress the mark ranges in MARKS."
4284   (let ((uncompressed '(score bookmark))
4285         out)
4286     (while marks
4287       (if (memq (caar marks) uncompressed)
4288           (push (car marks) out)
4289         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4290       (setq marks (cdr marks)))
4291     out))
4292
4293 (defun gnus-adjust-marked-articles (info)
4294   "Set all article lists and remove all marks that are no longer valid."
4295   (let* ((marked-lists (gnus-info-marks info))
4296          (active (gnus-active (gnus-info-group info)))
4297          (min (car active))
4298          (max (cdr active))
4299          (types gnus-article-mark-lists)
4300          (uncompressed '(score bookmark killed))
4301          marks var articles article mark)
4302
4303     (while marked-lists
4304       (setq marks (pop marked-lists))
4305       (set (setq var (intern (format "gnus-newsgroup-%s"
4306                                      (car (rassq (setq mark (car marks))
4307                                                  types)))))
4308            (if (memq (car marks) uncompressed) (cdr marks)
4309              (gnus-uncompress-range (cdr marks))))
4310
4311       (setq articles (symbol-value var))
4312
4313       ;; All articles have to be subsets of the active articles.
4314       (cond
4315        ;; Adjust "simple" lists.
4316        ((memq mark '(tick dormant expire reply save))
4317         (while articles
4318           (when (or (< (setq article (pop articles)) min) (> article max))
4319             (set var (delq article (symbol-value var))))))
4320        ;; Adjust assocs.
4321        ((memq mark uncompressed)
4322         (when (not (listp (cdr (symbol-value var))))
4323           (set var (list (symbol-value var))))
4324         (when (not (listp (cdr articles)))
4325           (setq articles (list articles)))
4326         (while articles
4327           (when (or (not (consp (setq article (pop articles))))
4328                     (< (car article) min)
4329                     (> (car article) max))
4330             (set var (delq article (symbol-value var))))))))))
4331
4332 (defun gnus-update-missing-marks (missing)
4333   "Go through the list of MISSING articles and remove them from the mark lists."
4334   (when missing
4335     (let ((types gnus-article-mark-lists)
4336           var m)
4337       ;; Go through all types.
4338       (while types
4339         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4340         (when (symbol-value var)
4341           ;; This list has articles.  So we delete all missing articles
4342           ;; from it.
4343           (setq m missing)
4344           (while m
4345             (set var (delq (pop m) (symbol-value var)))))))))
4346
4347 (defun gnus-update-marks ()
4348   "Enter the various lists of marked articles into the newsgroup info list."
4349   (let ((types gnus-article-mark-lists)
4350         (info (gnus-get-info gnus-newsgroup-name))
4351         (uncompressed '(score bookmark killed))
4352         type list newmarked symbol delta-marks)
4353     (when info
4354       ;; Add all marks lists to the list of marks lists.
4355       (while (setq type (pop types))
4356         (setq list (symbol-value
4357                     (setq symbol
4358                           (intern (format "gnus-newsgroup-%s"
4359                                           (car type))))))
4360
4361         (when list
4362           ;; Get rid of the entries of the articles that have the
4363           ;; default score.
4364           (when (and (eq (cdr type) 'score)
4365                      gnus-save-score
4366                      list)
4367             (let* ((arts list)
4368                    (prev (cons nil list))
4369                    (all prev))
4370               (while arts
4371                 (if (or (not (consp (car arts)))
4372                         (= (cdar arts) gnus-summary-default-score))
4373                     (setcdr prev (cdr arts))
4374                   (setq prev arts))
4375                 (setq arts (cdr arts)))
4376               (setq list (cdr all)))))
4377
4378         (unless (memq (cdr type) uncompressed)
4379           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4380        
4381         (when (gnus-check-backend-function
4382                'request-set-mark gnus-newsgroup-name)
4383           ;; uncompressed:s are not proper flags (they are cons cells)
4384           ;; cache is a internal gnus flag
4385           (unless (memq (cdr type) (cons 'cache uncompressed))
4386             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4387                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4388                    (add (gnus-remove-from-range
4389                          (gnus-copy-sequence list) old)))
4390               (when add
4391                 (push (list add 'add (list (cdr type))) delta-marks))
4392               (when del
4393                 (push (list del 'del (list (cdr type))) delta-marks)))))
4394           
4395         (when list
4396           (push (cons (cdr type) list) newmarked)))
4397
4398       (when delta-marks
4399         (unless (gnus-check-group gnus-newsgroup-name)
4400           (error "Can't open server for %s" gnus-newsgroup-name))
4401         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4402           
4403       ;; Enter these new marks into the info of the group.
4404       (if (nthcdr 3 info)
4405           (setcar (nthcdr 3 info) newmarked)
4406         ;; Add the marks lists to the end of the info.
4407         (when newmarked
4408           (setcdr (nthcdr 2 info) (list newmarked))))
4409
4410       ;; Cut off the end of the info if there's nothing else there.
4411       (let ((i 5))
4412         (while (and (> i 2)
4413                     (not (nth i info)))
4414           (when (nthcdr (decf i) info)
4415             (setcdr (nthcdr i info) nil)))))))
4416
4417 (defun gnus-set-mode-line (where)
4418   "This function sets the mode line of the article or summary buffers.
4419 If WHERE is `summary', the summary mode line format will be used."
4420   ;; Is this mode line one we keep updated?
4421   (when (and (memq where gnus-updated-mode-lines)
4422              (symbol-value
4423               (intern (format "gnus-%s-mode-line-format-spec" where))))
4424     (let (mode-string)
4425       (save-excursion
4426         ;; We evaluate this in the summary buffer since these
4427         ;; variables are buffer-local to that buffer.
4428         (set-buffer gnus-summary-buffer)
4429         ;; We bind all these variables that are used in the `eval' form
4430         ;; below.
4431         (let* ((mformat (symbol-value
4432                          (intern
4433                           (format "gnus-%s-mode-line-format-spec" where))))
4434                (gnus-tmp-group-name gnus-newsgroup-name)
4435                (gnus-tmp-article-number (or gnus-current-article 0))
4436                (gnus-tmp-unread gnus-newsgroup-unreads)
4437                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4438                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4439                (gnus-tmp-unread-and-unselected
4440                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4441                             (zerop gnus-tmp-unselected))
4442                        "")
4443                       ((zerop gnus-tmp-unselected)
4444                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4445                       (t (format "{%d(+%d) more}"
4446                                  gnus-tmp-unread-and-unticked
4447                                  gnus-tmp-unselected))))
4448                (gnus-tmp-subject
4449                 (if (and gnus-current-headers
4450                          (vectorp gnus-current-headers))
4451                     (gnus-mode-string-quote
4452                      (mail-header-subject gnus-current-headers))
4453                   ""))
4454                bufname-length max-len
4455                gnus-tmp-header);; passed as argument to any user-format-funcs
4456           (setq mode-string (eval mformat))
4457           (setq bufname-length (if (string-match "%b" mode-string)
4458                                    (- (length
4459                                        (buffer-name
4460                                         (if (eq where 'summary)
4461                                             nil
4462                                           (get-buffer gnus-article-buffer))))
4463                                       2)
4464                                  0))
4465           (setq max-len (max 4 (if gnus-mode-non-string-length
4466                                    (- (window-width)
4467                                       gnus-mode-non-string-length
4468                                       bufname-length)
4469                                  (length mode-string))))
4470           ;; We might have to chop a bit of the string off...
4471           (when (> (length mode-string) max-len)
4472             (setq mode-string
4473                   (concat (truncate-string-to-width mode-string (- max-len 3))
4474                           "...")))
4475           ;; Pad the mode string a bit.
4476           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4477       ;; Update the mode line.
4478       (setq mode-line-buffer-identification
4479             (gnus-mode-line-buffer-identification (list mode-string)))
4480       (set-buffer-modified-p t))))
4481
4482 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4483   "Go through the HEADERS list and add all Xrefs to a hash table.
4484 The resulting hash table is returned, or nil if no Xrefs were found."
4485   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4486          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4487          (xref-hashtb (gnus-make-hashtable))
4488          start group entry number xrefs header)
4489     (while headers
4490       (setq header (pop headers))
4491       (when (and (setq xrefs (mail-header-xref header))
4492                  (not (memq (setq number (mail-header-number header))
4493                             unreads)))
4494         (setq start 0)
4495         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4496           (setq start (match-end 0))
4497           (setq group (if prefix
4498                           (concat prefix (substring xrefs (match-beginning 1)
4499                                                     (match-end 1)))
4500                         (substring xrefs (match-beginning 1) (match-end 1))))
4501           (setq number
4502                 (string-to-int (substring xrefs (match-beginning 2)
4503                                           (match-end 2))))
4504           (if (setq entry (gnus-gethash group xref-hashtb))
4505               (setcdr entry (cons number (cdr entry)))
4506             (gnus-sethash group (cons number nil) xref-hashtb)))))
4507     (and start xref-hashtb)))
4508
4509 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4510   "Look through all the headers and mark the Xrefs as read."
4511   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4512         name entry info xref-hashtb idlist method nth4)
4513     (save-excursion
4514       (set-buffer gnus-group-buffer)
4515       (when (setq xref-hashtb
4516                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4517         (mapatoms
4518          (lambda (group)
4519            (unless (string= from-newsgroup (setq name (symbol-name group)))
4520              (setq idlist (symbol-value group))
4521              ;; Dead groups are not updated.
4522              (and (prog1
4523                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4524                             info (nth 2 entry))
4525                     (when (stringp (setq nth4 (gnus-info-method info)))
4526                       (setq nth4 (gnus-server-to-method nth4))))
4527                   ;; Only do the xrefs if the group has the same
4528                   ;; select method as the group we have just read.
4529                   (or (gnus-methods-equal-p
4530                        nth4 (gnus-find-method-for-group from-newsgroup))
4531                       virtual
4532                       (equal nth4 (setq method (gnus-find-method-for-group
4533                                                 from-newsgroup)))
4534                       (and (equal (car nth4) (car method))
4535                            (equal (nth 1 nth4) (nth 1 method))))
4536                   gnus-use-cross-reference
4537                   (or (not (eq gnus-use-cross-reference t))
4538                       virtual
4539                       ;; Only do cross-references on subscribed
4540                       ;; groups, if that is what is wanted.
4541                       (<= (gnus-info-level info) gnus-level-subscribed))
4542                   (gnus-group-make-articles-read name idlist))))
4543          xref-hashtb)))))
4544
4545 (defun gnus-compute-read-articles (group articles)
4546   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4547          (info (nth 2 entry))
4548          (active (gnus-active group))
4549          ninfo)
4550     (when entry
4551       ;; First peel off all invalid article numbers.
4552       (when active
4553         (let ((ids articles)
4554               id first)
4555           (while (setq id (pop ids))
4556             (when (and first (> id (cdr active)))
4557               ;; We'll end up in this situation in one particular
4558               ;; obscure situation.  If you re-scan a group and get
4559               ;; a new article that is cross-posted to a different
4560               ;; group that has not been re-scanned, you might get
4561               ;; crossposted article that has a higher number than
4562               ;; Gnus believes possible.  So we re-activate this
4563               ;; group as well.  This might mean doing the
4564               ;; crossposting thingy will *increase* the number
4565               ;; of articles in some groups.  Tsk, tsk.
4566               (setq active (or (gnus-activate-group group) active)))
4567             (when (or (> id (cdr active))
4568                       (< id (car active)))
4569               (setq articles (delq id articles))))))
4570       ;; If the read list is nil, we init it.
4571       (if (and active
4572                (null (gnus-info-read info))
4573                (> (car active) 1))
4574           (setq ninfo (cons 1 (1- (car active))))
4575         (setq ninfo (gnus-info-read info)))
4576       ;; Then we add the read articles to the range.
4577       (gnus-add-to-range
4578        ninfo (setq articles (sort articles '<))))))
4579
4580 (defun gnus-group-make-articles-read (group articles)
4581   "Update the info of GROUP to say that ARTICLES are read."
4582   (let* ((num 0)
4583          (entry (gnus-gethash group gnus-newsrc-hashtb))
4584          (info (nth 2 entry))
4585          (active (gnus-active group))
4586          range)
4587     (when entry
4588       (setq range (gnus-compute-read-articles group articles))
4589       (save-excursion
4590         (set-buffer gnus-group-buffer)
4591         (gnus-undo-register
4592           `(progn
4593              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4594              (gnus-info-set-read ',info ',(gnus-info-read info))
4595              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4596              (gnus-group-update-group ,group t))))
4597       ;; Add the read articles to the range.
4598       (gnus-info-set-read info range)
4599       ;; Then we have to re-compute how many unread
4600       ;; articles there are in this group.
4601       (when active
4602         (cond
4603          ((not range)
4604           (setq num (- (1+ (cdr active)) (car active))))
4605          ((not (listp (cdr range)))
4606           (setq num (- (cdr active) (- (1+ (cdr range))
4607                                        (car range)))))
4608          (t
4609           (while range
4610             (if (numberp (car range))
4611                 (setq num (1+ num))
4612               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4613             (setq range (cdr range)))
4614           (setq num (- (cdr active) num))))
4615         ;; Update the number of unread articles.
4616         (setcar entry num)
4617         ;; Update the group buffer.
4618         (gnus-group-update-group group t)))))
4619
4620 (defvar gnus-newsgroup-none-id 0)
4621
4622 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4623   (let ((cur nntp-server-buffer)
4624         (dependencies
4625          (or dependencies
4626              (save-excursion (set-buffer gnus-summary-buffer)
4627                              gnus-newsgroup-dependencies)))
4628         headers id end ref
4629         (mail-parse-charset gnus-newsgroup-charset)
4630         (mail-parse-ignored-charsets 
4631          (save-excursion (condition-case nil
4632                              (set-buffer gnus-summary-buffer)
4633                            (error))
4634                          gnus-newsgroup-ignored-charsets)))
4635     (save-excursion
4636       (set-buffer nntp-server-buffer)
4637       ;; Translate all TAB characters into SPACE characters.
4638       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4639       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4640       (gnus-run-hooks 'gnus-parse-headers-hook)
4641       (let ((case-fold-search t)
4642             in-reply-to header p lines chars)
4643         (goto-char (point-min))
4644         ;; Search to the beginning of the next header.  Error messages
4645         ;; do not begin with 2 or 3.
4646         (while (re-search-forward "^[23][0-9]+ " nil t)
4647           (setq id nil
4648                 ref nil)
4649           ;; This implementation of this function, with nine
4650           ;; search-forwards instead of the one re-search-forward and
4651           ;; a case (which basically was the old function) is actually
4652           ;; about twice as fast, even though it looks messier.  You
4653           ;; can't have everything, I guess.  Speed and elegance
4654           ;; doesn't always go hand in hand.
4655           (setq
4656            header
4657            (vector
4658             ;; Number.
4659             (prog1
4660                 (read cur)
4661               (end-of-line)
4662               (setq p (point))
4663               (narrow-to-region (point)
4664                                 (or (and (search-forward "\n.\n" nil t)
4665                                          (- (point) 2))
4666                                     (point))))
4667             ;; Subject.
4668             (progn
4669               (goto-char p)
4670               (if (search-forward "\nsubject: " nil t)
4671                   (funcall gnus-decode-encoded-word-function
4672                            (nnheader-header-value))
4673                 "(none)"))
4674             ;; From.
4675             (progn
4676               (goto-char p)
4677               (if (search-forward "\nfrom: " nil t)
4678                   (funcall gnus-decode-encoded-word-function
4679                            (nnheader-header-value))
4680                 "(nobody)"))
4681             ;; Date.
4682             (progn
4683               (goto-char p)
4684               (if (search-forward "\ndate: " nil t)
4685                   (nnheader-header-value) ""))
4686             ;; Message-ID.
4687             (progn
4688               (goto-char p)
4689               (setq id (if (re-search-forward
4690                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4691                            ;; We do it this way to make sure the Message-ID
4692                            ;; is (somewhat) syntactically valid.
4693                            (buffer-substring (match-beginning 1)
4694                                              (match-end 1))
4695                          ;; If there was no message-id, we just fake one
4696                          ;; to make subsequent routines simpler.
4697                          (nnheader-generate-fake-message-id))))
4698             ;; References.
4699             (progn
4700               (goto-char p)
4701               (if (search-forward "\nreferences: " nil t)
4702                   (progn
4703                     (setq end (point))
4704                     (prog1
4705                         (nnheader-header-value)
4706                       (setq ref
4707                             (buffer-substring
4708                              (progn
4709                                (end-of-line)
4710                                (search-backward ">" end t)
4711                                (1+ (point)))
4712                              (progn
4713                                (search-backward "<" end t)
4714                                (point))))))
4715                 ;; Get the references from the in-reply-to header if there
4716                 ;; were no references and the in-reply-to header looks
4717                 ;; promising.
4718                 (if (and (search-forward "\nin-reply-to: " nil t)
4719                          (setq in-reply-to (nnheader-header-value))
4720                          (string-match "<[^>]+>" in-reply-to))
4721                     (let (ref2)
4722                       (setq ref (substring in-reply-to (match-beginning 0)
4723                                            (match-end 0)))
4724                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4725                         (setq ref2 (substring in-reply-to (match-beginning 0)
4726                                               (match-end 0)))
4727                         (when (> (length ref2) (length ref))
4728                           (setq ref ref2)))
4729                       ref)
4730                   (setq ref nil))))
4731             ;; Chars.
4732             (progn
4733               (goto-char p)
4734               (if (search-forward "\nchars: " nil t)
4735                   (if (numberp (setq chars (ignore-errors (read cur))))
4736                       chars 0)
4737                 0))
4738             ;; Lines.
4739             (progn
4740               (goto-char p)
4741               (if (search-forward "\nlines: " nil t)
4742                   (if (numberp (setq lines (ignore-errors (read cur))))
4743                       lines 0)
4744                 0))
4745             ;; Xref.
4746             (progn
4747               (goto-char p)
4748               (and (search-forward "\nxref: " nil t)
4749                    (nnheader-header-value)))
4750             ;; Extra.
4751             (when gnus-extra-headers
4752               (let ((extra gnus-extra-headers)
4753                     out)
4754                 (while extra
4755                   (goto-char p)
4756                   (when (search-forward
4757                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4758                     (push (cons (car extra) (nnheader-header-value))
4759                           out))
4760                   (pop extra))
4761                 out))))
4762           (when (equal id ref)
4763             (setq ref nil))
4764
4765           (when gnus-alter-header-function
4766             (funcall gnus-alter-header-function header)
4767             (setq id (mail-header-id header)
4768                   ref (gnus-parent-id (mail-header-references header))))
4769
4770           (when (setq header
4771                       (gnus-dependencies-add-header
4772                        header dependencies force-new))
4773             (push header headers))
4774           (goto-char (point-max))
4775           (widen))
4776         (nreverse headers)))))
4777
4778 ;; Goes through the xover lines and returns a list of vectors
4779 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4780                                                   force-new dependencies
4781                                                   group also-fetch-heads)
4782   "Parse the news overview data in the server buffer, and return a
4783 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4784   ;; Get the Xref when the users reads the articles since most/some
4785   ;; NNTP servers do not include Xrefs when using XOVER.
4786   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4787   (let ((mail-parse-charset gnus-newsgroup-charset)
4788         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4789         (cur nntp-server-buffer)
4790         (dependencies (or dependencies gnus-newsgroup-dependencies))
4791         number headers header)
4792     (save-excursion
4793       (set-buffer nntp-server-buffer)
4794       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4795       ;; Allow the user to mangle the headers before parsing them.
4796       (gnus-run-hooks 'gnus-parse-headers-hook)
4797       (goto-char (point-min))
4798       (while (not (eobp))
4799         (condition-case ()
4800             (while (and sequence (not (eobp)))
4801               (setq number (read cur))
4802               (while (and sequence
4803                           (< (car sequence) number))
4804                 (setq sequence (cdr sequence)))
4805               (and sequence
4806                    (eq number (car sequence))
4807                    (progn
4808                      (setq sequence (cdr sequence))
4809                      (setq header (inline
4810                                     (gnus-nov-parse-line
4811                                      number dependencies force-new))))
4812                    (push header headers))
4813               (forward-line 1))
4814           (error
4815            (gnus-error 4 "Strange nov line (%d)"
4816                        (count-lines (point-min) (point)))))
4817         (forward-line 1))
4818       ;; A common bug in inn is that if you have posted an article and
4819       ;; then retrieves the active file, it will answer correctly --
4820       ;; the new article is included.  However, a NOV entry for the
4821       ;; article may not have been generated yet, so this may fail.
4822       ;; We work around this problem by retrieving the last few
4823       ;; headers using HEAD.
4824       (if (or (not also-fetch-heads)
4825               (not sequence))
4826           ;; We (probably) got all the headers.
4827           (nreverse headers)
4828         (let ((gnus-nov-is-evil t))
4829           (nconc
4830            (nreverse headers)
4831            (when (gnus-retrieve-headers sequence group)
4832              (gnus-get-newsgroup-headers))))))))
4833
4834 (defun gnus-article-get-xrefs ()
4835   "Fill in the Xref value in `gnus-current-headers', if necessary.
4836 This is meant to be called in `gnus-article-internal-prepare-hook'."
4837   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4838                                  gnus-current-headers)))
4839     (or (not gnus-use-cross-reference)
4840         (not headers)
4841         (and (mail-header-xref headers)
4842              (not (string= (mail-header-xref headers) "")))
4843         (let ((case-fold-search t)
4844               xref)
4845           (save-restriction
4846             (nnheader-narrow-to-headers)
4847             (goto-char (point-min))
4848             (when (or (and (not (eobp))
4849                            (eq (downcase (char-after)) ?x)
4850                            (looking-at "Xref:"))
4851                       (search-forward "\nXref:" nil t))
4852               (goto-char (1+ (match-end 0)))
4853               (setq xref (buffer-substring (point)
4854                                            (progn (end-of-line) (point))))
4855               (mail-header-set-xref headers xref)))))))
4856
4857 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4858   "Find article ID and insert the summary line for that article.
4859 OLD-HEADER can either be a header or a line number to insert
4860 the subject line on."
4861   (let* ((line (and (numberp old-header) old-header))
4862          (old-header (and (vectorp old-header) old-header))
4863          (header (cond ((and old-header use-old-header)
4864                         old-header)
4865                        ((and (numberp id)
4866                              (gnus-number-to-header id))
4867                         (gnus-number-to-header id))
4868                        (t
4869                         (gnus-read-header id))))
4870          (number (and (numberp id) id))
4871          d)
4872     (when header
4873       ;; Rebuild the thread that this article is part of and go to the
4874       ;; article we have fetched.
4875       (when (and (not gnus-show-threads)
4876                  old-header)
4877         (when (and number
4878                    (setq d (gnus-data-find (mail-header-number old-header))))
4879           (goto-char (gnus-data-pos d))
4880           (gnus-data-remove
4881            number
4882            (- (gnus-point-at-bol)
4883               (prog1
4884                   (1+ (gnus-point-at-eol))
4885                 (gnus-delete-line))))))
4886       (when old-header
4887         (mail-header-set-number header (mail-header-number old-header)))
4888       (setq gnus-newsgroup-sparse
4889             (delq (setq number (mail-header-number header))
4890                   gnus-newsgroup-sparse))
4891       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4892       (push number gnus-newsgroup-limit)
4893       (gnus-rebuild-thread (mail-header-id header) line)
4894       (gnus-summary-goto-subject number nil t))
4895     (when (and (numberp number)
4896                (> number 0))
4897       ;; We have to update the boundaries even if we can't fetch the
4898       ;; article if ID is a number -- so that the next `P' or `N'
4899       ;; command will fetch the previous (or next) article even
4900       ;; if the one we tried to fetch this time has been canceled.
4901       (when (> number gnus-newsgroup-end)
4902         (setq gnus-newsgroup-end number))
4903       (when (< number gnus-newsgroup-begin)
4904         (setq gnus-newsgroup-begin number))
4905       (setq gnus-newsgroup-unselected
4906             (delq number gnus-newsgroup-unselected)))
4907     ;; Report back a success?
4908     (and header (mail-header-number header))))
4909
4910 ;;; Process/prefix in the summary buffer
4911
4912 (defun gnus-summary-work-articles (n)
4913   "Return a list of articles to be worked upon.
4914 The prefix argument, the list of process marked articles, and the
4915 current article will be taken into consideration."
4916   (save-excursion
4917     (set-buffer gnus-summary-buffer)
4918     (cond
4919      (n
4920       ;; A numerical prefix has been given.
4921       (setq n (prefix-numeric-value n))
4922       (let ((backward (< n 0))
4923             (n (abs (prefix-numeric-value n)))
4924             articles article)
4925         (save-excursion
4926           (while
4927               (and (> n 0)
4928                    (push (setq article (gnus-summary-article-number))
4929                          articles)
4930                    (if backward
4931                        (gnus-summary-find-prev nil article)
4932                      (gnus-summary-find-next nil article)))
4933             (decf n)))
4934         (nreverse articles)))
4935      ((and (gnus-region-active-p) (mark))
4936       (message "region active")
4937       ;; Work on the region between point and mark.
4938       (let ((max (max (point) (mark)))
4939             articles article)
4940         (save-excursion
4941           (goto-char (min (min (point) (mark))))
4942           (while
4943               (and
4944                (push (setq article (gnus-summary-article-number)) articles)
4945                (gnus-summary-find-next nil article)
4946                (< (point) max)))
4947           (nreverse articles))))
4948      (gnus-newsgroup-processable
4949       ;; There are process-marked articles present.
4950       ;; Save current state.
4951       (gnus-summary-save-process-mark)
4952       ;; Return the list.
4953       (reverse gnus-newsgroup-processable))
4954      (t
4955       ;; Just return the current article.
4956       (list (gnus-summary-article-number))))))
4957
4958 (defmacro gnus-summary-iterate (arg &rest forms)
4959   "Iterate over the process/prefixed articles and do FORMS.
4960 ARG is the interactive prefix given to the command.  FORMS will be
4961 executed with point over the summary line of the articles."
4962   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4963     `(let ((,articles (gnus-summary-work-articles ,arg)))
4964        (while ,articles
4965          (gnus-summary-goto-subject (car ,articles))
4966          ,@forms
4967          (pop ,articles)))))
4968
4969 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4970 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4971
4972 (defun gnus-summary-save-process-mark ()
4973   "Push the current set of process marked articles on the stack."
4974   (interactive)
4975   (push (copy-sequence gnus-newsgroup-processable)
4976         gnus-newsgroup-process-stack))
4977
4978 (defun gnus-summary-kill-process-mark ()
4979   "Push the current set of process marked articles on the stack and unmark."
4980   (interactive)
4981   (gnus-summary-save-process-mark)
4982   (gnus-summary-unmark-all-processable))
4983
4984 (defun gnus-summary-yank-process-mark ()
4985   "Pop the last process mark state off the stack and restore it."
4986   (interactive)
4987   (unless gnus-newsgroup-process-stack
4988     (error "Empty mark stack"))
4989   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4990
4991 (defun gnus-summary-process-mark-set (set)
4992   "Make SET into the current process marked articles."
4993   (gnus-summary-unmark-all-processable)
4994   (while set
4995     (gnus-summary-set-process-mark (pop set))))
4996
4997 ;;; Searching and stuff
4998
4999 (defun gnus-summary-search-group (&optional backward use-level)
5000   "Search for next unread newsgroup.
5001 If optional argument BACKWARD is non-nil, search backward instead."
5002   (save-excursion
5003     (set-buffer gnus-group-buffer)
5004     (when (gnus-group-search-forward
5005            backward nil (if use-level (gnus-group-group-level) nil))
5006       (gnus-group-group-name))))
5007
5008 (defun gnus-summary-best-group (&optional exclude-group)
5009   "Find the name of the best unread group.
5010 If EXCLUDE-GROUP, do not go to this group."
5011   (save-excursion
5012     (set-buffer gnus-group-buffer)
5013     (save-excursion
5014       (gnus-group-best-unread-group exclude-group))))
5015
5016 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5017   (if backward (gnus-summary-find-prev)
5018     (let* ((dummy (gnus-summary-article-intangible-p))
5019            (article (or article (gnus-summary-article-number)))
5020            (arts (gnus-data-find-list article))
5021            result)
5022       (when (and (not dummy)
5023                  (or (not gnus-summary-check-current)
5024                      (not unread)
5025                      (not (gnus-data-unread-p (car arts)))))
5026         (setq arts (cdr arts)))
5027       (when (setq result
5028                   (if unread
5029                       (progn
5030                         (while arts
5031                           (when (or (and undownloaded
5032                                          (eq gnus-undownloaded-mark
5033                                              (gnus-data-mark (car arts))))
5034                                     (gnus-data-unread-p (car arts)))
5035                             (setq result (car arts)
5036                                   arts nil))
5037                           (setq arts (cdr arts)))
5038                         result)
5039                     (car arts)))
5040         (goto-char (gnus-data-pos result))
5041         (gnus-data-number result)))))
5042
5043 (defun gnus-summary-find-prev (&optional unread article)
5044   (let* ((eobp (eobp))
5045          (article (or article (gnus-summary-article-number)))
5046          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5047          result)
5048     (when (and (not eobp)
5049                (or (not gnus-summary-check-current)
5050                    (not unread)
5051                    (not (gnus-data-unread-p (car arts)))))
5052       (setq arts (cdr arts)))
5053     (when (setq result
5054                 (if unread
5055                     (progn
5056                       (while arts
5057                         (when (gnus-data-unread-p (car arts))
5058                           (setq result (car arts)
5059                                 arts nil))
5060                         (setq arts (cdr arts)))
5061                       result)
5062                   (car arts)))
5063       (goto-char (gnus-data-pos result))
5064       (gnus-data-number result))))
5065
5066 (defun gnus-summary-find-subject (subject &optional unread backward article)
5067   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5068          (article (or article (gnus-summary-article-number)))
5069          (articles (gnus-data-list backward))
5070          (arts (gnus-data-find-list article articles))
5071          result)
5072     (when (or (not gnus-summary-check-current)
5073               (not unread)
5074               (not (gnus-data-unread-p (car arts))))
5075       (setq arts (cdr arts)))
5076     (while arts
5077       (and (or (not unread)
5078                (gnus-data-unread-p (car arts)))
5079            (vectorp (gnus-data-header (car arts)))
5080            (gnus-subject-equal
5081             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5082            (setq result (car arts)
5083                  arts nil))
5084       (setq arts (cdr arts)))
5085     (and result
5086          (goto-char (gnus-data-pos result))
5087          (gnus-data-number result))))
5088
5089 (defun gnus-summary-search-forward (&optional unread subject backward)
5090   "Search forward for an article.
5091 If UNREAD, look for unread articles.  If SUBJECT, look for
5092 articles with that subject.  If BACKWARD, search backward instead."
5093   (cond (subject (gnus-summary-find-subject subject unread backward))
5094         (backward (gnus-summary-find-prev unread))
5095         (t (gnus-summary-find-next unread))))
5096
5097 (defun gnus-recenter (&optional n)
5098   "Center point in window and redisplay frame.
5099 Also do horizontal recentering."
5100   (interactive "P")
5101   (when (and gnus-auto-center-summary
5102              (not (eq gnus-auto-center-summary 'vertical)))
5103     (gnus-horizontal-recenter))
5104   (recenter n))
5105
5106 (defun gnus-summary-recenter ()
5107   "Center point in the summary window.
5108 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5109 displayed, no centering will be performed."
5110   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5111   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5112   (let* ((top (cond ((< (window-height) 4) 0)
5113                     ((< (window-height) 7) 1)
5114                     (t (if (numberp gnus-auto-center-summary)
5115                            gnus-auto-center-summary
5116                          2))))
5117          (height (1- (window-height)))
5118          (bottom (save-excursion (goto-char (point-max))
5119                                  (forward-line (- height))
5120                                  (point)))
5121          (window (get-buffer-window (current-buffer))))
5122     ;; The user has to want it.
5123     (when gnus-auto-center-summary
5124       (when (get-buffer-window gnus-article-buffer)
5125         ;; Only do recentering when the article buffer is displayed,
5126         ;; Set the window start to either `bottom', which is the biggest
5127         ;; possible valid number, or the second line from the top,
5128         ;; whichever is the least.
5129         (set-window-start
5130          window (min bottom (save-excursion
5131                               (forward-line (- top)) (point)))
5132          t))
5133       ;; Do horizontal recentering while we're at it.
5134       (when (and (get-buffer-window (current-buffer) t)
5135                  (not (eq gnus-auto-center-summary 'vertical)))
5136         (let ((selected (selected-window)))
5137           (select-window (get-buffer-window (current-buffer) t))
5138           (gnus-summary-position-point)
5139           (gnus-horizontal-recenter)
5140           (select-window selected))))))
5141
5142 (defun gnus-summary-jump-to-group (newsgroup)
5143   "Move point to NEWSGROUP in group mode buffer."
5144   ;; Keep update point of group mode buffer if visible.
5145   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5146       (save-window-excursion
5147         ;; Take care of tree window mode.
5148         (when (get-buffer-window gnus-group-buffer)
5149           (pop-to-buffer gnus-group-buffer))
5150         (gnus-group-jump-to-group newsgroup))
5151     (save-excursion
5152       ;; Take care of tree window mode.
5153       (if (get-buffer-window gnus-group-buffer)
5154           (pop-to-buffer gnus-group-buffer)
5155         (set-buffer gnus-group-buffer))
5156       (gnus-group-jump-to-group newsgroup))))
5157
5158 ;; This function returns a list of article numbers based on the
5159 ;; difference between the ranges of read articles in this group and
5160 ;; the range of active articles.
5161 (defun gnus-list-of-unread-articles (group)
5162   (let* ((read (gnus-info-read (gnus-get-info group)))
5163          (active (or (gnus-active group) (gnus-activate-group group)))
5164          (last (cdr active))
5165          first nlast unread)
5166     ;; If none are read, then all are unread.
5167     (if (not read)
5168         (setq first (car active))
5169       ;; If the range of read articles is a single range, then the
5170       ;; first unread article is the article after the last read
5171       ;; article.  Sounds logical, doesn't it?
5172       (if (and (not (listp (cdr read)))
5173                (or (< (car read) (car active))
5174                    (progn (setq read (list read))
5175                           nil)))
5176           (setq first (max (car active) (1+ (cdr read))))
5177         ;; `read' is a list of ranges.
5178         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5179                                   (caar read)))
5180                   1)
5181           (setq first (car active)))
5182         (while read
5183           (when first
5184             (while (< first nlast)
5185               (push first unread)
5186               (setq first (1+ first))))
5187           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5188           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5189           (setq read (cdr read)))))
5190     ;; And add the last unread articles.
5191     (while (<= first last)
5192       (push first unread)
5193       (setq first (1+ first)))
5194     ;; Return the list of unread articles.
5195     (delq 0 (nreverse unread))))
5196
5197 (defun gnus-list-of-read-articles (group)
5198   "Return a list of unread, unticked and non-dormant articles."
5199   (let* ((info (gnus-get-info group))
5200          (marked (gnus-info-marks info))
5201          (active (gnus-active group)))
5202     (and info active
5203          (gnus-set-difference
5204           (gnus-sorted-complement
5205            (gnus-uncompress-range active)
5206            (gnus-list-of-unread-articles group))
5207           (append
5208            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5209            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5210
5211 ;; Various summary commands
5212
5213 (defun gnus-summary-select-article-buffer ()
5214   "Reconfigure windows to show article buffer."
5215   (interactive)
5216   (if (not (gnus-buffer-live-p gnus-article-buffer))
5217       (error "There is no article buffer for this summary buffer")
5218     (gnus-configure-windows 'article)
5219     (select-window (get-buffer-window gnus-article-buffer))))
5220
5221 (defun gnus-summary-universal-argument (arg)
5222   "Perform any operation on all articles that are process/prefixed."
5223   (interactive "P")
5224   (let ((articles (gnus-summary-work-articles arg))
5225         func article)
5226     (if (eq
5227          (setq
5228           func
5229           (key-binding
5230            (read-key-sequence
5231             (substitute-command-keys
5232              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5233          'undefined)
5234         (gnus-error 1 "Undefined key")
5235       (save-excursion
5236         (while articles
5237           (gnus-summary-goto-subject (setq article (pop articles)))
5238           (let (gnus-newsgroup-processable)
5239             (command-execute func))
5240           (gnus-summary-remove-process-mark article)))))
5241   (gnus-summary-position-point))
5242
5243 (defun gnus-summary-toggle-truncation (&optional arg)
5244   "Toggle truncation of summary lines.
5245 With arg, turn line truncation on iff arg is positive."
5246   (interactive "P")
5247   (setq truncate-lines
5248         (if (null arg) (not truncate-lines)
5249           (> (prefix-numeric-value arg) 0)))
5250   (redraw-display))
5251
5252 (defun gnus-summary-reselect-current-group (&optional all rescan)
5253   "Exit and then reselect the current newsgroup.
5254 The prefix argument ALL means to select all articles."
5255   (interactive "P")
5256   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5257     (error "Ephemeral groups can't be reselected"))
5258   (let ((current-subject (gnus-summary-article-number))
5259         (group gnus-newsgroup-name))
5260     (setq gnus-newsgroup-begin nil)
5261     (gnus-summary-exit)
5262     ;; We have to adjust the point of group mode buffer because
5263     ;; point was moved to the next unread newsgroup by exiting.
5264     (gnus-summary-jump-to-group group)
5265     (when rescan
5266       (save-excursion
5267         (gnus-group-get-new-news-this-group 1)))
5268     (gnus-group-read-group all t)
5269     (gnus-summary-goto-subject current-subject nil t)))
5270
5271 (defun gnus-summary-rescan-group (&optional all)
5272   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5273   (interactive "P")
5274   (gnus-summary-reselect-current-group all t))
5275
5276 (defun gnus-summary-update-info (&optional non-destructive)
5277   (save-excursion
5278     (let ((group gnus-newsgroup-name))
5279       (when group
5280         (when gnus-newsgroup-kill-headers
5281           (setq gnus-newsgroup-killed
5282                 (gnus-compress-sequence
5283                  (nconc
5284                   (gnus-set-sorted-intersection
5285                    (gnus-uncompress-range gnus-newsgroup-killed)
5286                    (setq gnus-newsgroup-unselected
5287                          (sort gnus-newsgroup-unselected '<)))
5288                   (setq gnus-newsgroup-unreads
5289                         (sort gnus-newsgroup-unreads '<)))
5290                  t)))
5291         (unless (listp (cdr gnus-newsgroup-killed))
5292           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5293         (let ((headers gnus-newsgroup-headers))
5294           ;; Set the new ranges of read articles.
5295           (save-excursion
5296             (set-buffer gnus-group-buffer)
5297             (gnus-undo-force-boundary))
5298           (gnus-update-read-articles
5299            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5300           ;; Set the current article marks.
5301           (let ((gnus-newsgroup-scored
5302                  (if (and (not gnus-save-score)
5303                           (not non-destructive))
5304                      nil
5305                    gnus-newsgroup-scored)))
5306             (save-excursion
5307               (gnus-update-marks)))
5308           ;; Do the cross-ref thing.
5309           (when gnus-use-cross-reference
5310             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5311           ;; Do not switch windows but change the buffer to work.
5312           (set-buffer gnus-group-buffer)
5313           (unless (gnus-ephemeral-group-p group)
5314             (gnus-group-update-group group)))))))
5315
5316 (defun gnus-summary-save-newsrc (&optional force)
5317   "Save the current number of read/marked articles in the dribble buffer.
5318 The dribble buffer will then be saved.
5319 If FORCE (the prefix), also save the .newsrc file(s)."
5320   (interactive "P")
5321   (gnus-summary-update-info t)
5322   (if force
5323       (gnus-save-newsrc-file)
5324     (gnus-dribble-save)))
5325
5326 (defun gnus-summary-exit (&optional temporary)
5327   "Exit reading current newsgroup, and then return to group selection mode.
5328 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5329   (interactive)
5330   (gnus-set-global-variables)
5331   (when (gnus-buffer-live-p gnus-article-buffer)
5332     (save-excursion
5333       (set-buffer gnus-article-buffer)
5334       (mm-destroy-parts gnus-article-mime-handles)))
5335   (gnus-kill-save-kill-buffer)
5336   (gnus-async-halt-prefetch)
5337   (let* ((group gnus-newsgroup-name)
5338          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5339          (mode major-mode)
5340          (group-point nil)
5341          (buf (current-buffer)))
5342     (unless quit-config
5343       ;; Do adaptive scoring, and possibly save score files.
5344       (when gnus-newsgroup-adaptive
5345         (gnus-score-adaptive))
5346       (when gnus-use-scoring
5347         (gnus-score-save)))
5348     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5349     ;; If we have several article buffers, we kill them at exit.
5350     (unless gnus-single-article-buffer
5351       (gnus-kill-buffer gnus-original-article-buffer)
5352       (setq gnus-article-current nil))
5353     (when gnus-use-cache
5354       (gnus-cache-possibly-remove-articles)
5355       (gnus-cache-save-buffers))
5356     (gnus-async-prefetch-remove-group group)
5357     (when gnus-suppress-duplicates
5358       (gnus-dup-enter-articles))
5359     (when gnus-use-trees
5360       (gnus-tree-close group))
5361     (when gnus-use-cache
5362       (gnus-cache-write-active))
5363     ;; Remove entries for this group.
5364     (nnmail-purge-split-history (gnus-group-real-name group))
5365     ;; Make all changes in this group permanent.
5366     (unless quit-config
5367       (gnus-run-hooks 'gnus-exit-group-hook)
5368       (gnus-summary-update-info))
5369     (gnus-close-group group)
5370     ;; Make sure where we were, and go to next newsgroup.
5371     (set-buffer gnus-group-buffer)
5372     (unless quit-config
5373       (gnus-group-jump-to-group group))
5374     (gnus-run-hooks 'gnus-summary-exit-hook)
5375     (unless (or quit-config
5376                 ;; If this group has disappeared from the summary
5377                 ;; buffer, don't skip forwards.
5378                 (not (string= group (gnus-group-group-name))))
5379       (gnus-group-next-unread-group 1))
5380     (setq group-point (point))
5381     (if temporary
5382         nil                             ;Nothing to do.
5383       ;; If we have several article buffers, we kill them at exit.
5384       (unless gnus-single-article-buffer
5385         (gnus-kill-buffer gnus-article-buffer)
5386         (gnus-kill-buffer gnus-original-article-buffer)
5387         (setq gnus-article-current nil))
5388       (set-buffer buf)
5389       (if (not gnus-kill-summary-on-exit)
5390           (gnus-deaden-summary)
5391         ;; We set all buffer-local variables to nil.  It is unclear why
5392         ;; this is needed, but if we don't, buffer-local variables are
5393         ;; not garbage-collected, it seems.  This would the lead to en
5394         ;; ever-growing Emacs.
5395         (gnus-summary-clear-local-variables)
5396         (when (get-buffer gnus-article-buffer)
5397           (bury-buffer gnus-article-buffer))
5398         ;; We clear the global counterparts of the buffer-local
5399         ;; variables as well, just to be on the safe side.
5400         (set-buffer gnus-group-buffer)
5401         (gnus-summary-clear-local-variables)
5402         ;; Return to group mode buffer.
5403         (when (eq mode 'gnus-summary-mode)
5404           (gnus-kill-buffer buf)))
5405       (setq gnus-current-select-method gnus-select-method)
5406       (pop-to-buffer gnus-group-buffer)
5407       (if (not quit-config)
5408           (progn
5409             (goto-char group-point)
5410             (gnus-configure-windows 'group 'force))
5411         (gnus-handle-ephemeral-exit quit-config))
5412       ;; Clear the current group name.
5413       (unless quit-config
5414         (setq gnus-newsgroup-name nil)))))
5415
5416 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5417 (defun gnus-summary-exit-no-update (&optional no-questions)
5418   "Quit reading current newsgroup without updating read article info."
5419   (interactive)
5420   (let* ((group gnus-newsgroup-name)
5421          (quit-config (gnus-group-quit-config group)))
5422     (when (or no-questions
5423               gnus-expert-user
5424               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5425       (gnus-async-halt-prefetch)
5426       (mapcar 'funcall
5427               (delq 'gnus-summary-expire-articles
5428                     (copy-sequence gnus-summary-prepare-exit-hook)))
5429       (when (gnus-buffer-live-p gnus-article-buffer)
5430         (save-excursion
5431           (set-buffer gnus-article-buffer)
5432           (mm-destroy-parts gnus-article-mime-handles)))
5433       ;; If we have several article buffers, we kill them at exit.
5434       (unless gnus-single-article-buffer
5435         (gnus-kill-buffer gnus-article-buffer)
5436         (gnus-kill-buffer gnus-original-article-buffer)
5437         (setq gnus-article-current nil))
5438       (if (not gnus-kill-summary-on-exit)
5439           (gnus-deaden-summary)
5440         (gnus-close-group group)
5441         (gnus-summary-clear-local-variables)
5442         (set-buffer gnus-group-buffer)
5443         (gnus-summary-clear-local-variables)
5444         (when (get-buffer gnus-summary-buffer)
5445           (kill-buffer gnus-summary-buffer)))
5446       (unless gnus-single-article-buffer
5447         (setq gnus-article-current nil))
5448       (when gnus-use-trees
5449         (gnus-tree-close group))
5450       (gnus-async-prefetch-remove-group group)
5451       (when (get-buffer gnus-article-buffer)
5452         (bury-buffer gnus-article-buffer))
5453       ;; Return to the group buffer.
5454       (gnus-configure-windows 'group 'force)
5455       ;; Clear the current group name.
5456       (setq gnus-newsgroup-name nil)
5457       (when (equal (gnus-group-group-name) group)
5458         (gnus-group-next-unread-group 1))
5459       (when quit-config
5460         (gnus-handle-ephemeral-exit quit-config)))))
5461
5462 (defun gnus-handle-ephemeral-exit (quit-config)
5463   "Handle movement when leaving an ephemeral group.
5464 The state which existed when entering the ephemeral is reset."
5465   (if (not (buffer-name (car quit-config)))
5466       (gnus-configure-windows 'group 'force)
5467     (set-buffer (car quit-config))
5468     (cond ((eq major-mode 'gnus-summary-mode)
5469            (gnus-set-global-variables))
5470           ((eq major-mode 'gnus-article-mode)
5471            (save-excursion
5472              ;; The `gnus-summary-buffer' variable may point
5473              ;; to the old summary buffer when using a single
5474              ;; article buffer.
5475              (unless (gnus-buffer-live-p gnus-summary-buffer)
5476                (set-buffer gnus-group-buffer))
5477              (set-buffer gnus-summary-buffer)
5478              (gnus-set-global-variables))))
5479     (if (or (eq (cdr quit-config) 'article)
5480             (eq (cdr quit-config) 'pick))
5481         (progn
5482           ;; The current article may be from the ephemeral group
5483           ;; thus it is best that we reload this article
5484           (gnus-summary-show-article)
5485           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5486               (gnus-configure-windows 'pick 'force)
5487             (gnus-configure-windows (cdr quit-config) 'force)))
5488       (gnus-configure-windows (cdr quit-config) 'force))
5489     (when (eq major-mode 'gnus-summary-mode)
5490       (gnus-summary-next-subject 1 nil t)
5491       (gnus-summary-recenter)
5492       (gnus-summary-position-point))))
5493
5494 ;;; Dead summaries.
5495
5496 (defvar gnus-dead-summary-mode-map nil)
5497
5498 (unless gnus-dead-summary-mode-map
5499   (setq gnus-dead-summary-mode-map (make-keymap))
5500   (suppress-keymap gnus-dead-summary-mode-map)
5501   (substitute-key-definition
5502    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5503   (let ((keys '("\C-d" "\r" "\177" [delete])))
5504     (while keys
5505       (define-key gnus-dead-summary-mode-map
5506         (pop keys) 'gnus-summary-wake-up-the-dead))))
5507
5508 (defvar gnus-dead-summary-mode nil
5509   "Minor mode for Gnus summary buffers.")
5510
5511 (defun gnus-dead-summary-mode (&optional arg)
5512   "Minor mode for Gnus summary buffers."
5513   (interactive "P")
5514   (when (eq major-mode 'gnus-summary-mode)
5515     (make-local-variable 'gnus-dead-summary-mode)
5516     (setq gnus-dead-summary-mode
5517           (if (null arg) (not gnus-dead-summary-mode)
5518             (> (prefix-numeric-value arg) 0)))
5519     (when gnus-dead-summary-mode
5520       (gnus-add-minor-mode
5521        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5522
5523 (defun gnus-deaden-summary ()
5524   "Make the current summary buffer into a dead summary buffer."
5525   ;; Kill any previous dead summary buffer.
5526   (when (and gnus-dead-summary
5527              (buffer-name gnus-dead-summary))
5528     (save-excursion
5529       (set-buffer gnus-dead-summary)
5530       (when gnus-dead-summary-mode
5531         (kill-buffer (current-buffer)))))
5532   ;; Make this the current dead summary.
5533   (setq gnus-dead-summary (current-buffer))
5534   (gnus-dead-summary-mode 1)
5535   (let ((name (buffer-name)))
5536     (when (string-match "Summary" name)
5537       (rename-buffer
5538        (concat (substring name 0 (match-beginning 0)) "Dead "
5539                (substring name (match-beginning 0)))
5540        t))))
5541
5542 (defun gnus-kill-or-deaden-summary (buffer)
5543   "Kill or deaden the summary BUFFER."
5544   (save-excursion
5545     (when (and (buffer-name buffer)
5546                (not gnus-single-article-buffer))
5547       (save-excursion
5548         (set-buffer buffer)
5549         (gnus-kill-buffer gnus-article-buffer)
5550         (gnus-kill-buffer gnus-original-article-buffer)))
5551     (cond (gnus-kill-summary-on-exit
5552            (when (and gnus-use-trees
5553                       (gnus-buffer-exists-p buffer))
5554              (save-excursion
5555                (set-buffer buffer)
5556                (gnus-tree-close gnus-newsgroup-name)))
5557            (gnus-kill-buffer buffer))
5558           ((gnus-buffer-exists-p buffer)
5559            (save-excursion
5560              (set-buffer buffer)
5561              (gnus-deaden-summary))))))
5562
5563 (defun gnus-summary-wake-up-the-dead (&rest args)
5564   "Wake up the dead summary buffer."
5565   (interactive)
5566   (gnus-dead-summary-mode -1)
5567   (let ((name (buffer-name)))
5568     (when (string-match "Dead " name)
5569       (rename-buffer
5570        (concat (substring name 0 (match-beginning 0))
5571                (substring name (match-end 0)))
5572        t)))
5573   (gnus-message 3 "This dead summary is now alive again"))
5574
5575 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5576 (defun gnus-summary-fetch-faq (&optional faq-dir)
5577   "Fetch the FAQ for the current group.
5578 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5579 in."
5580   (interactive
5581    (list
5582     (when current-prefix-arg
5583       (completing-read
5584        "Faq dir: " (and (listp gnus-group-faq-directory)
5585                         (mapcar (lambda (file) (list file))
5586                                 gnus-group-faq-directory))))))
5587   (let (gnus-faq-buffer)
5588     (when (setq gnus-faq-buffer
5589                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5590       (gnus-configure-windows 'summary-faq))))
5591
5592 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5593 (defun gnus-summary-describe-group (&optional force)
5594   "Describe the current newsgroup."
5595   (interactive "P")
5596   (gnus-group-describe-group force gnus-newsgroup-name))
5597
5598 (defun gnus-summary-describe-briefly ()
5599   "Describe summary mode commands briefly."
5600   (interactive)
5601   (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")))
5602
5603 ;; Walking around group mode buffer from summary mode.
5604
5605 (defun gnus-summary-next-group (&optional no-article target-group backward)
5606   "Exit current newsgroup and then select next unread newsgroup.
5607 If prefix argument NO-ARTICLE is non-nil, no article is selected
5608 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5609 previous group instead."
5610   (interactive "P")
5611   ;; Stop pre-fetching.
5612   (gnus-async-halt-prefetch)
5613   (let ((current-group gnus-newsgroup-name)
5614         (current-buffer (current-buffer))
5615         entered)
5616     ;; First we semi-exit this group to update Xrefs and all variables.
5617     ;; We can't do a real exit, because the window conf must remain
5618     ;; the same in case the user is prompted for info, and we don't
5619     ;; want the window conf to change before that...
5620     (gnus-summary-exit t)
5621     (while (not entered)
5622       ;; Then we find what group we are supposed to enter.
5623       (set-buffer gnus-group-buffer)
5624       (gnus-group-jump-to-group current-group)
5625       (setq target-group
5626             (or target-group
5627                 (if (eq gnus-keep-same-level 'best)
5628                     (gnus-summary-best-group gnus-newsgroup-name)
5629                   (gnus-summary-search-group backward gnus-keep-same-level))))
5630       (if (not target-group)
5631           ;; There are no further groups, so we return to the group
5632           ;; buffer.
5633           (progn
5634             (gnus-message 5 "Returning to the group buffer")
5635             (setq entered t)
5636             (when (gnus-buffer-live-p current-buffer)
5637               (set-buffer current-buffer)
5638               (gnus-summary-exit))
5639             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5640         ;; We try to enter the target group.
5641         (gnus-group-jump-to-group target-group)
5642         (let ((unreads (gnus-group-group-unread)))
5643           (if (and (or (eq t unreads)
5644                        (and unreads (not (zerop unreads))))
5645                    (gnus-summary-read-group
5646                     target-group nil no-article
5647                     (and (buffer-name current-buffer) current-buffer)
5648                     nil backward))
5649               (setq entered t)
5650             (setq current-group target-group
5651                   target-group nil)))))))
5652
5653 (defun gnus-summary-prev-group (&optional no-article)
5654   "Exit current newsgroup and then select previous unread newsgroup.
5655 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5656   (interactive "P")
5657   (gnus-summary-next-group no-article nil t))
5658
5659 ;; Walking around summary lines.
5660
5661 (defun gnus-summary-first-subject (&optional unread undownloaded)
5662   "Go to the first unread subject.
5663 If UNREAD is non-nil, go to the first unread article.
5664 Returns the article selected or nil if there are no unread articles."
5665   (interactive "P")
5666   (prog1
5667       (cond
5668        ;; Empty summary.
5669        ((null gnus-newsgroup-data)
5670         (gnus-message 3 "No articles in the group")
5671         nil)
5672        ;; Pick the first article.
5673        ((not unread)
5674         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5675         (gnus-data-number (car gnus-newsgroup-data)))
5676        ;; No unread articles.
5677        ((null gnus-newsgroup-unreads)
5678         (gnus-message 3 "No more unread articles")
5679         nil)
5680        ;; Find the first unread article.
5681        (t
5682         (let ((data gnus-newsgroup-data))
5683           (while (and data
5684                       (and (not (and undownloaded
5685                                      (eq gnus-undownloaded-mark
5686                                          (gnus-data-mark (car data)))))
5687                            (not (gnus-data-unread-p (car data)))))
5688             (setq data (cdr data)))
5689           (when data
5690             (goto-char (gnus-data-pos (car data)))
5691             (gnus-data-number (car data))))))
5692     (gnus-summary-position-point)))
5693
5694 (defun gnus-summary-next-subject (n &optional unread dont-display)
5695   "Go to next N'th summary line.
5696 If N is negative, go to the previous N'th subject line.
5697 If UNREAD is non-nil, only unread articles are selected.
5698 The difference between N and the actual number of steps taken is
5699 returned."
5700   (interactive "p")
5701   (let ((backward (< n 0))
5702         (n (abs n)))
5703     (while (and (> n 0)
5704                 (if backward
5705                     (gnus-summary-find-prev unread)
5706                   (gnus-summary-find-next unread)))
5707       (unless (zerop (setq n (1- n)))
5708         (gnus-summary-show-thread)))
5709     (when (/= 0 n)
5710       (gnus-message 7 "No more%s articles"
5711                     (if unread " unread" "")))
5712     (unless dont-display
5713       (gnus-summary-recenter)
5714       (gnus-summary-position-point))
5715     n))
5716
5717 (defun gnus-summary-next-unread-subject (n)
5718   "Go to next N'th unread summary line."
5719   (interactive "p")
5720   (gnus-summary-next-subject n t))
5721
5722 (defun gnus-summary-prev-subject (n &optional unread)
5723   "Go to previous N'th summary line.
5724 If optional argument UNREAD is non-nil, only unread article is selected."
5725   (interactive "p")
5726   (gnus-summary-next-subject (- n) unread))
5727
5728 (defun gnus-summary-prev-unread-subject (n)
5729   "Go to previous N'th unread summary line."
5730   (interactive "p")
5731   (gnus-summary-next-subject (- n) t))
5732
5733 (defun gnus-summary-goto-subject (article &optional force silent)
5734   "Go the subject line of ARTICLE.
5735 If FORCE, also allow jumping to articles not currently shown."
5736   (interactive "nArticle number: ")
5737   (let ((b (point))
5738         (data (gnus-data-find article)))
5739     ;; We read in the article if we have to.
5740     (and (not data)
5741          force
5742          (gnus-summary-insert-subject
5743           article
5744           (if (or (numberp force) (vectorp force)) force)
5745           t)
5746          (setq data (gnus-data-find article)))
5747     (goto-char b)
5748     (if (not data)
5749         (progn
5750           (unless silent
5751             (gnus-message 3 "Can't find article %d" article))
5752           nil)
5753       (goto-char (gnus-data-pos data))
5754       (gnus-summary-position-point)
5755       article)))
5756
5757 ;; Walking around summary lines with displaying articles.
5758
5759 (defun gnus-summary-expand-window (&optional arg)
5760   "Make the summary buffer take up the entire Emacs frame.
5761 Given a prefix, will force an `article' buffer configuration."
5762   (interactive "P")
5763   (if arg
5764       (gnus-configure-windows 'article 'force)
5765     (gnus-configure-windows 'summary 'force)))
5766
5767 (defun gnus-summary-display-article (article &optional all-header)
5768   "Display ARTICLE in article buffer."
5769   (gnus-set-global-variables)
5770   (if (null article)
5771       nil
5772     (prog1
5773         (if gnus-summary-display-article-function
5774             (funcall gnus-summary-display-article-function article all-header)
5775           (gnus-article-prepare article all-header))
5776       (gnus-run-hooks 'gnus-select-article-hook)
5777       (when (and gnus-current-article
5778                  (not (zerop gnus-current-article)))
5779         (gnus-summary-goto-subject gnus-current-article))
5780       (gnus-summary-recenter)
5781       (when (and gnus-use-trees gnus-show-threads)
5782         (gnus-possibly-generate-tree article)
5783         (gnus-highlight-selected-tree article))
5784       ;; Successfully display article.
5785       (gnus-article-set-window-start
5786        (cdr (assq article gnus-newsgroup-bookmarks))))))
5787
5788 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5789   "Select the current article.
5790 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5791 non-nil, the article will be re-fetched even if it already present in
5792 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5793 be displayed."
5794   ;; Make sure we are in the summary buffer to work around bbdb bug.
5795   (unless (eq major-mode 'gnus-summary-mode)
5796     (set-buffer gnus-summary-buffer))
5797   (let ((article (or article (gnus-summary-article-number)))
5798         (all-headers (not (not all-headers))) ;Must be T or NIL.
5799         gnus-summary-display-article-function)
5800     (and (not pseudo)
5801          (gnus-summary-article-pseudo-p article)
5802          (error "This is a pseudo-article"))
5803     (save-excursion
5804       (set-buffer gnus-summary-buffer)
5805       (if (or (and gnus-single-article-buffer
5806                    (or (null gnus-current-article)
5807                        (null gnus-article-current)
5808                        (null (get-buffer gnus-article-buffer))
5809                        (not (eq article (cdr gnus-article-current)))
5810                        (not (equal (car gnus-article-current)
5811                                    gnus-newsgroup-name))))
5812               (and (not gnus-single-article-buffer)
5813                    (or (null gnus-current-article)
5814                        (not (eq gnus-current-article article))))
5815               force)
5816           ;; The requested article is different from the current article.
5817           (progn
5818             (gnus-summary-display-article article all-headers)
5819             (when (or all-headers gnus-show-all-headers)
5820               (gnus-article-show-all-headers))
5821             (gnus-article-set-window-start
5822              (cdr (assq article gnus-newsgroup-bookmarks)))
5823             article)
5824         (when (or all-headers gnus-show-all-headers)
5825           (gnus-article-show-all-headers))
5826         'old))))
5827
5828 (defun gnus-summary-set-current-mark (&optional current-mark)
5829   "Obsolete function."
5830   nil)
5831
5832 (defun gnus-summary-next-article (&optional unread subject backward push)
5833   "Select the next article.
5834 If UNREAD, only unread articles are selected.
5835 If SUBJECT, only articles with SUBJECT are selected.
5836 If BACKWARD, the previous article is selected instead of the next."
5837   (interactive "P")
5838   (cond
5839    ;; Is there such an article?
5840    ((and (gnus-summary-search-forward unread subject backward)
5841          (or (gnus-summary-display-article (gnus-summary-article-number))
5842              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5843     (gnus-summary-position-point))
5844    ;; If not, we try the first unread, if that is wanted.
5845    ((and subject
5846          gnus-auto-select-same
5847          (gnus-summary-first-unread-article))
5848     (gnus-summary-position-point)
5849     (gnus-message 6 "Wrapped"))
5850    ;; Try to get next/previous article not displayed in this group.
5851    ((and gnus-auto-extend-newsgroup
5852          (not unread) (not subject))
5853     (gnus-summary-goto-article
5854      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5855      nil (count-lines (point-min) (point))))
5856    ;; Go to next/previous group.
5857    (t
5858     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5859       (gnus-summary-jump-to-group gnus-newsgroup-name))
5860     (let ((cmd last-command-char)
5861           (point
5862            (save-excursion
5863              (set-buffer gnus-group-buffer)
5864              (point)))
5865           (group
5866            (if (eq gnus-keep-same-level 'best)
5867                (gnus-summary-best-group gnus-newsgroup-name)
5868              (gnus-summary-search-group backward gnus-keep-same-level))))
5869       ;; For some reason, the group window gets selected.  We change
5870       ;; it back.
5871       (select-window (get-buffer-window (current-buffer)))
5872       ;; Select next unread newsgroup automagically.
5873       (cond
5874        ((or (not gnus-auto-select-next)
5875             (not cmd))
5876         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5877        ((or (eq gnus-auto-select-next 'quietly)
5878             (and (eq gnus-auto-select-next 'slightly-quietly)
5879                  push)
5880             (and (eq gnus-auto-select-next 'almost-quietly)
5881                  (gnus-summary-last-article-p)))
5882         ;; Select quietly.
5883         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5884             (gnus-summary-exit)
5885           (gnus-message 7 "No more%s articles (%s)..."
5886                         (if unread " unread" "")
5887                         (if group (concat "selecting " group)
5888                           "exiting"))
5889           (gnus-summary-next-group nil group backward)))
5890        (t
5891         (when (gnus-key-press-event-p last-input-event)
5892           (gnus-summary-walk-group-buffer
5893            gnus-newsgroup-name cmd unread backward point))))))))
5894
5895 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5896   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5897                       (?\C-p (gnus-group-prev-unread-group 1))))
5898         (cursor-in-echo-area t)
5899         keve key group ended)
5900     (save-excursion
5901       (set-buffer gnus-group-buffer)
5902       (goto-char start)
5903       (setq group
5904             (if (eq gnus-keep-same-level 'best)
5905                 (gnus-summary-best-group gnus-newsgroup-name)
5906               (gnus-summary-search-group backward gnus-keep-same-level))))
5907     (while (not ended)
5908       (gnus-message
5909        5 "No more%s articles%s" (if unread " unread" "")
5910        (if (and group
5911                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5912            (format " (Type %s for %s [%s])"
5913                    (single-key-description cmd) group
5914                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5915          (format " (Type %s to exit %s)"
5916                  (single-key-description cmd)
5917                  gnus-newsgroup-name)))
5918       ;; Confirm auto selection.
5919       (setq key (car (setq keve (gnus-read-event-char))))
5920       (setq ended t)
5921       (cond
5922        ((assq key keystrokes)
5923         (let ((obuf (current-buffer)))
5924           (switch-to-buffer gnus-group-buffer)
5925           (when group
5926             (gnus-group-jump-to-group group))
5927           (eval (cadr (assq key keystrokes)))
5928           (setq group (gnus-group-group-name))
5929           (switch-to-buffer obuf))
5930         (setq ended nil))
5931        ((equal key cmd)
5932         (if (or (not group)
5933                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5934             (gnus-summary-exit)
5935           (gnus-summary-next-group nil group backward)))
5936        (t
5937         (push (cdr keve) unread-command-events))))))
5938
5939 (defun gnus-summary-next-unread-article ()
5940   "Select unread article after current one."
5941   (interactive)
5942   (gnus-summary-next-article
5943    (or (not (eq gnus-summary-goto-unread 'never))
5944        (gnus-summary-last-article-p (gnus-summary-article-number)))
5945    (and gnus-auto-select-same
5946         (gnus-summary-article-subject))))
5947
5948 (defun gnus-summary-prev-article (&optional unread subject)
5949   "Select the article after the current one.
5950 If UNREAD is non-nil, only unread articles are selected."
5951   (interactive "P")
5952   (gnus-summary-next-article unread subject t))
5953
5954 (defun gnus-summary-prev-unread-article ()
5955   "Select unread article before current one."
5956   (interactive)
5957   (gnus-summary-prev-article
5958    (or (not (eq gnus-summary-goto-unread 'never))
5959        (gnus-summary-first-article-p (gnus-summary-article-number)))
5960    (and gnus-auto-select-same
5961         (gnus-summary-article-subject))))
5962
5963 (defun gnus-summary-next-page (&optional lines circular)
5964   "Show next page of the selected article.
5965 If at the end of the current article, select the next article.
5966 LINES says how many lines should be scrolled up.
5967
5968 If CIRCULAR is non-nil, go to the start of the article instead of
5969 selecting the next article when reaching the end of the current
5970 article."
5971   (interactive "P")
5972   (setq gnus-summary-buffer (current-buffer))
5973   (gnus-set-global-variables)
5974   (let ((article (gnus-summary-article-number))
5975         (article-window (get-buffer-window gnus-article-buffer t))
5976         endp)
5977     ;; If the buffer is empty, we have no article.
5978     (unless article
5979       (error "No article to select"))
5980     (gnus-configure-windows 'article)
5981     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5982         (if (and (eq gnus-summary-goto-unread 'never)
5983                  (not (gnus-summary-last-article-p article)))
5984             (gnus-summary-next-article)
5985           (gnus-summary-next-unread-article))
5986       (if (or (null gnus-current-article)
5987               (null gnus-article-current)
5988               (/= article (cdr gnus-article-current))
5989               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5990           ;; Selected subject is different from current article's.
5991           (gnus-summary-display-article article)
5992         (when article-window
5993           (gnus-eval-in-buffer-window gnus-article-buffer
5994             (setq endp (gnus-article-next-page lines)))
5995           (when endp
5996             (cond (circular
5997                    (gnus-summary-beginning-of-article))
5998                   (lines
5999                    (gnus-message 3 "End of message"))
6000                   ((null lines)
6001                    (if (and (eq gnus-summary-goto-unread 'never)
6002                             (not (gnus-summary-last-article-p article)))
6003                        (gnus-summary-next-article)
6004                      (gnus-summary-next-unread-article))))))))
6005     (gnus-summary-recenter)
6006     (gnus-summary-position-point)))
6007
6008 (defun gnus-summary-prev-page (&optional lines move)
6009   "Show previous page of selected article.
6010 Argument LINES specifies lines to be scrolled down.
6011 If MOVE, move to the previous unread article if point is at
6012 the beginning of the buffer."
6013   (interactive "P")
6014   (let ((article (gnus-summary-article-number))
6015         (article-window (get-buffer-window gnus-article-buffer t))
6016         endp)
6017     (gnus-configure-windows 'article)
6018     (if (or (null gnus-current-article)
6019             (null gnus-article-current)
6020             (/= article (cdr gnus-article-current))
6021             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6022         ;; Selected subject is different from current article's.
6023         (gnus-summary-display-article article)
6024       (gnus-summary-recenter)
6025       (when article-window
6026         (gnus-eval-in-buffer-window gnus-article-buffer
6027           (setq endp (gnus-article-prev-page lines)))
6028         (when (and move endp)
6029           (cond (lines
6030                  (gnus-message 3 "Beginning of message"))
6031                 ((null lines)
6032                  (if (and (eq gnus-summary-goto-unread 'never)
6033                           (not (gnus-summary-first-article-p article)))
6034                      (gnus-summary-prev-article)
6035                    (gnus-summary-prev-unread-article))))))))
6036   (gnus-summary-position-point))
6037
6038 (defun gnus-summary-prev-page-or-article (&optional lines)
6039   "Show previous page of selected article.
6040 Argument LINES specifies lines to be scrolled down.
6041 If at the beginning of the article, go to the next article."
6042   (interactive "P")
6043   (gnus-summary-prev-page lines t))
6044
6045 (defun gnus-summary-scroll-up (lines)
6046   "Scroll up (or down) one line current article.
6047 Argument LINES specifies lines to be scrolled up (or down if negative)."
6048   (interactive "p")
6049   (gnus-configure-windows 'article)
6050   (gnus-summary-show-thread)
6051   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6052     (gnus-eval-in-buffer-window gnus-article-buffer
6053       (cond ((> lines 0)
6054              (when (gnus-article-next-page lines)
6055                (gnus-message 3 "End of message")))
6056             ((< lines 0)
6057              (gnus-article-prev-page (- lines))))))
6058   (gnus-summary-recenter)
6059   (gnus-summary-position-point))
6060
6061 (defun gnus-summary-scroll-down (lines)
6062   "Scroll down (or up) one line current article.
6063 Argument LINES specifies lines to be scrolled down (or up if negative)."
6064   (interactive "p")
6065   (gnus-summary-scroll-up (- lines)))
6066
6067 (defun gnus-summary-next-same-subject ()
6068   "Select next article which has the same subject as current one."
6069   (interactive)
6070   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6071
6072 (defun gnus-summary-prev-same-subject ()
6073   "Select previous article which has the same subject as current one."
6074   (interactive)
6075   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6076
6077 (defun gnus-summary-next-unread-same-subject ()
6078   "Select next unread article which has the same subject as current one."
6079   (interactive)
6080   (gnus-summary-next-article t (gnus-summary-article-subject)))
6081
6082 (defun gnus-summary-prev-unread-same-subject ()
6083   "Select previous unread article which has the same subject as current one."
6084   (interactive)
6085   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6086
6087 (defun gnus-summary-first-unread-article ()
6088   "Select the first unread article.
6089 Return nil if there are no unread articles."
6090   (interactive)
6091   (prog1
6092       (when (gnus-summary-first-subject t)
6093         (gnus-summary-show-thread)
6094         (gnus-summary-first-subject t)
6095         (gnus-summary-display-article (gnus-summary-article-number)))
6096     (gnus-summary-position-point)))
6097
6098 (defun gnus-summary-first-unread-subject ()
6099   "Place the point on the subject line of the first unread article.
6100 Return nil if there are no unread articles."
6101   (interactive)
6102   (prog1
6103       (when (gnus-summary-first-subject t)
6104         (gnus-summary-show-thread)
6105         (gnus-summary-first-subject t))
6106     (gnus-summary-position-point)))
6107
6108 (defun gnus-summary-first-article ()
6109   "Select the first article.
6110 Return nil if there are no articles."
6111   (interactive)
6112   (prog1
6113       (when (gnus-summary-first-subject)
6114         (gnus-summary-show-thread)
6115         (gnus-summary-first-subject)
6116         (gnus-summary-display-article (gnus-summary-article-number)))
6117     (gnus-summary-position-point)))
6118
6119 (defun gnus-summary-best-unread-article ()
6120   "Select the unread article with the highest score."
6121   (interactive)
6122   (let ((best -1000000)
6123         (data gnus-newsgroup-data)
6124         article score)
6125     (while data
6126       (and (gnus-data-unread-p (car data))
6127            (> (setq score
6128                     (gnus-summary-article-score (gnus-data-number (car data))))
6129               best)
6130            (setq best score
6131                  article (gnus-data-number (car data))))
6132       (setq data (cdr data)))
6133     (prog1
6134         (if article
6135             (gnus-summary-goto-article article)
6136           (error "No unread articles"))
6137       (gnus-summary-position-point))))
6138
6139 (defun gnus-summary-last-subject ()
6140   "Go to the last displayed subject line in the group."
6141   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6142     (when article
6143       (gnus-summary-goto-subject article))))
6144
6145 (defun gnus-summary-goto-article (article &optional all-headers force)
6146   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6147 If ALL-HEADERS is non-nil, no header lines are hidden.
6148 If FORCE, go to the article even if it isn't displayed.  If FORCE
6149 is a number, it is the line the article is to be displayed on."
6150   (interactive
6151    (list
6152     (completing-read
6153      "Article number or Message-ID: "
6154      (mapcar (lambda (number) (list (int-to-string number)))
6155              gnus-newsgroup-limit))
6156     current-prefix-arg
6157     t))
6158   (prog1
6159       (if (and (stringp article)
6160                (string-match "@" article))
6161           (gnus-summary-refer-article article)
6162         (when (stringp article)
6163           (setq article (string-to-number article)))
6164         (if (gnus-summary-goto-subject article force)
6165             (gnus-summary-display-article article all-headers)
6166           (gnus-message 4 "Couldn't go to article %s" article) nil))
6167     (gnus-summary-position-point)))
6168
6169 (defun gnus-summary-goto-last-article ()
6170   "Go to the previously read article."
6171   (interactive)
6172   (prog1
6173       (when gnus-last-article
6174         (gnus-summary-goto-article gnus-last-article nil t))
6175     (gnus-summary-position-point)))
6176
6177 (defun gnus-summary-pop-article (number)
6178   "Pop one article off the history and go to the previous.
6179 NUMBER articles will be popped off."
6180   (interactive "p")
6181   (let (to)
6182     (setq gnus-newsgroup-history
6183           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6184     (if to
6185         (gnus-summary-goto-article (car to) nil t)
6186       (error "Article history empty")))
6187   (gnus-summary-position-point))
6188
6189 ;; Summary commands and functions for limiting the summary buffer.
6190
6191 (defun gnus-summary-limit-to-articles (n)
6192   "Limit the summary buffer to the next N articles.
6193 If not given a prefix, use the process marked articles instead."
6194   (interactive "P")
6195   (prog1
6196       (let ((articles (gnus-summary-work-articles n)))
6197         (setq gnus-newsgroup-processable nil)
6198         (gnus-summary-limit articles))
6199     (gnus-summary-position-point)))
6200
6201 (defun gnus-summary-pop-limit (&optional total)
6202   "Restore the previous limit.
6203 If given a prefix, remove all limits."
6204   (interactive "P")
6205   (when total
6206     (setq gnus-newsgroup-limits
6207           (list (mapcar (lambda (h) (mail-header-number h))
6208                         gnus-newsgroup-headers))))
6209   (unless gnus-newsgroup-limits
6210     (error "No limit to pop"))
6211   (prog1
6212       (gnus-summary-limit nil 'pop)
6213     (gnus-summary-position-point)))
6214
6215 (defun gnus-summary-limit-to-subject (subject &optional header)
6216   "Limit the summary buffer to articles that have subjects that match a regexp."
6217   (interactive "sLimit to subject (regexp): ")
6218   (unless header
6219     (setq header "subject"))
6220   (when (not (equal "" subject))
6221     (prog1
6222         (let ((articles (gnus-summary-find-matching
6223                          (or header "subject") subject 'all)))
6224           (unless articles
6225             (error "Found no matches for \"%s\"" subject))
6226           (gnus-summary-limit articles))
6227       (gnus-summary-position-point))))
6228
6229 (defun gnus-summary-limit-to-author (from)
6230   "Limit the summary buffer to articles that have authors that match a regexp."
6231   (interactive "sLimit to author (regexp): ")
6232   (gnus-summary-limit-to-subject from "from"))
6233
6234 (defun gnus-summary-limit-to-age (age &optional younger-p)
6235   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6236 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6237 articles that are younger than AGE days."
6238   (interactive
6239    (let ((younger current-prefix-arg)
6240          (days-got nil)
6241          days)
6242      (while (not days-got)
6243        (setq days (if younger
6244                       (read-string "Limit to articles within (in days): ")
6245                     (read-string "Limit to articles old than (in days): ")))
6246        (when (> (length days) 0)
6247          (setq days (read days)))
6248        (if (numberp days)
6249            (setq days-got t)
6250          (message "Please enter a number.")
6251          (sleep-for 1)))
6252      (list days younger)))
6253   (prog1
6254       (let ((data gnus-newsgroup-data)
6255             (cutoff (days-to-time age))
6256             articles d date is-younger)
6257         (while (setq d (pop data))
6258           (when (and (vectorp (gnus-data-header d))
6259                      (setq date (mail-header-date (gnus-data-header d))))
6260             (setq is-younger (time-less-p
6261                               (time-since (condition-case ()
6262                                               (date-to-time date)
6263                                             (error '(0 0))))
6264                               cutoff))
6265             (when (if younger-p
6266                       is-younger
6267                     (not is-younger))
6268               (push (gnus-data-number d) articles))))
6269         (gnus-summary-limit (nreverse articles)))
6270     (gnus-summary-position-point)))
6271
6272 (defun gnus-summary-limit-to-extra (header regexp)
6273   "Limit the summary buffer to articles that match an 'extra' header."
6274   (interactive
6275    (let ((header
6276           (intern
6277            (gnus-completing-read
6278             (symbol-name (car gnus-extra-headers))      
6279             "Limit extra header:"       
6280             (mapcar (lambda (x) 
6281                       (cons (symbol-name x) x))
6282                     gnus-extra-headers)
6283             nil                 
6284             t))))
6285      (list header
6286            (read-string (format "Limit to header %s (regexp): " header)))))
6287   (when (not (equal "" regexp))
6288     (prog1
6289         (let ((articles (gnus-summary-find-matching
6290                          (cons 'extra header) regexp 'all)))
6291           (unless articles
6292             (error "Found no matches for \"%s\"" regexp))
6293           (gnus-summary-limit articles))
6294       (gnus-summary-position-point))))
6295
6296 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6297 (make-obsolete
6298  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6299
6300 (defun gnus-summary-limit-to-unread (&optional all)
6301   "Limit the summary buffer to articles that are not marked as read.
6302 If ALL is non-nil, limit strictly to unread articles."
6303   (interactive "P")
6304   (if all
6305       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6306     (gnus-summary-limit-to-marks
6307      ;; Concat all the marks that say that an article is read and have
6308      ;; those removed.
6309      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6310            gnus-killed-mark gnus-kill-file-mark
6311            gnus-low-score-mark gnus-expirable-mark
6312            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6313            gnus-duplicate-mark gnus-souped-mark)
6314      'reverse)))
6315
6316 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6317 (make-obsolete 'gnus-summary-delete-marked-with
6318                'gnus-summary-limit-exlude-marks)
6319
6320 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6321   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6322 If REVERSE, limit the summary buffer to articles that are marked
6323 with MARKS.  MARKS can either be a string of marks or a list of marks.
6324 Returns how many articles were removed."
6325   (interactive "sMarks: ")
6326   (gnus-summary-limit-to-marks marks t))
6327
6328 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6329   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6330 If REVERSE (the prefix), limit the summary buffer to articles that are
6331 not marked with MARKS.  MARKS can either be a string of marks or a
6332 list of marks.
6333 Returns how many articles were removed."
6334   (interactive "sMarks: \nP")
6335   (prog1
6336       (let ((data gnus-newsgroup-data)
6337             (marks (if (listp marks) marks
6338                      (append marks nil))) ; Transform to list.
6339             articles)
6340         (while data
6341           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6342                   (memq (gnus-data-mark (car data)) marks))
6343             (push (gnus-data-number (car data)) articles))
6344           (setq data (cdr data)))
6345         (gnus-summary-limit articles))
6346     (gnus-summary-position-point)))
6347
6348 (defun gnus-summary-limit-to-score (&optional score)
6349   "Limit to articles with score at or above SCORE."
6350   (interactive "P")
6351   (setq score (if score
6352                   (prefix-numeric-value score)
6353                 (or gnus-summary-default-score 0)))
6354   (let ((data gnus-newsgroup-data)
6355         articles)
6356     (while data
6357       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6358                 score)
6359         (push (gnus-data-number (car data)) articles))
6360       (setq data (cdr data)))
6361     (prog1
6362         (gnus-summary-limit articles)
6363       (gnus-summary-position-point))))
6364
6365 (defun gnus-summary-limit-include-thread (id)
6366   "Display all the hidden articles that in the current thread."
6367   (interactive (list (mail-header-id (gnus-summary-article-header))))
6368   (let ((articles (gnus-articles-in-thread
6369                    (gnus-id-to-thread (gnus-root-id id)))))
6370     (prog1
6371         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6372       (gnus-summary-position-point))))
6373
6374 (defun gnus-summary-limit-include-dormant ()
6375   "Display all the hidden articles that are marked as dormant.
6376 Note that this command only works on a subset of the articles currently
6377 fetched for this group."
6378   (interactive)
6379   (unless gnus-newsgroup-dormant
6380     (error "There are no dormant articles in this group"))
6381   (prog1
6382       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6383     (gnus-summary-position-point)))
6384
6385 (defun gnus-summary-limit-exclude-dormant ()
6386   "Hide all dormant articles."
6387   (interactive)
6388   (prog1
6389       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6390     (gnus-summary-position-point)))
6391
6392 (defun gnus-summary-limit-exclude-childless-dormant ()
6393   "Hide all dormant articles that have no children."
6394   (interactive)
6395   (let ((data (gnus-data-list t))
6396         articles d children)
6397     ;; Find all articles that are either not dormant or have
6398     ;; children.
6399     (while (setq d (pop data))
6400       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6401                 (and (setq children
6402                            (gnus-article-children (gnus-data-number d)))
6403                      (let (found)
6404                        (while children
6405                          (when (memq (car children) articles)
6406                            (setq children nil
6407                                  found t))
6408                          (pop children))
6409                        found)))
6410         (push (gnus-data-number d) articles)))
6411     ;; Do the limiting.
6412     (prog1
6413         (gnus-summary-limit articles)
6414       (gnus-summary-position-point))))
6415
6416 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6417   "Mark all unread excluded articles as read.
6418 If ALL, mark even excluded ticked and dormants as read."
6419   (interactive "P")
6420   (let ((articles (gnus-sorted-complement
6421                    (sort
6422                     (mapcar (lambda (h) (mail-header-number h))
6423                             gnus-newsgroup-headers)
6424                     '<)
6425                    (sort gnus-newsgroup-limit '<)))
6426         article)
6427     (setq gnus-newsgroup-unreads
6428           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6429     (if all
6430         (setq gnus-newsgroup-dormant nil
6431               gnus-newsgroup-marked nil
6432               gnus-newsgroup-reads
6433               (nconc
6434                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6435                gnus-newsgroup-reads))
6436       (while (setq article (pop articles))
6437         (unless (or (memq article gnus-newsgroup-dormant)
6438                     (memq article gnus-newsgroup-marked))
6439           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6440
6441 (defun gnus-summary-limit (articles &optional pop)
6442   (if pop
6443       ;; We pop the previous limit off the stack and use that.
6444       (setq articles (car gnus-newsgroup-limits)
6445             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6446     ;; We use the new limit, so we push the old limit on the stack.
6447     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6448   ;; Set the limit.
6449   (setq gnus-newsgroup-limit articles)
6450   (let ((total (length gnus-newsgroup-data))
6451         (data (gnus-data-find-list (gnus-summary-article-number)))
6452         (gnus-summary-mark-below nil)   ; Inhibit this.
6453         found)
6454     ;; This will do all the work of generating the new summary buffer
6455     ;; according to the new limit.
6456     (gnus-summary-prepare)
6457     ;; Hide any threads, possibly.
6458     (and gnus-show-threads
6459          gnus-thread-hide-subtree
6460          (gnus-summary-hide-all-threads))
6461     ;; Try to return to the article you were at, or one in the
6462     ;; neighborhood.
6463     (when data
6464       ;; We try to find some article after the current one.
6465       (while data
6466         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6467           (setq data nil
6468                 found t))
6469         (setq data (cdr data))))
6470     (unless found
6471       ;; If there is no data, that means that we were after the last
6472       ;; article.  The same goes when we can't find any articles
6473       ;; after the current one.
6474       (goto-char (point-max))
6475       (gnus-summary-find-prev))
6476     (gnus-set-mode-line 'summary)
6477     ;; We return how many articles were removed from the summary
6478     ;; buffer as a result of the new limit.
6479     (- total (length gnus-newsgroup-data))))
6480
6481 (defsubst gnus-invisible-cut-children (threads)
6482   (let ((num 0))
6483     (while threads
6484       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6485         (incf num))
6486       (pop threads))
6487     (< num 2)))
6488
6489 (defsubst gnus-cut-thread (thread)
6490   "Go forwards in the thread until we find an article that we want to display."
6491   (when (or (eq gnus-fetch-old-headers 'some)
6492             (eq gnus-fetch-old-headers 'invisible)
6493             (numberp gnus-fetch-old-headers)
6494             (eq gnus-build-sparse-threads 'some)
6495             (eq gnus-build-sparse-threads 'more))
6496     ;; Deal with old-fetched headers and sparse threads.
6497     (while (and
6498             thread
6499             (or
6500              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6501              (gnus-summary-article-ancient-p
6502               (mail-header-number (car thread))))
6503             (if (or (<= (length (cdr thread)) 1)
6504                     (eq gnus-fetch-old-headers 'invisible))
6505                 (setq gnus-newsgroup-limit
6506                       (delq (mail-header-number (car thread))
6507                             gnus-newsgroup-limit)
6508                       thread (cadr thread))
6509               (when (gnus-invisible-cut-children (cdr thread))
6510                 (let ((th (cdr thread)))
6511                   (while th
6512                     (if (memq (mail-header-number (caar th))
6513                               gnus-newsgroup-limit)
6514                         (setq thread (car th)
6515                               th nil)
6516                       (setq th (cdr th))))))))))
6517   thread)
6518
6519 (defun gnus-cut-threads (threads)
6520   "Cut off all uninteresting articles from the beginning of threads."
6521   (when (or (eq gnus-fetch-old-headers 'some)
6522             (eq gnus-fetch-old-headers 'invisible)
6523             (numberp gnus-fetch-old-headers)
6524             (eq gnus-build-sparse-threads 'some)
6525             (eq gnus-build-sparse-threads 'more))
6526     (let ((th threads))
6527       (while th
6528         (setcar th (gnus-cut-thread (car th)))
6529         (setq th (cdr th)))))
6530   ;; Remove nixed out threads.
6531   (delq nil threads))
6532
6533 (defun gnus-summary-initial-limit (&optional show-if-empty)
6534   "Figure out what the initial limit is supposed to be on group entry.
6535 This entails weeding out unwanted dormants, low-scored articles,
6536 fetch-old-headers verbiage, and so on."
6537   ;; Most groups have nothing to remove.
6538   (if (or gnus-inhibit-limiting
6539           (and (null gnus-newsgroup-dormant)
6540                (not (eq gnus-fetch-old-headers 'some))
6541                (not (numberp gnus-fetch-old-headers))
6542                (not (eq gnus-fetch-old-headers 'invisible))
6543                (null gnus-summary-expunge-below)
6544                (not (eq gnus-build-sparse-threads 'some))
6545                (not (eq gnus-build-sparse-threads 'more))
6546                (null gnus-thread-expunge-below)
6547                (not gnus-use-nocem)))
6548       ()                                ; Do nothing.
6549     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6550     (setq gnus-newsgroup-limit nil)
6551     (mapatoms
6552      (lambda (node)
6553        (unless (car (symbol-value node))
6554          ;; These threads have no parents -- they are roots.
6555          (let ((nodes (cdr (symbol-value node)))
6556                thread)
6557            (while nodes
6558              (if (and gnus-thread-expunge-below
6559                       (< (gnus-thread-total-score (car nodes))
6560                          gnus-thread-expunge-below))
6561                  (gnus-expunge-thread (pop nodes))
6562                (setq thread (pop nodes))
6563                (gnus-summary-limit-children thread))))))
6564      gnus-newsgroup-dependencies)
6565     ;; If this limitation resulted in an empty group, we might
6566     ;; pop the previous limit and use it instead.
6567     (when (and (not gnus-newsgroup-limit)
6568                show-if-empty)
6569       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6570     gnus-newsgroup-limit))
6571
6572 (defun gnus-summary-limit-children (thread)
6573   "Return 1 if this subthread is visible and 0 if it is not."
6574   ;; First we get the number of visible children to this thread.  This
6575   ;; is done by recursing down the thread using this function, so this
6576   ;; will really go down to a leaf article first, before slowly
6577   ;; working its way up towards the root.
6578   (when thread
6579     (let ((children
6580            (if (cdr thread)
6581                (apply '+ (mapcar 'gnus-summary-limit-children
6582                                  (cdr thread)))
6583              0))
6584           (number (mail-header-number (car thread)))
6585           score)
6586       (if (and
6587            (not (memq number gnus-newsgroup-marked))
6588            (or
6589             ;; If this article is dormant and has absolutely no visible
6590             ;; children, then this article isn't visible.
6591             (and (memq number gnus-newsgroup-dormant)
6592                  (zerop children))
6593             ;; If this is "fetch-old-headered" and there is no
6594             ;; visible children, then we don't want this article.
6595             (and (or (eq gnus-fetch-old-headers 'some)
6596                      (numberp gnus-fetch-old-headers))
6597                  (gnus-summary-article-ancient-p number)
6598                  (zerop children))
6599             ;; If this is "fetch-old-headered" and `invisible', then
6600             ;; we don't want this article.
6601             (and (eq gnus-fetch-old-headers 'invisible)
6602                  (gnus-summary-article-ancient-p number))
6603             ;; If this is a sparsely inserted article with no children,
6604             ;; we don't want it.
6605             (and (eq gnus-build-sparse-threads 'some)
6606                  (gnus-summary-article-sparse-p number)
6607                  (zerop children))
6608             ;; If we use expunging, and this article is really
6609             ;; low-scored, then we don't want this article.
6610             (when (and gnus-summary-expunge-below
6611                        (< (setq score
6612                                 (or (cdr (assq number gnus-newsgroup-scored))
6613                                     gnus-summary-default-score))
6614                           gnus-summary-expunge-below))
6615               ;; We increase the expunge-tally here, but that has
6616               ;; nothing to do with the limits, really.
6617               (incf gnus-newsgroup-expunged-tally)
6618               ;; We also mark as read here, if that's wanted.
6619               (when (and gnus-summary-mark-below
6620                          (< score gnus-summary-mark-below))
6621                 (setq gnus-newsgroup-unreads
6622                       (delq number gnus-newsgroup-unreads))
6623                 (if gnus-newsgroup-auto-expire
6624                     (push number gnus-newsgroup-expirable)
6625                   (push (cons number gnus-low-score-mark)
6626                         gnus-newsgroup-reads)))
6627               t)
6628             ;; Check NoCeM things.
6629             (if (and gnus-use-nocem
6630                      (gnus-nocem-unwanted-article-p
6631                       (mail-header-id (car thread))))
6632                 (progn
6633                   (setq gnus-newsgroup-unreads
6634                         (delq number gnus-newsgroup-unreads))
6635                   t))))
6636           ;; Nope, invisible article.
6637           0
6638         ;; Ok, this article is to be visible, so we add it to the limit
6639         ;; and return 1.
6640         (push number gnus-newsgroup-limit)
6641         1))))
6642
6643 (defun gnus-expunge-thread (thread)
6644   "Mark all articles in THREAD as read."
6645   (let* ((number (mail-header-number (car thread))))
6646     (incf gnus-newsgroup-expunged-tally)
6647     ;; We also mark as read here, if that's wanted.
6648     (setq gnus-newsgroup-unreads
6649           (delq number gnus-newsgroup-unreads))
6650     (if gnus-newsgroup-auto-expire
6651         (push number gnus-newsgroup-expirable)
6652       (push (cons number gnus-low-score-mark)
6653             gnus-newsgroup-reads)))
6654   ;; Go recursively through all subthreads.
6655   (mapcar 'gnus-expunge-thread (cdr thread)))
6656
6657 ;; Summary article oriented commands
6658
6659 (defun gnus-summary-refer-parent-article (n)
6660   "Refer parent article N times.
6661 If N is negative, go to ancestor -N instead.
6662 The difference between N and the number of articles fetched is returned."
6663   (interactive "p")
6664   (let ((skip 1)
6665         error header ref)
6666     (when (not (natnump n))
6667       (setq skip (abs n)
6668             n 1))
6669     (while (and (> n 0)
6670                 (not error))
6671       (setq header (gnus-summary-article-header))
6672       (if (and (eq (mail-header-number header)
6673                    (cdr gnus-article-current))
6674                (equal gnus-newsgroup-name
6675                       (car gnus-article-current)))
6676           ;; If we try to find the parent of the currently
6677           ;; displayed article, then we take a look at the actual
6678           ;; References header, since this is slightly more
6679           ;; reliable than the References field we got from the
6680           ;; server.
6681           (save-excursion
6682             (set-buffer gnus-original-article-buffer)
6683             (nnheader-narrow-to-headers)
6684             (unless (setq ref (message-fetch-field "references"))
6685               (setq ref (message-fetch-field "in-reply-to")))
6686             (widen))
6687         (setq ref
6688               ;; It's not the current article, so we take a bet on
6689               ;; the value we got from the server.
6690               (mail-header-references header)))
6691       (if (and ref
6692                (not (equal ref "")))
6693           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6694             (gnus-message 1 "Couldn't find parent"))
6695         (gnus-message 1 "No references in article %d"
6696                       (gnus-summary-article-number))
6697         (setq error t))
6698       (decf n))
6699     (gnus-summary-position-point)
6700     n))
6701
6702 (defun gnus-summary-refer-references ()
6703   "Fetch all articles mentioned in the References header.
6704 Return the number of articles fetched."
6705   (interactive)
6706   (let ((ref (mail-header-references (gnus-summary-article-header)))
6707         (current (gnus-summary-article-number))
6708         (n 0))
6709     (if (or (not ref)
6710             (equal ref ""))
6711         (error "No References in the current article")
6712       ;; For each Message-ID in the References header...
6713       (while (string-match "<[^>]*>" ref)
6714         (incf n)
6715         ;; ... fetch that article.
6716         (gnus-summary-refer-article
6717          (prog1 (match-string 0 ref)
6718            (setq ref (substring ref (match-end 0))))))
6719       (gnus-summary-goto-subject current)
6720       (gnus-summary-position-point)
6721       n)))
6722
6723 (defun gnus-summary-refer-thread (&optional limit)
6724   "Fetch all articles in the current thread.
6725 If LIMIT (the numerical prefix), fetch that many old headers instead
6726 of what's specified by the `gnus-refer-thread-limit' variable."
6727   (interactive "P")
6728   (let ((id (mail-header-id (gnus-summary-article-header)))
6729         (limit (if limit (prefix-numeric-value limit)
6730                  gnus-refer-thread-limit)))
6731     ;; We want to fetch LIMIT *old* headers, but we also have to
6732     ;; re-fetch all the headers in the current buffer, because many of
6733     ;; them may be undisplayed.  So we adjust LIMIT.
6734     (when (numberp limit)
6735       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6736     (unless (eq gnus-fetch-old-headers 'invisible)
6737       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6738       ;; Retrieve the headers and read them in.
6739       (if (eq (gnus-retrieve-headers
6740                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6741               'nov)
6742           (gnus-build-all-threads)
6743         (error "Can't fetch thread from backends that don't support NOV"))
6744       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6745     (gnus-summary-limit-include-thread id)))
6746
6747 (defun gnus-summary-refer-article (message-id)
6748   "Fetch an article specified by MESSAGE-ID."
6749   (interactive "sMessage-ID: ")
6750   (when (and (stringp message-id)
6751              (not (zerop (length message-id))))
6752     ;; Construct the correct Message-ID if necessary.
6753     ;; Suggested by tale@pawl.rpi.edu.
6754     (unless (string-match "^<" message-id)
6755       (setq message-id (concat "<" message-id)))
6756     (unless (string-match ">$" message-id)
6757       (setq message-id (concat message-id ">")))
6758     (let* ((header (gnus-id-to-header message-id))
6759            (sparse (and header
6760                         (gnus-summary-article-sparse-p
6761                          (mail-header-number header))
6762                         (memq (mail-header-number header)
6763                               gnus-newsgroup-limit)))
6764            number)
6765       (cond
6766        ;; If the article is present in the buffer we just go to it.
6767        ((and header
6768              (or (not (gnus-summary-article-sparse-p
6769                        (mail-header-number header)))
6770                  sparse))
6771         (prog1
6772             (gnus-summary-goto-article
6773              (mail-header-number header) nil t)
6774           (when sparse
6775             (gnus-summary-update-article (mail-header-number header)))))
6776        (t
6777         ;; We fetch the article.
6778         (catch 'found
6779           (dolist (gnus-override-method (gnus-refer-article-methods))
6780             (gnus-check-server gnus-override-method)
6781             ;; Fetch the header, and display the article.
6782             (when (setq number (gnus-summary-insert-subject message-id))
6783               (gnus-summary-select-article nil nil nil number)
6784               (throw 'found t)))
6785           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6786
6787 (defun gnus-refer-article-methods ()
6788   "Return a list of referrable methods."
6789   (cond
6790    ;; No method, so we default to current and native.
6791    ((null gnus-refer-article-method)
6792     (list gnus-current-select-method gnus-select-method))
6793    ;; Current.
6794    ((eq 'current gnus-refer-article-method)
6795     (list gnus-current-select-method))
6796    ;; List of select methods.
6797    ((not (stringp (cadr gnus-refer-article-method)))
6798     (let (out)
6799       (dolist (method gnus-refer-article-method)
6800         (push (if (eq 'current method)
6801                   gnus-current-select-method
6802                 method)
6803               out))
6804       (nreverse out)))
6805    ;; One single select method.
6806    (t
6807     (list gnus-refer-article-method))))
6808
6809 (defun gnus-summary-edit-parameters ()
6810   "Edit the group parameters of the current group."
6811   (interactive)
6812   (gnus-group-edit-group gnus-newsgroup-name 'params))
6813
6814 (defun gnus-summary-customize-parameters ()
6815   "Customize the group parameters of the current group."
6816   (interactive)
6817   (gnus-group-customize gnus-newsgroup-name))
6818
6819 (defun gnus-summary-enter-digest-group (&optional force)
6820   "Enter an nndoc group based on the current article.
6821 If FORCE, force a digest interpretation.  If not, try
6822 to guess what the document format is."
6823   (interactive "P")
6824   (let ((conf gnus-current-window-configuration))
6825     (save-excursion
6826       (gnus-summary-select-article))
6827     (setq gnus-current-window-configuration conf)
6828     (let* ((name (format "%s-%d"
6829                          (gnus-group-prefixed-name
6830                           gnus-newsgroup-name (list 'nndoc ""))
6831                          (save-excursion
6832                            (set-buffer gnus-summary-buffer)
6833                            gnus-current-article)))
6834            (ogroup gnus-newsgroup-name)
6835            (params (append (gnus-info-params (gnus-get-info ogroup))
6836                            (list (cons 'to-group ogroup))
6837                            (list (cons 'save-article-group ogroup))))
6838            (case-fold-search t)
6839            (buf (current-buffer))
6840            dig to-address)
6841       (save-excursion
6842         (set-buffer gnus-original-article-buffer)
6843         ;; Have the digest group inherit the main mail address of
6844         ;; the parent article.
6845         (when (setq to-address (or (message-fetch-field "reply-to")
6846                                    (message-fetch-field "from")))
6847           (setq params (append (list (cons 'to-address to-address)))))
6848         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6849         (insert-buffer-substring gnus-original-article-buffer)
6850         ;; Remove lines that may lead nndoc to misinterpret the
6851         ;; document type.
6852         (narrow-to-region
6853          (goto-char (point-min))
6854          (or (search-forward "\n\n" nil t) (point)))
6855         (goto-char (point-min))
6856         (delete-matching-lines "^Path:\\|^From ")
6857         (widen))
6858       (unwind-protect
6859           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6860                     (gnus-newsgroup-ephemeral-ignored-charsets
6861                      gnus-newsgroup-ignored-charsets))
6862                 (gnus-group-read-ephemeral-group
6863                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6864                               (nndoc-article-type
6865                                ,(if force 'mbox 'guess))) t))
6866               ;; Make all postings to this group go to the parent group.
6867               (nconc (gnus-info-params (gnus-get-info name))
6868                      params)
6869             ;; Couldn't select this doc group.
6870             (switch-to-buffer buf)
6871             (gnus-set-global-variables)
6872             (gnus-configure-windows 'summary)
6873             (gnus-message 3 "Article couldn't be entered?"))
6874         (kill-buffer dig)))))
6875
6876 (defun gnus-summary-read-document (n)
6877   "Open a new group based on the current article(s).
6878 This will allow you to read digests and other similar
6879 documents as newsgroups.
6880 Obeys the standard process/prefix convention."
6881   (interactive "P")
6882   (let* ((articles (gnus-summary-work-articles n))
6883          (ogroup gnus-newsgroup-name)
6884          (params (append (gnus-info-params (gnus-get-info ogroup))
6885                          (list (cons 'to-group ogroup))))
6886          article group egroup groups vgroup)
6887     (while (setq article (pop articles))
6888       (setq group (format "%s-%d" gnus-newsgroup-name article))
6889       (gnus-summary-remove-process-mark article)
6890       (when (gnus-summary-display-article article)
6891         (save-excursion
6892           (with-temp-buffer
6893             (insert-buffer-substring gnus-original-article-buffer)
6894             ;; Remove some headers that may lead nndoc to make
6895             ;; the wrong guess.
6896             (message-narrow-to-head)
6897             (goto-char (point-min))
6898             (delete-matching-lines "^\\(Path\\):\\|^From ")
6899             (widen)
6900             (if (setq egroup
6901                       (gnus-group-read-ephemeral-group
6902                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6903                                      (nndoc-article-type guess))
6904                        t nil t))
6905                 (progn
6906                   ;; Make all postings to this group go to the parent group.
6907                   (nconc (gnus-info-params (gnus-get-info egroup))
6908                          params)
6909                   (push egroup groups))
6910               ;; Couldn't select this doc group.
6911               (gnus-error 3 "Article couldn't be entered"))))))
6912     ;; Now we have selected all the documents.
6913     (cond
6914      ((not groups)
6915       (error "None of the articles could be interpreted as documents"))
6916      ((gnus-group-read-ephemeral-group
6917        (setq vgroup (format
6918                      "nnvirtual:%s-%s" gnus-newsgroup-name
6919                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6920        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6921        t
6922        (cons (current-buffer) 'summary)))
6923      (t
6924       (error "Couldn't select virtual nndoc group")))))
6925
6926 (defun gnus-summary-isearch-article (&optional regexp-p)
6927   "Do incremental search forward on the current article.
6928 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6929   (interactive "P")
6930   (gnus-summary-select-article)
6931   (gnus-configure-windows 'article)
6932   (gnus-eval-in-buffer-window gnus-article-buffer
6933     (save-restriction
6934       (widen)
6935       (isearch-forward regexp-p))))
6936
6937 (defun gnus-summary-search-article-forward (regexp &optional backward)
6938   "Search for an article containing REGEXP forward.
6939 If BACKWARD, search backward instead."
6940   (interactive
6941    (list (read-string
6942           (format "Search article %s (regexp%s): "
6943                   (if current-prefix-arg "backward" "forward")
6944                   (if gnus-last-search-regexp
6945                       (concat ", default " gnus-last-search-regexp)
6946                     "")))
6947          current-prefix-arg))
6948   (if (string-equal regexp "")
6949       (setq regexp (or gnus-last-search-regexp ""))
6950     (setq gnus-last-search-regexp regexp))
6951   (if (gnus-summary-search-article regexp backward)
6952       (gnus-summary-show-thread)
6953     (error "Search failed: \"%s\"" regexp)))
6954
6955 (defun gnus-summary-search-article-backward (regexp)
6956   "Search for an article containing REGEXP backward."
6957   (interactive
6958    (list (read-string
6959           (format "Search article backward (regexp%s): "
6960                   (if gnus-last-search-regexp
6961                       (concat ", default " gnus-last-search-regexp)
6962                     "")))))
6963   (gnus-summary-search-article-forward regexp 'backward))
6964
6965 (defun gnus-summary-search-article (regexp &optional backward)
6966   "Search for an article containing REGEXP.
6967 Optional argument BACKWARD means do search for backward.
6968 `gnus-select-article-hook' is not called during the search."
6969   ;; We have to require this here to make sure that the following
6970   ;; dynamic binding isn't shadowed by autoloading.
6971   (require 'gnus-async)
6972   (require 'gnus-art)
6973   (let ((gnus-select-article-hook nil)  ;Disable hook.
6974         (gnus-article-prepare-hook nil)
6975         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6976         (gnus-use-article-prefetch nil)
6977         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6978         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6979         (sum (current-buffer))
6980         (gnus-display-mime-function nil)
6981         (found nil)
6982         point)
6983     (gnus-save-hidden-threads
6984       (gnus-summary-select-article)
6985       (set-buffer gnus-article-buffer)
6986       (goto-char (window-point (get-buffer-window (current-buffer))))
6987       (when backward
6988         (forward-line -1))
6989       (while (not found)
6990         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6991         (if (if backward
6992                 (re-search-backward regexp nil t)
6993               (re-search-forward regexp nil t))
6994             ;; We found the regexp.
6995             (progn
6996               (setq found 'found)
6997               (beginning-of-line)
6998               (set-window-start
6999                (get-buffer-window (current-buffer))
7000                (point))
7001               (forward-line 1)
7002               (set-window-point
7003                (get-buffer-window (current-buffer))
7004                (point))
7005               (set-buffer sum)
7006               (setq point (point)))
7007           ;; We didn't find it, so we go to the next article.
7008           (set-buffer sum)
7009           (setq found 'not)
7010           (while (eq found 'not)
7011             (if (not (if backward (gnus-summary-find-prev)
7012                        (gnus-summary-find-next)))
7013                 ;; No more articles.
7014                 (setq found t)
7015               ;; Select the next article and adjust point.
7016               (unless (gnus-summary-article-sparse-p
7017                        (gnus-summary-article-number))
7018                 (setq found nil)
7019                 (gnus-summary-select-article)
7020                 (set-buffer gnus-article-buffer)
7021                 (widen)
7022                 (goto-char (if backward (point-max) (point-min))))))))
7023       (gnus-message 7 ""))
7024     ;; Return whether we found the regexp.
7025     (when (eq found 'found)
7026       (goto-char point)
7027       (gnus-summary-show-thread)
7028       (gnus-summary-goto-subject gnus-current-article)
7029       (gnus-summary-position-point)
7030       t)))
7031
7032 (defun gnus-summary-find-matching (header regexp &optional backward unread
7033                                           not-case-fold)
7034   "Return a list of all articles that match REGEXP on HEADER.
7035 The search stars on the current article and goes forwards unless
7036 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7037 If UNREAD is non-nil, only unread articles will
7038 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7039 in the comparisons."
7040   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7041                 (gnus-data-find-list
7042                  (gnus-summary-article-number) (gnus-data-list backward))))
7043         (case-fold-search (not not-case-fold))
7044         articles d func)
7045     (if (consp header)
7046         (if (eq (car header) 'extra)
7047             (setq func
7048                   `(lambda (h)
7049                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7050                          "")))
7051           (error "%s is an invalid header" header))
7052       (unless (fboundp (intern (concat "mail-header-" header)))
7053         (error "%s is not a valid header" header))
7054       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7055     (while data
7056       (setq d (car data))
7057       (and (or (not unread)             ; We want all articles...
7058                (gnus-data-unread-p d))  ; Or just unreads.
7059            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7060            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7061            (push (gnus-data-number d) articles)) ; Success!
7062       (setq data (cdr data)))
7063     (nreverse articles)))
7064
7065 (defun gnus-summary-execute-command (header regexp command &optional backward)
7066   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7067 If HEADER is an empty string (or nil), the match is done on the entire
7068 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7069   (interactive
7070    (list (let ((completion-ignore-case t))
7071            (completing-read
7072             "Header name: "
7073             (mapcar (lambda (string) (list string))
7074                     '("Number" "Subject" "From" "Lines" "Date"
7075                       "Message-ID" "Xref" "References" "Body"))
7076             nil 'require-match))
7077          (read-string "Regexp: ")
7078          (read-key-sequence "Command: ")
7079          current-prefix-arg))
7080   (when (equal header "Body")
7081     (setq header ""))
7082   ;; Hidden thread subtrees must be searched as well.
7083   (gnus-summary-show-all-threads)
7084   ;; We don't want to change current point nor window configuration.
7085   (save-excursion
7086     (save-window-excursion
7087       (gnus-message 6 "Executing %s..." (key-description command))
7088       ;; We'd like to execute COMMAND interactively so as to give arguments.
7089       (gnus-execute header regexp
7090                     `(call-interactively ',(key-binding command))
7091                     backward)
7092       (gnus-message 6 "Executing %s...done" (key-description command)))))
7093
7094 (defun gnus-summary-beginning-of-article ()
7095   "Scroll the article back to the beginning."
7096   (interactive)
7097   (gnus-summary-select-article)
7098   (gnus-configure-windows 'article)
7099   (gnus-eval-in-buffer-window gnus-article-buffer
7100     (widen)
7101     (goto-char (point-min))
7102     (when gnus-page-broken
7103       (gnus-narrow-to-page))))
7104
7105 (defun gnus-summary-end-of-article ()
7106   "Scroll to the end of the article."
7107   (interactive)
7108   (gnus-summary-select-article)
7109   (gnus-configure-windows 'article)
7110   (gnus-eval-in-buffer-window gnus-article-buffer
7111     (widen)
7112     (goto-char (point-max))
7113     (recenter -3)
7114     (when gnus-page-broken
7115       (gnus-narrow-to-page))))
7116
7117 (defun gnus-summary-print-article (&optional filename n)
7118   "Generate and print a PostScript image of the N next (mail) articles.
7119
7120 If N is negative, print the N previous articles.  If N is nil and articles
7121 have been marked with the process mark, print these instead.
7122
7123 If the optional first argument FILENAME is nil, send the image to the
7124 printer.  If FILENAME is a string, save the PostScript image in a file with
7125 that name.  If FILENAME is a number, prompt the user for the name of the file
7126 to save in."
7127   (interactive (list (ps-print-preprint current-prefix-arg)
7128                      current-prefix-arg))
7129   (dolist (article (gnus-summary-work-articles n))
7130     (gnus-summary-select-article nil nil 'pseudo article)
7131     (gnus-eval-in-buffer-window gnus-article-buffer
7132       (let ((buffer (generate-new-buffer " *print*")))
7133         (unwind-protect
7134             (progn
7135               (copy-to-buffer buffer (point-min) (point-max))
7136               (set-buffer buffer)
7137               (gnus-article-delete-invisible-text)
7138               (let ((ps-left-header
7139                      (list
7140                       (concat "("
7141                               (mail-header-subject gnus-current-headers) ")")
7142                       (concat "("
7143                               (mail-header-from gnus-current-headers) ")")))
7144                     (ps-right-header
7145                      (list
7146                       "/pagenumberstring load"
7147                       (concat "("
7148                               (mail-header-date gnus-current-headers) ")"))))
7149                 (gnus-run-hooks 'gnus-ps-print-hook)
7150                 (save-excursion
7151                   (ps-print-buffer-with-faces filename))))
7152           (kill-buffer buffer))))))
7153
7154 (defun gnus-summary-show-article (&optional arg)
7155   "Force re-fetching of the current article.
7156 If ARG (the prefix) is a number, show the article with the charset 
7157 defined in `gnus-summary-show-article-charset-alist', or the charset
7158 inputed.
7159 If ARG (the prefix) is non-nil and not a number, show the raw article 
7160 without any article massaging functions being run."
7161   (interactive "P")
7162   (cond 
7163    ((numberp arg)
7164     (let ((gnus-newsgroup-charset 
7165            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7166                (read-coding-system "Charset: ")))
7167           (gnus-newsgroup-ignored-charsets 'gnus-all))
7168       (gnus-summary-select-article nil 'force)))
7169    ((not arg)
7170     ;; Select the article the normal way.
7171     (gnus-summary-select-article nil 'force))
7172    (t
7173     ;; We have to require this here to make sure that the following
7174     ;; dynamic binding isn't shadowed by autoloading.
7175     (require 'gnus-async)
7176     (require 'gnus-art)
7177     ;; Bind the article treatment functions to nil.
7178     (let ((gnus-have-all-headers t)
7179           gnus-article-prepare-hook
7180           gnus-article-decode-hook
7181           gnus-display-mime-function
7182           gnus-break-pages)
7183       ;; Destroy any MIME parts.
7184       (when (gnus-buffer-live-p gnus-article-buffer)
7185         (save-excursion
7186           (set-buffer gnus-article-buffer)
7187           (mm-destroy-parts gnus-article-mime-handles)))
7188       (gnus-summary-select-article nil 'force))))
7189   (gnus-summary-goto-subject gnus-current-article)
7190   (gnus-summary-position-point))
7191
7192 (defun gnus-summary-verbose-headers (&optional arg)
7193   "Toggle permanent full header display.
7194 If ARG is a positive number, turn header display on.
7195 If ARG is a negative number, turn header display off."
7196   (interactive "P")
7197   (setq gnus-show-all-headers
7198         (cond ((or (not (numberp arg))
7199                    (zerop arg))
7200                (not gnus-show-all-headers))
7201               ((natnump arg)
7202                t)))
7203   (gnus-summary-show-article))
7204
7205 (defun gnus-summary-toggle-header (&optional arg)
7206   "Show the headers if they are hidden, or hide them if they are shown.
7207 If ARG is a positive number, show the entire header.
7208 If ARG is a negative number, hide the unwanted header lines."
7209   (interactive "P")
7210   (save-excursion
7211     (set-buffer gnus-article-buffer)
7212     (save-restriction
7213       (let* ((buffer-read-only nil)
7214              (inhibit-point-motion-hooks t)
7215              hidden e)
7216         (setq hidden
7217               (if (numberp arg)
7218                   (>= arg 0)
7219                 (save-restriction 
7220                   (article-narrow-to-head)
7221                   (gnus-article-hidden-text-p 'headers))))
7222         (goto-char (point-min))
7223         (when (search-forward "\n\n" nil t)
7224           (delete-region (point-min) (1- (point))))
7225         (goto-char (point-min))
7226         (save-excursion
7227           (set-buffer gnus-original-article-buffer)
7228           (goto-char (point-min))
7229           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7230         (insert-buffer-substring gnus-original-article-buffer 1 e)
7231         (save-restriction
7232           (narrow-to-region (point-min) (point))
7233           (article-decode-encoded-words)
7234           (if  hidden
7235               (let ((gnus-treat-hide-headers nil)
7236                     (gnus-treat-hide-boring-headers nil))
7237                 (gnus-treat-article 'head))
7238             (gnus-treat-article 'head)))))))
7239
7240 (defun gnus-summary-show-all-headers ()
7241   "Make all header lines visible."
7242   (interactive)
7243   (gnus-article-show-all-headers))
7244
7245 (defun gnus-summary-caesar-message (&optional arg)
7246   "Caesar rotate the current article by 13.
7247 The numerical prefix specifies how many places to rotate each letter
7248 forward."
7249   (interactive "P")
7250   (gnus-summary-select-article)
7251   (let ((mail-header-separator ""))
7252     (gnus-eval-in-buffer-window gnus-article-buffer
7253       (save-restriction
7254         (widen)
7255         (let ((start (window-start))
7256               buffer-read-only)
7257           (message-caesar-buffer-body arg)
7258           (set-window-start (get-buffer-window (current-buffer)) start))))))
7259
7260 (defun gnus-summary-stop-page-breaking ()
7261   "Stop page breaking in the current article."
7262   (interactive)
7263   (gnus-summary-select-article)
7264   (gnus-eval-in-buffer-window gnus-article-buffer
7265     (widen)
7266     (when (gnus-visual-p 'page-marker)
7267       (let ((buffer-read-only nil))
7268         (gnus-remove-text-with-property 'gnus-prev)
7269         (gnus-remove-text-with-property 'gnus-next))
7270       (setq gnus-page-broken nil))))
7271
7272 (defun gnus-summary-move-article (&optional n to-newsgroup
7273                                             select-method action)
7274   "Move the current article to a different newsgroup.
7275 If N is a positive number, move the N next articles.
7276 If N is a negative number, move the N previous articles.
7277 If N is nil and any articles have been marked with the process mark,
7278 move those articles instead.
7279 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7280 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7281 re-spool using this method.
7282
7283 For this function to work, both the current newsgroup and the
7284 newsgroup that you want to move to have to support the `request-move'
7285 and `request-accept' functions.
7286
7287 ACTION can be either `move' (the default), `crosspost' or `copy'."
7288   (interactive "P")
7289   (unless action
7290     (setq action 'move))
7291   ;; Disable marking as read.
7292   (let (gnus-mark-article-hook)
7293     (save-window-excursion
7294       (gnus-summary-select-article)))
7295   ;; Check whether the source group supports the required functions.
7296   (cond ((and (eq action 'move)
7297               (not (gnus-check-backend-function
7298                     'request-move-article gnus-newsgroup-name)))
7299          (error "The current group does not support article moving"))
7300         ((and (eq action 'crosspost)
7301               (not (gnus-check-backend-function
7302                     'request-replace-article gnus-newsgroup-name)))
7303          (error "The current group does not support article editing")))
7304   (let ((articles (gnus-summary-work-articles n))
7305         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7306         (names '((move "Move" "Moving")
7307                  (copy "Copy" "Copying")
7308                  (crosspost "Crosspost" "Crossposting")))
7309         (copy-buf (save-excursion
7310                     (nnheader-set-temp-buffer " *copy article*")))
7311         art-group to-method new-xref article to-groups)
7312     (unless (assq action names)
7313       (error "Unknown action %s" action))
7314     ;; Read the newsgroup name.
7315     (when (and (not to-newsgroup)
7316                (not select-method))
7317       (setq to-newsgroup
7318             (gnus-read-move-group-name
7319              (cadr (assq action names))
7320              (symbol-value (intern (format "gnus-current-%s-group" action)))
7321              articles prefix))
7322       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7323     (setq to-method (or select-method
7324                         (gnus-group-name-to-method to-newsgroup)))
7325     ;; Check the method we are to move this article to...
7326     (unless (gnus-check-backend-function
7327              'request-accept-article (car to-method))
7328       (error "%s does not support article copying" (car to-method)))
7329     (unless (gnus-check-server to-method)
7330       (error "Can't open server %s" (car to-method)))
7331     (gnus-message 6 "%s to %s: %s..."
7332                   (caddr (assq action names))
7333                   (or (car select-method) to-newsgroup) articles)
7334     (while articles
7335       (setq article (pop articles))
7336       (setq
7337        art-group
7338        (cond
7339         ;; Move the article.
7340         ((eq action 'move)
7341          ;; Remove this article from future suppression.
7342          (gnus-dup-unsuppress-article article)
7343          (gnus-request-move-article
7344           article                       ; Article to move
7345           gnus-newsgroup-name           ; From newsgroup
7346           (nth 1 (gnus-find-method-for-group
7347                   gnus-newsgroup-name)) ; Server
7348           (list 'gnus-request-accept-article
7349                 to-newsgroup (list 'quote select-method)
7350                 (not articles) t)       ; Accept form
7351           (not articles)))              ; Only save nov last time
7352         ;; Copy the article.
7353         ((eq action 'copy)
7354          (save-excursion
7355            (set-buffer copy-buf)
7356            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7357              (gnus-request-accept-article
7358               to-newsgroup select-method (not articles) t))))
7359         ;; Crosspost the article.
7360         ((eq action 'crosspost)
7361          (let ((xref (message-tokenize-header
7362                       (mail-header-xref (gnus-summary-article-header article))
7363                       " ")))
7364            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7365                                   ":" article))
7366            (unless xref
7367              (setq xref (list (system-name))))
7368            (setq new-xref
7369                  (concat
7370                   (mapconcat 'identity
7371                              (delete "Xref:" (delete new-xref xref))
7372                              " ")
7373                   " " new-xref))
7374            (save-excursion
7375              (set-buffer copy-buf)
7376              ;; First put the article in the destination group.
7377              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7378              (when (consp (setq art-group
7379                                 (gnus-request-accept-article
7380                                  to-newsgroup select-method (not articles))))
7381                (setq new-xref (concat new-xref " " (car art-group)
7382                                       ":" (cdr art-group)))
7383                ;; Now we have the new Xrefs header, so we insert
7384                ;; it and replace the new article.
7385                (nnheader-replace-header "Xref" new-xref)
7386                (gnus-request-replace-article
7387                 (cdr art-group) to-newsgroup (current-buffer))
7388                art-group))))))
7389       (cond
7390        ((not art-group)
7391         (gnus-message 1 "Couldn't %s article %s: %s"
7392                       (cadr (assq action names)) article
7393                       (nnheader-get-report (car to-method))))
7394        ((and (eq art-group 'junk)
7395              (eq action 'move))
7396         (gnus-summary-mark-article article gnus-canceled-mark)
7397         (gnus-message 4 "Deleted article %s" article))
7398        (t
7399         (let* ((pto-group (gnus-group-prefixed-name
7400                            (car art-group) to-method))
7401                (entry
7402                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7403                (info (nth 2 entry))
7404                (to-group (gnus-info-group info))
7405                to-marks)
7406           ;; Update the group that has been moved to.
7407           (when (and info
7408                      (memq action '(move copy)))
7409             (unless (member to-group to-groups)
7410               (push to-group to-groups))
7411
7412             (unless (memq article gnus-newsgroup-unreads)
7413               (push 'read to-marks)
7414               (gnus-info-set-read
7415                info (gnus-add-to-range (gnus-info-read info)
7416                                        (list (cdr art-group)))))
7417
7418             ;; See whether the article is to be put in the cache.
7419             (let ((marks gnus-article-mark-lists)
7420                   (to-article (cdr art-group)))
7421
7422               ;; Enter the article into the cache in the new group,
7423               ;; if that is required.
7424               (when gnus-use-cache
7425                 (gnus-cache-possibly-enter-article
7426                  to-group to-article
7427                  (memq article gnus-newsgroup-marked)
7428                  (memq article gnus-newsgroup-dormant)
7429                  (memq article gnus-newsgroup-unreads)))
7430
7431               (when gnus-preserve-marks 
7432                 ;; Copy any marks over to the new group.
7433                 (when (and (equal to-group gnus-newsgroup-name)
7434                            (not (memq article gnus-newsgroup-unreads)))
7435                   ;; Mark this article as read in this group.
7436                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7437                   (setcdr (gnus-active to-group) to-article)
7438                   (setcdr gnus-newsgroup-active to-article))
7439
7440                 (while marks
7441                   (when (memq article (symbol-value
7442                                        (intern (format "gnus-newsgroup-%s"
7443                                                        (caar marks)))))
7444                     (push (cdar marks) to-marks)
7445                     ;; If the other group is the same as this group,
7446                     ;; then we have to add the mark to the list.
7447                     (when (equal to-group gnus-newsgroup-name)
7448                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7449                            (cons to-article
7450                                  (symbol-value
7451                                   (intern (format "gnus-newsgroup-%s"
7452                                                   (caar marks)))))))
7453                     ;; Copy the marks to other group.
7454                     (gnus-add-marked-articles
7455                      to-group (cdar marks) (list to-article) info))
7456                   (setq marks (cdr marks)))
7457
7458                 (gnus-request-set-mark to-group (list (list (list to-article)
7459                                                             'set
7460                                                             to-marks))))
7461
7462               (gnus-dribble-enter
7463                (concat "(gnus-group-set-info '"
7464                        (gnus-prin1-to-string (gnus-get-info to-group))
7465                        ")"))))
7466
7467           ;; Update the Xref header in this article to point to
7468           ;; the new crossposted article we have just created.
7469           (when (eq action 'crosspost)
7470             (save-excursion
7471               (set-buffer copy-buf)
7472               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7473               (nnheader-replace-header "Xref" new-xref)
7474               (gnus-request-replace-article
7475                article gnus-newsgroup-name (current-buffer)))))
7476
7477         ;;;!!!Why is this necessary?
7478         (set-buffer gnus-summary-buffer)
7479
7480         (gnus-summary-goto-subject article)
7481         (when (eq action 'move)
7482           (gnus-summary-mark-article article gnus-canceled-mark))))
7483       (gnus-summary-remove-process-mark article))
7484     ;; Re-activate all groups that have been moved to.
7485     (while to-groups
7486       (save-excursion
7487         (set-buffer gnus-group-buffer)
7488         (when (gnus-group-goto-group (car to-groups) t)
7489           (gnus-group-get-new-news-this-group 1 t))
7490         (pop to-groups)))
7491
7492     (gnus-kill-buffer copy-buf)
7493     (gnus-summary-position-point)
7494     (gnus-set-mode-line 'summary)))
7495
7496 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7497   "Move the current article to a different newsgroup.
7498 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7499 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7500 re-spool using this method."
7501   (interactive "P")
7502   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7503
7504 (defun gnus-summary-crosspost-article (&optional n)
7505   "Crosspost the current article to some other group."
7506   (interactive "P")
7507   (gnus-summary-move-article n nil nil 'crosspost))
7508
7509 (defcustom gnus-summary-respool-default-method nil
7510   "Default method for respooling an article.
7511 If nil, use to the current newsgroup method."
7512   :type '(choice (gnus-select-method :value (nnml ""))
7513                  (const nil))
7514   :group 'gnus-summary-mail)
7515
7516 (defun gnus-summary-respool-article (&optional n method)
7517   "Respool the current article.
7518 The article will be squeezed through the mail spooling process again,
7519 which means that it will be put in some mail newsgroup or other
7520 depending on `nnmail-split-methods'.
7521 If N is a positive number, respool the N next articles.
7522 If N is a negative number, respool the N previous articles.
7523 If N is nil and any articles have been marked with the process mark,
7524 respool those articles instead.
7525
7526 Respooling can be done both from mail groups and \"real\" newsgroups.
7527 In the former case, the articles in question will be moved from the
7528 current group into whatever groups they are destined to.  In the
7529 latter case, they will be copied into the relevant groups."
7530   (interactive
7531    (list current-prefix-arg
7532          (let* ((methods (gnus-methods-using 'respool))
7533                 (methname
7534                  (symbol-name (or gnus-summary-respool-default-method
7535                                   (car (gnus-find-method-for-group
7536                                         gnus-newsgroup-name)))))
7537                 (method
7538                  (gnus-completing-read
7539                   methname "What backend do you want to use when respooling?"
7540                   methods nil t nil 'gnus-mail-method-history))
7541                 ms)
7542            (cond
7543             ((zerop (length (setq ms (gnus-servers-using-backend
7544                                       (intern method)))))
7545              (list (intern method) ""))
7546             ((= 1 (length ms))
7547              (car ms))
7548             (t
7549              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7550                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7551                            ms-alist))))))))
7552   (unless method
7553     (error "No method given for respooling"))
7554   (if (assoc (symbol-name
7555               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7556              (gnus-methods-using 'respool))
7557       (gnus-summary-move-article n nil method)
7558     (gnus-summary-copy-article n nil method)))
7559
7560 (defun gnus-summary-import-article (file)
7561   "Import an arbitrary file into a mail newsgroup."
7562   (interactive "fImport file: ")
7563   (let ((group gnus-newsgroup-name)
7564         (now (current-time))
7565         atts lines)
7566     (unless (gnus-check-backend-function 'request-accept-article group)
7567       (error "%s does not support article importing" group))
7568     (or (file-readable-p file)
7569         (not (file-regular-p file))
7570         (error "Can't read %s" file))
7571     (save-excursion
7572       (set-buffer (gnus-get-buffer-create " *import file*"))
7573       (erase-buffer)
7574       (insert-file-contents file)
7575       (goto-char (point-min))
7576       (unless (nnheader-article-p)
7577         ;; This doesn't look like an article, so we fudge some headers.
7578         (setq atts (file-attributes file)
7579               lines (count-lines (point-min) (point-max)))
7580         (insert "From: " (read-string "From: ") "\n"
7581                 "Subject: " (read-string "Subject: ") "\n"
7582                 "Date: " (message-make-date (nth 5 atts))
7583                 "\n"
7584                 "Message-ID: " (message-make-message-id) "\n"
7585                 "Lines: " (int-to-string lines) "\n"
7586                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7587       (gnus-request-accept-article group nil t)
7588       (kill-buffer (current-buffer)))))
7589
7590 (defun gnus-summary-article-posted-p ()
7591   "Say whether the current (mail) article is available from news as well.
7592 This will be the case if the article has both been mailed and posted."
7593   (interactive)
7594   (let ((id (mail-header-references (gnus-summary-article-header)))
7595         (gnus-override-method (car (gnus-refer-article-methods))))
7596     (if (gnus-request-head id "")
7597         (gnus-message 2 "The current message was found on %s"
7598                       gnus-override-method)
7599       (gnus-message 2 "The current message couldn't be found on %s"
7600                     gnus-override-method)
7601       nil)))
7602
7603 (defun gnus-summary-expire-articles (&optional now)
7604   "Expire all articles that are marked as expirable in the current group."
7605   (interactive)
7606   (when (gnus-check-backend-function
7607          'request-expire-articles gnus-newsgroup-name)
7608     ;; This backend supports expiry.
7609     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7610            (expirable (if total
7611                           (progn
7612                             ;; We need to update the info for
7613                             ;; this group for `gnus-list-of-read-articles'
7614                             ;; to give us the right answer.
7615                             (gnus-run-hooks 'gnus-exit-group-hook)
7616                             (gnus-summary-update-info)
7617                             (gnus-list-of-read-articles gnus-newsgroup-name))
7618                         (setq gnus-newsgroup-expirable
7619                               (sort gnus-newsgroup-expirable '<))))
7620            (expiry-wait (if now 'immediate
7621                           (gnus-group-find-parameter
7622                            gnus-newsgroup-name 'expiry-wait)))
7623            es)
7624       (when expirable
7625         ;; There are expirable articles in this group, so we run them
7626         ;; through the expiry process.
7627         (gnus-message 6 "Expiring articles...")
7628         (unless (gnus-check-group gnus-newsgroup-name)
7629           (error "Can't open server for %s" gnus-newsgroup-name))
7630         ;; The list of articles that weren't expired is returned.
7631         (save-excursion
7632           (if expiry-wait
7633               (let ((nnmail-expiry-wait-function nil)
7634                     (nnmail-expiry-wait expiry-wait))
7635                 (setq es (gnus-request-expire-articles
7636                           expirable gnus-newsgroup-name)))
7637             (setq es (gnus-request-expire-articles
7638                       expirable gnus-newsgroup-name))))
7639         (unless total
7640           (setq gnus-newsgroup-expirable es))
7641         ;; We go through the old list of expirable, and mark all
7642         ;; really expired articles as nonexistent.
7643         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7644           (let ((gnus-use-cache nil))
7645             (while expirable
7646               (unless (memq (car expirable) es)
7647                 (when (gnus-data-find (car expirable))
7648                   (gnus-summary-mark-article
7649                    (car expirable) gnus-canceled-mark)))
7650               (setq expirable (cdr expirable)))))
7651         (gnus-message 6 "Expiring articles...done")))))
7652
7653 (defun gnus-summary-expire-articles-now ()
7654   "Expunge all expirable articles in the current group.
7655 This means that *all* articles that are marked as expirable will be
7656 deleted forever, right now."
7657   (interactive)
7658   (or gnus-expert-user
7659       (gnus-yes-or-no-p
7660        "Are you really, really, really sure you want to delete all these messages? ")
7661       (error "Phew!"))
7662   (gnus-summary-expire-articles t))
7663
7664 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7665 (defun gnus-summary-delete-article (&optional n)
7666   "Delete the N next (mail) articles.
7667 This command actually deletes articles.  This is not a marking
7668 command.  The article will disappear forever from your life, never to
7669 return.
7670 If N is negative, delete backwards.
7671 If N is nil and articles have been marked with the process mark,
7672 delete these instead."
7673   (interactive "P")
7674   (unless (gnus-check-backend-function 'request-expire-articles
7675                                        gnus-newsgroup-name)
7676     (error "The current newsgroup does not support article deletion"))
7677   ;; Compute the list of articles to delete.
7678   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7679         not-deleted)
7680     (if (and gnus-novice-user
7681              (not (gnus-yes-or-no-p
7682                    (format "Do you really want to delete %s forever? "
7683                            (if (> (length articles) 1)
7684                                (format "these %s articles" (length articles))
7685                              "this article")))))
7686         ()
7687       ;; Delete the articles.
7688       (setq not-deleted (gnus-request-expire-articles
7689                          articles gnus-newsgroup-name 'force))
7690       (while articles
7691         (gnus-summary-remove-process-mark (car articles))
7692         ;; The backend might not have been able to delete the article
7693         ;; after all.
7694         (unless (memq (car articles) not-deleted)
7695           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7696         (setq articles (cdr articles)))
7697       (when not-deleted
7698         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7699     (gnus-summary-position-point)
7700     (gnus-set-mode-line 'summary)
7701     not-deleted))
7702
7703 (defun gnus-summary-edit-article (&optional force)
7704   "Edit the current article.
7705 This will have permanent effect only in mail groups.
7706 If FORCE is non-nil, allow editing of articles even in read-only
7707 groups."
7708   (interactive "P")
7709   (save-excursion
7710     (set-buffer gnus-summary-buffer)
7711     (let ((mail-parse-charset gnus-newsgroup-charset)
7712           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7713       (gnus-set-global-variables)
7714       (when (and (not force)
7715                  (gnus-group-read-only-p))
7716         (error "The current newsgroup does not support article editing"))
7717       (gnus-summary-show-article t)
7718       (gnus-article-edit-article
7719        'mime-to-mml
7720        `(lambda (no-highlight)
7721           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7722                 (mail-parse-ignored-charsets 
7723                  ',gnus-newsgroup-ignored-charsets))
7724             (mml-to-mime)
7725             (gnus-summary-edit-article-done
7726              ,(or (mail-header-references gnus-current-headers) "")
7727              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7728
7729 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7730
7731 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7732                                                  no-highlight)
7733   "Make edits to the current article permanent."
7734   (interactive)
7735   ;; Replace the article.
7736   (let ((buf (current-buffer)))
7737     (with-temp-buffer
7738       (insert-buffer-substring buf)
7739       (if (and (not read-only)
7740                (not (gnus-request-replace-article
7741                      (cdr gnus-article-current) (car gnus-article-current)
7742                      (current-buffer) t)))
7743           (error "Couldn't replace article")
7744         ;; Update the summary buffer.
7745         (if (and references
7746                  (equal (message-tokenize-header references " ")
7747                         (message-tokenize-header
7748                          (or (message-fetch-field "references") "") " ")))
7749             ;; We only have to update this line.
7750             (save-excursion
7751               (save-restriction
7752                 (message-narrow-to-head)
7753                 (let ((head (buffer-string))
7754                       header)
7755                   (with-temp-buffer
7756                     (insert (format "211 %d Article retrieved.\n"
7757                                     (cdr gnus-article-current)))
7758                     (insert head)
7759                     (insert ".\n")
7760                     (let ((nntp-server-buffer (current-buffer)))
7761                       (setq header (car (gnus-get-newsgroup-headers
7762                                          (save-excursion
7763                                            (set-buffer gnus-summary-buffer)
7764                                            gnus-newsgroup-dependencies)
7765                                          t))))
7766                     (save-excursion
7767                       (set-buffer gnus-summary-buffer)
7768                       (gnus-data-set-header
7769                        (gnus-data-find (cdr gnus-article-current))
7770                        header)
7771                       (gnus-summary-update-article-line
7772                        (cdr gnus-article-current) header))))))
7773           ;; Update threads.
7774           (set-buffer (or buffer gnus-summary-buffer))
7775           (gnus-summary-update-article (cdr gnus-article-current)))
7776         ;; Prettify the article buffer again.
7777         (unless no-highlight
7778           (save-excursion
7779             (set-buffer gnus-article-buffer)
7780             ;;;!!! Fix this -- article should be rehighlighted.
7781             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7782             (set-buffer gnus-original-article-buffer)
7783             (gnus-request-article
7784              (cdr gnus-article-current)
7785              (car gnus-article-current) (current-buffer))))
7786         ;; Prettify the summary buffer line.
7787         (when (gnus-visual-p 'summary-highlight 'highlight)
7788           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7789
7790 (defun gnus-summary-edit-wash (key)
7791   "Perform editing command KEY in the article buffer."
7792   (interactive
7793    (list
7794     (progn
7795       (message "%s" (concat (this-command-keys) "- "))
7796       (read-char))))
7797   (message "")
7798   (gnus-summary-edit-article)
7799   (execute-kbd-macro (concat (this-command-keys) key))
7800   (gnus-article-edit-done))
7801
7802 ;;; Respooling
7803
7804 (defun gnus-summary-respool-query (&optional silent trace)
7805   "Query where the respool algorithm would put this article."
7806   (interactive)
7807   (let (gnus-mark-article-hook)
7808     (gnus-summary-select-article)
7809     (save-excursion
7810       (set-buffer gnus-original-article-buffer)
7811       (save-restriction
7812         (message-narrow-to-head)
7813         (let ((groups (nnmail-article-group 'identity trace)))
7814           (unless silent
7815             (if groups
7816                 (message "This message would go to %s"
7817                          (mapconcat 'car groups ", "))
7818               (message "This message would go to no groups"))
7819             groups))))))
7820
7821 (defun gnus-summary-respool-trace ()
7822   "Trace where the respool algorithm would put this article.
7823 Display a buffer showing all fancy splitting patterns which matched."
7824   (interactive)
7825   (gnus-summary-respool-query nil t))
7826
7827 ;; Summary marking commands.
7828
7829 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7830   "Mark articles which has the same subject as read, and then select the next.
7831 If UNMARK is positive, remove any kind of mark.
7832 If UNMARK is negative, tick articles."
7833   (interactive "P")
7834   (when unmark
7835     (setq unmark (prefix-numeric-value unmark)))
7836   (let ((count
7837          (gnus-summary-mark-same-subject
7838           (gnus-summary-article-subject) unmark)))
7839     ;; Select next unread article.  If auto-select-same mode, should
7840     ;; select the first unread article.
7841     (gnus-summary-next-article t (and gnus-auto-select-same
7842                                       (gnus-summary-article-subject)))
7843     (gnus-message 7 "%d article%s marked as %s"
7844                   count (if (= count 1) " is" "s are")
7845                   (if unmark "unread" "read"))))
7846
7847 (defun gnus-summary-kill-same-subject (&optional unmark)
7848   "Mark articles which has the same subject as read.
7849 If UNMARK is positive, remove any kind of mark.
7850 If UNMARK is negative, tick articles."
7851   (interactive "P")
7852   (when unmark
7853     (setq unmark (prefix-numeric-value unmark)))
7854   (let ((count
7855          (gnus-summary-mark-same-subject
7856           (gnus-summary-article-subject) unmark)))
7857     ;; If marked as read, go to next unread subject.
7858     (when (null unmark)
7859       ;; Go to next unread subject.
7860       (gnus-summary-next-subject 1 t))
7861     (gnus-message 7 "%d articles are marked as %s"
7862                   count (if unmark "unread" "read"))))
7863
7864 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7865   "Mark articles with same SUBJECT as read, and return marked number.
7866 If optional argument UNMARK is positive, remove any kinds of marks.
7867 If optional argument UNMARK is negative, mark articles as unread instead."
7868   (let ((count 1))
7869     (save-excursion
7870       (cond
7871        ((null unmark)                   ; Mark as read.
7872         (while (and
7873                 (progn
7874                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7875                   (gnus-summary-show-thread) t)
7876                 (gnus-summary-find-subject subject))
7877           (setq count (1+ count))))
7878        ((> unmark 0)                    ; Tick.
7879         (while (and
7880                 (progn
7881                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7882                   (gnus-summary-show-thread) t)
7883                 (gnus-summary-find-subject subject))
7884           (setq count (1+ count))))
7885        (t                               ; Mark as unread.
7886         (while (and
7887                 (progn
7888                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7889                   (gnus-summary-show-thread) t)
7890                 (gnus-summary-find-subject subject))
7891           (setq count (1+ count)))))
7892       (gnus-set-mode-line 'summary)
7893       ;; Return the number of marked articles.
7894       count)))
7895
7896 (defun gnus-summary-mark-as-processable (n &optional unmark)
7897   "Set the process mark on the next N articles.
7898 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7899 the process mark instead.  The difference between N and the actual
7900 number of articles marked is returned."
7901   (interactive "p")
7902   (let ((backward (< n 0))
7903         (n (abs n)))
7904     (while (and
7905             (> n 0)
7906             (if unmark
7907                 (gnus-summary-remove-process-mark
7908                  (gnus-summary-article-number))
7909               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7910             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7911       (setq n (1- n)))
7912     (when (/= 0 n)
7913       (gnus-message 7 "No more articles"))
7914     (gnus-summary-recenter)
7915     (gnus-summary-position-point)
7916     n))
7917
7918 (defun gnus-summary-unmark-as-processable (n)
7919   "Remove the process mark from the next N articles.
7920 If N is negative, unmark backward instead.  The difference between N and
7921 the actual number of articles unmarked is returned."
7922   (interactive "p")
7923   (gnus-summary-mark-as-processable n t))
7924
7925 (defun gnus-summary-unmark-all-processable ()
7926   "Remove the process mark from all articles."
7927   (interactive)
7928   (save-excursion
7929     (while gnus-newsgroup-processable
7930       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7931   (gnus-summary-position-point))
7932
7933 (defun gnus-summary-mark-as-expirable (n)
7934   "Mark N articles forward as expirable.
7935 If N is negative, mark backward instead.  The difference between N and
7936 the actual number of articles marked is returned."
7937   (interactive "p")
7938   (gnus-summary-mark-forward n gnus-expirable-mark))
7939
7940 (defun gnus-summary-mark-article-as-replied (article)
7941   "Mark ARTICLE replied and update the summary line."
7942   (push article gnus-newsgroup-replied)
7943   (let ((buffer-read-only nil))
7944     (when (gnus-summary-goto-subject article nil t)
7945       (gnus-summary-update-secondary-mark article))))
7946
7947 (defun gnus-summary-set-bookmark (article)
7948   "Set a bookmark in current article."
7949   (interactive (list (gnus-summary-article-number)))
7950   (when (or (not (get-buffer gnus-article-buffer))
7951             (not gnus-current-article)
7952             (not gnus-article-current)
7953             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7954     (error "No current article selected"))
7955   ;; Remove old bookmark, if one exists.
7956   (let ((old (assq article gnus-newsgroup-bookmarks)))
7957     (when old
7958       (setq gnus-newsgroup-bookmarks
7959             (delq old gnus-newsgroup-bookmarks))))
7960   ;; Set the new bookmark, which is on the form
7961   ;; (article-number . line-number-in-body).
7962   (push
7963    (cons article
7964          (save-excursion
7965            (set-buffer gnus-article-buffer)
7966            (count-lines
7967             (min (point)
7968                  (save-excursion
7969                    (goto-char (point-min))
7970                    (search-forward "\n\n" nil t)
7971                    (point)))
7972             (point))))
7973    gnus-newsgroup-bookmarks)
7974   (gnus-message 6 "A bookmark has been added to the current article."))
7975
7976 (defun gnus-summary-remove-bookmark (article)
7977   "Remove the bookmark from the current article."
7978   (interactive (list (gnus-summary-article-number)))
7979   ;; Remove old bookmark, if one exists.
7980   (let ((old (assq article gnus-newsgroup-bookmarks)))
7981     (if old
7982         (progn
7983           (setq gnus-newsgroup-bookmarks
7984                 (delq old gnus-newsgroup-bookmarks))
7985           (gnus-message 6 "Removed bookmark."))
7986       (gnus-message 6 "No bookmark in current article."))))
7987
7988 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7989 (defun gnus-summary-mark-as-dormant (n)
7990   "Mark N articles forward as dormant.
7991 If N is negative, mark backward instead.  The difference between N and
7992 the actual number of articles marked is returned."
7993   (interactive "p")
7994   (gnus-summary-mark-forward n gnus-dormant-mark))
7995
7996 (defun gnus-summary-set-process-mark (article)
7997   "Set the process mark on ARTICLE and update the summary line."
7998   (setq gnus-newsgroup-processable
7999         (cons article
8000               (delq article gnus-newsgroup-processable)))
8001   (when (gnus-summary-goto-subject article)
8002     (gnus-summary-show-thread)
8003     (gnus-summary-goto-subject article)
8004     (gnus-summary-update-secondary-mark article)))
8005
8006 (defun gnus-summary-remove-process-mark (article)
8007   "Remove the process mark from ARTICLE and update the summary line."
8008   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8009   (when (gnus-summary-goto-subject article)
8010     (gnus-summary-show-thread)
8011     (gnus-summary-goto-subject article)
8012     (gnus-summary-update-secondary-mark article)))
8013
8014 (defun gnus-summary-set-saved-mark (article)
8015   "Set the process mark on ARTICLE and update the summary line."
8016   (push article gnus-newsgroup-saved)
8017   (when (gnus-summary-goto-subject article)
8018     (gnus-summary-update-secondary-mark article)))
8019
8020 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8021   "Mark N articles as read forwards.
8022 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8023 The difference between N and the actual number of articles marked is
8024 returned.
8025 Iff NO-EXPIRE, auto-expiry will be inhibited."
8026   (interactive "p")
8027   (gnus-summary-show-thread)
8028   (let ((backward (< n 0))
8029         (gnus-summary-goto-unread
8030          (and gnus-summary-goto-unread
8031               (not (eq gnus-summary-goto-unread 'never))
8032               (not (memq mark (list gnus-unread-mark
8033                                     gnus-ticked-mark gnus-dormant-mark)))))
8034         (n (abs n))
8035         (mark (or mark gnus-del-mark)))
8036     (while (and (> n 0)
8037                 (gnus-summary-mark-article nil mark no-expire)
8038                 (zerop (gnus-summary-next-subject
8039                         (if backward -1 1)
8040                         (and gnus-summary-goto-unread
8041                              (not (eq gnus-summary-goto-unread 'never)))
8042                         t)))
8043       (setq n (1- n)))
8044     (when (/= 0 n)
8045       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8046     (gnus-summary-recenter)
8047     (gnus-summary-position-point)
8048     (gnus-set-mode-line 'summary)
8049     n))
8050
8051 (defun gnus-summary-mark-article-as-read (mark)
8052   "Mark the current article quickly as read with MARK."
8053   (let ((article (gnus-summary-article-number)))
8054     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8055     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8056     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8057     (push (cons article mark) gnus-newsgroup-reads)
8058     ;; Possibly remove from cache, if that is used.
8059     (when gnus-use-cache
8060       (gnus-cache-enter-remove-article article))
8061     ;; Allow the backend to change the mark.
8062     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8063     ;; Check for auto-expiry.
8064     (when (and gnus-newsgroup-auto-expire
8065                (memq mark gnus-auto-expirable-marks))
8066       (setq mark gnus-expirable-mark)
8067       ;; Let the backend know about the mark change.
8068       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8069       (push article gnus-newsgroup-expirable))
8070     ;; Set the mark in the buffer.
8071     (gnus-summary-update-mark mark 'unread)
8072     t))
8073
8074 (defun gnus-summary-mark-article-as-unread (mark)
8075   "Mark the current article quickly as unread with MARK."
8076   (let* ((article (gnus-summary-article-number))
8077          (old-mark (gnus-summary-article-mark article)))
8078     ;; Allow the backend to change the mark.
8079     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8080     (if (eq mark old-mark)
8081         t
8082       (if (<= article 0)
8083           (progn
8084             (gnus-error 1 "Can't mark negative article numbers")
8085             nil)
8086         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8087         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8088         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8089         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8090         (cond ((= mark gnus-ticked-mark)
8091                (push article gnus-newsgroup-marked))
8092               ((= mark gnus-dormant-mark)
8093                (push article gnus-newsgroup-dormant))
8094               (t
8095                (push article gnus-newsgroup-unreads)))
8096         (gnus-pull article gnus-newsgroup-reads)
8097
8098         ;; See whether the article is to be put in the cache.
8099         (and gnus-use-cache
8100              (vectorp (gnus-summary-article-header article))
8101              (save-excursion
8102                (gnus-cache-possibly-enter-article
8103                 gnus-newsgroup-name article
8104                 (= mark gnus-ticked-mark)
8105                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8106
8107         ;; Fix the mark.
8108         (gnus-summary-update-mark mark 'unread)
8109         t))))
8110
8111 (defun gnus-summary-mark-article (&optional article mark no-expire)
8112   "Mark ARTICLE with MARK.  MARK can be any character.
8113 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8114 `??' (dormant) and `?E' (expirable).
8115 If MARK is nil, then the default character `?r' is used.
8116 If ARTICLE is nil, then the article on the current line will be
8117 marked.
8118 Iff NO-EXPIRE, auto-expiry will be inhibited."
8119   ;; The mark might be a string.
8120   (when (stringp mark)
8121     (setq mark (aref mark 0)))
8122   ;; If no mark is given, then we check auto-expiring.
8123   (when (null mark)
8124     (setq mark gnus-del-mark))
8125   (when (and (not no-expire)
8126              gnus-newsgroup-auto-expire
8127              (memq mark gnus-auto-expirable-marks))
8128     (setq mark gnus-expirable-mark))
8129   (let ((article (or article (gnus-summary-article-number)))
8130         (old-mark (gnus-summary-article-mark article)))
8131     ;; Allow the backend to change the mark.
8132     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8133     (if (eq mark old-mark)
8134         t
8135       (unless article
8136         (error "No article on current line"))
8137       (if (not (if (or (= mark gnus-unread-mark)
8138                        (= mark gnus-ticked-mark)
8139                        (= mark gnus-dormant-mark))
8140                    (gnus-mark-article-as-unread article mark)
8141                  (gnus-mark-article-as-read article mark)))
8142           t
8143         ;; See whether the article is to be put in the cache.
8144         (and gnus-use-cache
8145              (not (= mark gnus-canceled-mark))
8146              (vectorp (gnus-summary-article-header article))
8147              (save-excursion
8148                (gnus-cache-possibly-enter-article
8149                 gnus-newsgroup-name article
8150                 (= mark gnus-ticked-mark)
8151                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8152
8153         (when (gnus-summary-goto-subject article nil t)
8154           (let ((buffer-read-only nil))
8155             (gnus-summary-show-thread)
8156             ;; Fix the mark.
8157             (gnus-summary-update-mark mark 'unread)
8158             t))))))
8159
8160 (defun gnus-summary-update-secondary-mark (article)
8161   "Update the secondary (read, process, cache) mark."
8162   (gnus-summary-update-mark
8163    (cond ((memq article gnus-newsgroup-processable)
8164           gnus-process-mark)
8165          ((memq article gnus-newsgroup-cached)
8166           gnus-cached-mark)
8167          ((memq article gnus-newsgroup-replied)
8168           gnus-replied-mark)
8169          ((memq article gnus-newsgroup-saved)
8170           gnus-saved-mark)
8171          (t gnus-unread-mark))
8172    'replied)
8173   (when (gnus-visual-p 'summary-highlight 'highlight)
8174     (gnus-run-hooks 'gnus-summary-update-hook))
8175   t)
8176
8177 (defun gnus-summary-update-mark (mark type)
8178   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8179         (buffer-read-only nil))
8180     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8181     (when forward
8182       (when (looking-at "\r")
8183         (incf forward))
8184       (when (<= (+ forward (point)) (point-max))
8185         ;; Go to the right position on the line.
8186         (goto-char (+ forward (point)))
8187         ;; Replace the old mark with the new mark.
8188         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8189         ;; Optionally update the marks by some user rule.
8190         (when (eq type 'unread)
8191           (gnus-data-set-mark
8192            (gnus-data-find (gnus-summary-article-number)) mark)
8193           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8194
8195 (defun gnus-mark-article-as-read (article &optional mark)
8196   "Enter ARTICLE in the pertinent lists and remove it from others."
8197   ;; Make the article expirable.
8198   (let ((mark (or mark gnus-del-mark)))
8199     (if (= mark gnus-expirable-mark)
8200         (push article gnus-newsgroup-expirable)
8201       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8202     ;; Remove from unread and marked lists.
8203     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8204     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8205     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8206     (push (cons article mark) gnus-newsgroup-reads)
8207     ;; Possibly remove from cache, if that is used.
8208     (when gnus-use-cache
8209       (gnus-cache-enter-remove-article article))
8210     t))
8211
8212 (defun gnus-mark-article-as-unread (article &optional mark)
8213   "Enter ARTICLE in the pertinent lists and remove it from others."
8214   (let ((mark (or mark gnus-ticked-mark)))
8215     (if (<= article 0)
8216         (progn
8217           (gnus-error 1 "Can't mark negative article numbers")
8218           nil)
8219       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8220             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8221             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8222             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8223
8224       ;; Unsuppress duplicates?
8225       (when gnus-suppress-duplicates
8226         (gnus-dup-unsuppress-article article))
8227
8228       (cond ((= mark gnus-ticked-mark)
8229              (push article gnus-newsgroup-marked))
8230             ((= mark gnus-dormant-mark)
8231              (push article gnus-newsgroup-dormant))
8232             (t
8233              (push article gnus-newsgroup-unreads)))
8234       (gnus-pull article gnus-newsgroup-reads)
8235       t)))
8236
8237 (defalias 'gnus-summary-mark-as-unread-forward
8238   'gnus-summary-tick-article-forward)
8239 (make-obsolete 'gnus-summary-mark-as-unread-forward
8240                'gnus-summary-tick-article-forward)
8241 (defun gnus-summary-tick-article-forward (n)
8242   "Tick N articles forwards.
8243 If N is negative, tick backwards instead.
8244 The difference between N and the number of articles ticked is returned."
8245   (interactive "p")
8246   (gnus-summary-mark-forward n gnus-ticked-mark))
8247
8248 (defalias 'gnus-summary-mark-as-unread-backward
8249   'gnus-summary-tick-article-backward)
8250 (make-obsolete 'gnus-summary-mark-as-unread-backward
8251                'gnus-summary-tick-article-backward)
8252 (defun gnus-summary-tick-article-backward (n)
8253   "Tick N articles backwards.
8254 The difference between N and the number of articles ticked is returned."
8255   (interactive "p")
8256   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8257
8258 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8259 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8260 (defun gnus-summary-tick-article (&optional article clear-mark)
8261   "Mark current article as unread.
8262 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8263 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8264   (interactive)
8265   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8266                                        gnus-ticked-mark)))
8267
8268 (defun gnus-summary-mark-as-read-forward (n)
8269   "Mark N articles as read forwards.
8270 If N is negative, mark backwards instead.
8271 The difference between N and the actual number of articles marked is
8272 returned."
8273   (interactive "p")
8274   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8275
8276 (defun gnus-summary-mark-as-read-backward (n)
8277   "Mark the N articles as read backwards.
8278 The difference between N and the actual number of articles marked is
8279 returned."
8280   (interactive "p")
8281   (gnus-summary-mark-forward
8282    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8283
8284 (defun gnus-summary-mark-as-read (&optional article mark)
8285   "Mark current article as read.
8286 ARTICLE specifies the article to be marked as read.
8287 MARK specifies a string to be inserted at the beginning of the line."
8288   (gnus-summary-mark-article article mark))
8289
8290 (defun gnus-summary-clear-mark-forward (n)
8291   "Clear marks from N articles forward.
8292 If N is negative, clear backward instead.
8293 The difference between N and the number of marks cleared is returned."
8294   (interactive "p")
8295   (gnus-summary-mark-forward n gnus-unread-mark))
8296
8297 (defun gnus-summary-clear-mark-backward (n)
8298   "Clear marks from N articles backward.
8299 The difference between N and the number of marks cleared is returned."
8300   (interactive "p")
8301   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8302
8303 (defun gnus-summary-mark-unread-as-read ()
8304   "Intended to be used by `gnus-summary-mark-article-hook'."
8305   (when (memq gnus-current-article gnus-newsgroup-unreads)
8306     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8307
8308 (defun gnus-summary-mark-read-and-unread-as-read ()
8309   "Intended to be used by `gnus-summary-mark-article-hook'."
8310   (let ((mark (gnus-summary-article-mark)))
8311     (when (or (gnus-unread-mark-p mark)
8312               (gnus-read-mark-p mark))
8313       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8314
8315 (defun gnus-summary-mark-region-as-read (point mark all)
8316   "Mark all unread articles between point and mark as read.
8317 If given a prefix, mark all articles between point and mark as read,
8318 even ticked and dormant ones."
8319   (interactive "r\nP")
8320   (save-excursion
8321     (let (article)
8322       (goto-char point)
8323       (beginning-of-line)
8324       (while (and
8325               (< (point) mark)
8326               (progn
8327                 (when (or all
8328                           (memq (setq article (gnus-summary-article-number))
8329                                 gnus-newsgroup-unreads))
8330                   (gnus-summary-mark-article article gnus-del-mark))
8331                 t)
8332               (gnus-summary-find-next))))))
8333
8334 (defun gnus-summary-mark-below (score mark)
8335   "Mark articles with score less than SCORE with MARK."
8336   (interactive "P\ncMark: ")
8337   (setq score (if score
8338                   (prefix-numeric-value score)
8339                 (or gnus-summary-default-score 0)))
8340   (save-excursion
8341     (set-buffer gnus-summary-buffer)
8342     (goto-char (point-min))
8343     (while
8344         (progn
8345           (and (< (gnus-summary-article-score) score)
8346                (gnus-summary-mark-article nil mark))
8347           (gnus-summary-find-next)))))
8348
8349 (defun gnus-summary-kill-below (&optional score)
8350   "Mark articles with score below SCORE as read."
8351   (interactive "P")
8352   (gnus-summary-mark-below score gnus-killed-mark))
8353
8354 (defun gnus-summary-clear-above (&optional score)
8355   "Clear all marks from articles with score above SCORE."
8356   (interactive "P")
8357   (gnus-summary-mark-above score gnus-unread-mark))
8358
8359 (defun gnus-summary-tick-above (&optional score)
8360   "Tick all articles with score above SCORE."
8361   (interactive "P")
8362   (gnus-summary-mark-above score gnus-ticked-mark))
8363
8364 (defun gnus-summary-mark-above (score mark)
8365   "Mark articles with score over SCORE with MARK."
8366   (interactive "P\ncMark: ")
8367   (setq score (if score
8368                   (prefix-numeric-value score)
8369                 (or gnus-summary-default-score 0)))
8370   (save-excursion
8371     (set-buffer gnus-summary-buffer)
8372     (goto-char (point-min))
8373     (while (and (progn
8374                   (when (> (gnus-summary-article-score) score)
8375                     (gnus-summary-mark-article nil mark))
8376                   t)
8377                 (gnus-summary-find-next)))))
8378
8379 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8380 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8381 (defun gnus-summary-limit-include-expunged (&optional no-error)
8382   "Display all the hidden articles that were expunged for low scores."
8383   (interactive)
8384   (let ((buffer-read-only nil))
8385     (let ((scored gnus-newsgroup-scored)
8386           headers h)
8387       (while scored
8388         (unless (gnus-summary-goto-subject (caar scored))
8389           (and (setq h (gnus-summary-article-header (caar scored)))
8390                (< (cdar scored) gnus-summary-expunge-below)
8391                (push h headers)))
8392         (setq scored (cdr scored)))
8393       (if (not headers)
8394           (when (not no-error)
8395             (error "No expunged articles hidden"))
8396         (goto-char (point-min))
8397         (gnus-summary-prepare-unthreaded (nreverse headers))
8398         (goto-char (point-min))
8399         (gnus-summary-position-point)
8400         t))))
8401
8402 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8403   "Mark all unread articles in this newsgroup as read.
8404 If prefix argument ALL is non-nil, ticked and dormant articles will
8405 also be marked as read.
8406 If QUIETLY is non-nil, no questions will be asked.
8407 If TO-HERE is non-nil, it should be a point in the buffer.  All
8408 articles before this point will be marked as read.
8409 Note that this function will only catch up the unread article
8410 in the current summary buffer limitation.
8411 The number of articles marked as read is returned."
8412   (interactive "P")
8413   (prog1
8414       (save-excursion
8415         (when (or quietly
8416                   (not gnus-interactive-catchup) ;Without confirmation?
8417                   gnus-expert-user
8418                   (gnus-y-or-n-p
8419                    (if all
8420                        "Mark absolutely all articles as read? "
8421                      "Mark all unread articles as read? ")))
8422           (if (and not-mark
8423                    (not gnus-newsgroup-adaptive)
8424                    (not gnus-newsgroup-auto-expire)
8425                    (not gnus-suppress-duplicates)
8426                    (or (not gnus-use-cache)
8427                        (eq gnus-use-cache 'passive)))
8428               (progn
8429                 (when all
8430                   (setq gnus-newsgroup-marked nil
8431                         gnus-newsgroup-dormant nil))
8432                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8433             ;; We actually mark all articles as canceled, which we
8434             ;; have to do when using auto-expiry or adaptive scoring.
8435             (gnus-summary-show-all-threads)
8436             (when (gnus-summary-first-subject (not all) t)
8437               (while (and
8438                       (if to-here (< (point) to-here) t)
8439                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8440                       (gnus-summary-find-next (not all) nil nil t))))
8441             (gnus-set-mode-line 'summary))
8442           t))
8443     (gnus-summary-position-point)))
8444
8445 (defun gnus-summary-catchup-to-here (&optional all)
8446   "Mark all unticked articles before the current one as read.
8447 If ALL is non-nil, also mark ticked and dormant articles as read."
8448   (interactive "P")
8449   (save-excursion
8450     (gnus-save-hidden-threads
8451       (let ((beg (point)))
8452         ;; We check that there are unread articles.
8453         (when (or all (gnus-summary-find-prev))
8454           (gnus-summary-catchup all t beg)))))
8455   (gnus-summary-position-point))
8456
8457 (defun gnus-summary-catchup-all (&optional quietly)
8458   "Mark all articles in this newsgroup as read."
8459   (interactive "P")
8460   (gnus-summary-catchup t quietly))
8461
8462 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8463   "Mark all unread articles in this group as read, then exit.
8464 If prefix argument ALL is non-nil, all articles are marked as read."
8465   (interactive "P")
8466   (when (gnus-summary-catchup all quietly nil 'fast)
8467     ;; Select next newsgroup or exit.
8468     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8469              (eq gnus-auto-select-next 'quietly))
8470         (gnus-summary-next-group nil)
8471       (gnus-summary-exit))))
8472
8473 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8474   "Mark all articles in this newsgroup as read, and then exit."
8475   (interactive "P")
8476   (gnus-summary-catchup-and-exit t quietly))
8477
8478 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8479   "Mark all articles in this group as read and select the next group.
8480 If given a prefix, mark all articles, unread as well as ticked, as
8481 read."
8482   (interactive "P")
8483   (save-excursion
8484     (gnus-summary-catchup all))
8485   (gnus-summary-next-group))
8486
8487 ;; Thread-based commands.
8488
8489 (defun gnus-summary-articles-in-thread (&optional article)
8490   "Return a list of all articles in the current thread.
8491 If ARTICLE is non-nil, return all articles in the thread that starts
8492 with that article."
8493   (let* ((article (or article (gnus-summary-article-number)))
8494          (data (gnus-data-find-list article))
8495          (top-level (gnus-data-level (car data)))
8496          (top-subject
8497           (cond ((null gnus-thread-operation-ignore-subject)
8498                  (gnus-simplify-subject-re
8499                   (mail-header-subject (gnus-data-header (car data)))))
8500                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8501                  (gnus-simplify-subject-fuzzy
8502                   (mail-header-subject (gnus-data-header (car data)))))
8503                 (t nil)))
8504          (end-point (save-excursion
8505                       (if (gnus-summary-go-to-next-thread)
8506                           (point) (point-max))))
8507          articles)
8508     (while (and data
8509                 (< (gnus-data-pos (car data)) end-point))
8510       (when (or (not top-subject)
8511                 (string= top-subject
8512                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8513                              (gnus-simplify-subject-fuzzy
8514                               (mail-header-subject
8515                                (gnus-data-header (car data))))
8516                            (gnus-simplify-subject-re
8517                             (mail-header-subject
8518                              (gnus-data-header (car data)))))))
8519         (push (gnus-data-number (car data)) articles))
8520       (unless (and (setq data (cdr data))
8521                    (> (gnus-data-level (car data)) top-level))
8522         (setq data nil)))
8523     ;; Return the list of articles.
8524     (nreverse articles)))
8525
8526 (defun gnus-summary-rethread-current ()
8527   "Rethread the thread the current article is part of."
8528   (interactive)
8529   (let* ((gnus-show-threads t)
8530          (article (gnus-summary-article-number))
8531          (id (mail-header-id (gnus-summary-article-header)))
8532          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8533     (unless id
8534       (error "No article on the current line"))
8535     (gnus-rebuild-thread id)
8536     (gnus-summary-goto-subject article)))
8537
8538 (defun gnus-summary-reparent-thread ()
8539   "Make the current article child of the marked (or previous) article.
8540
8541 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8542 is non-nil or the Subject: of both articles are the same."
8543   (interactive)
8544   (unless (not (gnus-group-read-only-p))
8545     (error "The current newsgroup does not support article editing"))
8546   (unless (<= (length gnus-newsgroup-processable) 1)
8547     (error "No more than one article may be marked"))
8548   (save-window-excursion
8549     (let ((gnus-article-buffer " *reparent*")
8550           (current-article (gnus-summary-article-number))
8551           ;; First grab the marked article, otherwise one line up.
8552           (parent-article (if (not (null gnus-newsgroup-processable))
8553                               (car gnus-newsgroup-processable)
8554                             (save-excursion
8555                               (if (eq (forward-line -1) 0)
8556                                   (gnus-summary-article-number)
8557                                 (error "Beginning of summary buffer"))))))
8558       (unless (not (eq current-article parent-article))
8559         (error "An article may not be self-referential"))
8560       (let ((message-id (mail-header-id
8561                          (gnus-summary-article-header parent-article))))
8562         (unless (and message-id (not (equal message-id "")))
8563           (error "No message-id in desired parent"))
8564         (gnus-with-article current-article
8565           (goto-char (point-min))
8566           (if (re-search-forward "^References: " nil t)
8567               (progn
8568                 (re-search-forward "^[^ \t]" nil t)
8569                 (forward-line -1)
8570                 (end-of-line)
8571                 (insert " " message-id))
8572             (insert "References: " message-id "\n")))
8573         (set-buffer gnus-summary-buffer)
8574         (gnus-summary-unmark-all-processable)
8575         (gnus-summary-update-article current-article)
8576         (gnus-summary-rethread-current)
8577         (gnus-message 3 "Article %d is now the child of article %d"
8578                       current-article parent-article)))))
8579
8580 (defun gnus-summary-toggle-threads (&optional arg)
8581   "Toggle showing conversation threads.
8582 If ARG is positive number, turn showing conversation threads on."
8583   (interactive "P")
8584   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8585     (setq gnus-show-threads
8586           (if (null arg) (not gnus-show-threads)
8587             (> (prefix-numeric-value arg) 0)))
8588     (gnus-summary-prepare)
8589     (gnus-summary-goto-subject current)
8590     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8591     (gnus-summary-position-point)))
8592
8593 (defun gnus-summary-show-all-threads ()
8594   "Show all threads."
8595   (interactive)
8596   (save-excursion
8597     (let ((buffer-read-only nil))
8598       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8599   (gnus-summary-position-point))
8600
8601 (defun gnus-summary-show-thread ()
8602   "Show thread subtrees.
8603 Returns nil if no thread was there to be shown."
8604   (interactive)
8605   (let ((buffer-read-only nil)
8606         (orig (point))
8607         ;; first goto end then to beg, to have point at beg after let
8608         (end (progn (end-of-line) (point)))
8609         (beg (progn (beginning-of-line) (point))))
8610     (prog1
8611         ;; Any hidden lines here?
8612         (search-forward "\r" end t)
8613       (subst-char-in-region beg end ?\^M ?\n t)
8614       (goto-char orig)
8615       (gnus-summary-position-point))))
8616
8617 (defun gnus-summary-hide-all-threads ()
8618   "Hide all thread subtrees."
8619   (interactive)
8620   (save-excursion
8621     (goto-char (point-min))
8622     (gnus-summary-hide-thread)
8623     (while (zerop (gnus-summary-next-thread 1 t))
8624       (gnus-summary-hide-thread)))
8625   (gnus-summary-position-point))
8626
8627 (defun gnus-summary-hide-thread ()
8628   "Hide thread subtrees.
8629 Returns nil if no threads were there to be hidden."
8630   (interactive)
8631   (let ((buffer-read-only nil)
8632         (start (point))
8633         (article (gnus-summary-article-number)))
8634     (goto-char start)
8635     ;; Go forward until either the buffer ends or the subthread
8636     ;; ends.
8637     (when (and (not (eobp))
8638                (or (zerop (gnus-summary-next-thread 1 t))
8639                    (goto-char (point-max))))
8640       (prog1
8641           (if (and (> (point) start)
8642                    (search-backward "\n" start t))
8643               (progn
8644                 (subst-char-in-region start (point) ?\n ?\^M)
8645                 (gnus-summary-goto-subject article))
8646             (goto-char start)
8647             nil)))))
8648
8649 (defun gnus-summary-go-to-next-thread (&optional previous)
8650   "Go to the same level (or less) next thread.
8651 If PREVIOUS is non-nil, go to previous thread instead.
8652 Return the article number moved to, or nil if moving was impossible."
8653   (let ((level (gnus-summary-thread-level))
8654         (way (if previous -1 1))
8655         (beg (point)))
8656     (forward-line way)
8657     (while (and (not (eobp))
8658                 (< level (gnus-summary-thread-level)))
8659       (forward-line way))
8660     (if (eobp)
8661         (progn
8662           (goto-char beg)
8663           nil)
8664       (setq beg (point))
8665       (prog1
8666           (gnus-summary-article-number)
8667         (goto-char beg)))))
8668
8669 (defun gnus-summary-next-thread (n &optional silent)
8670   "Go to the same level next N'th thread.
8671 If N is negative, search backward instead.
8672 Returns the difference between N and the number of skips actually
8673 done.
8674
8675 If SILENT, don't output messages."
8676   (interactive "p")
8677   (let ((backward (< n 0))
8678         (n (abs n)))
8679     (while (and (> n 0)
8680                 (gnus-summary-go-to-next-thread backward))
8681       (decf n))
8682     (unless silent
8683       (gnus-summary-position-point))
8684     (when (and (not silent) (/= 0 n))
8685       (gnus-message 7 "No more threads"))
8686     n))
8687
8688 (defun gnus-summary-prev-thread (n)
8689   "Go to the same level previous N'th thread.
8690 Returns the difference between N and the number of skips actually
8691 done."
8692   (interactive "p")
8693   (gnus-summary-next-thread (- n)))
8694
8695 (defun gnus-summary-go-down-thread ()
8696   "Go down one level in the current thread."
8697   (let ((children (gnus-summary-article-children)))
8698     (when children
8699       (gnus-summary-goto-subject (car children)))))
8700
8701 (defun gnus-summary-go-up-thread ()
8702   "Go up one level in the current thread."
8703   (let ((parent (gnus-summary-article-parent)))
8704     (when parent
8705       (gnus-summary-goto-subject parent))))
8706
8707 (defun gnus-summary-down-thread (n)
8708   "Go down thread N steps.
8709 If N is negative, go up instead.
8710 Returns the difference between N and how many steps down that were
8711 taken."
8712   (interactive "p")
8713   (let ((up (< n 0))
8714         (n (abs n)))
8715     (while (and (> n 0)
8716                 (if up (gnus-summary-go-up-thread)
8717                   (gnus-summary-go-down-thread)))
8718       (setq n (1- n)))
8719     (gnus-summary-position-point)
8720     (when (/= 0 n)
8721       (gnus-message 7 "Can't go further"))
8722     n))
8723
8724 (defun gnus-summary-up-thread (n)
8725   "Go up thread N steps.
8726 If N is negative, go up instead.
8727 Returns the difference between N and how many steps down that were
8728 taken."
8729   (interactive "p")
8730   (gnus-summary-down-thread (- n)))
8731
8732 (defun gnus-summary-top-thread ()
8733   "Go to the top of the thread."
8734   (interactive)
8735   (while (gnus-summary-go-up-thread))
8736   (gnus-summary-article-number))
8737
8738 (defun gnus-summary-kill-thread (&optional unmark)
8739   "Mark articles under current thread as read.
8740 If the prefix argument is positive, remove any kinds of marks.
8741 If the prefix argument is negative, tick articles instead."
8742   (interactive "P")
8743   (when unmark
8744     (setq unmark (prefix-numeric-value unmark)))
8745   (let ((articles (gnus-summary-articles-in-thread)))
8746     (save-excursion
8747       ;; Expand the thread.
8748       (gnus-summary-show-thread)
8749       ;; Mark all the articles.
8750       (while articles
8751         (gnus-summary-goto-subject (car articles))
8752         (cond ((null unmark)
8753                (gnus-summary-mark-article-as-read gnus-killed-mark))
8754               ((> unmark 0)
8755                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8756               (t
8757                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8758         (setq articles (cdr articles))))
8759     ;; Hide killed subtrees.
8760     (and (null unmark)
8761          gnus-thread-hide-killed
8762          (gnus-summary-hide-thread))
8763     ;; If marked as read, go to next unread subject.
8764     (when (null unmark)
8765       ;; Go to next unread subject.
8766       (gnus-summary-next-subject 1 t)))
8767   (gnus-set-mode-line 'summary))
8768
8769 ;; Summary sorting commands
8770
8771 (defun gnus-summary-sort-by-number (&optional reverse)
8772   "Sort the summary buffer by article number.
8773 Argument REVERSE means reverse order."
8774   (interactive "P")
8775   (gnus-summary-sort 'number reverse))
8776
8777 (defun gnus-summary-sort-by-author (&optional reverse)
8778   "Sort the summary buffer by author name alphabetically.
8779 If case-fold-search is non-nil, case of letters is ignored.
8780 Argument REVERSE means reverse order."
8781   (interactive "P")
8782   (gnus-summary-sort 'author reverse))
8783
8784 (defun gnus-summary-sort-by-subject (&optional reverse)
8785   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8786 If case-fold-search is non-nil, case of letters is ignored.
8787 Argument REVERSE means reverse order."
8788   (interactive "P")
8789   (gnus-summary-sort 'subject reverse))
8790
8791 (defun gnus-summary-sort-by-date (&optional reverse)
8792   "Sort the summary buffer by date.
8793 Argument REVERSE means reverse order."
8794   (interactive "P")
8795   (gnus-summary-sort 'date reverse))
8796
8797 (defun gnus-summary-sort-by-score (&optional reverse)
8798   "Sort the summary buffer by score.
8799 Argument REVERSE means reverse order."
8800   (interactive "P")
8801   (gnus-summary-sort 'score reverse))
8802
8803 (defun gnus-summary-sort-by-lines (&optional reverse)
8804   "Sort the summary buffer by the number of lines.
8805 Argument REVERSE means reverse order."
8806   (interactive "P")
8807   (gnus-summary-sort 'lines reverse))
8808
8809 (defun gnus-summary-sort-by-chars (&optional reverse)
8810   "Sort the summary buffer by article length.
8811 Argument REVERSE means reverse order."
8812   (interactive "P")
8813   (gnus-summary-sort 'chars reverse))   
8814
8815 (defun gnus-summary-sort (predicate reverse)
8816   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8817   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8818          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8819          (gnus-thread-sort-functions
8820           (if (not reverse)
8821               thread
8822             `(lambda (t1 t2)
8823                (,thread t2 t1))))
8824          (gnus-article-sort-functions
8825           (if (not reverse)
8826               article
8827             `(lambda (t1 t2)
8828                (,article t2 t1))))
8829          (buffer-read-only)
8830          (gnus-summary-prepare-hook nil))
8831     ;; We do the sorting by regenerating the threads.
8832     (gnus-summary-prepare)
8833     ;; Hide subthreads if needed.
8834     (when (and gnus-show-threads gnus-thread-hide-subtree)
8835       (gnus-summary-hide-all-threads))))
8836
8837 ;; Summary saving commands.
8838
8839 (defun gnus-summary-save-article (&optional n not-saved)
8840   "Save the current article using the default saver function.
8841 If N is a positive number, save the N next articles.
8842 If N is a negative number, save the N previous articles.
8843 If N is nil and any articles have been marked with the process mark,
8844 save those articles instead.
8845 The variable `gnus-default-article-saver' specifies the saver function."
8846   (interactive "P")
8847   (let* ((articles (gnus-summary-work-articles n))
8848          (save-buffer (save-excursion
8849                         (nnheader-set-temp-buffer " *Gnus Save*")))
8850          (num (length articles))
8851          header file)
8852     (dolist (article articles)
8853       (setq header (gnus-summary-article-header article))
8854       (if (not (vectorp header))
8855           ;; This is a pseudo-article.
8856           (if (assq 'name header)
8857               (gnus-copy-file (cdr (assq 'name header)))
8858             (gnus-message 1 "Article %d is unsaveable" article))
8859         ;; This is a real article.
8860         (save-window-excursion
8861           (gnus-summary-select-article t nil nil article))
8862         (save-excursion
8863           (set-buffer save-buffer)
8864           (erase-buffer)
8865           (insert-buffer-substring gnus-original-article-buffer))
8866         (setq file (gnus-article-save save-buffer file num))
8867         (gnus-summary-remove-process-mark article)
8868         (unless not-saved
8869           (gnus-summary-set-saved-mark article))))
8870     (gnus-kill-buffer save-buffer)
8871     (gnus-summary-position-point)
8872     (gnus-set-mode-line 'summary)
8873     n))
8874
8875 (defun gnus-summary-pipe-output (&optional arg)
8876   "Pipe the current article to a subprocess.
8877 If N is a positive number, pipe the N next articles.
8878 If N is a negative number, pipe the N previous articles.
8879 If N is nil and any articles have been marked with the process mark,
8880 pipe those articles instead."
8881   (interactive "P")
8882   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8883     (gnus-summary-save-article arg t))
8884   (gnus-configure-windows 'pipe))
8885
8886 (defun gnus-summary-save-article-mail (&optional arg)
8887   "Append the current article to an mail file.
8888 If N is a positive number, save the N next articles.
8889 If N is a negative number, save the N previous articles.
8890 If N is nil and any articles have been marked with the process mark,
8891 save those articles instead."
8892   (interactive "P")
8893   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8894     (gnus-summary-save-article arg)))
8895
8896 (defun gnus-summary-save-article-rmail (&optional arg)
8897   "Append the current article to an rmail file.
8898 If N is a positive number, save the N next articles.
8899 If N is a negative number, save the N previous articles.
8900 If N is nil and any articles have been marked with the process mark,
8901 save those articles instead."
8902   (interactive "P")
8903   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8904     (gnus-summary-save-article arg)))
8905
8906 (defun gnus-summary-save-article-file (&optional arg)
8907   "Append the current article to a file.
8908 If N is a positive number, save the N next articles.
8909 If N is a negative number, save the N previous articles.
8910 If N is nil and any articles have been marked with the process mark,
8911 save those articles instead."
8912   (interactive "P")
8913   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8914     (gnus-summary-save-article arg)))
8915
8916 (defun gnus-summary-write-article-file (&optional arg)
8917   "Write the current article to a file, deleting the previous file.
8918 If N is a positive number, save the N next articles.
8919 If N is a negative number, save the N previous articles.
8920 If N is nil and any articles have been marked with the process mark,
8921 save those articles instead."
8922   (interactive "P")
8923   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8924     (gnus-summary-save-article arg)))
8925
8926 (defun gnus-summary-save-article-body-file (&optional arg)
8927   "Append the current article body to a file.
8928 If N is a positive number, save the N next articles.
8929 If N is a negative number, save the N previous articles.
8930 If N is nil and any articles have been marked with the process mark,
8931 save those articles instead."
8932   (interactive "P")
8933   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8934     (gnus-summary-save-article arg)))
8935
8936 (defun gnus-summary-pipe-message (program)
8937   "Pipe the current article through PROGRAM."
8938   (interactive "sProgram: ")
8939   (gnus-summary-select-article)
8940   (let ((mail-header-separator ""))
8941     (gnus-eval-in-buffer-window gnus-article-buffer
8942       (save-restriction
8943         (widen)
8944         (let ((start (window-start))
8945               buffer-read-only)
8946           (message-pipe-buffer-body program)
8947           (set-window-start (get-buffer-window (current-buffer)) start))))))
8948
8949 (defun gnus-get-split-value (methods)
8950   "Return a value based on the split METHODS."
8951   (let (split-name method result match)
8952     (when methods
8953       (save-excursion
8954         (set-buffer gnus-original-article-buffer)
8955         (save-restriction
8956           (nnheader-narrow-to-headers)
8957           (while methods
8958             (goto-char (point-min))
8959             (setq method (pop methods))
8960             (setq match (car method))
8961             (when (cond
8962                    ((stringp match)
8963                     ;; Regular expression.
8964                     (ignore-errors
8965                       (re-search-forward match nil t)))
8966                    ((gnus-functionp match)
8967                     ;; Function.
8968                     (save-restriction
8969                       (widen)
8970                       (setq result (funcall match gnus-newsgroup-name))))
8971                    ((consp match)
8972                     ;; Form.
8973                     (save-restriction
8974                       (widen)
8975                       (setq result (eval match)))))
8976               (setq split-name (append (cdr method) split-name))
8977               (cond ((stringp result)
8978                      (push (expand-file-name
8979                             result gnus-article-save-directory)
8980                            split-name))
8981                     ((consp result)
8982                      (setq split-name (append result split-name)))))))))
8983     (nreverse split-name)))
8984
8985 (defun gnus-valid-move-group-p (group)
8986   (and (boundp group)
8987        (symbol-name group)
8988        (symbol-value group)
8989        (gnus-get-function (gnus-find-method-for-group
8990                            (symbol-name group)) 'request-accept-article t)))
8991
8992 (defun gnus-read-move-group-name (prompt default articles prefix)
8993   "Read a group name."
8994   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8995          (minibuffer-confirm-incomplete nil) ; XEmacs
8996          (prom
8997           (format "%s %s to:"
8998                   prompt
8999                   (if (> (length articles) 1)
9000                       (format "these %d articles" (length articles))
9001                     "this article")))
9002          (to-newsgroup
9003           (cond
9004            ((null split-name)
9005             (gnus-completing-read default prom
9006                                   gnus-active-hashtb
9007                                   'gnus-valid-move-group-p
9008                                   nil prefix
9009                                   'gnus-group-history))
9010            ((= 1 (length split-name))
9011             (gnus-completing-read (car split-name) prom
9012                                   gnus-active-hashtb
9013                                   'gnus-valid-move-group-p
9014                                   nil nil
9015                                   'gnus-group-history))
9016            (t
9017             (gnus-completing-read nil prom
9018                                   (mapcar (lambda (el) (list el))
9019                                           (nreverse split-name))
9020                                   nil nil nil
9021                                   'gnus-group-history)))))
9022     (when to-newsgroup
9023       (if (or (string= to-newsgroup "")
9024               (string= to-newsgroup prefix))
9025           (setq to-newsgroup default))
9026       (unless to-newsgroup
9027         (error "No group name entered"))
9028       (or (gnus-active to-newsgroup)
9029           (gnus-activate-group to-newsgroup)
9030           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9031                                      to-newsgroup))
9032               (or (and (gnus-request-create-group
9033                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9034                        (gnus-activate-group
9035                         to-newsgroup nil nil
9036                         (gnus-group-name-to-method to-newsgroup))
9037                        (gnus-subscribe-group to-newsgroup))
9038                   (error "Couldn't create group %s" to-newsgroup)))
9039           (error "No such group: %s" to-newsgroup)))
9040     to-newsgroup))
9041
9042 (defun gnus-summary-save-parts (type dir n reverse)
9043   "Save parts matching TYPE to DIR.
9044 If REVERSE, save parts that do not match TYPE."
9045   (interactive
9046    (list (read-string "Save parts of type: " "image/.*")
9047          (read-file-name "Save to directory: " t nil t)
9048          current-prefix-arg))
9049   (gnus-summary-iterate n
9050     (let ((gnus-display-mime-function nil)
9051           (gnus-inhibit-treatment t))
9052       (gnus-summary-select-article))
9053     (save-excursion
9054       (set-buffer gnus-article-buffer)
9055       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9056         (when handles
9057           (gnus-summary-save-parts-1 type dir handles reverse)
9058           (mm-destroy-parts handles))))))
9059
9060 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9061   (if (stringp (car handle))
9062       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9063               (cdr handle))
9064     (when (if reverse
9065               (not (string-match type (mm-handle-media-type handle)))
9066             (string-match type (mm-handle-media-type handle)))
9067       (let ((file (expand-file-name
9068                    (file-name-nondirectory
9069                     (or
9070                      (mail-content-type-get
9071                       (mm-handle-disposition handle) 'filename)
9072                      (concat gnus-newsgroup-name "." gnus-current-article)))
9073                    dir)))
9074         (unless (file-exists-p file)
9075           (mm-save-part-to-file handle file))))))
9076
9077 ;; Summary extract commands
9078
9079 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9080   (let ((buffer-read-only nil)
9081         (article (gnus-summary-article-number))
9082         after-article b e)
9083     (unless (gnus-summary-goto-subject article)
9084       (error "No such article: %d" article))
9085     (gnus-summary-position-point)
9086     ;; If all commands are to be bunched up on one line, we collect
9087     ;; them here.
9088     (unless gnus-view-pseudos-separately
9089       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9090             files action)
9091         (while ps
9092           (setq action (cdr (assq 'action (car ps))))
9093           (setq files (list (cdr (assq 'name (car ps)))))
9094           (while (and ps (cdr ps)
9095                       (string= (or action "1")
9096                                (or (cdr (assq 'action (cadr ps))) "2")))
9097             (push (cdr (assq 'name (cadr ps))) files)
9098             (setcdr ps (cddr ps)))
9099           (when files
9100             (when (not (string-match "%s" action))
9101               (push " " files))
9102             (push " " files)
9103             (when (assq 'execute (car ps))
9104               (setcdr (assq 'execute (car ps))
9105                       (funcall (if (string-match "%s" action)
9106                                    'format 'concat)
9107                                action
9108                                (mapconcat
9109                                 (lambda (f)
9110                                   (if (equal f " ")
9111                                       f
9112                                     (mm-quote-arg f)))
9113                                 files " ")))))
9114           (setq ps (cdr ps)))))
9115     (if (and gnus-view-pseudos (not not-view))
9116         (while pslist
9117           (when (assq 'execute (car pslist))
9118             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9119                                   (eq gnus-view-pseudos 'not-confirm)))
9120           (setq pslist (cdr pslist)))
9121       (save-excursion
9122         (while pslist
9123           (setq after-article (or (cdr (assq 'article (car pslist)))
9124                                   (gnus-summary-article-number)))
9125           (gnus-summary-goto-subject after-article)
9126           (forward-line 1)
9127           (setq b (point))
9128           (insert "    " (file-name-nondirectory
9129                           (cdr (assq 'name (car pslist))))
9130                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9131           (setq e (point))
9132           (forward-line -1)             ; back to `b'
9133           (gnus-add-text-properties
9134            b (1- e) (list 'gnus-number gnus-reffed-article-number
9135                           gnus-mouse-face-prop gnus-mouse-face))
9136           (gnus-data-enter
9137            after-article gnus-reffed-article-number
9138            gnus-unread-mark b (car pslist) 0 (- e b))
9139           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9140           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9141           (setq pslist (cdr pslist)))))))
9142
9143 (defun gnus-pseudos< (p1 p2)
9144   (let ((c1 (cdr (assq 'action p1)))
9145         (c2 (cdr (assq 'action p2))))
9146     (and c1 c2 (string< c1 c2))))
9147
9148 (defun gnus-request-pseudo-article (props)
9149   (cond ((assq 'execute props)
9150          (gnus-execute-command (cdr (assq 'execute props)))))
9151   (let ((gnus-current-article (gnus-summary-article-number)))
9152     (gnus-run-hooks 'gnus-mark-article-hook)))
9153
9154 (defun gnus-execute-command (command &optional automatic)
9155   (save-excursion
9156     (gnus-article-setup-buffer)
9157     (set-buffer gnus-article-buffer)
9158     (setq buffer-read-only nil)
9159     (let ((command (if automatic command
9160                      (read-string "Command: " (cons command 0)))))
9161       (erase-buffer)
9162       (insert "$ " command "\n\n")
9163       (if gnus-view-pseudo-asynchronously
9164           (start-process "gnus-execute" (current-buffer) shell-file-name
9165                          shell-command-switch command)
9166         (call-process shell-file-name nil t nil
9167                       shell-command-switch command)))))
9168
9169 ;; Summary kill commands.
9170
9171 (defun gnus-summary-edit-global-kill (article)
9172   "Edit the \"global\" kill file."
9173   (interactive (list (gnus-summary-article-number)))
9174   (gnus-group-edit-global-kill article))
9175
9176 (defun gnus-summary-edit-local-kill ()
9177   "Edit a local kill file applied to the current newsgroup."
9178   (interactive)
9179   (setq gnus-current-headers (gnus-summary-article-header))
9180   (gnus-group-edit-local-kill
9181    (gnus-summary-article-number) gnus-newsgroup-name))
9182
9183 ;;; Header reading.
9184
9185 (defun gnus-read-header (id &optional header)
9186   "Read the headers of article ID and enter them into the Gnus system."
9187   (let ((group gnus-newsgroup-name)
9188         (gnus-override-method
9189          (or
9190           gnus-override-method
9191           (and (gnus-news-group-p gnus-newsgroup-name)
9192                (car (gnus-refer-article-methods)))))
9193         where)
9194     ;; First we check to see whether the header in question is already
9195     ;; fetched.
9196     (if (stringp id)
9197         ;; This is a Message-ID.
9198         (setq header (or header (gnus-id-to-header id)))
9199       ;; This is an article number.
9200       (setq header (or header (gnus-summary-article-header id))))
9201     (if (and header
9202              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9203         ;; We have found the header.
9204         header
9205       ;; If this is a sparse article, we have to nix out its
9206       ;; previous entry in the thread hashtb.
9207       (when (and header
9208                  (gnus-summary-article-sparse-p (mail-header-number header)))
9209         (let* ((parent (gnus-parent-id (mail-header-references header)))
9210                (thread (and parent (gnus-id-to-thread parent))))
9211           (when thread
9212             (delq (assq header thread) thread))))
9213       ;; We have to really fetch the header to this article.
9214       (save-excursion
9215         (set-buffer nntp-server-buffer)
9216         (when (setq where (gnus-request-head id group))
9217           (nnheader-fold-continuation-lines)
9218           (goto-char (point-max))
9219           (insert ".\n")
9220           (goto-char (point-min))
9221           (insert "211 ")
9222           (princ (cond
9223                   ((numberp id) id)
9224                   ((cdr where) (cdr where))
9225                   (header (mail-header-number header))
9226                   (t gnus-reffed-article-number))
9227                  (current-buffer))
9228           (insert " Article retrieved.\n"))
9229         (if (or (not where)
9230                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9231             ()                          ; Malformed head.
9232           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9233             (when (and (stringp id)
9234                        (not (string= (gnus-group-real-name group)
9235                                      (car where))))
9236               ;; If we fetched by Message-ID and the article came
9237               ;; from a different group, we fudge some bogus article
9238               ;; numbers for this article.
9239               (mail-header-set-number header gnus-reffed-article-number))
9240             (save-excursion
9241               (set-buffer gnus-summary-buffer)
9242               (decf gnus-reffed-article-number)
9243               (gnus-remove-header (mail-header-number header))
9244               (push header gnus-newsgroup-headers)
9245               (setq gnus-current-headers header)
9246               (push (mail-header-number header) gnus-newsgroup-limit)))
9247           header)))))
9248
9249 (defun gnus-remove-header (number)
9250   "Remove header NUMBER from `gnus-newsgroup-headers'."
9251   (if (and gnus-newsgroup-headers
9252            (= number (mail-header-number (car gnus-newsgroup-headers))))
9253       (pop gnus-newsgroup-headers)
9254     (let ((headers gnus-newsgroup-headers))
9255       (while (and (cdr headers)
9256                   (not (= number (mail-header-number (cadr headers)))))
9257         (pop headers))
9258       (when (cdr headers)
9259         (setcdr headers (cddr headers))))))
9260
9261 ;;;
9262 ;;; summary highlights
9263 ;;;
9264
9265 (defun gnus-highlight-selected-summary ()
9266   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9267   ;; Highlight selected article in summary buffer
9268   (when gnus-summary-selected-face
9269     (save-excursion
9270       (let* ((beg (progn (beginning-of-line) (point)))
9271              (end (progn (end-of-line) (point)))
9272              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9273              (from (if (get-text-property beg gnus-mouse-face-prop)
9274                        beg
9275                      (or (next-single-property-change
9276                           beg gnus-mouse-face-prop nil end)
9277                          beg)))
9278              (to
9279               (if (= from end)
9280                   (- from 2)
9281                 (or (next-single-property-change
9282                      from gnus-mouse-face-prop nil end)
9283                     end))))
9284         ;; If no mouse-face prop on line we will have to = from = end,
9285         ;; so we highlight the entire line instead.
9286         (when (= (+ to 2) from)
9287           (setq from beg)
9288           (setq to end))
9289         (if gnus-newsgroup-selected-overlay
9290             ;; Move old overlay.
9291             (gnus-move-overlay
9292              gnus-newsgroup-selected-overlay from to (current-buffer))
9293           ;; Create new overlay.
9294           (gnus-overlay-put
9295            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9296            'face gnus-summary-selected-face))))))
9297
9298 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9299 (defun gnus-summary-highlight-line ()
9300   "Highlight current line according to `gnus-summary-highlight'."
9301   (let* ((list gnus-summary-highlight)
9302          (p (point))
9303          (end (progn (end-of-line) (point)))
9304          ;; now find out where the line starts and leave point there.
9305          (beg (progn (beginning-of-line) (point)))
9306          (article (gnus-summary-article-number))
9307          (score (or (cdr (assq (or article gnus-current-article)
9308                                gnus-newsgroup-scored))
9309                     gnus-summary-default-score 0))
9310          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9311          (inhibit-read-only t))
9312     ;; Eval the cars of the lists until we find a match.
9313     (let ((default gnus-summary-default-score))
9314       (while (and list
9315                   (not (eval (caar list))))
9316         (setq list (cdr list))))
9317     (let ((face (cdar list)))
9318       (unless (eq face (get-text-property beg 'face))
9319         (gnus-put-text-property-excluding-characters-with-faces
9320          beg end 'face
9321          (setq face (if (boundp face) (symbol-value face) face)))
9322         (when gnus-summary-highlight-line-function
9323           (funcall gnus-summary-highlight-line-function article face))))
9324     (goto-char p)))
9325
9326 (defun gnus-update-read-articles (group unread &optional compute)
9327   "Update the list of read articles in GROUP."
9328   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9329          (entry (gnus-gethash group gnus-newsrc-hashtb))
9330          (info (nth 2 entry))
9331          (prev 1)
9332          (unread (sort (copy-sequence unread) '<))
9333          read)
9334     (if (or (not info) (not active))
9335         ;; There is no info on this group if it was, in fact,
9336         ;; killed.  Gnus stores no information on killed groups, so
9337         ;; there's nothing to be done.
9338         ;; One could store the information somewhere temporarily,
9339         ;; perhaps...  Hmmm...
9340         ()
9341       ;; Remove any negative articles numbers.
9342       (while (and unread (< (car unread) 0))
9343         (setq unread (cdr unread)))
9344       ;; Remove any expired article numbers
9345       (while (and unread (< (car unread) (car active)))
9346         (setq unread (cdr unread)))
9347       ;; Compute the ranges of read articles by looking at the list of
9348       ;; unread articles.
9349       (while unread
9350         (when (/= (car unread) prev)
9351           (push (if (= prev (1- (car unread))) prev
9352                   (cons prev (1- (car unread))))
9353                 read))
9354         (setq prev (1+ (car unread)))
9355         (setq unread (cdr unread)))
9356       (when (<= prev (cdr active))
9357         (push (cons prev (cdr active)) read))
9358       (setq read (if (> (length read) 1) (nreverse read) read))
9359       (if compute
9360           read
9361         (save-excursion
9362           (set-buffer gnus-group-buffer)
9363           (gnus-undo-register
9364             `(progn
9365                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9366                (gnus-info-set-read ',info ',(gnus-info-read info))
9367                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9368                (gnus-group-update-group ,group t))))
9369         ;; Propagate the read marks to the backend.
9370         (if (gnus-check-backend-function 'request-set-mark group)
9371             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9372                   (add (gnus-remove-from-range read (gnus-info-read info))))
9373               (when (or add del)
9374                 (unless (gnus-check-group group)
9375                   (error "Can't open server for %s" group))
9376                 (gnus-request-set-mark
9377                  group (delq nil (list (if add (list add 'add '(read)))
9378                                        (if del (list del 'del '(read)))))))))
9379         ;; Enter this list into the group info.
9380         (gnus-info-set-read info read)
9381         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9382         (gnus-get-unread-articles-in-group info (gnus-active group))
9383         t))))
9384
9385 (defun gnus-offer-save-summaries ()
9386   "Offer to save all active summary buffers."
9387   (save-excursion
9388     (let ((buflist (buffer-list))
9389           buffers bufname)
9390       ;; Go through all buffers and find all summaries.
9391       (while buflist
9392         (and (setq bufname (buffer-name (car buflist)))
9393              (string-match "Summary" bufname)
9394              (save-excursion
9395                (set-buffer bufname)
9396                ;; We check that this is, indeed, a summary buffer.
9397                (and (eq major-mode 'gnus-summary-mode)
9398                     ;; Also make sure this isn't bogus.
9399                     gnus-newsgroup-prepared
9400                     ;; Also make sure that this isn't a dead summary buffer.
9401                     (not gnus-dead-summary-mode)))
9402              (push bufname buffers))
9403         (setq buflist (cdr buflist)))
9404       ;; Go through all these summary buffers and offer to save them.
9405       (when buffers
9406         (map-y-or-n-p
9407          "Update summary buffer %s? "
9408          (lambda (buf)
9409            (switch-to-buffer buf)
9410            (gnus-summary-exit))
9411          buffers)))))
9412
9413 (defun gnus-summary-setup-default-charset ()
9414   "Setup newsgroup default charset."
9415   (if (equal gnus-newsgroup-name "nndraft:drafts")
9416       (setq gnus-newsgroup-charset nil)
9417     (let* ((name (and gnus-newsgroup-name
9418                       (gnus-group-real-name gnus-newsgroup-name)))
9419            (ignored-charsets 
9420             (or gnus-newsgroup-ephemeral-ignored-charsets
9421                 (append
9422                  (and gnus-newsgroup-name
9423                       (or (gnus-group-find-parameter gnus-newsgroup-name
9424                                                      'ignored-charsets t)
9425                           (let ((alist gnus-group-ignored-charsets-alist)
9426                                 elem (charsets nil))
9427                             (while (setq elem (pop alist))
9428                               (when (and name
9429                                          (string-match (car elem) name))
9430                                 (setq alist nil
9431                                       charsets (cdr elem))))
9432                             charsets)))
9433                  gnus-newsgroup-ignored-charsets))))
9434       (setq gnus-newsgroup-charset
9435             (or gnus-newsgroup-ephemeral-charset
9436                 (and gnus-newsgroup-name
9437                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9438                          (let ((alist gnus-group-charset-alist)
9439                                elem charset)
9440                            (while (setq elem (pop alist))
9441                              (when (and name
9442                                         (string-match (car elem) name))
9443                                (setq alist nil
9444                                      charset (cadr elem))))
9445                            charset)))
9446                 gnus-default-charset))
9447       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9448            ignored-charsets))))
9449
9450 ;;;
9451 ;;; Mime Commands
9452 ;;;
9453
9454 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9455   "Display the current article buffer fully MIME-buttonized.
9456 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9457 treated as multipart/mixed."
9458   (interactive "P")
9459   (require 'gnus-art)
9460   (let ((gnus-unbuttonized-mime-types nil)
9461         (gnus-mime-display-multipart-as-mixed show-all-parts))
9462     (gnus-summary-show-article)))
9463
9464 (defun gnus-summary-repair-multipart (article)
9465   "Add a Content-Type header to a multipart article without one."
9466   (interactive (list (gnus-summary-article-number)))
9467   (gnus-with-article article
9468     (message-narrow-to-head)
9469     (goto-char (point-max))
9470     (widen)
9471     (when (search-forward "\n--" nil t)
9472       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9473         (message-narrow-to-head)
9474         (message-remove-header "Mime-Version")
9475         (message-remove-header "Content-Type")
9476         (goto-char (point-max))
9477         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9478                         separator))
9479         (insert "Mime-Version: 1.0\n")
9480         (widen))))
9481   (let (gnus-mark-article-hook)
9482     (gnus-summary-select-article t t nil article)))
9483
9484 (defun gnus-summary-toggle-display-buttonized ()
9485   "Toggle the buttonizing of the article buffer."
9486   (interactive)
9487   (require 'gnus-art)
9488   (if (setq gnus-inhibit-mime-unbuttonizing
9489             (not gnus-inhibit-mime-unbuttonizing))
9490       (let ((gnus-unbuttonized-mime-types nil))
9491         (gnus-summary-show-article))
9492     (gnus-summary-show-article)))
9493
9494 ;;;
9495 ;;; with article
9496 ;;;
9497
9498 (defmacro gnus-with-article (article &rest forms)
9499   "Select ARTICLE and perform FORMS in the original article buffer.
9500 Then replace the article with the result."
9501   `(progn
9502      ;; We don't want the article to be marked as read.
9503      (let (gnus-mark-article-hook)
9504        (gnus-summary-select-article t t nil ,article))
9505      (set-buffer gnus-original-article-buffer)
9506      ,@forms
9507      (if (not (gnus-check-backend-function
9508                'request-replace-article (car gnus-article-current)))
9509          (gnus-message 5 "Read-only group; not replacing")
9510        (unless (gnus-request-replace-article
9511                 ,article (car gnus-article-current)
9512                 (current-buffer) t)
9513          (error "Couldn't replace article")))
9514      ;; The cache and backlog have to be flushed somewhat.
9515      (when gnus-keep-backlog
9516        (gnus-backlog-remove-article
9517         (car gnus-article-current) (cdr gnus-article-current)))
9518      (when gnus-use-cache
9519        (gnus-cache-update-article
9520         (car gnus-article-current) (cdr gnus-article-current)))))
9521
9522 (put 'gnus-with-article 'lisp-indent-function 1)
9523 (put 'gnus-with-article 'edebug-form-spec '(form body))
9524
9525 ;;;
9526 ;;; Generic summary marking commands
9527 ;;;
9528
9529 (defvar gnus-summary-marking-alist
9530   '((read gnus-del-mark "d")
9531     (unread gnus-unread-mark "u")
9532     (ticked gnus-ticked-mark "!")
9533     (dormant gnus-dormant-mark "?")
9534     (expirable gnus-expirable-mark "e"))
9535   "An alist of names/marks/keystrokes.")
9536
9537 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9538 (defvar gnus-summary-mark-map)
9539
9540 (defun gnus-summary-make-all-marking-commands ()
9541   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9542   (dolist (elem gnus-summary-marking-alist)
9543     (apply 'gnus-summary-make-marking-command elem)))
9544
9545 (defun gnus-summary-make-marking-command (name mark keystroke)
9546   (let ((map (make-sparse-keymap)))
9547     (define-key gnus-summary-generic-mark-map keystroke map)
9548     (dolist (lway `((next "next" next nil "n")
9549                     (next-unread "next unread" next t "N")
9550                     (prev "previous" prev nil "p")
9551                     (prev-unread "previous unread" prev t "P")
9552                     (nomove "" nil nil ,keystroke)))
9553       (let ((func (gnus-summary-make-marking-command-1
9554                    mark (car lway) lway name)))
9555         (setq func (eval func))
9556         (define-key map (nth 4 lway) func)))))
9557       
9558 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9559   `(defun ,(intern
9560             (format "gnus-summary-put-mark-as-%s%s"
9561                     name (if (eq way 'nomove)
9562                              ""
9563                            (concat "-" (symbol-name way)))))
9564      (n)
9565      ,(format
9566        "Mark the current article as %s%s.
9567 If N, the prefix, then repeat N times.
9568 If N is negative, move in reverse order.
9569 The difference between N and the actual number of articles marked is
9570 returned."
9571        name (cadr lway))
9572      (interactive "p")
9573      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9574     
9575 (defun gnus-summary-generic-mark (n mark move unread)
9576   "Mark N articles with MARK."
9577   (unless (eq major-mode 'gnus-summary-mode)
9578     (error "This command can only be used in the summary buffer"))
9579   (gnus-summary-show-thread)
9580   (let ((nummove
9581          (cond
9582           ((eq move 'next) 1)
9583           ((eq move 'prev) -1)
9584           (t 0))))
9585     (if (zerop nummove)
9586         (setq n 1)
9587       (when (< n 0)
9588         (setq n (abs n)
9589               nummove (* -1 nummove))))
9590     (while (and (> n 0)
9591                 (gnus-summary-mark-article nil mark)
9592                 (zerop (gnus-summary-next-subject nummove unread t)))
9593       (setq n (1- n)))
9594     (when (/= 0 n)
9595       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9596     (gnus-summary-recenter)
9597     (gnus-summary-position-point)
9598     (gnus-set-mode-line 'summary)
9599     n))
9600
9601 (gnus-summary-make-all-marking-commands)
9602
9603 (gnus-ems-redefine)
9604
9605 (provide 'gnus-sum)
9606
9607 (run-hooks 'gnus-sum-load-hook)
9608
9609 ;;; gnus-sum.el ends here