*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, many commands will go to the next unread article.
172 This applies to marking commands as well as other commands that
173 \"naturally\" select the next article, like, for instance, `SPC' at
174 the end of an article.
175 If nil, only the marking commands will go to the next (un)read article.
176 If `never', commands that usually go to the next unread article, will
177 go to the next article, whether it is read or not."
178   :group 'gnus-summary-marks
179   :link '(custom-manual "(gnus)Setting Marks")
180   :type '(choice (const :tag "off" nil)
181                  (const never)
182                  (sexp :menu-tag "on" t)))
183
184 (defcustom gnus-summary-default-score 0
185   "*Default article score level.
186 All scores generated by the score files will be added to this score.
187 If this variable is nil, scoring will be disabled."
188   :group 'gnus-score-default
189   :type '(choice (const :tag "disable")
190                  integer))
191
192 (defcustom gnus-summary-zcore-fuzz 0
193   "*Fuzziness factor for the zcore in the summary buffer.
194 Articles with scores closer than this to `gnus-summary-default-score'
195 will not be marked."
196   :group 'gnus-summary-format
197   :type 'integer)
198
199 (defcustom gnus-simplify-subject-fuzzy-regexp nil
200   "*Strings to be removed when doing fuzzy matches.
201 This can either be a regular expression or list of regular expressions
202 that will be removed from subject strings if fuzzy subject
203 simplification is selected."
204   :group 'gnus-thread
205   :type '(repeat regexp))
206
207 (defcustom gnus-show-threads t
208   "*If non-nil, display threads in summary mode."
209   :group 'gnus-thread
210   :type 'boolean)
211
212 (defcustom gnus-thread-hide-subtree nil
213   "*If non-nil, hide all threads initially.
214 If threads are hidden, you have to run the command
215 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
216 to expose hidden threads."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-killed t
221   "*If non-nil, hide killed threads automatically."
222   :group 'gnus-thread
223   :type 'boolean)
224
225 (defcustom gnus-thread-ignore-subject t
226   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
227 If nil, articles that have different subjects from their parents will
228 start separate threads."
229   :group 'gnus-thread
230   :type 'boolean)
231
232 (defcustom gnus-thread-operation-ignore-subject t
233   "*If non-nil, subjects will be ignored when doing thread commands.
234 This affects commands like `gnus-summary-kill-thread' and
235 `gnus-summary-lower-thread'.
236
237 If this variable is nil, articles in the same thread with different
238 subjects will not be included in the operation in question.  If this
239 variable is `fuzzy', only articles that have subjects that are fuzzily
240 equal will be included."
241   :group 'gnus-thread
242   :type '(choice (const :tag "off" nil)
243                  (const fuzzy)
244                  (sexp :tag "on" t)))
245
246 (defcustom gnus-thread-indent-level 4
247   "*Number that says how much each sub-thread should be indented."
248   :group 'gnus-thread
249   :type 'integer)
250
251 (defcustom gnus-auto-extend-newsgroup t
252   "*If non-nil, extend newsgroup forward and backward when requested."
253   :group 'gnus-summary-choose
254   :type 'boolean)
255
256 (defcustom gnus-auto-select-first t
257   "*If nil, don't select the first unread article when entering a group.
258 If this variable is `best', select the highest-scored unread article
259 in the group.  If t, select the first unread article.
260
261 This variable can also be a function to place point on a likely
262 subject line.  Useful values include `gnus-summary-first-unread-subject',
263 `gnus-summary-first-unread-article' and
264 `gnus-summary-best-unread-article'.
265
266 If you want to prevent automatic selection of the first unread article
267 in some newsgroups, set the variable to nil in
268 `gnus-select-group-hook'."
269   :group 'gnus-group-select
270   :type '(choice (const :tag "none" nil)
271                  (const best)
272                  (sexp :menu-tag "first" t)
273                  (function-item gnus-summary-first-unread-subject)
274                  (function-item gnus-summary-first-unread-article)
275                  (function-item gnus-summary-best-unread-article)))
276
277 (defcustom gnus-auto-select-next t
278   "*If non-nil, offer to go to the next group from the end of the previous.
279 If the value is t and the next newsgroup is empty, Gnus will exit
280 summary mode and go back to group mode.  If the value is neither nil
281 nor t, Gnus will select the following unread newsgroup.  In
282 particular, if the value is the symbol `quietly', the next unread
283 newsgroup will be selected without any confirmation, and if it is
284 `almost-quietly', the next group will be selected without any
285 confirmation if you are located on the last article in the group.
286 Finally, if this variable is `slightly-quietly', the `Z n' command
287 will go to the next group without confirmation."
288   :group 'gnus-summary-maneuvering
289   :type '(choice (const :tag "off" nil)
290                  (const quietly)
291                  (const almost-quietly)
292                  (const slightly-quietly)
293                  (sexp :menu-tag "on" t)))
294
295 (defcustom gnus-auto-select-same nil
296   "*If non-nil, select the next article with the same subject.
297 If there are no more articles with the same subject, go to
298 the first unread article."
299   :group 'gnus-summary-maneuvering
300   :type 'boolean)
301
302 (defcustom gnus-summary-check-current nil
303   "*If non-nil, consider the current article when moving.
304 The \"unread\" movement commands will stay on the same line if the
305 current article is unread."
306   :group 'gnus-summary-maneuvering
307   :type 'boolean)
308
309 (defcustom gnus-auto-center-summary t
310   "*If non-nil, always center the current summary buffer.
311 In particular, if `vertical' do only vertical recentering.  If non-nil
312 and non-`vertical', do both horizontal and vertical recentering."
313   :group 'gnus-summary-maneuvering
314   :type '(choice (const :tag "none" nil)
315                  (const vertical)
316                  (integer :tag "height")
317                  (sexp :menu-tag "both" t)))
318
319 (defcustom gnus-show-all-headers nil
320   "*If non-nil, don't hide any headers."
321   :group 'gnus-article-hiding
322   :group 'gnus-article-headers
323   :type 'boolean)
324
325 (defcustom gnus-summary-ignore-duplicates nil
326   "*If non-nil, ignore articles with identical Message-ID headers."
327   :group 'gnus-summary
328   :type 'boolean)
329
330 (defcustom gnus-single-article-buffer t
331   "*If non-nil, display all articles in the same buffer.
332 If nil, each group will get its own article buffer."
333   :group 'gnus-article-various
334   :type 'boolean)
335
336 (defcustom gnus-break-pages t
337   "*If non-nil, do page breaking on articles.
338 The page delimiter is specified by the `gnus-page-delimiter'
339 variable."
340   :group 'gnus-article-various
341   :type 'boolean)
342
343 (defcustom gnus-move-split-methods nil
344   "*Variable used to suggest where articles are to be moved to.
345 It uses the same syntax as the `gnus-split-methods' variable."
346   :group 'gnus-summary-mail
347   :type '(repeat (choice (list :value (fun) function)
348                          (cons :value ("" "") regexp (repeat string))
349                          (sexp :value nil))))
350
351 (defcustom gnus-unread-mark ?  ;Whitespace
352   "*Mark used for unread articles."
353   :group 'gnus-summary-marks
354   :type 'character)
355
356 (defcustom gnus-ticked-mark ?!
357   "*Mark used for ticked articles."
358   :group 'gnus-summary-marks
359   :type 'character)
360
361 (defcustom gnus-dormant-mark ??
362   "*Mark used for dormant articles."
363   :group 'gnus-summary-marks
364   :type 'character)
365
366 (defcustom gnus-del-mark ?r
367   "*Mark used for del'd articles."
368   :group 'gnus-summary-marks
369   :type 'character)
370
371 (defcustom gnus-read-mark ?R
372   "*Mark used for read articles."
373   :group 'gnus-summary-marks
374   :type 'character)
375
376 (defcustom gnus-expirable-mark ?E
377   "*Mark used for expirable articles."
378   :group 'gnus-summary-marks
379   :type 'character)
380
381 (defcustom gnus-killed-mark ?K
382   "*Mark used for killed articles."
383   :group 'gnus-summary-marks
384   :type 'character)
385
386 (defcustom gnus-souped-mark ?F
387   "*Mark used for killed articles."
388   :group 'gnus-summary-marks
389   :type 'character)
390
391 (defcustom gnus-kill-file-mark ?X
392   "*Mark used for articles killed by kill files."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-low-score-mark ?Y
397   "*Mark used for articles with a low score."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-catchup-mark ?C
402   "*Mark used for articles that are caught up."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-replied-mark ?A
407   "*Mark used for articles that have been replied to."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-cached-mark ?*
412   "*Mark used for articles that are in the cache."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-saved-mark ?S
417   "*Mark used for articles that have been saved to."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-ancient-mark ?O
422   "*Mark used for ancient articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-sparse-mark ?Q
427   "*Mark used for sparsely reffed articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-canceled-mark ?G
432   "*Mark used for canceled articles."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-duplicate-mark ?M
437   "*Mark used for duplicate articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-undownloaded-mark ?@
442   "*Mark used for articles that weren't downloaded."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-downloadable-mark ?%
447   "*Mark used for articles that are to be downloaded."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-unsendable-mark ?=
452   "*Mark used for articles that won't be sent."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-score-over-mark ?+
457   "*Score mark used for articles with high scores."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-score-below-mark ?-
462   "*Score mark used for articles with low scores."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-empty-thread-mark ?  ;Whitespace
467   "*There is no thread under the article."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-not-empty-thread-mark ?=
472   "*There is a thread under the article."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-view-pseudo-asynchronously nil
477   "*If non-nil, Gnus will view pseudo-articles asynchronously."
478   :group 'gnus-extract-view
479   :type 'boolean)
480
481 (defcustom gnus-auto-expirable-marks
482   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
483         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
484         gnus-souped-mark gnus-duplicate-mark)
485   "*The list of marks converted into expiration if a group is auto-expirable."
486   :group 'gnus-summary
487   :type '(repeat character))
488
489 (defcustom gnus-inhibit-user-auto-expire t
490   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
491   :group 'gnus-summary
492   :type 'boolean)
493
494 (defcustom gnus-view-pseudos nil
495   "*If `automatic', pseudo-articles will be viewed automatically.
496 If `not-confirm', pseudos will be viewed automatically, and the user
497 will not be asked to confirm the command."
498   :group 'gnus-extract-view
499   :type '(choice (const :tag "off" nil)
500                  (const automatic)
501                  (const not-confirm)))
502
503 (defcustom gnus-view-pseudos-separately t
504   "*If non-nil, one pseudo-article will be created for each file to be viewed.
505 If nil, all files that use the same viewing command will be given as a
506 list of parameters to that command."
507   :group 'gnus-extract-view
508   :type 'boolean)
509
510 (defcustom gnus-insert-pseudo-articles t
511   "*If non-nil, insert pseudo-articles when decoding articles."
512   :group 'gnus-extract-view
513   :type 'boolean)
514
515 (defcustom gnus-summary-dummy-line-format
516   "  %(:                          :%) %S\n"
517   "*The format specification for the dummy roots in the summary buffer.
518 It works along the same lines as a normal formatting string,
519 with some simple extensions.
520
521 %S  The subject"
522   :group 'gnus-threading
523   :type 'string)
524
525 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
526   "*The format specification for the summary mode line.
527 It works along the same lines as a normal formatting string,
528 with some simple extensions:
529
530 %G  Group name
531 %p  Unprefixed group name
532 %A  Current article number
533 %z  Current article score
534 %V  Gnus version
535 %U  Number of unread articles in the group
536 %e  Number of unselected articles in the group
537 %Z  A string with unread/unselected article counts
538 %g  Shortish group name
539 %S  Subject of the current article
540 %u  User-defined spec
541 %s  Current score file name
542 %d  Number of dormant articles
543 %r  Number of articles that have been marked as read in this session
544 %E  Number of articles expunged by the score files"
545   :group 'gnus-summary-format
546   :type 'string)
547
548 (defcustom gnus-list-identifiers nil
549   "Regexp that matches list identifiers to be removed from subject.
550 This can also be a list of regexps."
551   :group 'gnus-summary-format
552   :group 'gnus-article-hiding
553   :type '(choice (const :tag "none" nil)
554                  (regexp :value ".*")
555                  (repeat :value (".*") regexp)))
556
557 (defcustom gnus-summary-mark-below 0
558   "*Mark all articles with a score below this variable as read.
559 This variable is local to each summary buffer and usually set by the
560 score file."
561   :group 'gnus-score-default
562   :type 'integer)
563
564 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
565   "*List of functions used for sorting articles in the summary buffer.
566 This variable is only used when not using a threaded display."
567   :group 'gnus-summary-sort
568   :type '(repeat (choice (function-item gnus-article-sort-by-number)
569                          (function-item gnus-article-sort-by-author)
570                          (function-item gnus-article-sort-by-subject)
571                          (function-item gnus-article-sort-by-date)
572                          (function-item gnus-article-sort-by-score)
573                          (function :tag "other"))))
574
575 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
576   "*List of functions used for sorting threads in the summary buffer.
577 By default, threads are sorted by article number.
578
579 Each function takes two threads and return non-nil if the first thread
580 should be sorted before the other.  If you use more than one function,
581 the primary sort function should be the last.  You should probably
582 always include `gnus-thread-sort-by-number' in the list of sorting
583 functions -- preferably first.
584
585 Ready-made functions include `gnus-thread-sort-by-number',
586 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
587 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
588 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
589   :group 'gnus-summary-sort
590   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
591                          (function-item gnus-thread-sort-by-author)
592                          (function-item gnus-thread-sort-by-subject)
593                          (function-item gnus-thread-sort-by-date)
594                          (function-item gnus-thread-sort-by-score)
595                          (function-item gnus-thread-sort-by-total-score)
596                          (function :tag "other"))))
597
598 (defcustom gnus-thread-score-function '+
599   "*Function used for calculating the total score of a thread.
600
601 The function is called with the scores of the article and each
602 subthread and should then return the score of the thread.
603
604 Some functions you can use are `+', `max', or `min'."
605   :group 'gnus-summary-sort
606   :type 'function)
607
608 (defcustom gnus-summary-expunge-below nil
609   "All articles that have a score less than this variable will be expunged.
610 This variable is local to the summary buffers."
611   :group 'gnus-score-default
612   :type '(choice (const :tag "off" nil)
613                  integer))
614
615 (defcustom gnus-thread-expunge-below nil
616   "All threads that have a total score less than this variable will be expunged.
617 See `gnus-thread-score-function' for en explanation of what a
618 \"thread score\" is.
619
620 This variable is local to the summary buffers."
621   :group 'gnus-threading
622   :group 'gnus-score-default
623   :type '(choice (const :tag "off" nil)
624                  integer))
625
626 (defcustom gnus-summary-mode-hook nil
627   "*A hook for Gnus summary mode.
628 This hook is run before any variables are set in the summary buffer."
629   :group 'gnus-summary-various
630   :type 'hook)
631
632 (defcustom gnus-summary-menu-hook nil
633   "*Hook run after the creation of the summary mode menu."
634   :group 'gnus-summary-visual
635   :type 'hook)
636
637 (defcustom gnus-summary-exit-hook nil
638   "*A hook called on exit from the summary buffer.
639 It will be called with point in the group buffer."
640   :group 'gnus-summary-exit
641   :type 'hook)
642
643 (defcustom gnus-summary-prepare-hook nil
644   "*A hook called after the summary buffer has been generated.
645 If you want to modify the summary buffer, you can use this hook."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-summary-prepared-hook nil
650   "*A hook called as the last thing after the summary buffer has been generated."
651   :group 'gnus-summary-various
652   :type 'hook)
653
654 (defcustom gnus-summary-generate-hook nil
655   "*A hook run just before generating the summary buffer.
656 This hook is commonly used to customize threading variables and the
657 like."
658   :group 'gnus-summary-various
659   :type 'hook)
660
661 (defcustom gnus-select-group-hook nil
662   "*A hook called when a newsgroup is selected.
663
664 If you'd like to simplify subjects like the
665 `gnus-summary-next-same-subject' command does, you can use the
666 following hook:
667
668  (setq gnus-select-group-hook
669       (list
670         (lambda ()
671           (mapcar (lambda (header)
672                      (mail-header-set-subject
673                       header
674                       (gnus-simplify-subject
675                        (mail-header-subject header) 're-only)))
676                   gnus-newsgroup-headers))))"
677   :group 'gnus-group-select
678   :type 'hook)
679
680 (defcustom gnus-select-article-hook nil
681   "*A hook called when an article is selected."
682   :group 'gnus-summary-choose
683   :type 'hook)
684
685 (defcustom gnus-visual-mark-article-hook
686   (list 'gnus-highlight-selected-summary)
687   "*Hook run after selecting an article in the summary buffer.
688 It is meant to be used for highlighting the article in some way.  It
689 is not run if `gnus-visual' is nil."
690   :group 'gnus-summary-visual
691   :type 'hook)
692
693 (defcustom gnus-parse-headers-hook nil
694   "*A hook called before parsing the headers."
695   :group 'gnus-various
696   :type 'hook)
697
698 (defcustom gnus-exit-group-hook nil
699   "*A hook called when exiting summary mode.
700 This hook is not called from the non-updating exit commands like `Q'."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-summary-update-hook
705   (list 'gnus-summary-highlight-line)
706   "*A hook called when a summary line is changed.
707 The hook will not be called if `gnus-visual' is nil.
708
709 The default function `gnus-summary-highlight-line' will
710 highlight the line according to the `gnus-summary-highlight'
711 variable."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
716   "*A hook called when an article is selected for the first time.
717 The hook is intended to mark an article as read (or unread)
718 automatically when it is selected."
719   :group 'gnus-summary-choose
720   :type 'hook)
721
722 (defcustom gnus-group-no-more-groups-hook nil
723   "*A hook run when returning to group mode having no more (unread) groups."
724   :group 'gnus-group-select
725   :type 'hook)
726
727 (defcustom gnus-ps-print-hook nil
728   "*A hook run before ps-printing something from Gnus."
729   :group 'gnus-summary
730   :type 'hook)
731
732 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
733   "Face used for highlighting the current article in the summary buffer."
734   :group 'gnus-summary-visual
735   :type 'face)
736
737 (defcustom gnus-summary-highlight
738   '(((= mark gnus-canceled-mark)
739      . gnus-summary-cancelled-face)
740     ((and (> score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-high-ticked-face)
744     ((and (< score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-low-ticked-face)
748     ((or (= mark gnus-dormant-mark)
749          (= mark gnus-ticked-mark))
750      . gnus-summary-normal-ticked-face)
751     ((and (> score default) (= mark gnus-ancient-mark))
752      . gnus-summary-high-ancient-face)
753     ((and (< score default) (= mark gnus-ancient-mark))
754      . gnus-summary-low-ancient-face)
755     ((= mark gnus-ancient-mark)
756      . gnus-summary-normal-ancient-face)
757     ((and (> score default) (= mark gnus-unread-mark))
758      . gnus-summary-high-unread-face)
759     ((and (< score default) (= mark gnus-unread-mark))
760      . gnus-summary-low-unread-face)
761     ((= mark gnus-unread-mark)
762      . gnus-summary-normal-unread-face)
763     ((and (> score default) (memq mark (list gnus-downloadable-mark
764                                              gnus-undownloaded-mark)))
765      . gnus-summary-high-unread-face)
766     ((and (< score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-low-unread-face)
769     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
770      . gnus-summary-normal-unread-face)
771     ((> score default)
772      . gnus-summary-high-read-face)
773     ((< score default)
774      . gnus-summary-low-read-face)
775     (t
776      . gnus-summary-normal-read-face))
777   "*Controls the highlighting of summary buffer lines.
778
779 A list of (FORM . FACE) pairs.  When deciding how a a particular
780 summary line should be displayed, each form is evaluated.  The content
781 of the face field after the first true form is used.  You can change
782 how those summary lines are displayed, by editing the face field.
783
784 You can use the following variables in the FORM field.
785
786 score:   The articles score
787 default: The default article score.
788 below:   The score below which articles are automatically marked as read.
789 mark:    The articles mark."
790   :group 'gnus-summary-visual
791   :type '(repeat (cons (sexp :tag "Form" nil)
792                        face)))
793
794 (defcustom gnus-alter-header-function nil
795   "Function called to allow alteration of article header structures.
796 The function is called with one parameter, the article header vector,
797 which it may alter in any way.")
798
799 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
800   "Variable that says which function should be used to decode a string with encoded words.")
801
802 (defcustom gnus-extra-headers nil
803   "*Extra headers to parse."
804   :group 'gnus-summary
805   :type '(repeat symbol))
806
807 (defcustom gnus-ignored-from-addresses
808   (and user-mail-address (regexp-quote user-mail-address))
809   "*Regexp of From headers that may be suppressed in favor of To headers."
810   :group 'gnus-summary
811   :type 'regexp)
812
813 (defcustom gnus-group-charset-alist
814   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
815     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
816     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
817     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
818     ("^relcom\\>" koi8-r)
819     ("^fido7\\>" koi8-r)
820     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
821     ("^israel\\>" iso-8859-1)
822     ("^han\\>" euc-kr)
823     ("^alt.chinese.text.big5\\>" chinese-big5)
824     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
825     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
826     (".*" iso-8859-1))
827   "Alist of regexps (to match group names) and default charsets to be used when reading."
828   :type '(repeat (list (regexp :tag "Group")
829                        (symbol :tag "Charset")))
830   :group 'gnus-charset)
831
832 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
833   "List of charsets that should be ignored.
834 When these charsets are used in the \"charset\" parameter, the
835 default charset will be used instead."
836   :type '(repeat symbol)
837   :group 'gnus-charset)
838
839 (defcustom gnus-group-ignored-charsets-alist 
840   '(("alt\\.chinese\\.text" iso-8859-1))
841   "Alist of regexps (to match group names) and charsets that should be ignored.
842 When these charsets are used in the \"charset\" parameter, the
843 default charset will be used instead."
844   :type '(repeat (cons (regexp :tag "Group")
845                        (repeat symbol)))
846   :group 'gnus-charset)
847
848 (defcustom gnus-group-highlight-words-alist nil
849   "Alist of group regexps and highlight regexps.
850 This variable uses the same syntax as `gnus-emphasis-alist'."
851   :type '(repeat (cons (regexp :tag "Group")
852                        (repeat (list (regexp :tag "Highlight regexp")
853                                      (number :tag "Group for entire word" 0)
854                                      (number :tag "Group for displayed part" 0)
855                                      (symbol :tag "Face" 
856                                              gnus-emphasis-highlight-words)))))
857   :group 'gnus-summary-visual)
858
859 (defcustom gnus-summary-show-article-charset-alist
860   nil
861   "Alist of number and charset.
862 The article will be shown with the charset corresponding to the
863 numbered argument.
864 For example: ((1 . cn-gb-2312) (2 . big5))."
865   :type '(repeat (cons (number :tag "Argument" 1)
866                        (symbol :tag "Charset")))
867   :group 'gnus-charset)
868
869 (defcustom gnus-preserve-marks t
870   "Whether marks are preserved when moving, copying and respooling messages."
871   :type 'boolean
872   :group 'gnus-summary-marks)
873
874 ;;; Internal variables
875
876 (defvar gnus-article-mime-handles nil)
877 (defvar gnus-article-decoded-p nil)
878 (defvar gnus-scores-exclude-files nil)
879 (defvar gnus-page-broken nil)
880 (defvar gnus-inhibit-mime-unbuttonizing nil)
881
882 (defvar gnus-original-article nil)
883 (defvar gnus-article-internal-prepare-hook nil)
884 (defvar gnus-newsgroup-process-stack nil)
885
886 (defvar gnus-thread-indent-array nil)
887 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
888 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
889   "Function called to sort the articles within a thread after it has been gathered together.")
890
891 ;; Avoid highlighting in kill files.
892 (defvar gnus-summary-inhibit-highlight nil)
893 (defvar gnus-newsgroup-selected-overlay nil)
894 (defvar gnus-inhibit-limiting nil)
895 (defvar gnus-newsgroup-adaptive-score-file nil)
896 (defvar gnus-current-score-file nil)
897 (defvar gnus-current-move-group nil)
898 (defvar gnus-current-copy-group nil)
899 (defvar gnus-current-crosspost-group nil)
900
901 (defvar gnus-newsgroup-dependencies nil)
902 (defvar gnus-newsgroup-adaptive nil)
903 (defvar gnus-summary-display-article-function nil)
904 (defvar gnus-summary-highlight-line-function nil
905   "Function called after highlighting a summary line.")
906
907 (defvar gnus-summary-line-format-alist
908   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
909     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
910     (?s gnus-tmp-subject-or-nil ?s)
911     (?n gnus-tmp-name ?s)
912     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
913         ?s)
914     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
915             gnus-tmp-from) ?s)
916     (?F gnus-tmp-from ?s)
917     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
918     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
919     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
920     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
921     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
922     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
923     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
924     (?L gnus-tmp-lines ?d)
925     (?I gnus-tmp-indentation ?s)
926     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
927     (?R gnus-tmp-replied ?c)
928     (?\[ gnus-tmp-opening-bracket ?c)
929     (?\] gnus-tmp-closing-bracket ?c)
930     (?\> (make-string gnus-tmp-level ? ) ?s)
931     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
932     (?i gnus-tmp-score ?d)
933     (?z gnus-tmp-score-char ?c)
934     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
935     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
936     (?U gnus-tmp-unread ?c)
937     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
938     (?t (gnus-summary-number-of-articles-in-thread
939          (and (boundp 'thread) (car thread)) gnus-tmp-level)
940         ?d)
941     (?e (gnus-summary-number-of-articles-in-thread
942          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
943         ?c)
944     (?u gnus-tmp-user-defined ?s)
945     (?P (gnus-pick-line-number) ?d))
946   "An alist of format specifications that can appear in summary lines,
947 and what variables they correspond with, along with the type of the
948 variable (string, integer, character, etc).")
949
950 (defvar gnus-summary-dummy-line-format-alist
951   `((?S gnus-tmp-subject ?s)
952     (?N gnus-tmp-number ?d)
953     (?u gnus-tmp-user-defined ?s)))
954
955 (defvar gnus-summary-mode-line-format-alist
956   `((?G gnus-tmp-group-name ?s)
957     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
958     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
959     (?A gnus-tmp-article-number ?d)
960     (?Z gnus-tmp-unread-and-unselected ?s)
961     (?V gnus-version ?s)
962     (?U gnus-tmp-unread-and-unticked ?d)
963     (?S gnus-tmp-subject ?s)
964     (?e gnus-tmp-unselected ?d)
965     (?u gnus-tmp-user-defined ?s)
966     (?d (length gnus-newsgroup-dormant) ?d)
967     (?t (length gnus-newsgroup-marked) ?d)
968     (?r (length gnus-newsgroup-reads) ?d)
969     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
970     (?E gnus-newsgroup-expunged-tally ?d)
971     (?s (gnus-current-score-file-nondirectory) ?s)))
972
973 (defvar gnus-last-search-regexp nil
974   "Default regexp for article search command.")
975
976 (defvar gnus-last-shell-command nil
977   "Default shell command on article.")
978
979 (defvar gnus-newsgroup-begin nil)
980 (defvar gnus-newsgroup-end nil)
981 (defvar gnus-newsgroup-last-rmail nil)
982 (defvar gnus-newsgroup-last-mail nil)
983 (defvar gnus-newsgroup-last-folder nil)
984 (defvar gnus-newsgroup-last-file nil)
985 (defvar gnus-newsgroup-auto-expire nil)
986 (defvar gnus-newsgroup-active nil)
987
988 (defvar gnus-newsgroup-data nil)
989 (defvar gnus-newsgroup-data-reverse nil)
990 (defvar gnus-newsgroup-limit nil)
991 (defvar gnus-newsgroup-limits nil)
992
993 (defvar gnus-newsgroup-unreads nil
994   "List of unread articles in the current newsgroup.")
995
996 (defvar gnus-newsgroup-unselected nil
997   "List of unselected unread articles in the current newsgroup.")
998
999 (defvar gnus-newsgroup-reads nil
1000   "Alist of read articles and article marks in the current newsgroup.")
1001
1002 (defvar gnus-newsgroup-expunged-tally nil)
1003
1004 (defvar gnus-newsgroup-marked nil
1005   "List of ticked articles in the current newsgroup (a subset of unread art).")
1006
1007 (defvar gnus-newsgroup-killed nil
1008   "List of ranges of articles that have been through the scoring process.")
1009
1010 (defvar gnus-newsgroup-cached nil
1011   "List of articles that come from the article cache.")
1012
1013 (defvar gnus-newsgroup-saved nil
1014   "List of articles that have been saved.")
1015
1016 (defvar gnus-newsgroup-kill-headers nil)
1017
1018 (defvar gnus-newsgroup-replied nil
1019   "List of articles that have been replied to in the current newsgroup.")
1020
1021 (defvar gnus-newsgroup-expirable nil
1022   "List of articles in the current newsgroup that can be expired.")
1023
1024 (defvar gnus-newsgroup-processable nil
1025   "List of articles in the current newsgroup that can be processed.")
1026
1027 (defvar gnus-newsgroup-downloadable nil
1028   "List of articles in the current newsgroup that can be processed.")
1029
1030 (defvar gnus-newsgroup-undownloaded nil
1031   "List of articles in the current newsgroup that haven't been downloaded..")
1032
1033 (defvar gnus-newsgroup-unsendable nil
1034   "List of articles in the current newsgroup that won't be sent.")
1035
1036 (defvar gnus-newsgroup-bookmarks nil
1037   "List of articles in the current newsgroup that have bookmarks.")
1038
1039 (defvar gnus-newsgroup-dormant nil
1040   "List of dormant articles in the current newsgroup.")
1041
1042 (defvar gnus-newsgroup-scored nil
1043   "List of scored articles in the current newsgroup.")
1044
1045 (defvar gnus-newsgroup-headers nil
1046   "List of article headers in the current newsgroup.")
1047
1048 (defvar gnus-newsgroup-threads nil)
1049
1050 (defvar gnus-newsgroup-prepared nil
1051   "Whether the current group has been prepared properly.")
1052
1053 (defvar gnus-newsgroup-ancient nil
1054   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-sparse nil)
1057
1058 (defvar gnus-current-article nil)
1059 (defvar gnus-article-current nil)
1060 (defvar gnus-current-headers nil)
1061 (defvar gnus-have-all-headers nil)
1062 (defvar gnus-last-article nil)
1063 (defvar gnus-newsgroup-history nil)
1064 (defvar gnus-newsgroup-charset nil)
1065 (defvar gnus-newsgroup-ephemeral-charset nil)
1066 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1067
1068 (defconst gnus-summary-local-variables
1069   '(gnus-newsgroup-name
1070     gnus-newsgroup-begin gnus-newsgroup-end
1071     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1072     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1073     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1074     gnus-newsgroup-unselected gnus-newsgroup-marked
1075     gnus-newsgroup-reads gnus-newsgroup-saved
1076     gnus-newsgroup-replied gnus-newsgroup-expirable
1077     gnus-newsgroup-processable gnus-newsgroup-killed
1078     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1079     gnus-newsgroup-unsendable
1080     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1081     gnus-newsgroup-headers gnus-newsgroup-threads
1082     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1083     gnus-current-article gnus-current-headers gnus-have-all-headers
1084     gnus-last-article gnus-article-internal-prepare-hook
1085     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1086     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1087     gnus-thread-expunge-below
1088     gnus-score-alist gnus-current-score-file
1089     (gnus-summary-expunge-below . global)
1090     (gnus-summary-mark-below . global)
1091     gnus-newsgroup-active gnus-scores-exclude-files
1092     gnus-newsgroup-history gnus-newsgroup-ancient
1093     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1094     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1095     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1096     (gnus-newsgroup-expunged-tally . 0)
1097     gnus-cache-removable-articles gnus-newsgroup-cached
1098     gnus-newsgroup-data gnus-newsgroup-data-reverse
1099     gnus-newsgroup-limit gnus-newsgroup-limits
1100     gnus-newsgroup-charset)
1101   "Variables that are buffer-local to the summary buffers.")
1102
1103 ;; Byte-compiler warning.
1104 (defvar gnus-article-mode-map)
1105
1106 ;; MIME stuff.
1107
1108 (defvar gnus-decode-encoded-word-methods
1109   '(mail-decode-encoded-word-string)
1110   "List of methods used to decode encoded words.
1111
1112 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1113 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1114 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1115 whose names match REGEXP.
1116
1117 For example:
1118 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1119  mail-decode-encoded-word-string
1120  (\"chinese\" . rfc1843-decode-string))
1121 ")
1122
1123 (defvar gnus-decode-encoded-word-methods-cache nil)
1124
1125 (defun gnus-multi-decode-encoded-word-string (string)
1126   "Apply the functions from `gnus-encoded-word-methods' that match."
1127   (unless (and gnus-decode-encoded-word-methods-cache
1128                (eq gnus-newsgroup-name
1129                    (car gnus-decode-encoded-word-methods-cache)))
1130     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1131     (mapcar (lambda (x)
1132               (if (symbolp x)
1133                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1134                 (if (and gnus-newsgroup-name
1135                          (string-match (car x) gnus-newsgroup-name))
1136                     (nconc gnus-decode-encoded-word-methods-cache
1137                            (list (cdr x))))))
1138           gnus-decode-encoded-word-methods))
1139   (let ((xlist gnus-decode-encoded-word-methods-cache))
1140     (pop xlist)
1141     (while xlist
1142       (setq string (funcall (pop xlist) string))))
1143   string)
1144
1145 ;; Subject simplification.
1146
1147 (defun gnus-simplify-whitespace (str)
1148   "Remove excessive whitespace."
1149   (let ((mystr str))
1150     ;; Multiple spaces.
1151     (while (string-match "[ \t][ \t]+" mystr)
1152       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1153                           " "
1154                           (substring mystr (match-end 0)))))
1155     ;; Leading spaces.
1156     (when (string-match "^[ \t]+" mystr)
1157       (setq mystr (substring mystr (match-end 0))))
1158     ;; Trailing spaces.
1159     (when (string-match "[ \t]+$" mystr)
1160       (setq mystr (substring mystr 0 (match-beginning 0))))
1161     mystr))
1162
1163 (defsubst gnus-simplify-subject-re (subject)
1164   "Remove \"Re:\" from subject lines."
1165   (if (string-match "^[Rr][Ee]: *" subject)
1166       (substring subject (match-end 0))
1167     subject))
1168
1169 (defun gnus-simplify-subject (subject &optional re-only)
1170   "Remove `Re:' and words in parentheses.
1171 If RE-ONLY is non-nil, strip leading `Re:'s only."
1172   (let ((case-fold-search t))           ;Ignore case.
1173     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1174     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1175       (setq subject (substring subject (match-end 0))))
1176     ;; Remove uninteresting prefixes.
1177     (when (and (not re-only)
1178                gnus-simplify-ignored-prefixes
1179                (string-match gnus-simplify-ignored-prefixes subject))
1180       (setq subject (substring subject (match-end 0))))
1181     ;; Remove words in parentheses from end.
1182     (unless re-only
1183       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1184         (setq subject (substring subject 0 (match-beginning 0)))))
1185     ;; Return subject string.
1186     subject))
1187
1188 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1189 ;; all whitespace.
1190 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1191   (goto-char (point-min))
1192   (while (re-search-forward regexp nil t)
1193     (replace-match (or newtext ""))))
1194
1195 (defun gnus-simplify-buffer-fuzzy ()
1196   "Simplify string in the buffer fuzzily.
1197 The string in the accessible portion of the current buffer is simplified.
1198 It is assumed to be a single-line subject.
1199 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1200 matter is removed.  Additional things can be deleted by setting
1201 gnus-simplify-subject-fuzzy-regexp."
1202   (let ((case-fold-search t)
1203         (modified-tick))
1204     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1205
1206     (while (not (eq modified-tick (buffer-modified-tick)))
1207       (setq modified-tick (buffer-modified-tick))
1208       (cond
1209        ((listp gnus-simplify-subject-fuzzy-regexp)
1210         (mapcar 'gnus-simplify-buffer-fuzzy-step
1211                 gnus-simplify-subject-fuzzy-regexp))
1212        (gnus-simplify-subject-fuzzy-regexp
1213         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1214       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1215       (gnus-simplify-buffer-fuzzy-step
1216        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1217       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1218
1219     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1220     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1221     (gnus-simplify-buffer-fuzzy-step " $")
1222     (gnus-simplify-buffer-fuzzy-step "^ +")))
1223
1224 (defun gnus-simplify-subject-fuzzy (subject)
1225   "Simplify a subject string fuzzily.
1226 See `gnus-simplify-buffer-fuzzy' for details."
1227   (save-excursion
1228     (gnus-set-work-buffer)
1229     (let ((case-fold-search t))
1230       ;; Remove uninteresting prefixes.
1231       (when (and gnus-simplify-ignored-prefixes
1232                  (string-match gnus-simplify-ignored-prefixes subject))
1233         (setq subject (substring subject (match-end 0))))
1234       (insert subject)
1235       (inline (gnus-simplify-buffer-fuzzy))
1236       (buffer-string))))
1237
1238 (defsubst gnus-simplify-subject-fully (subject)
1239   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1240   (cond
1241    (gnus-simplify-subject-functions
1242     (gnus-map-function gnus-simplify-subject-functions subject))
1243    ((null gnus-summary-gather-subject-limit)
1244     (gnus-simplify-subject-re subject))
1245    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1246     (gnus-simplify-subject-fuzzy subject))
1247    ((numberp gnus-summary-gather-subject-limit)
1248     (gnus-limit-string (gnus-simplify-subject-re subject)
1249                        gnus-summary-gather-subject-limit))
1250    (t
1251     subject)))
1252
1253 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1254   "Check whether two subjects are equal.
1255 If optional argument simple-first is t, first argument is already
1256 simplified."
1257   (cond
1258    ((null simple-first)
1259     (equal (gnus-simplify-subject-fully s1)
1260            (gnus-simplify-subject-fully s2)))
1261    (t
1262     (equal s1
1263            (gnus-simplify-subject-fully s2)))))
1264
1265 (defun gnus-summary-bubble-group ()
1266   "Increase the score of the current group.
1267 This is a handy function to add to `gnus-summary-exit-hook' to
1268 increase the score of each group you read."
1269   (gnus-group-add-score gnus-newsgroup-name))
1270
1271 \f
1272 ;;;
1273 ;;; Gnus summary mode
1274 ;;;
1275
1276 (put 'gnus-summary-mode 'mode-class 'special)
1277
1278 (when t
1279   ;; Non-orthogonal keys
1280
1281   (gnus-define-keys gnus-summary-mode-map
1282     " " gnus-summary-next-page
1283     "\177" gnus-summary-prev-page
1284     [delete] gnus-summary-prev-page
1285     [backspace] gnus-summary-prev-page
1286     "\r" gnus-summary-scroll-up
1287     "\M-\r" gnus-summary-scroll-down
1288     "n" gnus-summary-next-unread-article
1289     "p" gnus-summary-prev-unread-article
1290     "N" gnus-summary-next-article
1291     "P" gnus-summary-prev-article
1292     "\M-\C-n" gnus-summary-next-same-subject
1293     "\M-\C-p" gnus-summary-prev-same-subject
1294     "\M-n" gnus-summary-next-unread-subject
1295     "\M-p" gnus-summary-prev-unread-subject
1296     "." gnus-summary-first-unread-article
1297     "," gnus-summary-best-unread-article
1298     "\M-s" gnus-summary-search-article-forward
1299     "\M-r" gnus-summary-search-article-backward
1300     "<" gnus-summary-beginning-of-article
1301     ">" gnus-summary-end-of-article
1302     "j" gnus-summary-goto-article
1303     "^" gnus-summary-refer-parent-article
1304     "\M-^" gnus-summary-refer-article
1305     "u" gnus-summary-tick-article-forward
1306     "!" gnus-summary-tick-article-forward
1307     "U" gnus-summary-tick-article-backward
1308     "d" gnus-summary-mark-as-read-forward
1309     "D" gnus-summary-mark-as-read-backward
1310     "E" gnus-summary-mark-as-expirable
1311     "\M-u" gnus-summary-clear-mark-forward
1312     "\M-U" gnus-summary-clear-mark-backward
1313     "k" gnus-summary-kill-same-subject-and-select
1314     "\C-k" gnus-summary-kill-same-subject
1315     "\M-\C-k" gnus-summary-kill-thread
1316     "\M-\C-l" gnus-summary-lower-thread
1317     "e" gnus-summary-edit-article
1318     "#" gnus-summary-mark-as-processable
1319     "\M-#" gnus-summary-unmark-as-processable
1320     "\M-\C-t" gnus-summary-toggle-threads
1321     "\M-\C-s" gnus-summary-show-thread
1322     "\M-\C-h" gnus-summary-hide-thread
1323     "\M-\C-f" gnus-summary-next-thread
1324     "\M-\C-b" gnus-summary-prev-thread
1325     "\M-\C-u" gnus-summary-up-thread
1326     "\M-\C-d" gnus-summary-down-thread
1327     "&" gnus-summary-execute-command
1328     "c" gnus-summary-catchup-and-exit
1329     "\C-w" gnus-summary-mark-region-as-read
1330     "\C-t" gnus-summary-toggle-truncation
1331     "?" gnus-summary-mark-as-dormant
1332     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1333     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1334     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1335     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1336     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1337     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1338     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1339     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1340     "=" gnus-summary-expand-window
1341     "\C-x\C-s" gnus-summary-reselect-current-group
1342     "\M-g" gnus-summary-rescan-group
1343     "w" gnus-summary-stop-page-breaking
1344     "\C-c\C-r" gnus-summary-caesar-message
1345     "f" gnus-summary-followup
1346     "F" gnus-summary-followup-with-original
1347     "C" gnus-summary-cancel-article
1348     "r" gnus-summary-reply
1349     "R" gnus-summary-reply-with-original
1350     "\C-c\C-f" gnus-summary-mail-forward
1351     "o" gnus-summary-save-article
1352     "\C-o" gnus-summary-save-article-mail
1353     "|" gnus-summary-pipe-output
1354     "\M-k" gnus-summary-edit-local-kill
1355     "\M-K" gnus-summary-edit-global-kill
1356     ;; "V" gnus-version
1357     "\C-c\C-d" gnus-summary-describe-group
1358     "q" gnus-summary-exit
1359     "Q" gnus-summary-exit-no-update
1360     "\C-c\C-i" gnus-info-find-node
1361     gnus-mouse-2 gnus-mouse-pick-article
1362     "m" gnus-summary-mail-other-window
1363     "a" gnus-summary-post-news
1364     "x" gnus-summary-limit-to-unread
1365     "s" gnus-summary-isearch-article
1366     "t" gnus-summary-toggle-header
1367     "g" gnus-summary-show-article
1368     "l" gnus-summary-goto-last-article
1369     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1370     "\C-d" gnus-summary-enter-digest-group
1371     "\M-\C-d" gnus-summary-read-document
1372     "\M-\C-e" gnus-summary-edit-parameters
1373     "\M-\C-a" gnus-summary-customize-parameters
1374     "\C-c\C-b" gnus-bug
1375     "*" gnus-cache-enter-article
1376     "\M-*" gnus-cache-remove-article
1377     "\M-&" gnus-summary-universal-argument
1378     "\C-l" gnus-recenter
1379     "I" gnus-summary-increase-score
1380     "L" gnus-summary-lower-score
1381     "\M-i" gnus-symbolic-argument
1382     "h" gnus-summary-select-article-buffer
1383
1384     "b" gnus-article-view-part
1385     "\M-t" gnus-summary-toggle-display-buttonized
1386
1387     "V" gnus-summary-score-map
1388     "X" gnus-uu-extract-map
1389     "S" gnus-summary-send-map)
1390
1391   ;; Sort of orthogonal keymap
1392   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1393     "t" gnus-summary-tick-article-forward
1394     "!" gnus-summary-tick-article-forward
1395     "d" gnus-summary-mark-as-read-forward
1396     "r" gnus-summary-mark-as-read-forward
1397     "c" gnus-summary-clear-mark-forward
1398     " " gnus-summary-clear-mark-forward
1399     "e" gnus-summary-mark-as-expirable
1400     "x" gnus-summary-mark-as-expirable
1401     "?" gnus-summary-mark-as-dormant
1402     "b" gnus-summary-set-bookmark
1403     "B" gnus-summary-remove-bookmark
1404     "#" gnus-summary-mark-as-processable
1405     "\M-#" gnus-summary-unmark-as-processable
1406     "S" gnus-summary-limit-include-expunged
1407     "C" gnus-summary-catchup
1408     "H" gnus-summary-catchup-to-here
1409     "\C-c" gnus-summary-catchup-all
1410     "k" gnus-summary-kill-same-subject-and-select
1411     "K" gnus-summary-kill-same-subject
1412     "P" gnus-uu-mark-map)
1413
1414   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1415     "c" gnus-summary-clear-above
1416     "u" gnus-summary-tick-above
1417     "m" gnus-summary-mark-above
1418     "k" gnus-summary-kill-below)
1419
1420   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1421     "/" gnus-summary-limit-to-subject
1422     "n" gnus-summary-limit-to-articles
1423     "w" gnus-summary-pop-limit
1424     "s" gnus-summary-limit-to-subject
1425     "a" gnus-summary-limit-to-author
1426     "u" gnus-summary-limit-to-unread
1427     "m" gnus-summary-limit-to-marks
1428     "M" gnus-summary-limit-exclude-marks
1429     "v" gnus-summary-limit-to-score
1430     "*" gnus-summary-limit-include-cached
1431     "D" gnus-summary-limit-include-dormant
1432     "T" gnus-summary-limit-include-thread
1433     "d" gnus-summary-limit-exclude-dormant
1434     "t" gnus-summary-limit-to-age
1435     "x" gnus-summary-limit-to-extra 
1436     "E" gnus-summary-limit-include-expunged
1437     "c" gnus-summary-limit-exclude-childless-dormant
1438     "C" gnus-summary-limit-mark-excluded-as-read)
1439
1440   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1441     "n" gnus-summary-next-unread-article
1442     "p" gnus-summary-prev-unread-article
1443     "N" gnus-summary-next-article
1444     "P" gnus-summary-prev-article
1445     "\C-n" gnus-summary-next-same-subject
1446     "\C-p" gnus-summary-prev-same-subject
1447     "\M-n" gnus-summary-next-unread-subject
1448     "\M-p" gnus-summary-prev-unread-subject
1449     "f" gnus-summary-first-unread-article
1450     "b" gnus-summary-best-unread-article
1451     "j" gnus-summary-goto-article
1452     "g" gnus-summary-goto-subject
1453     "l" gnus-summary-goto-last-article
1454     "o" gnus-summary-pop-article)
1455
1456   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1457     "k" gnus-summary-kill-thread
1458     "l" gnus-summary-lower-thread
1459     "i" gnus-summary-raise-thread
1460     "T" gnus-summary-toggle-threads
1461     "t" gnus-summary-rethread-current
1462     "^" gnus-summary-reparent-thread
1463     "s" gnus-summary-show-thread
1464     "S" gnus-summary-show-all-threads
1465     "h" gnus-summary-hide-thread
1466     "H" gnus-summary-hide-all-threads
1467     "n" gnus-summary-next-thread
1468     "p" gnus-summary-prev-thread
1469     "u" gnus-summary-up-thread
1470     "o" gnus-summary-top-thread
1471     "d" gnus-summary-down-thread
1472     "#" gnus-uu-mark-thread
1473     "\M-#" gnus-uu-unmark-thread)
1474
1475   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1476     "g" gnus-summary-prepare
1477     "c" gnus-summary-insert-cached-articles)
1478
1479   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1480     "c" gnus-summary-catchup-and-exit
1481     "C" gnus-summary-catchup-all-and-exit
1482     "E" gnus-summary-exit-no-update
1483     "Q" gnus-summary-exit
1484     "Z" gnus-summary-exit
1485     "n" gnus-summary-catchup-and-goto-next-group
1486     "R" gnus-summary-reselect-current-group
1487     "G" gnus-summary-rescan-group
1488     "N" gnus-summary-next-group
1489     "s" gnus-summary-save-newsrc
1490     "P" gnus-summary-prev-group)
1491
1492   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1493     " " gnus-summary-next-page
1494     "n" gnus-summary-next-page
1495     "\177" gnus-summary-prev-page
1496     [delete] gnus-summary-prev-page
1497     "p" gnus-summary-prev-page
1498     "\r" gnus-summary-scroll-up
1499     "\M-\r" gnus-summary-scroll-down
1500     "<" gnus-summary-beginning-of-article
1501     ">" gnus-summary-end-of-article
1502     "b" gnus-summary-beginning-of-article
1503     "e" gnus-summary-end-of-article
1504     "^" gnus-summary-refer-parent-article
1505     "r" gnus-summary-refer-parent-article
1506     "D" gnus-summary-enter-digest-group
1507     "R" gnus-summary-refer-references
1508     "T" gnus-summary-refer-thread
1509     "g" gnus-summary-show-article
1510     "s" gnus-summary-isearch-article
1511     "P" gnus-summary-print-article
1512     "t" gnus-article-babel)
1513
1514   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1515     "b" gnus-article-add-buttons
1516     "B" gnus-article-add-buttons-to-head
1517     "o" gnus-article-treat-overstrike
1518     "e" gnus-article-emphasize
1519     "w" gnus-article-fill-cited-article
1520     "Q" gnus-article-fill-long-lines
1521     "C" gnus-article-capitalize-sentences
1522     "c" gnus-article-remove-cr
1523     "q" gnus-article-de-quoted-unreadable
1524     "f" gnus-article-display-x-face
1525     "l" gnus-summary-stop-page-breaking
1526     "r" gnus-summary-caesar-message
1527     "t" gnus-article-hide-headers
1528     "v" gnus-summary-verbose-headers
1529     "H" gnus-article-strip-headers-in-body
1530     "d" gnus-article-treat-dumbquotes)
1531
1532   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1533     "a" gnus-article-hide
1534     "h" gnus-article-hide-headers
1535     "b" gnus-article-hide-boring-headers
1536     "s" gnus-article-hide-signature
1537     "c" gnus-article-hide-citation
1538     "C" gnus-article-hide-citation-in-followups
1539     "l" gnus-article-hide-list-identifiers
1540     "p" gnus-article-hide-pgp
1541     "B" gnus-article-strip-banner
1542     "P" gnus-article-hide-pem
1543     "\C-c" gnus-article-hide-citation-maybe)
1544
1545   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1546     "a" gnus-article-highlight
1547     "h" gnus-article-highlight-headers
1548     "c" gnus-article-highlight-citation
1549     "s" gnus-article-highlight-signature)
1550
1551   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1552     "w" gnus-article-decode-mime-words
1553     "c" gnus-article-decode-charset
1554     "v" gnus-mime-view-all-parts
1555     "b" gnus-article-view-part)
1556
1557   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1558     "z" gnus-article-date-ut
1559     "u" gnus-article-date-ut
1560     "l" gnus-article-date-local
1561     "e" gnus-article-date-lapsed
1562     "o" gnus-article-date-original
1563     "i" gnus-article-date-iso8601
1564     "s" gnus-article-date-user)
1565
1566   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1567     "t" gnus-article-remove-trailing-blank-lines
1568     "l" gnus-article-strip-leading-blank-lines
1569     "m" gnus-article-strip-multiple-blank-lines
1570     "a" gnus-article-strip-blank-lines
1571     "A" gnus-article-strip-all-blank-lines
1572     "s" gnus-article-strip-leading-space
1573     "e" gnus-article-strip-trailing-space)
1574
1575   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1576     "v" gnus-version
1577     "f" gnus-summary-fetch-faq
1578     "d" gnus-summary-describe-group
1579     "h" gnus-summary-describe-briefly
1580     "i" gnus-info-find-node)
1581
1582   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1583     "e" gnus-summary-expire-articles
1584     "\M-\C-e" gnus-summary-expire-articles-now
1585     "\177" gnus-summary-delete-article
1586     [delete] gnus-summary-delete-article
1587     [backspace] gnus-summary-delete-article
1588     "m" gnus-summary-move-article
1589     "r" gnus-summary-respool-article
1590     "w" gnus-summary-edit-article
1591     "c" gnus-summary-copy-article
1592     "B" gnus-summary-crosspost-article
1593     "q" gnus-summary-respool-query
1594     "t" gnus-summary-respool-trace
1595     "i" gnus-summary-import-article
1596     "p" gnus-summary-article-posted-p)
1597
1598   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1599     "o" gnus-summary-save-article
1600     "m" gnus-summary-save-article-mail
1601     "F" gnus-summary-write-article-file
1602     "r" gnus-summary-save-article-rmail
1603     "f" gnus-summary-save-article-file
1604     "b" gnus-summary-save-article-body-file
1605     "h" gnus-summary-save-article-folder
1606     "v" gnus-summary-save-article-vm
1607     "p" gnus-summary-pipe-output
1608     "s" gnus-soup-add-article)
1609
1610   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1611     "b" gnus-summary-display-buttonized
1612     "m" gnus-summary-repair-multipart
1613     "v" gnus-article-view-part
1614     "o" gnus-article-save-part
1615     "c" gnus-article-copy-part
1616     "e" gnus-article-externalize-part
1617     "i" gnus-article-inline-part
1618     "|" gnus-article-pipe-part))
1619
1620 (defun gnus-summary-make-menu-bar ()
1621   (gnus-turn-off-edit-menu 'summary)
1622
1623   (unless (boundp 'gnus-summary-misc-menu)
1624
1625     (easy-menu-define
1626      gnus-summary-kill-menu gnus-summary-mode-map ""
1627      (cons
1628       "Score"
1629       (nconc
1630        (list
1631         ["Enter score..." gnus-summary-score-entry t]
1632         ["Customize" gnus-score-customize t])
1633        (gnus-make-score-map 'increase)
1634        (gnus-make-score-map 'lower)
1635        '(("Mark"
1636           ["Kill below" gnus-summary-kill-below t]
1637           ["Mark above" gnus-summary-mark-above t]
1638           ["Tick above" gnus-summary-tick-above t]
1639           ["Clear above" gnus-summary-clear-above t])
1640          ["Current score" gnus-summary-current-score t]
1641          ["Set score" gnus-summary-set-score t]
1642          ["Switch current score file..." gnus-score-change-score-file t]
1643          ["Set mark below..." gnus-score-set-mark-below t]
1644          ["Set expunge below..." gnus-score-set-expunge-below t]
1645          ["Edit current score file" gnus-score-edit-current-scores t]
1646          ["Edit score file" gnus-score-edit-file t]
1647          ["Trace score" gnus-score-find-trace t]
1648          ["Find words" gnus-score-find-favourite-words t]
1649          ["Rescore buffer" gnus-summary-rescore t]
1650          ["Increase score..." gnus-summary-increase-score t]
1651          ["Lower score..." gnus-summary-lower-score t]))))
1652
1653     ;; Define both the Article menu in the summary buffer and the equivalent
1654     ;; Commands menu in the article buffer here for consistency.
1655     (let ((innards
1656            '(("Hide"
1657               ["All" gnus-article-hide t]
1658               ["Headers" gnus-article-hide-headers t]
1659               ["Signature" gnus-article-hide-signature t]
1660               ["Citation" gnus-article-hide-citation t]
1661               ["List identifiers" gnus-article-hide-list-identifiers t]
1662               ["PGP" gnus-article-hide-pgp t]
1663               ["Banner" gnus-article-strip-banner t]
1664               ["Boring headers" gnus-article-hide-boring-headers t])
1665              ("Highlight"
1666               ["All" gnus-article-highlight t]
1667               ["Headers" gnus-article-highlight-headers t]
1668               ["Signature" gnus-article-highlight-signature t]
1669               ["Citation" gnus-article-highlight-citation t])
1670              ("MIME"
1671               ["Words" gnus-article-decode-mime-words t]
1672               ["Charset" gnus-article-decode-charset t]
1673               ["QP" gnus-article-de-quoted-unreadable t]
1674               ["View all" gnus-mime-view-all-parts t])
1675              ("Date"
1676               ["Local" gnus-article-date-local t]
1677               ["ISO8601" gnus-article-date-iso8601 t]
1678               ["UT" gnus-article-date-ut t]
1679               ["Original" gnus-article-date-original t]
1680               ["Lapsed" gnus-article-date-lapsed t]
1681               ["User-defined" gnus-article-date-user t])
1682              ("Washing"
1683               ("Remove Blanks"
1684                ["Leading" gnus-article-strip-leading-blank-lines t]
1685                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1686                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1687                ["All of the above" gnus-article-strip-blank-lines t]
1688                ["All" gnus-article-strip-all-blank-lines t]
1689                ["Leading space" gnus-article-strip-leading-space t]
1690                ["Trailing space" gnus-article-strip-trailing-space t])
1691               ["Overstrike" gnus-article-treat-overstrike t]
1692               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1693               ["Emphasis" gnus-article-emphasize t]
1694               ["Word wrap" gnus-article-fill-cited-article t]
1695               ["Fill long lines" gnus-article-fill-long-lines t]
1696               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1697               ["CR" gnus-article-remove-cr t]
1698               ["Show X-Face" gnus-article-display-x-face t]
1699               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1700               ["Rot 13" gnus-summary-caesar-message t]
1701               ["Unix pipe" gnus-summary-pipe-message t]
1702               ["Add buttons" gnus-article-add-buttons t]
1703               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1704               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1705               ["Verbose header" gnus-summary-verbose-headers t]
1706               ["Toggle header" gnus-summary-toggle-header t]
1707               ["HZ" gnus-article-decode-HZ t])
1708              ("Output"
1709               ["Save in default format" gnus-summary-save-article t]
1710               ["Save in file" gnus-summary-save-article-file t]
1711               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1712               ["Save in MH folder" gnus-summary-save-article-folder t]
1713               ["Save in VM folder" gnus-summary-save-article-vm t]
1714               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1715               ["Save body in file" gnus-summary-save-article-body-file t]
1716               ["Pipe through a filter" gnus-summary-pipe-output t]
1717               ["Add to SOUP packet" gnus-soup-add-article t]
1718               ["Print" gnus-summary-print-article t])
1719              ("Backend"
1720               ["Respool article..." gnus-summary-respool-article t]
1721               ["Move article..." gnus-summary-move-article
1722                (gnus-check-backend-function
1723                 'request-move-article gnus-newsgroup-name)]
1724               ["Copy article..." gnus-summary-copy-article t]
1725               ["Crosspost article..." gnus-summary-crosspost-article
1726                (gnus-check-backend-function
1727                 'request-replace-article gnus-newsgroup-name)]
1728               ["Import file..." gnus-summary-import-article t]
1729               ["Check if posted" gnus-summary-article-posted-p t]
1730               ["Edit article" gnus-summary-edit-article
1731                (not (gnus-group-read-only-p))]
1732               ["Delete article" gnus-summary-delete-article
1733                (gnus-check-backend-function
1734                 'request-expire-articles gnus-newsgroup-name)]
1735               ["Query respool" gnus-summary-respool-query t]
1736               ["Trace respool" gnus-summary-respool-trace t]
1737               ["Delete expirable articles" gnus-summary-expire-articles-now
1738                (gnus-check-backend-function
1739                 'request-expire-articles gnus-newsgroup-name)])
1740              ("Extract"
1741               ["Uudecode" gnus-uu-decode-uu t]
1742               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1743               ["Unshar" gnus-uu-decode-unshar t]
1744               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1745               ["Save" gnus-uu-decode-save t]
1746               ["Binhex" gnus-uu-decode-binhex t]
1747               ["Postscript" gnus-uu-decode-postscript t])
1748              ("Cache"
1749               ["Enter article" gnus-cache-enter-article t]
1750               ["Remove article" gnus-cache-remove-article t])
1751              ["Translate" gnus-article-babel t]
1752              ["Select article buffer" gnus-summary-select-article-buffer t]
1753              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1754              ["Isearch article..." gnus-summary-isearch-article t]
1755              ["Beginning of the article" gnus-summary-beginning-of-article t]
1756              ["End of the article" gnus-summary-end-of-article t]
1757              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1758              ["Fetch referenced articles" gnus-summary-refer-references t]
1759              ["Fetch current thread" gnus-summary-refer-thread t]
1760              ["Fetch article with id..." gnus-summary-refer-article t]
1761              ["Redisplay" gnus-summary-show-article t])))
1762       (easy-menu-define
1763        gnus-summary-article-menu gnus-summary-mode-map ""
1764        (cons "Article" innards))
1765
1766       (easy-menu-define
1767        gnus-article-commands-menu gnus-article-mode-map ""
1768        (cons "Commands" innards)))
1769
1770     (easy-menu-define
1771      gnus-summary-thread-menu gnus-summary-mode-map ""
1772      '("Threads"
1773        ["Toggle threading" gnus-summary-toggle-threads t]
1774        ["Hide threads" gnus-summary-hide-all-threads t]
1775        ["Show threads" gnus-summary-show-all-threads t]
1776        ["Hide thread" gnus-summary-hide-thread t]
1777        ["Show thread" gnus-summary-show-thread t]
1778        ["Go to next thread" gnus-summary-next-thread t]
1779        ["Go to previous thread" gnus-summary-prev-thread t]
1780        ["Go down thread" gnus-summary-down-thread t]
1781        ["Go up thread" gnus-summary-up-thread t]
1782        ["Top of thread" gnus-summary-top-thread t]
1783        ["Mark thread as read" gnus-summary-kill-thread t]
1784        ["Lower thread score" gnus-summary-lower-thread t]
1785        ["Raise thread score" gnus-summary-raise-thread t]
1786        ["Rethread current" gnus-summary-rethread-current t]))
1787
1788     (easy-menu-define
1789      gnus-summary-post-menu gnus-summary-mode-map ""
1790      '("Post"
1791        ["Post an article" gnus-summary-post-news t]
1792        ["Followup" gnus-summary-followup t]
1793        ["Followup and yank" gnus-summary-followup-with-original t]
1794        ["Supersede article" gnus-summary-supersede-article t]
1795        ["Cancel article" gnus-summary-cancel-article t]
1796        ["Reply" gnus-summary-reply t]
1797        ["Reply and yank" gnus-summary-reply-with-original t]
1798        ["Wide reply" gnus-summary-wide-reply t]
1799        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1800        ["Mail forward" gnus-summary-mail-forward t]
1801        ["Post forward" gnus-summary-post-forward t]
1802        ["Digest and mail" gnus-uu-digest-mail-forward t]
1803        ["Digest and post" gnus-uu-digest-post-forward t]
1804        ["Resend message" gnus-summary-resend-message t]
1805        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1806        ["Send a mail" gnus-summary-mail-other-window t]
1807        ["Uuencode and post" gnus-uu-post-news t]
1808        ["Followup via news" gnus-summary-followup-to-mail t]
1809        ["Followup via news and yank"
1810         gnus-summary-followup-to-mail-with-original t]
1811        ;;("Draft"
1812        ;;["Send" gnus-summary-send-draft t]
1813        ;;["Send bounced" gnus-resend-bounced-mail t])
1814        ))
1815
1816     (easy-menu-define
1817      gnus-summary-misc-menu gnus-summary-mode-map ""
1818      '("Misc"
1819        ("Mark Read"
1820         ["Mark as read" gnus-summary-mark-as-read-forward t]
1821         ["Mark same subject and select"
1822          gnus-summary-kill-same-subject-and-select t]
1823         ["Mark same subject" gnus-summary-kill-same-subject t]
1824         ["Catchup" gnus-summary-catchup t]
1825         ["Catchup all" gnus-summary-catchup-all t]
1826         ["Catchup to here" gnus-summary-catchup-to-here t]
1827         ["Catchup region" gnus-summary-mark-region-as-read t]
1828         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1829        ("Mark Various"
1830         ["Tick" gnus-summary-tick-article-forward t]
1831         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1832         ["Remove marks" gnus-summary-clear-mark-forward t]
1833         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1834         ["Set bookmark" gnus-summary-set-bookmark t]
1835         ["Remove bookmark" gnus-summary-remove-bookmark t])
1836        ("Mark Limit"
1837         ["Marks..." gnus-summary-limit-to-marks t]
1838         ["Subject..." gnus-summary-limit-to-subject t]
1839         ["Author..." gnus-summary-limit-to-author t]
1840         ["Age..." gnus-summary-limit-to-age t]
1841         ["Extra..." gnus-summary-limit-to-extra t]
1842         ["Score" gnus-summary-limit-to-score t]
1843         ["Unread" gnus-summary-limit-to-unread t]
1844         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1845         ["Articles" gnus-summary-limit-to-articles t]
1846         ["Pop limit" gnus-summary-pop-limit t]
1847         ["Show dormant" gnus-summary-limit-include-dormant t]
1848         ["Hide childless dormant"
1849          gnus-summary-limit-exclude-childless-dormant t]
1850         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1851         ["Hide marked" gnus-summary-limit-exclude-marks t]
1852         ["Show expunged" gnus-summary-show-all-expunged t])
1853        ("Process Mark"
1854         ["Set mark" gnus-summary-mark-as-processable t]
1855         ["Remove mark" gnus-summary-unmark-as-processable t]
1856         ["Remove all marks" gnus-summary-unmark-all-processable t]
1857         ["Mark above" gnus-uu-mark-over t]
1858         ["Mark series" gnus-uu-mark-series t]
1859         ["Mark region" gnus-uu-mark-region t]
1860         ["Unmark region" gnus-uu-unmark-region t]
1861         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1862         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1863         ["Mark all" gnus-uu-mark-all t]
1864         ["Mark buffer" gnus-uu-mark-buffer t]
1865         ["Mark sparse" gnus-uu-mark-sparse t]
1866         ["Mark thread" gnus-uu-mark-thread t]
1867         ["Unmark thread" gnus-uu-unmark-thread t]
1868         ("Process Mark Sets"
1869          ["Kill" gnus-summary-kill-process-mark t]
1870          ["Yank" gnus-summary-yank-process-mark
1871           gnus-newsgroup-process-stack]
1872          ["Save" gnus-summary-save-process-mark t]))
1873        ("Scroll article"
1874         ["Page forward" gnus-summary-next-page t]
1875         ["Page backward" gnus-summary-prev-page t]
1876         ["Line forward" gnus-summary-scroll-up t])
1877        ("Move"
1878         ["Next unread article" gnus-summary-next-unread-article t]
1879         ["Previous unread article" gnus-summary-prev-unread-article t]
1880         ["Next article" gnus-summary-next-article t]
1881         ["Previous article" gnus-summary-prev-article t]
1882         ["Next unread subject" gnus-summary-next-unread-subject t]
1883         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1884         ["Next article same subject" gnus-summary-next-same-subject t]
1885         ["Previous article same subject" gnus-summary-prev-same-subject t]
1886         ["First unread article" gnus-summary-first-unread-article t]
1887         ["Best unread article" gnus-summary-best-unread-article t]
1888         ["Go to subject number..." gnus-summary-goto-subject t]
1889         ["Go to article number..." gnus-summary-goto-article t]
1890         ["Go to the last article" gnus-summary-goto-last-article t]
1891         ["Pop article off history" gnus-summary-pop-article t])
1892        ("Sort"
1893         ["Sort by number" gnus-summary-sort-by-number t]
1894         ["Sort by author" gnus-summary-sort-by-author t]
1895         ["Sort by subject" gnus-summary-sort-by-subject t]
1896         ["Sort by date" gnus-summary-sort-by-date t]
1897         ["Sort by score" gnus-summary-sort-by-score t]
1898         ["Sort by lines" gnus-summary-sort-by-lines t]
1899         ["Sort by characters" gnus-summary-sort-by-chars t])
1900        ("Help"
1901         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1902         ["Describe group" gnus-summary-describe-group t]
1903         ["Read manual" gnus-info-find-node t])
1904        ("Modes"
1905         ["Pick and read" gnus-pick-mode t]
1906         ["Binary" gnus-binary-mode t])
1907        ("Regeneration"
1908         ["Regenerate" gnus-summary-prepare t]
1909         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1910         ["Toggle threading" gnus-summary-toggle-threads t])
1911        ["Filter articles..." gnus-summary-execute-command t]
1912        ["Run command on subjects..." gnus-summary-universal-argument t]
1913        ["Search articles forward..." gnus-summary-search-article-forward t]
1914        ["Search articles backward..." gnus-summary-search-article-backward t]
1915        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1916        ["Expand window" gnus-summary-expand-window t]
1917        ["Expire expirable articles" gnus-summary-expire-articles
1918         (gnus-check-backend-function
1919          'request-expire-articles gnus-newsgroup-name)]
1920        ["Edit local kill file" gnus-summary-edit-local-kill t]
1921        ["Edit main kill file" gnus-summary-edit-global-kill t]
1922        ["Edit group parameters" gnus-summary-edit-parameters t]
1923        ["Customize group parameters" gnus-summary-customize-parameters t]
1924        ["Send a bug report" gnus-bug t]
1925        ("Exit"
1926         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1927         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1928         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1929         ["Exit group" gnus-summary-exit t]
1930         ["Exit group without updating" gnus-summary-exit-no-update t]
1931         ["Exit and goto next group" gnus-summary-next-group t]
1932         ["Exit and goto prev group" gnus-summary-prev-group t]
1933         ["Reselect group" gnus-summary-reselect-current-group t]
1934         ["Rescan group" gnus-summary-rescan-group t]
1935         ["Update dribble" gnus-summary-save-newsrc t])))
1936
1937     (gnus-run-hooks 'gnus-summary-menu-hook)))
1938
1939 (defun gnus-score-set-default (var value)
1940   "A version of set that updates the GNU Emacs menu-bar."
1941   (set var value)
1942   ;; It is the message that forces the active status to be updated.
1943   (message ""))
1944
1945 (defun gnus-make-score-map (type)
1946   "Make a summary score map of type TYPE."
1947   (if t
1948       nil
1949     (let ((headers '(("author" "from" string)
1950                      ("subject" "subject" string)
1951                      ("article body" "body" string)
1952                      ("article head" "head" string)
1953                      ("xref" "xref" string)
1954                      ("extra header" "extra" string)
1955                      ("lines" "lines" number)
1956                      ("followups to author" "followup" string)))
1957           (types '((number ("less than" <)
1958                            ("greater than" >)
1959                            ("equal" =))
1960                    (string ("substring" s)
1961                            ("exact string" e)
1962                            ("fuzzy string" f)
1963                            ("regexp" r))))
1964           (perms '(("temporary" (current-time-string))
1965                    ("permanent" nil)
1966                    ("immediate" now)))
1967           header)
1968       (list
1969        (apply
1970         'nconc
1971         (list
1972          (if (eq type 'lower)
1973              "Lower score"
1974            "Increase score"))
1975         (let (outh)
1976           (while headers
1977             (setq header (car headers))
1978             (setq outh
1979                   (cons
1980                    (apply
1981                     'nconc
1982                     (list (car header))
1983                     (let ((ts (cdr (assoc (nth 2 header) types)))
1984                           outt)
1985                       (while ts
1986                         (setq outt
1987                               (cons
1988                                (apply
1989                                 'nconc
1990                                 (list (caar ts))
1991                                 (let ((ps perms)
1992                                       outp)
1993                                   (while ps
1994                                     (setq outp
1995                                           (cons
1996                                            (vector
1997                                             (caar ps)
1998                                             (list
1999                                              'gnus-summary-score-entry
2000                                              (nth 1 header)
2001                                              (if (or (string= (nth 1 header)
2002                                                               "head")
2003                                                      (string= (nth 1 header)
2004                                                               "body"))
2005                                                  ""
2006                                                (list 'gnus-summary-header
2007                                                      (nth 1 header)))
2008                                              (list 'quote (nth 1 (car ts)))
2009                                              (list 'gnus-score-delta-default
2010                                                    nil)
2011                                              (nth 1 (car ps))
2012                                              t)
2013                                             t)
2014                                            outp))
2015                                     (setq ps (cdr ps)))
2016                                   (list (nreverse outp))))
2017                                outt))
2018                         (setq ts (cdr ts)))
2019                       (list (nreverse outt))))
2020                    outh))
2021             (setq headers (cdr headers)))
2022           (list (nreverse outh))))))))
2023
2024 \f
2025
2026 (defun gnus-summary-mode (&optional group)
2027   "Major mode for reading articles.
2028
2029 All normal editing commands are switched off.
2030 \\<gnus-summary-mode-map>
2031 Each line in this buffer represents one article.  To read an
2032 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2033 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2034 respectively.
2035
2036 You can also post articles and send mail from this buffer.  To
2037 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2038 of an article, type `\\[gnus-summary-reply]'.
2039
2040 There are approx. one gazillion commands you can execute in this
2041 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2042
2043 The following commands are available:
2044
2045 \\{gnus-summary-mode-map}"
2046   (interactive)
2047   (when (gnus-visual-p 'summary-menu 'menu)
2048     (gnus-summary-make-menu-bar))
2049   (kill-all-local-variables)
2050   (gnus-summary-make-local-variables)
2051   (gnus-make-thread-indent-array)
2052   (gnus-simplify-mode-line)
2053   (setq major-mode 'gnus-summary-mode)
2054   (setq mode-name "Summary")
2055   (make-local-variable 'minor-mode-alist)
2056   (use-local-map gnus-summary-mode-map)
2057   (buffer-disable-undo)
2058   (setq buffer-read-only t)             ;Disable modification
2059   (setq truncate-lines t)
2060   (setq selective-display t)
2061   (setq selective-display-ellipses t)   ;Display `...'
2062   (gnus-summary-set-display-table)
2063   (gnus-set-default-directory)
2064   (setq gnus-newsgroup-name group)
2065   (make-local-variable 'gnus-summary-line-format)
2066   (make-local-variable 'gnus-summary-line-format-spec)
2067   (make-local-variable 'gnus-summary-dummy-line-format)
2068   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2069   (make-local-variable 'gnus-summary-mark-positions)
2070   (make-local-hook 'pre-command-hook)
2071   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2072   (gnus-run-hooks 'gnus-summary-mode-hook)
2073   (mm-enable-multibyte)
2074   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2075   (gnus-update-summary-mark-positions))
2076
2077 (defun gnus-summary-make-local-variables ()
2078   "Make all the local summary buffer variables."
2079   (let (global)
2080     (dolist (local gnus-summary-local-variables)
2081       (if (consp local)
2082           (progn
2083             (if (eq (cdr local) 'global)
2084                 ;; Copy the global value of the variable.
2085                 (setq global (symbol-value (car local)))
2086               ;; Use the value from the list.
2087               (setq global (eval (cdr local))))
2088             (set (make-local-variable (car local)) global))
2089         ;; Simple nil-valued local variable.
2090         (set (make-local-variable local) nil)))))
2091
2092 (defun gnus-summary-clear-local-variables ()
2093   (let ((locals gnus-summary-local-variables))
2094     (while locals
2095       (if (consp (car locals))
2096           (and (vectorp (caar locals))
2097                (set (caar locals) nil))
2098         (and (vectorp (car locals))
2099              (set (car locals) nil)))
2100       (setq locals (cdr locals)))))
2101
2102 ;; Summary data functions.
2103
2104 (defmacro gnus-data-number (data)
2105   `(car ,data))
2106
2107 (defmacro gnus-data-set-number (data number)
2108   `(setcar ,data ,number))
2109
2110 (defmacro gnus-data-mark (data)
2111   `(nth 1 ,data))
2112
2113 (defmacro gnus-data-set-mark (data mark)
2114   `(setcar (nthcdr 1 ,data) ,mark))
2115
2116 (defmacro gnus-data-pos (data)
2117   `(nth 2 ,data))
2118
2119 (defmacro gnus-data-set-pos (data pos)
2120   `(setcar (nthcdr 2 ,data) ,pos))
2121
2122 (defmacro gnus-data-header (data)
2123   `(nth 3 ,data))
2124
2125 (defmacro gnus-data-set-header (data header)
2126   `(setf (nth 3 ,data) ,header))
2127
2128 (defmacro gnus-data-level (data)
2129   `(nth 4 ,data))
2130
2131 (defmacro gnus-data-unread-p (data)
2132   `(= (nth 1 ,data) gnus-unread-mark))
2133
2134 (defmacro gnus-data-read-p (data)
2135   `(/= (nth 1 ,data) gnus-unread-mark))
2136
2137 (defmacro gnus-data-pseudo-p (data)
2138   `(consp (nth 3 ,data)))
2139
2140 (defmacro gnus-data-find (number)
2141   `(assq ,number gnus-newsgroup-data))
2142
2143 (defmacro gnus-data-find-list (number &optional data)
2144   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2145      (memq (assq ,number bdata)
2146            bdata)))
2147
2148 (defmacro gnus-data-make (number mark pos header level)
2149   `(list ,number ,mark ,pos ,header ,level))
2150
2151 (defun gnus-data-enter (after-article number mark pos header level offset)
2152   (let ((data (gnus-data-find-list after-article)))
2153     (unless data
2154       (error "No such article: %d" after-article))
2155     (setcdr data (cons (gnus-data-make number mark pos header level)
2156                        (cdr data)))
2157     (setq gnus-newsgroup-data-reverse nil)
2158     (gnus-data-update-list (cddr data) offset)))
2159
2160 (defun gnus-data-enter-list (after-article list &optional offset)
2161   (when list
2162     (let ((data (and after-article (gnus-data-find-list after-article)))
2163           (ilist list))
2164       (if (not (or data
2165                    after-article))
2166           (let ((odata gnus-newsgroup-data))
2167             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2168             (when offset
2169               (gnus-data-update-list odata offset)))
2170         ;; Find the last element in the list to be spliced into the main
2171         ;; list.
2172         (while (cdr list)
2173           (setq list (cdr list)))
2174         (if (not data)
2175             (progn
2176               (setcdr list gnus-newsgroup-data)
2177               (setq gnus-newsgroup-data ilist)
2178               (when offset
2179                 (gnus-data-update-list (cdr list) offset)))
2180           (setcdr list (cdr data))
2181           (setcdr data ilist)
2182           (when offset
2183             (gnus-data-update-list (cdr list) offset))))
2184       (setq gnus-newsgroup-data-reverse nil))))
2185
2186 (defun gnus-data-remove (article &optional offset)
2187   (let ((data gnus-newsgroup-data))
2188     (if (= (gnus-data-number (car data)) article)
2189         (progn
2190           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2191                 gnus-newsgroup-data-reverse nil)
2192           (when offset
2193             (gnus-data-update-list gnus-newsgroup-data offset)))
2194       (while (cdr data)
2195         (when (= (gnus-data-number (cadr data)) article)
2196           (setcdr data (cddr data))
2197           (when offset
2198             (gnus-data-update-list (cdr data) offset))
2199           (setq data nil
2200                 gnus-newsgroup-data-reverse nil))
2201         (setq data (cdr data))))))
2202
2203 (defmacro gnus-data-list (backward)
2204   `(if ,backward
2205        (or gnus-newsgroup-data-reverse
2206            (setq gnus-newsgroup-data-reverse
2207                  (reverse gnus-newsgroup-data)))
2208      gnus-newsgroup-data))
2209
2210 (defun gnus-data-update-list (data offset)
2211   "Add OFFSET to the POS of all data entries in DATA."
2212   (setq gnus-newsgroup-data-reverse nil)
2213   (while data
2214     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2215     (setq data (cdr data))))
2216
2217 (defun gnus-summary-article-pseudo-p (article)
2218   "Say whether this article is a pseudo article or not."
2219   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2220
2221 (defmacro gnus-summary-article-sparse-p (article)
2222   "Say whether this article is a sparse article or not."
2223   `(memq ,article gnus-newsgroup-sparse))
2224
2225 (defmacro gnus-summary-article-ancient-p (article)
2226   "Say whether this article is a sparse article or not."
2227   `(memq ,article gnus-newsgroup-ancient))
2228
2229 (defun gnus-article-parent-p (number)
2230   "Say whether this article is a parent or not."
2231   (let ((data (gnus-data-find-list number)))
2232     (and (cdr data)                     ; There has to be an article after...
2233          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2234             (gnus-data-level (nth 1 data))))))
2235
2236 (defun gnus-article-children (number)
2237   "Return a list of all children to NUMBER."
2238   (let* ((data (gnus-data-find-list number))
2239          (level (gnus-data-level (car data)))
2240          children)
2241     (setq data (cdr data))
2242     (while (and data
2243                 (= (gnus-data-level (car data)) (1+ level)))
2244       (push (gnus-data-number (car data)) children)
2245       (setq data (cdr data)))
2246     children))
2247
2248 (defmacro gnus-summary-skip-intangible ()
2249   "If the current article is intangible, then jump to a different article."
2250   '(let ((to (get-text-property (point) 'gnus-intangible)))
2251      (and to (gnus-summary-goto-subject to))))
2252
2253 (defmacro gnus-summary-article-intangible-p ()
2254   "Say whether this article is intangible or not."
2255   '(get-text-property (point) 'gnus-intangible))
2256
2257 (defun gnus-article-read-p (article)
2258   "Say whether ARTICLE is read or not."
2259   (not (or (memq article gnus-newsgroup-marked)
2260            (memq article gnus-newsgroup-unreads)
2261            (memq article gnus-newsgroup-unselected)
2262            (memq article gnus-newsgroup-dormant))))
2263
2264 ;; Some summary mode macros.
2265
2266 (defmacro gnus-summary-article-number ()
2267   "The article number of the article on the current line.
2268 If there isn's an article number here, then we return the current
2269 article number."
2270   '(progn
2271      (gnus-summary-skip-intangible)
2272      (or (get-text-property (point) 'gnus-number)
2273          (gnus-summary-last-subject))))
2274
2275 (defmacro gnus-summary-article-header (&optional number)
2276   "Return the header of article NUMBER."
2277   `(gnus-data-header (gnus-data-find
2278                       ,(or number '(gnus-summary-article-number)))))
2279
2280 (defmacro gnus-summary-thread-level (&optional number)
2281   "Return the level of thread that starts with article NUMBER."
2282   `(if (and (eq gnus-summary-make-false-root 'dummy)
2283             (get-text-property (point) 'gnus-intangible))
2284        0
2285      (gnus-data-level (gnus-data-find
2286                        ,(or number '(gnus-summary-article-number))))))
2287
2288 (defmacro gnus-summary-article-mark (&optional number)
2289   "Return the mark of article NUMBER."
2290   `(gnus-data-mark (gnus-data-find
2291                     ,(or number '(gnus-summary-article-number)))))
2292
2293 (defmacro gnus-summary-article-pos (&optional number)
2294   "Return the position of the line of article NUMBER."
2295   `(gnus-data-pos (gnus-data-find
2296                    ,(or number '(gnus-summary-article-number)))))
2297
2298 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2299 (defmacro gnus-summary-article-subject (&optional number)
2300   "Return current subject string or nil if nothing."
2301   `(let ((headers
2302           ,(if number
2303                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2304              '(gnus-data-header (assq (gnus-summary-article-number)
2305                                       gnus-newsgroup-data)))))
2306      (and headers
2307           (vectorp headers)
2308           (mail-header-subject headers))))
2309
2310 (defmacro gnus-summary-article-score (&optional number)
2311   "Return current article score."
2312   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2313                   gnus-newsgroup-scored))
2314        gnus-summary-default-score 0))
2315
2316 (defun gnus-summary-article-children (&optional number)
2317   "Return a list of article numbers that are children of article NUMBER."
2318   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2319          (level (gnus-data-level (car data)))
2320          l children)
2321     (while (and (setq data (cdr data))
2322                 (> (setq l (gnus-data-level (car data))) level))
2323       (and (= (1+ level) l)
2324            (push (gnus-data-number (car data))
2325                  children)))
2326     (nreverse children)))
2327
2328 (defun gnus-summary-article-parent (&optional number)
2329   "Return the article number of the parent of article NUMBER."
2330   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2331                                     (gnus-data-list t)))
2332          (level (gnus-data-level (car data))))
2333     (if (zerop level)
2334         ()                              ; This is a root.
2335       ;; We search until we find an article with a level less than
2336       ;; this one.  That function has to be the parent.
2337       (while (and (setq data (cdr data))
2338                   (not (< (gnus-data-level (car data)) level))))
2339       (and data (gnus-data-number (car data))))))
2340
2341 (defun gnus-unread-mark-p (mark)
2342   "Say whether MARK is the unread mark."
2343   (= mark gnus-unread-mark))
2344
2345 (defun gnus-read-mark-p (mark)
2346   "Say whether MARK is one of the marks that mark as read.
2347 This is all marks except unread, ticked, dormant, and expirable."
2348   (not (or (= mark gnus-unread-mark)
2349            (= mark gnus-ticked-mark)
2350            (= mark gnus-dormant-mark)
2351            (= mark gnus-expirable-mark))))
2352
2353 (defmacro gnus-article-mark (number)
2354   "Return the MARK of article NUMBER.
2355 This macro should only be used when computing the mark the \"first\"
2356 time; i.e., when generating the summary lines.  After that,
2357 `gnus-summary-article-mark' should be used to examine the
2358 marks of articles."
2359   `(cond
2360     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2361     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2362     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2363     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2364     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2365     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2366     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2367     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2368            gnus-ancient-mark))))
2369
2370 ;; Saving hidden threads.
2371
2372 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2373 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2374
2375 (defmacro gnus-save-hidden-threads (&rest forms)
2376   "Save hidden threads, eval FORMS, and restore the hidden threads."
2377   (let ((config (make-symbol "config")))
2378     `(let ((,config (gnus-hidden-threads-configuration)))
2379        (unwind-protect
2380            (save-excursion
2381              ,@forms)
2382          (gnus-restore-hidden-threads-configuration ,config)))))
2383
2384 (defun gnus-data-compute-positions ()
2385   "Compute the positions of all articles."
2386   (setq gnus-newsgroup-data-reverse nil)
2387   (let ((data gnus-newsgroup-data))
2388     (save-excursion
2389       (gnus-save-hidden-threads
2390         (gnus-summary-show-all-threads)
2391         (goto-char (point-min))
2392         (while data
2393           (while (get-text-property (point) 'gnus-intangible)
2394             (forward-line 1))
2395           (gnus-data-set-pos (car data) (+ (point) 3))
2396           (setq data (cdr data))
2397           (forward-line 1))))))
2398
2399 (defun gnus-hidden-threads-configuration ()
2400   "Return the current hidden threads configuration."
2401   (save-excursion
2402     (let (config)
2403       (goto-char (point-min))
2404       (while (search-forward "\r" nil t)
2405         (push (1- (point)) config))
2406       config)))
2407
2408 (defun gnus-restore-hidden-threads-configuration (config)
2409   "Restore hidden threads configuration from CONFIG."
2410   (let (point buffer-read-only)
2411     (while (setq point (pop config))
2412       (when (and (< point (point-max))
2413                  (goto-char point)
2414                  (eq (char-after) ?\n))
2415         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2416
2417 ;; Various summary mode internalish functions.
2418
2419 (defun gnus-mouse-pick-article (e)
2420   (interactive "e")
2421   (mouse-set-point e)
2422   (gnus-summary-next-page nil t))
2423
2424 (defun gnus-summary-set-display-table ()
2425   ;; Change the display table.  Odd characters have a tendency to mess
2426   ;; up nicely formatted displays - we make all possible glyphs
2427   ;; display only a single character.
2428
2429   ;; We start from the standard display table, if any.
2430   (let ((table (or (copy-sequence standard-display-table)
2431                    (make-display-table)))
2432         (i 32))
2433     ;; Nix out all the control chars...
2434     (while (>= (setq i (1- i)) 0)
2435       (aset table i [??]))
2436     ;; ... but not newline and cr, of course.  (cr is necessary for the
2437     ;; selective display).
2438     (aset table ?\n nil)
2439     (aset table ?\r nil)
2440     ;; We keep TAB as well.
2441     (aset table ?\t nil)
2442     ;; We nix out any glyphs over 126 that are not set already.
2443     (let ((i 256))
2444       (while (>= (setq i (1- i)) 127)
2445         ;; Only modify if the entry is nil.
2446         (unless (aref table i)
2447           (aset table i [??]))))
2448     (setq buffer-display-table table)))
2449
2450 (defun gnus-summary-setup-buffer (group)
2451   "Initialize summary buffer."
2452   (let ((buffer (concat "*Summary " group "*")))
2453     (if (get-buffer buffer)
2454         (progn
2455           (set-buffer buffer)
2456           (setq gnus-summary-buffer (current-buffer))
2457           (not gnus-newsgroup-prepared))
2458       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2459       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2460       (gnus-summary-mode group)
2461       (when gnus-carpal
2462         (gnus-carpal-setup-buffer 'summary))
2463       (unless gnus-single-article-buffer
2464         (make-local-variable 'gnus-article-buffer)
2465         (make-local-variable 'gnus-article-current)
2466         (make-local-variable 'gnus-original-article-buffer))
2467       (setq gnus-newsgroup-name group)
2468       t)))
2469
2470 (defun gnus-set-global-variables ()
2471   ;; Set the global equivalents of the summary buffer-local variables
2472   ;; to the latest values they had.  These reflect the summary buffer
2473   ;; that was in action when the last article was fetched.
2474   (when (eq major-mode 'gnus-summary-mode)
2475     (setq gnus-summary-buffer (current-buffer))
2476     (let ((name gnus-newsgroup-name)
2477           (marked gnus-newsgroup-marked)
2478           (unread gnus-newsgroup-unreads)
2479           (headers gnus-current-headers)
2480           (data gnus-newsgroup-data)
2481           (summary gnus-summary-buffer)
2482           (article-buffer gnus-article-buffer)
2483           (original gnus-original-article-buffer)
2484           (gac gnus-article-current)
2485           (reffed gnus-reffed-article-number)
2486           (score-file gnus-current-score-file)
2487           (default-charset gnus-newsgroup-charset))
2488       (save-excursion
2489         (set-buffer gnus-group-buffer)
2490         (setq gnus-newsgroup-name name
2491               gnus-newsgroup-marked marked
2492               gnus-newsgroup-unreads unread
2493               gnus-current-headers headers
2494               gnus-newsgroup-data data
2495               gnus-article-current gac
2496               gnus-summary-buffer summary
2497               gnus-article-buffer article-buffer
2498               gnus-original-article-buffer original
2499               gnus-reffed-article-number reffed
2500               gnus-current-score-file score-file
2501               gnus-newsgroup-charset default-charset)
2502         ;; The article buffer also has local variables.
2503         (when (gnus-buffer-live-p gnus-article-buffer)
2504           (set-buffer gnus-article-buffer)
2505           (setq gnus-summary-buffer summary))))))
2506
2507 (defun gnus-summary-article-unread-p (article)
2508   "Say whether ARTICLE is unread or not."
2509   (memq article gnus-newsgroup-unreads))
2510
2511 (defun gnus-summary-first-article-p (&optional article)
2512   "Return whether ARTICLE is the first article in the buffer."
2513   (if (not (setq article (or article (gnus-summary-article-number))))
2514       nil
2515     (eq article (caar gnus-newsgroup-data))))
2516
2517 (defun gnus-summary-last-article-p (&optional article)
2518   "Return whether ARTICLE is the last article in the buffer."
2519   (if (not (setq article (or article (gnus-summary-article-number))))
2520       ;; All non-existent numbers are the last article.  :-)
2521       t
2522     (not (cdr (gnus-data-find-list article)))))
2523
2524 (defun gnus-make-thread-indent-array ()
2525   (let ((n 200))
2526     (unless (and gnus-thread-indent-array
2527                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2528       (setq gnus-thread-indent-array (make-vector 201 "")
2529             gnus-thread-indent-array-level gnus-thread-indent-level)
2530       (while (>= n 0)
2531         (aset gnus-thread-indent-array n
2532               (make-string (* n gnus-thread-indent-level) ? ))
2533         (setq n (1- n))))))
2534
2535 (defun gnus-update-summary-mark-positions ()
2536   "Compute where the summary marks are to go."
2537   (save-excursion
2538     (when (gnus-buffer-exists-p gnus-summary-buffer)
2539       (set-buffer gnus-summary-buffer))
2540     (let ((gnus-replied-mark 129)
2541           (gnus-score-below-mark 130)
2542           (gnus-score-over-mark 130)
2543           (gnus-download-mark 131)
2544           (spec gnus-summary-line-format-spec)
2545           gnus-visual pos)
2546       (save-excursion
2547         (gnus-set-work-buffer)
2548         (let ((gnus-summary-line-format-spec spec)
2549               (gnus-newsgroup-downloadable '((0 . t))))
2550           (gnus-summary-insert-line
2551            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2552           (goto-char (point-min))
2553           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2554                                              (- (point) 2)))))
2555           (goto-char (point-min))
2556           (push (cons 'replied (and (search-forward "\201" nil t)
2557                                     (- (point) 2)))
2558                 pos)
2559           (goto-char (point-min))
2560           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2561                 pos)
2562           (goto-char (point-min))
2563           (push (cons 'download
2564                       (and (search-forward "\203" nil t) (- (point) 2)))
2565                 pos)))
2566       (setq gnus-summary-mark-positions pos))))
2567
2568 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2569   "Insert a dummy root in the summary buffer."
2570   (beginning-of-line)
2571   (gnus-add-text-properties
2572    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2573    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2574
2575 (defun gnus-summary-from-or-to-or-newsgroups (header)
2576   (let ((to (cdr (assq 'To (mail-header-extra header))))
2577         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2578         (mail-parse-charset gnus-newsgroup-charset)
2579         (mail-parse-ignored-charsets 
2580          (save-excursion (set-buffer gnus-summary-buffer)
2581                          gnus-newsgroup-ignored-charsets)))
2582     (cond
2583      ((and to
2584            gnus-ignored-from-addresses
2585            (string-match gnus-ignored-from-addresses
2586                          (mail-header-from header)))
2587       (concat "-> "
2588               (or (car (funcall gnus-extract-address-components
2589                                 (funcall
2590                                  gnus-decode-encoded-word-function to)))
2591                   (funcall gnus-decode-encoded-word-function to))))
2592      ((and newsgroups
2593            gnus-ignored-from-addresses
2594            (string-match gnus-ignored-from-addresses
2595                          (mail-header-from header)))
2596       (concat "=> " newsgroups))
2597      (t
2598       (or (car (funcall gnus-extract-address-components
2599                         (mail-header-from header)))
2600           (mail-header-from header))))))
2601
2602 (defun gnus-summary-insert-line (gnus-tmp-header
2603                                  gnus-tmp-level gnus-tmp-current
2604                                  gnus-tmp-unread gnus-tmp-replied
2605                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2606                                  &optional gnus-tmp-dummy gnus-tmp-score
2607                                  gnus-tmp-process)
2608   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2609          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2610          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2611          (gnus-tmp-score-char
2612           (if (or (null gnus-summary-default-score)
2613                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2614                       gnus-summary-zcore-fuzz))
2615               ?  ;Whitespace
2616             (if (< gnus-tmp-score gnus-summary-default-score)
2617                 gnus-score-below-mark gnus-score-over-mark)))
2618          (gnus-tmp-replied
2619           (cond (gnus-tmp-process gnus-process-mark)
2620                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2621                  gnus-cached-mark)
2622                 (gnus-tmp-replied gnus-replied-mark)
2623                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2624                  gnus-saved-mark)
2625                 (t gnus-unread-mark)))
2626          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2627          (gnus-tmp-name
2628           (cond
2629            ((string-match "<[^>]+> *$" gnus-tmp-from)
2630             (let ((beg (match-beginning 0)))
2631               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2632                        (substring gnus-tmp-from (1+ (match-beginning 0))
2633                                   (1- (match-end 0))))
2634                   (substring gnus-tmp-from 0 beg))))
2635            ((string-match "(.+)" gnus-tmp-from)
2636             (substring gnus-tmp-from
2637                        (1+ (match-beginning 0)) (1- (match-end 0))))
2638            (t gnus-tmp-from)))
2639          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2640          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2641          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2642          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2643          (buffer-read-only nil))
2644     (when (string= gnus-tmp-name "")
2645       (setq gnus-tmp-name gnus-tmp-from))
2646     (unless (numberp gnus-tmp-lines)
2647       (setq gnus-tmp-lines 0))
2648     (gnus-put-text-property
2649      (point)
2650      (progn (eval gnus-summary-line-format-spec) (point))
2651      'gnus-number gnus-tmp-number)
2652     (when (gnus-visual-p 'summary-highlight 'highlight)
2653       (forward-line -1)
2654       (gnus-run-hooks 'gnus-summary-update-hook)
2655       (forward-line 1))))
2656
2657 (defun gnus-summary-update-line (&optional dont-update)
2658   ;; Update summary line after change.
2659   (when (and gnus-summary-default-score
2660              (not gnus-summary-inhibit-highlight))
2661     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2662            (article (gnus-summary-article-number))
2663            (score (gnus-summary-article-score article)))
2664       (unless dont-update
2665         (if (and gnus-summary-mark-below
2666                  (< (gnus-summary-article-score)
2667                     gnus-summary-mark-below))
2668             ;; This article has a low score, so we mark it as read.
2669             (when (memq article gnus-newsgroup-unreads)
2670               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2671           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2672             ;; This article was previously marked as read on account
2673             ;; of a low score, but now it has risen, so we mark it as
2674             ;; unread.
2675             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2676         (gnus-summary-update-mark
2677          (if (or (null gnus-summary-default-score)
2678                  (<= (abs (- score gnus-summary-default-score))
2679                      gnus-summary-zcore-fuzz))
2680              ?  ;Whitespace
2681            (if (< score gnus-summary-default-score)
2682                gnus-score-below-mark gnus-score-over-mark))
2683          'score))
2684       ;; Do visual highlighting.
2685       (when (gnus-visual-p 'summary-highlight 'highlight)
2686         (gnus-run-hooks 'gnus-summary-update-hook)))))
2687
2688 (defvar gnus-tmp-new-adopts nil)
2689
2690 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2691   "Return the number of articles in THREAD.
2692 This may be 0 in some cases -- if none of the articles in
2693 the thread are to be displayed."
2694   (let* ((number
2695           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2696           (cond
2697            ((not (listp thread))
2698             1)
2699            ((and (consp thread) (cdr thread))
2700             (apply
2701              '+ 1 (mapcar
2702                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2703            ((null thread)
2704             1)
2705            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2706             1)
2707            (t 0))))
2708     (when (and level (zerop level) gnus-tmp-new-adopts)
2709       (incf number
2710             (apply '+ (mapcar
2711                        'gnus-summary-number-of-articles-in-thread
2712                        gnus-tmp-new-adopts))))
2713     (if char
2714         (if (> number 1) gnus-not-empty-thread-mark
2715           gnus-empty-thread-mark)
2716       number)))
2717
2718 (defun gnus-summary-set-local-parameters (group)
2719   "Go through the local params of GROUP and set all variable specs in that list."
2720   (let ((params (gnus-group-find-parameter group))
2721         elem)
2722     (while params
2723       (setq elem (car params)
2724             params (cdr params))
2725       (and (consp elem)                 ; Has to be a cons.
2726            (consp (cdr elem))           ; The cdr has to be a list.
2727            (symbolp (car elem))         ; Has to be a symbol in there.
2728            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2729            (ignore-errors               ; So we set it.
2730              (make-local-variable (car elem))
2731              (set (car elem) (eval (nth 1 elem))))))))
2732
2733 (defun gnus-summary-read-group (group &optional show-all no-article
2734                                       kill-buffer no-display backward
2735                                       select-articles)
2736   "Start reading news in newsgroup GROUP.
2737 If SHOW-ALL is non-nil, already read articles are also listed.
2738 If NO-ARTICLE is non-nil, no article is selected initially.
2739 If NO-DISPLAY, don't generate a summary buffer."
2740   (let (result)
2741     (while (and group
2742                 (null (setq result
2743                             (let ((gnus-auto-select-next nil))
2744                               (or (gnus-summary-read-group-1
2745                                    group show-all no-article
2746                                    kill-buffer no-display
2747                                    select-articles)
2748                                   (setq show-all nil
2749                                         select-articles nil)))))
2750                 (eq gnus-auto-select-next 'quietly))
2751       (set-buffer gnus-group-buffer)
2752       ;; The entry function called above goes to the next
2753       ;; group automatically, so we go two groups back
2754       ;; if we are searching for the previous group.
2755       (when backward
2756         (gnus-group-prev-unread-group 2))
2757       (if (not (equal group (gnus-group-group-name)))
2758           (setq group (gnus-group-group-name))
2759         (setq group nil)))
2760     result))
2761
2762 (defun gnus-summary-read-group-1 (group show-all no-article
2763                                         kill-buffer no-display
2764                                         &optional select-articles)
2765   ;; Killed foreign groups can't be entered.
2766   (when (and (not (gnus-group-native-p group))
2767              (not (gnus-gethash group gnus-newsrc-hashtb)))
2768     (error "Dead non-native groups can't be entered"))
2769   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2770   (let* ((new-group (gnus-summary-setup-buffer group))
2771          (quit-config (gnus-group-quit-config group))
2772          (did-select (and new-group (gnus-select-newsgroup
2773                                      group show-all select-articles))))
2774     (cond
2775      ;; This summary buffer exists already, so we just select it.
2776      ((not new-group)
2777       (gnus-set-global-variables)
2778       (when kill-buffer
2779         (gnus-kill-or-deaden-summary kill-buffer))
2780       (gnus-configure-windows 'summary 'force)
2781       (gnus-set-mode-line 'summary)
2782       (gnus-summary-position-point)
2783       (message "")
2784       t)
2785      ;; We couldn't select this group.
2786      ((null did-select)
2787       (when (and (eq major-mode 'gnus-summary-mode)
2788                  (not (equal (current-buffer) kill-buffer)))
2789         (kill-buffer (current-buffer))
2790         (if (not quit-config)
2791             (progn
2792               ;; Update the info -- marks might need to be removed,
2793               ;; for instance.
2794               (gnus-summary-update-info)
2795               (set-buffer gnus-group-buffer)
2796               (gnus-group-jump-to-group group)
2797               (gnus-group-next-unread-group 1))
2798           (gnus-handle-ephemeral-exit quit-config)))
2799       (gnus-message 3 "Can't select group")
2800       nil)
2801      ;; The user did a `C-g' while prompting for number of articles,
2802      ;; so we exit this group.
2803      ((eq did-select 'quit)
2804       (and (eq major-mode 'gnus-summary-mode)
2805            (not (equal (current-buffer) kill-buffer))
2806            (kill-buffer (current-buffer)))
2807       (when kill-buffer
2808         (gnus-kill-or-deaden-summary kill-buffer))
2809       (if (not quit-config)
2810           (progn
2811             (set-buffer gnus-group-buffer)
2812             (gnus-group-jump-to-group group)
2813             (gnus-group-next-unread-group 1)
2814             (gnus-configure-windows 'group 'force))
2815         (gnus-handle-ephemeral-exit quit-config))
2816       ;; Finally signal the quit.
2817       (signal 'quit nil))
2818      ;; The group was successfully selected.
2819      (t
2820       (gnus-set-global-variables)
2821       ;; Save the active value in effect when the group was entered.
2822       (setq gnus-newsgroup-active
2823             (gnus-copy-sequence
2824              (gnus-active gnus-newsgroup-name)))
2825       ;; You can change the summary buffer in some way with this hook.
2826       (gnus-run-hooks 'gnus-select-group-hook)
2827       ;; Set any local variables in the group parameters.
2828       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2829       (gnus-update-format-specifications
2830        nil 'summary 'summary-mode 'summary-dummy)
2831       (gnus-update-summary-mark-positions)
2832       ;; Do score processing.
2833       (when gnus-use-scoring
2834         (gnus-possibly-score-headers))
2835       ;; Check whether to fill in the gaps in the threads.
2836       (when gnus-build-sparse-threads
2837         (gnus-build-sparse-threads))
2838       ;; Find the initial limit.
2839       (if gnus-show-threads
2840           (if show-all
2841               (let ((gnus-newsgroup-dormant nil))
2842                 (gnus-summary-initial-limit show-all))
2843             (gnus-summary-initial-limit show-all))
2844         ;; When untreaded, all articles are always shown.
2845         (setq gnus-newsgroup-limit
2846               (mapcar
2847                (lambda (header) (mail-header-number header))
2848                gnus-newsgroup-headers)))
2849       ;; Generate the summary buffer.
2850       (unless no-display
2851         (gnus-summary-prepare))
2852       (when gnus-use-trees
2853         (gnus-tree-open group)
2854         (setq gnus-summary-highlight-line-function
2855               'gnus-tree-highlight-article))
2856       ;; If the summary buffer is empty, but there are some low-scored
2857       ;; articles or some excluded dormants, we include these in the
2858       ;; buffer.
2859       (when (and (zerop (buffer-size))
2860                  (not no-display))
2861         (cond (gnus-newsgroup-dormant
2862                (gnus-summary-limit-include-dormant))
2863               ((and gnus-newsgroup-scored show-all)
2864                (gnus-summary-limit-include-expunged t))))
2865       ;; Function `gnus-apply-kill-file' must be called in this hook.
2866       (gnus-run-hooks 'gnus-apply-kill-hook)
2867       (if (and (zerop (buffer-size))
2868                (not no-display))
2869           (progn
2870             ;; This newsgroup is empty.
2871             (gnus-summary-catchup-and-exit nil t)
2872             (gnus-message 6 "No unread news")
2873             (when kill-buffer
2874               (gnus-kill-or-deaden-summary kill-buffer))
2875             ;; Return nil from this function.
2876             nil)
2877         ;; Hide conversation thread subtrees.  We cannot do this in
2878         ;; gnus-summary-prepare-hook since kill processing may not
2879         ;; work with hidden articles.
2880         (and gnus-show-threads
2881              gnus-thread-hide-subtree
2882              (gnus-summary-hide-all-threads))
2883         (when kill-buffer
2884           (gnus-kill-or-deaden-summary kill-buffer))
2885         ;; Show first unread article if requested.
2886         (if (and (not no-article)
2887                  (not no-display)
2888                  gnus-newsgroup-unreads
2889                  gnus-auto-select-first)
2890             (progn
2891               (gnus-configure-windows 'summary)
2892               (cond
2893                ((eq gnus-auto-select-first 'best)
2894                 (gnus-summary-best-unread-article))
2895                ((eq gnus-auto-select-first t)
2896                 (gnus-summary-first-unread-article))
2897                ((gnus-functionp gnus-auto-select-first)
2898                 (funcall gnus-auto-select-first))))
2899           ;; Don't select any articles, just move point to the first
2900           ;; article in the group.
2901           (goto-char (point-min))
2902           (gnus-summary-position-point)
2903           (gnus-configure-windows 'summary 'force)
2904           (gnus-set-mode-line 'summary))
2905         (when (get-buffer-window gnus-group-buffer t)
2906           ;; Gotta use windows, because recenter does weird stuff if
2907           ;; the current buffer ain't the displayed window.
2908           (let ((owin (selected-window)))
2909             (select-window (get-buffer-window gnus-group-buffer t))
2910             (when (gnus-group-goto-group group)
2911               (recenter))
2912             (select-window owin)))
2913         ;; Mark this buffer as "prepared".
2914         (setq gnus-newsgroup-prepared t)
2915         (gnus-run-hooks 'gnus-summary-prepared-hook)
2916         t)))))
2917
2918 (defun gnus-summary-prepare ()
2919   "Generate the summary buffer."
2920   (interactive)
2921   (let ((buffer-read-only nil))
2922     (erase-buffer)
2923     (setq gnus-newsgroup-data nil
2924           gnus-newsgroup-data-reverse nil)
2925     (gnus-run-hooks 'gnus-summary-generate-hook)
2926     ;; Generate the buffer, either with threads or without.
2927     (when gnus-newsgroup-headers
2928       (gnus-summary-prepare-threads
2929        (if gnus-show-threads
2930            (gnus-sort-gathered-threads
2931             (funcall gnus-summary-thread-gathering-function
2932                      (gnus-sort-threads
2933                       (gnus-cut-threads (gnus-make-threads)))))
2934          ;; Unthreaded display.
2935          (gnus-sort-articles gnus-newsgroup-headers))))
2936     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2937     ;; Call hooks for modifying summary buffer.
2938     (goto-char (point-min))
2939     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2940
2941 (defsubst gnus-general-simplify-subject (subject)
2942   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2943   (setq subject
2944         (cond
2945          ;; Truncate the subject.
2946          (gnus-simplify-subject-functions
2947           (gnus-map-function gnus-simplify-subject-functions subject))
2948          ((numberp gnus-summary-gather-subject-limit)
2949           (setq subject (gnus-simplify-subject-re subject))
2950           (if (> (length subject) gnus-summary-gather-subject-limit)
2951               (substring subject 0 gnus-summary-gather-subject-limit)
2952             subject))
2953          ;; Fuzzily simplify it.
2954          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2955           (gnus-simplify-subject-fuzzy subject))
2956          ;; Just remove the leading "Re:".
2957          (t
2958           (gnus-simplify-subject-re subject))))
2959
2960   (if (and gnus-summary-gather-exclude-subject
2961            (string-match gnus-summary-gather-exclude-subject subject))
2962       nil                               ; This article shouldn't be gathered
2963     subject))
2964
2965 (defun gnus-summary-simplify-subject-query ()
2966   "Query where the respool algorithm would put this article."
2967   (interactive)
2968   (gnus-summary-select-article)
2969   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2970
2971 (defun gnus-gather-threads-by-subject (threads)
2972   "Gather threads by looking at Subject headers."
2973   (if (not gnus-summary-make-false-root)
2974       threads
2975     (let ((hashtb (gnus-make-hashtable 1024))
2976           (prev threads)
2977           (result threads)
2978           subject hthread whole-subject)
2979       (while threads
2980         (setq subject (gnus-general-simplify-subject
2981                        (setq whole-subject (mail-header-subject
2982                                             (caar threads)))))
2983         (when subject
2984           (if (setq hthread (gnus-gethash subject hashtb))
2985               (progn
2986                 ;; We enter a dummy root into the thread, if we
2987                 ;; haven't done that already.
2988                 (unless (stringp (caar hthread))
2989                   (setcar hthread (list whole-subject (car hthread))))
2990                 ;; We add this new gathered thread to this gathered
2991                 ;; thread.
2992                 (setcdr (car hthread)
2993                         (nconc (cdar hthread) (list (car threads))))
2994                 ;; Remove it from the list of threads.
2995                 (setcdr prev (cdr threads))
2996                 (setq threads prev))
2997             ;; Enter this thread into the hash table.
2998             (gnus-sethash subject threads hashtb)))
2999         (setq prev threads)
3000         (setq threads (cdr threads)))
3001       result)))
3002
3003 (defun gnus-gather-threads-by-references (threads)
3004   "Gather threads by looking at References headers."
3005   (let ((idhashtb (gnus-make-hashtable 1024))
3006         (thhashtb (gnus-make-hashtable 1024))
3007         (prev threads)
3008         (result threads)
3009         ids references id gthread gid entered ref)
3010     (while threads
3011       (when (setq references (mail-header-references (caar threads)))
3012         (setq id (mail-header-id (caar threads))
3013               ids (gnus-split-references references)
3014               entered nil)
3015         (while (setq ref (pop ids))
3016           (setq ids (delete ref ids))
3017           (if (not (setq gid (gnus-gethash ref idhashtb)))
3018               (progn
3019                 (gnus-sethash ref id idhashtb)
3020                 (gnus-sethash id threads thhashtb))
3021             (setq gthread (gnus-gethash gid thhashtb))
3022             (unless entered
3023               ;; We enter a dummy root into the thread, if we
3024               ;; haven't done that already.
3025               (unless (stringp (caar gthread))
3026                 (setcar gthread (list (mail-header-subject (caar gthread))
3027                                       (car gthread))))
3028               ;; We add this new gathered thread to this gathered
3029               ;; thread.
3030               (setcdr (car gthread)
3031                       (nconc (cdar gthread) (list (car threads)))))
3032             ;; Add it into the thread hash table.
3033             (gnus-sethash id gthread thhashtb)
3034             (setq entered t)
3035             ;; Remove it from the list of threads.
3036             (setcdr prev (cdr threads))
3037             (setq threads prev))))
3038       (setq prev threads)
3039       (setq threads (cdr threads)))
3040     result))
3041
3042 (defun gnus-sort-gathered-threads (threads)
3043   "Sort subtreads inside each gathered thread by article number."
3044   (let ((result threads))
3045     (while threads
3046       (when (stringp (caar threads))
3047         (setcdr (car threads)
3048                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3049       (setq threads (cdr threads)))
3050     result))
3051
3052 (defun gnus-thread-loop-p (root thread)
3053   "Say whether ROOT is in THREAD."
3054   (let ((stack (list thread))
3055         (infloop 0)
3056         th)
3057     (while (setq thread (pop stack))
3058       (setq th (cdr thread))
3059       (while (and th
3060                   (not (eq (caar th) root)))
3061         (pop th))
3062       (if th
3063           ;; We have found a loop.
3064           (let (ref-dep)
3065             (setcdr thread (delq (car th) (cdr thread)))
3066             (if (boundp (setq ref-dep (intern "none"
3067                                               gnus-newsgroup-dependencies)))
3068                 (setcdr (symbol-value ref-dep)
3069                         (nconc (cdr (symbol-value ref-dep))
3070                                (list (car th))))
3071               (set ref-dep (list nil (car th))))
3072             (setq infloop 1
3073                   stack nil))
3074         ;; Push all the subthreads onto the stack.
3075         (push (cdr thread) stack)))
3076     infloop))
3077
3078 (defun gnus-make-threads ()
3079   "Go through the dependency hashtb and find the roots.  Return all threads."
3080   (let (threads)
3081     (while (catch 'infloop
3082              (mapatoms
3083               (lambda (refs)
3084                 ;; Deal with self-referencing References loops.
3085                 (when (and (car (symbol-value refs))
3086                            (not (zerop
3087                                  (apply
3088                                   '+
3089                                   (mapcar
3090                                    (lambda (thread)
3091                                      (gnus-thread-loop-p
3092                                       (car (symbol-value refs)) thread))
3093                                    (cdr (symbol-value refs)))))))
3094                   (setq threads nil)
3095                   (throw 'infloop t))
3096                 (unless (car (symbol-value refs))
3097                   ;; These threads do not refer back to any other articles,
3098                   ;; so they're roots.
3099                   (setq threads (append (cdr (symbol-value refs)) threads))))
3100               gnus-newsgroup-dependencies)))
3101     threads))
3102
3103 ;; Build the thread tree.
3104 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3105   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3106
3107 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3108 if it was already present.
3109
3110 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3111 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3112 Message-IDs will be renamed be renamed to a unique Message-ID before
3113 being entered.
3114
3115 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3116   (let* ((id (mail-header-id header))
3117          (id-dep (and id (intern id dependencies)))
3118          ref ref-dep ref-header)
3119     ;; Enter this `header' in the `dependencies' table.
3120     (cond
3121      ((not id-dep)
3122       (setq header nil))
3123      ;; The first two cases do the normal part: enter a new `header'
3124      ;; in the `dependencies' table.
3125      ((not (boundp id-dep))
3126       (set id-dep (list header)))
3127      ((null (car (symbol-value id-dep)))
3128       (setcar (symbol-value id-dep) header))
3129
3130      ;; From here the `header' was already present in the
3131      ;; `dependencies' table.
3132      (force-new
3133       ;; Overrides an existing entry;
3134       ;; just set the header part of the entry.
3135       (setcar (symbol-value id-dep) header))
3136
3137      ;; Renames the existing `header' to a unique Message-ID.
3138      ((not gnus-summary-ignore-duplicates)
3139       ;; An article with this Message-ID has already been seen.
3140       ;; We rename the Message-ID.
3141       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3142            (list header))
3143       (mail-header-set-id header id))
3144
3145      ;; The last case ignores an existing entry, except it adds any
3146      ;; additional Xrefs (in case the two articles came from different
3147      ;; servers.
3148      ;; Also sets `header' to `nil' meaning that the `dependencies'
3149      ;; table was *not* modified.
3150      (t
3151       (mail-header-set-xref
3152        (car (symbol-value id-dep))
3153        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3154                    "")
3155                (or (mail-header-xref header) "")))
3156       (setq header nil)))
3157
3158     (when header
3159       ;; First check if that we are not creating a References loop.
3160       (setq ref (gnus-parent-id (mail-header-references header)))
3161       (while (and ref
3162                   (setq ref-dep (intern-soft ref dependencies))
3163                   (boundp ref-dep)
3164                   (setq ref-header (car (symbol-value ref-dep))))
3165         (if (string= id ref)
3166             ;; Yuk!  This is a reference loop.  Make the article be a
3167             ;; root article.
3168             (progn
3169               (mail-header-set-references (car (symbol-value id-dep)) "none")
3170               (setq ref nil))
3171           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3172       (setq ref (gnus-parent-id (mail-header-references header)))
3173       (setq ref-dep (intern (or ref "none") dependencies))
3174       (if (boundp ref-dep)
3175           (setcdr (symbol-value ref-dep)
3176                   (nconc (cdr (symbol-value ref-dep))
3177                          (list (symbol-value id-dep))))
3178         (set ref-dep (list nil (symbol-value id-dep)))))
3179     header))
3180
3181 (defun gnus-build-sparse-threads ()
3182   (let ((headers gnus-newsgroup-headers)
3183         (mail-parse-charset gnus-newsgroup-charset)
3184         (gnus-summary-ignore-duplicates t)
3185         header references generation relations
3186         subject child end new-child date)
3187     ;; First we create an alist of generations/relations, where
3188     ;; generations is how much we trust the relation, and the relation
3189     ;; is parent/child.
3190     (gnus-message 7 "Making sparse threads...")
3191     (save-excursion
3192       (nnheader-set-temp-buffer " *gnus sparse threads*")
3193       (while (setq header (pop headers))
3194         (when (and (setq references (mail-header-references header))
3195                    (not (string= references "")))
3196           (insert references)
3197           (setq child (mail-header-id header)
3198                 subject (mail-header-subject header)
3199                 date (mail-header-date header)
3200                 generation 0)
3201           (while (search-backward ">" nil t)
3202             (setq end (1+ (point)))
3203             (when (search-backward "<" nil t)
3204               (setq new-child (buffer-substring (point) end))
3205               (push (list (incf generation)
3206                           child (setq child new-child)
3207                           subject date)
3208                     relations)))
3209           (when child
3210             (push (list (1+ generation) child nil subject) relations))
3211           (erase-buffer)))
3212       (kill-buffer (current-buffer)))
3213     ;; Sort over trustworthiness.
3214     (mapcar
3215      (lambda (relation)
3216        (when (gnus-dependencies-add-header
3217               (make-full-mail-header
3218                gnus-reffed-article-number
3219                (nth 3 relation) "" (or (nth 4 relation) "")
3220                (nth 1 relation)
3221                (or (nth 2 relation) "") 0 0 "")
3222               gnus-newsgroup-dependencies nil)
3223          (push gnus-reffed-article-number gnus-newsgroup-limit)
3224          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3225          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3226                gnus-newsgroup-reads)
3227          (decf gnus-reffed-article-number)))
3228      (sort relations 'car-less-than-car))
3229     (gnus-message 7 "Making sparse threads...done")))
3230
3231 (defun gnus-build-old-threads ()
3232   ;; Look at all the articles that refer back to old articles, and
3233   ;; fetch the headers for the articles that aren't there.  This will
3234   ;; build complete threads - if the roots haven't been expired by the
3235   ;; server, that is.
3236   (let ((mail-parse-charset gnus-newsgroup-charset)
3237         id heads)
3238     (mapatoms
3239      (lambda (refs)
3240        (when (not (car (symbol-value refs)))
3241          (setq heads (cdr (symbol-value refs)))
3242          (while heads
3243            (if (memq (mail-header-number (caar heads))
3244                      gnus-newsgroup-dormant)
3245                (setq heads (cdr heads))
3246              (setq id (symbol-name refs))
3247              (while (and (setq id (gnus-build-get-header id))
3248                          (not (car (gnus-id-to-thread id)))))
3249              (setq heads nil)))))
3250      gnus-newsgroup-dependencies)))
3251
3252 ;; This function has to be called with point after the article number
3253 ;; on the beginning of the line.
3254 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3255   (let ((eol (gnus-point-at-eol))
3256         (buffer (current-buffer))
3257         header)
3258
3259     ;; overview: [num subject from date id refs chars lines misc]
3260     (unwind-protect
3261         (progn
3262           (narrow-to-region (point) eol)
3263           (unless (eobp)
3264             (forward-char))
3265
3266           (setq header
3267                 (make-full-mail-header
3268                  number                 ; number
3269                  (funcall gnus-decode-encoded-word-function
3270                           (nnheader-nov-field)) ; subject
3271                  (funcall gnus-decode-encoded-word-function
3272                           (nnheader-nov-field)) ; from
3273                  (nnheader-nov-field)   ; date
3274                  (nnheader-nov-read-message-id) ; id
3275                  (nnheader-nov-field)   ; refs
3276                  (nnheader-nov-read-integer) ; chars
3277                  (nnheader-nov-read-integer) ; lines
3278                  (unless (eobp)
3279                    (nnheader-nov-field)) ; misc
3280                  (nnheader-nov-parse-extra)))) ; extra
3281
3282       (widen))
3283
3284     (when gnus-alter-header-function
3285       (funcall gnus-alter-header-function header))
3286     (gnus-dependencies-add-header header dependencies force-new)))
3287
3288 (defun gnus-build-get-header (id)
3289   ;; Look through the buffer of NOV lines and find the header to
3290   ;; ID.  Enter this line into the dependencies hash table, and return
3291   ;; the id of the parent article (if any).
3292   (let ((deps gnus-newsgroup-dependencies)
3293         found header)
3294     (prog1
3295         (save-excursion
3296           (set-buffer nntp-server-buffer)
3297           (let ((case-fold-search nil))
3298             (goto-char (point-min))
3299             (while (and (not found)
3300                         (search-forward id nil t))
3301               (beginning-of-line)
3302               (setq found (looking-at
3303                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3304                                    (regexp-quote id))))
3305               (or found (beginning-of-line 2)))
3306             (when found
3307               (beginning-of-line)
3308               (and
3309                (setq header (gnus-nov-parse-line
3310                              (read (current-buffer)) deps))
3311                (gnus-parent-id (mail-header-references header))))))
3312       (when header
3313         (let ((number (mail-header-number header)))
3314           (push number gnus-newsgroup-limit)
3315           (push header gnus-newsgroup-headers)
3316           (if (memq number gnus-newsgroup-unselected)
3317               (progn
3318                 (push number gnus-newsgroup-unreads)
3319                 (setq gnus-newsgroup-unselected
3320                       (delq number gnus-newsgroup-unselected)))
3321             (push number gnus-newsgroup-ancient)))))))
3322
3323 (defun gnus-build-all-threads ()
3324   "Read all the headers."
3325   (let ((gnus-summary-ignore-duplicates t)
3326         (mail-parse-charset gnus-newsgroup-charset)
3327         (dependencies gnus-newsgroup-dependencies)
3328         header article)
3329     (save-excursion
3330       (set-buffer nntp-server-buffer)
3331       (let ((case-fold-search nil))
3332         (goto-char (point-min))
3333         (while (not (eobp))
3334           (ignore-errors
3335             (setq article (read (current-buffer))
3336                   header (gnus-nov-parse-line article dependencies)))
3337           (when header
3338             (save-excursion
3339               (set-buffer gnus-summary-buffer)
3340               (push header gnus-newsgroup-headers)
3341               (if (memq (setq article (mail-header-number header))
3342                         gnus-newsgroup-unselected)
3343                   (progn
3344                     (push article gnus-newsgroup-unreads)
3345                     (setq gnus-newsgroup-unselected
3346                           (delq article gnus-newsgroup-unselected)))
3347                 (push article gnus-newsgroup-ancient)))
3348             (forward-line 1)))))))
3349
3350 (defun gnus-summary-update-article-line (article header)
3351   "Update the line for ARTICLE using HEADERS."
3352   (let* ((id (mail-header-id header))
3353          (thread (gnus-id-to-thread id)))
3354     (unless thread
3355       (error "Article in no thread"))
3356     ;; Update the thread.
3357     (setcar thread header)
3358     (gnus-summary-goto-subject article)
3359     (let* ((datal (gnus-data-find-list article))
3360            (data (car datal))
3361            (length (when (cdr datal)
3362                      (- (gnus-data-pos data)
3363                         (gnus-data-pos (cadr datal)))))
3364            (buffer-read-only nil)
3365            (level (gnus-summary-thread-level)))
3366       (gnus-delete-line)
3367       (gnus-summary-insert-line
3368        header level nil (gnus-article-mark article)
3369        (memq article gnus-newsgroup-replied)
3370        (memq article gnus-newsgroup-expirable)
3371        ;; Only insert the Subject string when it's different
3372        ;; from the previous Subject string.
3373        (if (gnus-subject-equal
3374             (condition-case ()
3375                 (mail-header-subject
3376                  (gnus-data-header
3377                   (cadr
3378                    (gnus-data-find-list
3379                     article
3380                     (gnus-data-list t)))))
3381               ;; Error on the side of excessive subjects.
3382               (error ""))
3383             (mail-header-subject header))
3384            ""
3385          (mail-header-subject header))
3386        nil (cdr (assq article gnus-newsgroup-scored))
3387        (memq article gnus-newsgroup-processable))
3388       (when length
3389         (gnus-data-update-list
3390          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3391
3392 (defun gnus-summary-update-article (article &optional iheader)
3393   "Update ARTICLE in the summary buffer."
3394   (set-buffer gnus-summary-buffer)
3395   (let* ((header (gnus-summary-article-header article))
3396          (id (mail-header-id header))
3397          (data (gnus-data-find article))
3398          (thread (gnus-id-to-thread id))
3399          (references (mail-header-references header))
3400          (parent
3401           (gnus-id-to-thread
3402            (or (gnus-parent-id
3403                 (when (and references
3404                            (not (equal "" references)))
3405                   references))
3406                "none")))
3407          (buffer-read-only nil)
3408          (old (car thread)))
3409     (when thread
3410       (unless iheader
3411         (setcar thread nil)
3412         (when parent
3413           (delq thread parent)))
3414       (if (gnus-summary-insert-subject id header)
3415           ;; Set the (possibly) new article number in the data structure.
3416           (gnus-data-set-number data (gnus-id-to-article id))
3417         (setcar thread old)
3418         nil))))
3419
3420 (defun gnus-rebuild-thread (id &optional line)
3421   "Rebuild the thread containing ID.
3422 If LINE, insert the rebuilt thread starting on line LINE."
3423   (let ((buffer-read-only nil)
3424         old-pos current thread data)
3425     (if (not gnus-show-threads)
3426         (setq thread (list (car (gnus-id-to-thread id))))
3427       ;; Get the thread this article is part of.
3428       (setq thread (gnus-remove-thread id)))
3429     (setq old-pos (gnus-point-at-bol))
3430     (setq current (save-excursion
3431                     (and (zerop (forward-line -1))
3432                          (gnus-summary-article-number))))
3433     ;; If this is a gathered thread, we have to go some re-gathering.
3434     (when (stringp (car thread))
3435       (let ((subject (car thread))
3436             roots thr)
3437         (setq thread (cdr thread))
3438         (while thread
3439           (unless (memq (setq thr (gnus-id-to-thread
3440                                    (gnus-root-id
3441                                     (mail-header-id (caar thread)))))
3442                         roots)
3443             (push thr roots))
3444           (setq thread (cdr thread)))
3445         ;; We now have all (unique) roots.
3446         (if (= (length roots) 1)
3447             ;; All the loose roots are now one solid root.
3448             (setq thread (car roots))
3449           (setq thread (cons subject (gnus-sort-threads roots))))))
3450     (let (threads)
3451       ;; We then insert this thread into the summary buffer.
3452       (when line
3453         (goto-char (point-min))
3454         (forward-line (1- line)))
3455       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3456         (if gnus-show-threads
3457             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3458           (gnus-summary-prepare-unthreaded thread))
3459         (setq data (nreverse gnus-newsgroup-data))
3460         (setq threads gnus-newsgroup-threads))
3461       ;; We splice the new data into the data structure.
3462       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3463       ;;!!! then we want to insert at the beginning of the buffer.
3464       ;;!!! That happens to be true with Gnus now, but that may
3465       ;;!!! change in the future.  Perhaps.
3466       (gnus-data-enter-list
3467        (if line nil current) data (- (point) old-pos))
3468       (setq gnus-newsgroup-threads
3469             (nconc threads gnus-newsgroup-threads))
3470       (gnus-data-compute-positions))))
3471
3472 (defun gnus-number-to-header (number)
3473   "Return the header for article NUMBER."
3474   (let ((headers gnus-newsgroup-headers))
3475     (while (and headers
3476                 (not (= number (mail-header-number (car headers)))))
3477       (pop headers))
3478     (when headers
3479       (car headers))))
3480
3481 (defun gnus-parent-headers (in-headers &optional generation)
3482   "Return the headers of the GENERATIONeth parent of HEADERS."
3483   (unless generation
3484     (setq generation 1))
3485   (let ((parent t)
3486         (headers in-headers)
3487         references)
3488     (while (and parent
3489                 (not (zerop generation))
3490                 (setq references (mail-header-references headers)))
3491       (setq headers (if (and references
3492                              (setq parent (gnus-parent-id references)))
3493                         (car (gnus-id-to-thread parent))
3494                       nil))
3495       (decf generation))
3496     (and (not (eq headers in-headers))
3497          headers)))
3498
3499 (defun gnus-id-to-thread (id)
3500   "Return the (sub-)thread where ID appears."
3501   (gnus-gethash id gnus-newsgroup-dependencies))
3502
3503 (defun gnus-id-to-article (id)
3504   "Return the article number of ID."
3505   (let ((thread (gnus-id-to-thread id)))
3506     (when (and thread
3507                (car thread))
3508       (mail-header-number (car thread)))))
3509
3510 (defun gnus-id-to-header (id)
3511   "Return the article headers of ID."
3512   (car (gnus-id-to-thread id)))
3513
3514 (defun gnus-article-displayed-root-p (article)
3515   "Say whether ARTICLE is a root(ish) article."
3516   (let ((level (gnus-summary-thread-level article))
3517         (refs (mail-header-references  (gnus-summary-article-header article)))
3518         particle)
3519     (cond
3520      ((null level) nil)
3521      ((zerop level) t)
3522      ((null refs) t)
3523      ((null (gnus-parent-id refs)) t)
3524      ((and (= 1 level)
3525            (null (setq particle (gnus-id-to-article
3526                                  (gnus-parent-id refs))))
3527            (null (gnus-summary-thread-level particle)))))))
3528
3529 (defun gnus-root-id (id)
3530   "Return the id of the root of the thread where ID appears."
3531   (let (last-id prev)
3532     (while (and id (setq prev (car (gnus-id-to-thread id))))
3533       (setq last-id id
3534             id (gnus-parent-id (mail-header-references prev))))
3535     last-id))
3536
3537 (defun gnus-articles-in-thread (thread)
3538   "Return the list of articles in THREAD."
3539   (cons (mail-header-number (car thread))
3540         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3541
3542 (defun gnus-remove-thread (id &optional dont-remove)
3543   "Remove the thread that has ID in it."
3544   (let (headers thread last-id)
3545     ;; First go up in this thread until we find the root.
3546     (setq last-id (gnus-root-id id)
3547           headers (message-flatten-list (gnus-id-to-thread last-id)))
3548     ;; We have now found the real root of this thread.  It might have
3549     ;; been gathered into some loose thread, so we have to search
3550     ;; through the threads to find the thread we wanted.
3551     (let ((threads gnus-newsgroup-threads)
3552           sub)
3553       (while threads
3554         (setq sub (car threads))
3555         (if (stringp (car sub))
3556             ;; This is a gathered thread, so we look at the roots
3557             ;; below it to find whether this article is in this
3558             ;; gathered root.
3559             (progn
3560               (setq sub (cdr sub))
3561               (while sub
3562                 (when (member (caar sub) headers)
3563                   (setq thread (car threads)
3564                         threads nil
3565                         sub nil))
3566                 (setq sub (cdr sub))))
3567           ;; It's an ordinary thread, so we check it.
3568           (when (eq (car sub) (car headers))
3569             (setq thread sub
3570                   threads nil)))
3571         (setq threads (cdr threads)))
3572       ;; If this article is in no thread, then it's a root.
3573       (if thread
3574           (unless dont-remove
3575             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3576         (setq thread (gnus-id-to-thread last-id)))
3577       (when thread
3578         (prog1
3579             thread                      ; We return this thread.
3580           (unless dont-remove
3581             (if (stringp (car thread))
3582                 (progn
3583                   ;; If we use dummy roots, then we have to remove the
3584                   ;; dummy root as well.
3585                   (when (eq gnus-summary-make-false-root 'dummy)
3586                     ;; We go to the dummy root by going to
3587                     ;; the first sub-"thread", and then one line up.
3588                     (gnus-summary-goto-article
3589                      (mail-header-number (caadr thread)))
3590                     (forward-line -1)
3591                     (gnus-delete-line)
3592                     (gnus-data-compute-positions))
3593                   (setq thread (cdr thread))
3594                   (while thread
3595                     (gnus-remove-thread-1 (car thread))
3596                     (setq thread (cdr thread))))
3597               (gnus-summary-show-all-threads)
3598               (gnus-remove-thread-1 thread))))))))
3599
3600 (defun gnus-remove-thread-1 (thread)
3601   "Remove the thread THREAD recursively."
3602   (let ((number (mail-header-number (pop thread)))
3603         d)
3604     (setq thread (reverse thread))
3605     (while thread
3606       (gnus-remove-thread-1 (pop thread)))
3607     (when (setq d (gnus-data-find number))
3608       (goto-char (gnus-data-pos d))
3609       (gnus-data-remove
3610        number
3611        (- (gnus-point-at-bol)
3612           (prog1
3613               (1+ (gnus-point-at-eol))
3614             (gnus-delete-line)))))))
3615
3616 (defun gnus-sort-threads (threads)
3617   "Sort THREADS."
3618   (if (not gnus-thread-sort-functions)
3619       threads
3620     (gnus-message 8 "Sorting threads...")
3621     (prog1
3622         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3623       (gnus-message 8 "Sorting threads...done"))))
3624
3625 (defun gnus-sort-articles (articles)
3626   "Sort ARTICLES."
3627   (when gnus-article-sort-functions
3628     (gnus-message 7 "Sorting articles...")
3629     (prog1
3630         (setq gnus-newsgroup-headers
3631               (sort articles (gnus-make-sort-function
3632                               gnus-article-sort-functions)))
3633       (gnus-message 7 "Sorting articles...done"))))
3634
3635 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3636 (defmacro gnus-thread-header (thread)
3637   ;; Return header of first article in THREAD.
3638   ;; Note that THREAD must never, ever be anything else than a variable -
3639   ;; using some other form will lead to serious barfage.
3640   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3641   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3642   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3643         (vector thread) 2))
3644
3645 (defsubst gnus-article-sort-by-number (h1 h2)
3646   "Sort articles by article number."
3647   (< (mail-header-number h1)
3648      (mail-header-number h2)))
3649
3650 (defun gnus-thread-sort-by-number (h1 h2)
3651   "Sort threads by root article number."
3652   (gnus-article-sort-by-number
3653    (gnus-thread-header h1) (gnus-thread-header h2)))
3654
3655 (defsubst gnus-article-sort-by-lines (h1 h2)
3656   "Sort articles by article Lines header."
3657   (< (mail-header-lines h1)
3658      (mail-header-lines h2)))
3659
3660 (defun gnus-thread-sort-by-lines (h1 h2)
3661   "Sort threads by root article Lines header."
3662   (gnus-article-sort-by-lines
3663    (gnus-thread-header h1) (gnus-thread-header h2)))
3664
3665 (defsubst gnus-article-sort-by-chars (h1 h2)
3666   "Sort articles by octet length."
3667   (< (mail-header-chars h1)
3668      (mail-header-chars h2)))
3669
3670 (defun gnus-thread-sort-by-chars (h1 h2)
3671   "Sort threads by root article octet length."
3672   (gnus-article-sort-by-chars
3673    (gnus-thread-header h1) (gnus-thread-header h2)))
3674
3675 (defsubst gnus-article-sort-by-author (h1 h2)
3676   "Sort articles by root author."
3677   (string-lessp
3678    (let ((extract (funcall
3679                    gnus-extract-address-components
3680                    (mail-header-from h1))))
3681      (or (car extract) (cadr extract) ""))
3682    (let ((extract (funcall
3683                    gnus-extract-address-components
3684                    (mail-header-from h2))))
3685      (or (car extract) (cadr extract) ""))))
3686
3687 (defun gnus-thread-sort-by-author (h1 h2)
3688   "Sort threads by root author."
3689   (gnus-article-sort-by-author
3690    (gnus-thread-header h1)  (gnus-thread-header h2)))
3691
3692 (defsubst gnus-article-sort-by-subject (h1 h2)
3693   "Sort articles by root subject."
3694   (string-lessp
3695    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3696    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3697
3698 (defun gnus-thread-sort-by-subject (h1 h2)
3699   "Sort threads by root subject."
3700   (gnus-article-sort-by-subject
3701    (gnus-thread-header h1) (gnus-thread-header h2)))
3702
3703 (defsubst gnus-article-sort-by-date (h1 h2)
3704   "Sort articles by root article date."
3705   (time-less-p
3706    (gnus-date-get-time (mail-header-date h1))
3707    (gnus-date-get-time (mail-header-date h2))))
3708
3709 (defun gnus-thread-sort-by-date (h1 h2)
3710   "Sort threads by root article date."
3711   (gnus-article-sort-by-date
3712    (gnus-thread-header h1) (gnus-thread-header h2)))
3713
3714 (defsubst gnus-article-sort-by-score (h1 h2)
3715   "Sort articles by root article score.
3716 Unscored articles will be counted as having a score of zero."
3717   (> (or (cdr (assq (mail-header-number h1)
3718                     gnus-newsgroup-scored))
3719          gnus-summary-default-score 0)
3720      (or (cdr (assq (mail-header-number h2)
3721                     gnus-newsgroup-scored))
3722          gnus-summary-default-score 0)))
3723
3724 (defun gnus-thread-sort-by-score (h1 h2)
3725   "Sort threads by root article score."
3726   (gnus-article-sort-by-score
3727    (gnus-thread-header h1) (gnus-thread-header h2)))
3728
3729 (defun gnus-thread-sort-by-total-score (h1 h2)
3730   "Sort threads by the sum of all scores in the thread.
3731 Unscored articles will be counted as having a score of zero."
3732   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3733
3734 (defun gnus-thread-total-score (thread)
3735   ;; This function find the total score of THREAD.
3736   (cond ((null thread)
3737          0)
3738         ((consp thread)
3739          (if (stringp (car thread))
3740              (apply gnus-thread-score-function 0
3741                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3742            (gnus-thread-total-score-1 thread)))
3743         (t
3744          (gnus-thread-total-score-1 (list thread)))))
3745
3746 (defun gnus-thread-total-score-1 (root)
3747   ;; This function find the total score of the thread below ROOT.
3748   (setq root (car root))
3749   (apply gnus-thread-score-function
3750          (or (append
3751               (mapcar 'gnus-thread-total-score
3752                       (cdr (gnus-id-to-thread (mail-header-id root))))
3753               (when (> (mail-header-number root) 0)
3754                 (list (or (cdr (assq (mail-header-number root)
3755                                      gnus-newsgroup-scored))
3756                           gnus-summary-default-score 0))))
3757              (list gnus-summary-default-score)
3758              '(0))))
3759
3760 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3761 (defvar gnus-tmp-prev-subject nil)
3762 (defvar gnus-tmp-false-parent nil)
3763 (defvar gnus-tmp-root-expunged nil)
3764 (defvar gnus-tmp-dummy-line nil)
3765
3766 (defvar gnus-tmp-header)
3767 (defun gnus-extra-header (type &optional header)
3768   "Return the extra header of TYPE."
3769   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3770       ""))
3771
3772 (defun gnus-summary-prepare-threads (threads)
3773   "Prepare summary buffer from THREADS and indentation LEVEL.
3774 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3775 or a straight list of headers."
3776   (gnus-message 7 "Generating summary...")
3777
3778   (setq gnus-newsgroup-threads threads)
3779   (beginning-of-line)
3780
3781   (let ((gnus-tmp-level 0)
3782         (default-score (or gnus-summary-default-score 0))
3783         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3784         thread number subject stack state gnus-tmp-gathered beg-match
3785         new-roots gnus-tmp-new-adopts thread-end
3786         gnus-tmp-header gnus-tmp-unread
3787         gnus-tmp-replied gnus-tmp-subject-or-nil
3788         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3789         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3790         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3791
3792     (setq gnus-tmp-prev-subject nil)
3793
3794     (if (vectorp (car threads))
3795         ;; If this is a straight (sic) list of headers, then a
3796         ;; threaded summary display isn't required, so we just create
3797         ;; an unthreaded one.
3798         (gnus-summary-prepare-unthreaded threads)
3799
3800       ;; Do the threaded display.
3801
3802       (while (or threads stack gnus-tmp-new-adopts new-roots)
3803
3804         (if (and (= gnus-tmp-level 0)
3805                  (or (not stack)
3806                      (= (caar stack) 0))
3807                  (not gnus-tmp-false-parent)
3808                  (or gnus-tmp-new-adopts new-roots))
3809             (if gnus-tmp-new-adopts
3810                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3811                       thread (list (car gnus-tmp-new-adopts))
3812                       gnus-tmp-header (caar thread)
3813                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3814               (when new-roots
3815                 (setq thread (list (car new-roots))
3816                       gnus-tmp-header (caar thread)
3817                       new-roots (cdr new-roots))))
3818
3819           (if threads
3820               ;; If there are some threads, we do them before the
3821               ;; threads on the stack.
3822               (setq thread threads
3823                     gnus-tmp-header (caar thread))
3824             ;; There were no current threads, so we pop something off
3825             ;; the stack.
3826             (setq state (car stack)
3827                   gnus-tmp-level (car state)
3828                   thread (cdr state)
3829                   stack (cdr stack)
3830                   gnus-tmp-header (caar thread))))
3831
3832         (setq gnus-tmp-false-parent nil)
3833         (setq gnus-tmp-root-expunged nil)
3834         (setq thread-end nil)
3835
3836         (if (stringp gnus-tmp-header)
3837             ;; The header is a dummy root.
3838             (cond
3839              ((eq gnus-summary-make-false-root 'adopt)
3840               ;; We let the first article adopt the rest.
3841               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3842                                                (cddar thread)))
3843               (setq gnus-tmp-gathered
3844                     (nconc (mapcar
3845                             (lambda (h) (mail-header-number (car h)))
3846                             (cddar thread))
3847                            gnus-tmp-gathered))
3848               (setq thread (cons (list (caar thread)
3849                                        (cadar thread))
3850                                  (cdr thread)))
3851               (setq gnus-tmp-level -1
3852                     gnus-tmp-false-parent t))
3853              ((eq gnus-summary-make-false-root 'empty)
3854               ;; We print adopted articles with empty subject fields.
3855               (setq gnus-tmp-gathered
3856                     (nconc (mapcar
3857                             (lambda (h) (mail-header-number (car h)))
3858                             (cddar thread))
3859                            gnus-tmp-gathered))
3860               (setq gnus-tmp-level -1))
3861              ((eq gnus-summary-make-false-root 'dummy)
3862               ;; We remember that we probably want to output a dummy
3863               ;; root.
3864               (setq gnus-tmp-dummy-line gnus-tmp-header)
3865               (setq gnus-tmp-prev-subject gnus-tmp-header))
3866              (t
3867               ;; We do not make a root for the gathered
3868               ;; sub-threads at all.
3869               (setq gnus-tmp-level -1)))
3870
3871           (setq number (mail-header-number gnus-tmp-header)
3872                 subject (mail-header-subject gnus-tmp-header))
3873
3874           (cond
3875            ;; If the thread has changed subject, we might want to make
3876            ;; this subthread into a root.
3877            ((and (null gnus-thread-ignore-subject)
3878                  (not (zerop gnus-tmp-level))
3879                  gnus-tmp-prev-subject
3880                  (not (inline
3881                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3882             (setq new-roots (nconc new-roots (list (car thread)))
3883                   thread-end t
3884                   gnus-tmp-header nil))
3885            ;; If the article lies outside the current limit,
3886            ;; then we do not display it.
3887            ((not (memq number gnus-newsgroup-limit))
3888             (setq gnus-tmp-gathered
3889                   (nconc (mapcar
3890                           (lambda (h) (mail-header-number (car h)))
3891                           (cdar thread))
3892                          gnus-tmp-gathered))
3893             (setq gnus-tmp-new-adopts (if (cdar thread)
3894                                           (append gnus-tmp-new-adopts
3895                                                   (cdar thread))
3896                                         gnus-tmp-new-adopts)
3897                   thread-end t
3898                   gnus-tmp-header nil)
3899             (when (zerop gnus-tmp-level)
3900               (setq gnus-tmp-root-expunged t)))
3901            ;; Perhaps this article is to be marked as read?
3902            ((and gnus-summary-mark-below
3903                  (< (or (cdr (assq number gnus-newsgroup-scored))
3904                         default-score)
3905                     gnus-summary-mark-below)
3906                  ;; Don't touch sparse articles.
3907                  (not (gnus-summary-article-sparse-p number))
3908                  (not (gnus-summary-article-ancient-p number)))
3909             (setq gnus-newsgroup-unreads
3910                   (delq number gnus-newsgroup-unreads))
3911             (if gnus-newsgroup-auto-expire
3912                 (push number gnus-newsgroup-expirable)
3913               (push (cons number gnus-low-score-mark)
3914                     gnus-newsgroup-reads))))
3915
3916           (when gnus-tmp-header
3917             ;; We may have an old dummy line to output before this
3918             ;; article.
3919             (when (and gnus-tmp-dummy-line
3920                        (gnus-subject-equal
3921                         gnus-tmp-dummy-line
3922                         (mail-header-subject gnus-tmp-header)))
3923               (gnus-summary-insert-dummy-line
3924                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3925               (setq gnus-tmp-dummy-line nil))
3926
3927             ;; Compute the mark.
3928             (setq gnus-tmp-unread (gnus-article-mark number))
3929
3930             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3931                                   gnus-tmp-header gnus-tmp-level)
3932                   gnus-newsgroup-data)
3933
3934             ;; Actually insert the line.
3935             (setq
3936              gnus-tmp-subject-or-nil
3937              (cond
3938               ((and gnus-thread-ignore-subject
3939                     gnus-tmp-prev-subject
3940                     (not (inline (gnus-subject-equal
3941                                   gnus-tmp-prev-subject subject))))
3942                subject)
3943               ((zerop gnus-tmp-level)
3944                (if (and (eq gnus-summary-make-false-root 'empty)
3945                         (memq number gnus-tmp-gathered)
3946                         gnus-tmp-prev-subject
3947                         (inline (gnus-subject-equal
3948                                  gnus-tmp-prev-subject subject)))
3949                    gnus-summary-same-subject
3950                  subject))
3951               (t gnus-summary-same-subject)))
3952             (if (and (eq gnus-summary-make-false-root 'adopt)
3953                      (= gnus-tmp-level 1)
3954                      (memq number gnus-tmp-gathered))
3955                 (setq gnus-tmp-opening-bracket ?\<
3956                       gnus-tmp-closing-bracket ?\>)
3957               (setq gnus-tmp-opening-bracket ?\[
3958                     gnus-tmp-closing-bracket ?\]))
3959             (setq
3960              gnus-tmp-indentation
3961              (aref gnus-thread-indent-array gnus-tmp-level)
3962              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3963              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3964                                 gnus-summary-default-score 0)
3965              gnus-tmp-score-char
3966              (if (or (null gnus-summary-default-score)
3967                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3968                          gnus-summary-zcore-fuzz))
3969                  ?  ;Whitespace
3970                (if (< gnus-tmp-score gnus-summary-default-score)
3971                    gnus-score-below-mark gnus-score-over-mark))
3972              gnus-tmp-replied
3973              (cond ((memq number gnus-newsgroup-processable)
3974                     gnus-process-mark)
3975                    ((memq number gnus-newsgroup-cached)
3976                     gnus-cached-mark)
3977                    ((memq number gnus-newsgroup-replied)
3978                     gnus-replied-mark)
3979                    ((memq number gnus-newsgroup-saved)
3980                     gnus-saved-mark)
3981                    (t gnus-unread-mark))
3982              gnus-tmp-from (mail-header-from gnus-tmp-header)
3983              gnus-tmp-name
3984              (cond
3985               ((string-match "<[^>]+> *$" gnus-tmp-from)
3986                (setq beg-match (match-beginning 0))
3987                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3988                         (substring gnus-tmp-from (1+ (match-beginning 0))
3989                                    (1- (match-end 0))))
3990                    (substring gnus-tmp-from 0 beg-match)))
3991               ((string-match "(.+)" gnus-tmp-from)
3992                (substring gnus-tmp-from
3993                           (1+ (match-beginning 0)) (1- (match-end 0))))
3994               (t gnus-tmp-from)))
3995             (when (string= gnus-tmp-name "")
3996               (setq gnus-tmp-name gnus-tmp-from))
3997             (unless (numberp gnus-tmp-lines)
3998               (setq gnus-tmp-lines 0))
3999             (gnus-put-text-property
4000              (point)
4001              (progn (eval gnus-summary-line-format-spec) (point))
4002              'gnus-number number)
4003             (when gnus-visual-p
4004               (forward-line -1)
4005               (gnus-run-hooks 'gnus-summary-update-hook)
4006               (forward-line 1))
4007
4008             (setq gnus-tmp-prev-subject subject)))
4009
4010         (when (nth 1 thread)
4011           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4012         (incf gnus-tmp-level)
4013         (setq threads (if thread-end nil (cdar thread)))
4014         (unless threads
4015           (setq gnus-tmp-level 0)))))
4016   (gnus-message 7 "Generating summary...done"))
4017
4018 (defun gnus-summary-prepare-unthreaded (headers)
4019   "Generate an unthreaded summary buffer based on HEADERS."
4020   (let (header number mark)
4021
4022     (beginning-of-line)
4023
4024     (while headers
4025       ;; We may have to root out some bad articles...
4026       (when (memq (setq number (mail-header-number
4027                                 (setq header (pop headers))))
4028                   gnus-newsgroup-limit)
4029         ;; Mark article as read when it has a low score.
4030         (when (and gnus-summary-mark-below
4031                    (< (or (cdr (assq number gnus-newsgroup-scored))
4032                           gnus-summary-default-score 0)
4033                       gnus-summary-mark-below)
4034                    (not (gnus-summary-article-ancient-p number)))
4035           (setq gnus-newsgroup-unreads
4036                 (delq number gnus-newsgroup-unreads))
4037           (if gnus-newsgroup-auto-expire
4038               (push number gnus-newsgroup-expirable)
4039             (push (cons number gnus-low-score-mark)
4040                   gnus-newsgroup-reads)))
4041
4042         (setq mark (gnus-article-mark number))
4043         (push (gnus-data-make number mark (1+ (point)) header 0)
4044               gnus-newsgroup-data)
4045         (gnus-summary-insert-line
4046          header 0 number
4047          mark (memq number gnus-newsgroup-replied)
4048          (memq number gnus-newsgroup-expirable)
4049          (mail-header-subject header) nil
4050          (cdr (assq number gnus-newsgroup-scored))
4051          (memq number gnus-newsgroup-processable))))))
4052
4053 (defun gnus-summary-remove-list-identifiers ()
4054   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4055   (let ((regexp (if (stringp gnus-list-identifiers)
4056                     gnus-list-identifiers
4057                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4058     (dolist (header gnus-newsgroup-headers)
4059       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4060                           (mail-header-subject header))
4061         (mail-header-set-subject
4062          header (concat (substring (mail-header-subject header)
4063                                    0 (match-beginning 2))
4064                         (substring (mail-header-subject header)
4065                                    (match-end 2))))))))
4066
4067 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4068   "Select newsgroup GROUP.
4069 If READ-ALL is non-nil, all articles in the group are selected.
4070 If SELECT-ARTICLES, only select those articles from GROUP."
4071   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4072          ;;!!! Dirty hack; should be removed.
4073          (gnus-summary-ignore-duplicates
4074           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4075               t
4076             gnus-summary-ignore-duplicates))
4077          (info (nth 2 entry))
4078          articles fetched-articles cached)
4079
4080     (unless (gnus-check-server
4081              (setq gnus-current-select-method
4082                    (gnus-find-method-for-group group)))
4083       (error "Couldn't open server"))
4084
4085     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4086         (gnus-activate-group group)     ; Or we can activate it...
4087         (progn                          ; Or we bug out.
4088           (when (equal major-mode 'gnus-summary-mode)
4089             (kill-buffer (current-buffer)))
4090           (error "Couldn't request group %s: %s"
4091                  group (gnus-status-message group))))
4092
4093     (unless (gnus-request-group group t)
4094       (when (equal major-mode 'gnus-summary-mode)
4095         (kill-buffer (current-buffer)))
4096       (error "Couldn't request group %s: %s"
4097              group (gnus-status-message group)))
4098
4099     (setq gnus-newsgroup-name group)
4100     (setq gnus-newsgroup-unselected nil)
4101     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4102     (gnus-summary-setup-default-charset)
4103
4104     ;; Adjust and set lists of article marks.
4105     (when info
4106       (gnus-adjust-marked-articles info))
4107
4108     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4109     (when (gnus-virtual-group-p group)
4110       (setq cached gnus-newsgroup-cached))
4111
4112     (setq gnus-newsgroup-unreads
4113           (gnus-set-difference
4114            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4115            gnus-newsgroup-dormant))
4116
4117     (setq gnus-newsgroup-processable nil)
4118
4119     (gnus-update-read-articles group gnus-newsgroup-unreads)
4120
4121     (if (setq articles select-articles)
4122         (setq gnus-newsgroup-unselected
4123               (gnus-sorted-intersection
4124                gnus-newsgroup-unreads
4125                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4126       (setq articles (gnus-articles-to-read group read-all)))
4127
4128     (cond
4129      ((null articles)
4130       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4131       'quit)
4132      ((eq articles 0) nil)
4133      (t
4134       ;; Init the dependencies hash table.
4135       (setq gnus-newsgroup-dependencies
4136             (gnus-make-hashtable (length articles)))
4137       (gnus-set-global-variables)
4138       ;; Retrieve the headers and read them in.
4139       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4140       (setq gnus-newsgroup-headers
4141             (if (eq 'nov
4142                     (setq gnus-headers-retrieved-by
4143                           (gnus-retrieve-headers
4144                            articles gnus-newsgroup-name
4145                            ;; We might want to fetch old headers, but
4146                            ;; not if there is only 1 article.
4147                            (and (or (and
4148                                      (not (eq gnus-fetch-old-headers 'some))
4149                                      (not (numberp gnus-fetch-old-headers)))
4150                                     (> (length articles) 1))
4151                                 gnus-fetch-old-headers))))
4152                 (gnus-get-newsgroup-headers-xover
4153                  articles nil nil gnus-newsgroup-name t)
4154               (gnus-get-newsgroup-headers)))
4155       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4156
4157       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4158       (when cached
4159         (setq gnus-newsgroup-cached cached))
4160
4161       ;; Suppress duplicates?
4162       (when gnus-suppress-duplicates
4163         (gnus-dup-suppress-articles))
4164
4165       ;; Set the initial limit.
4166       (setq gnus-newsgroup-limit (copy-sequence articles))
4167       ;; Remove canceled articles from the list of unread articles.
4168       (setq gnus-newsgroup-unreads
4169             (gnus-set-sorted-intersection
4170              gnus-newsgroup-unreads
4171              (setq fetched-articles
4172                    (mapcar (lambda (headers) (mail-header-number headers))
4173                            gnus-newsgroup-headers))))
4174       ;; Removed marked articles that do not exist.
4175       (gnus-update-missing-marks
4176        (gnus-sorted-complement fetched-articles articles))
4177       ;; We might want to build some more threads first.
4178       (when (and gnus-fetch-old-headers
4179                  (eq gnus-headers-retrieved-by 'nov))
4180         (if (eq gnus-fetch-old-headers 'invisible)
4181             (gnus-build-all-threads)
4182           (gnus-build-old-threads)))
4183       ;; Let the Gnus agent mark articles as read.
4184       (when gnus-agent
4185         (gnus-agent-get-undownloaded-list))
4186       ;; Remove list identifiers from subject
4187       (when gnus-list-identifiers
4188         (gnus-summary-remove-list-identifiers))
4189       ;; Check whether auto-expire is to be done in this group.
4190       (setq gnus-newsgroup-auto-expire
4191             (gnus-group-auto-expirable-p group))
4192       ;; Set up the article buffer now, if necessary.
4193       (unless gnus-single-article-buffer
4194         (gnus-article-setup-buffer))
4195       ;; First and last article in this newsgroup.
4196       (when gnus-newsgroup-headers
4197         (setq gnus-newsgroup-begin
4198               (mail-header-number (car gnus-newsgroup-headers))
4199               gnus-newsgroup-end
4200               (mail-header-number
4201                (gnus-last-element gnus-newsgroup-headers))))
4202       ;; GROUP is successfully selected.
4203       (or gnus-newsgroup-headers t)))))
4204
4205 (defun gnus-articles-to-read (group &optional read-all)
4206   ;; Find out what articles the user wants to read.
4207   (let* ((articles
4208           ;; Select all articles if `read-all' is non-nil, or if there
4209           ;; are no unread articles.
4210           (if (or read-all
4211                   (and (zerop (length gnus-newsgroup-marked))
4212                        (zerop (length gnus-newsgroup-unreads)))
4213                   (eq (gnus-group-find-parameter group 'display)
4214                       'all))
4215               (or
4216                (gnus-uncompress-range (gnus-active group))
4217                (gnus-cache-articles-in-group group))
4218             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4219                           (copy-sequence gnus-newsgroup-unreads))
4220                   '<)))
4221          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4222          (scored (length scored-list))
4223          (number (length articles))
4224          (marked (+ (length gnus-newsgroup-marked)
4225                     (length gnus-newsgroup-dormant)))
4226          (select
4227           (cond
4228            ((numberp read-all)
4229             read-all)
4230            (t
4231             (condition-case ()
4232                 (cond
4233                  ((and (or (<= scored marked) (= scored number))
4234                        (numberp gnus-large-newsgroup)
4235                        (> number gnus-large-newsgroup))
4236                   (let ((input
4237                          (read-string
4238                           (format
4239                            "How many articles from %s (default %d): "
4240                            (gnus-limit-string gnus-newsgroup-name 35)
4241                            number))))
4242                     (if (string-match "^[ \t]*$" input) number input)))
4243                  ((and (> scored marked) (< scored number)
4244                        (> (- scored number) 20))
4245                   (let ((input
4246                          (read-string
4247                           (format "%s %s (%d scored, %d total): "
4248                                   "How many articles from"
4249                                   group scored number))))
4250                     (if (string-match "^[ \t]*$" input)
4251                         number input)))
4252                  (t number))
4253               (quit nil))))))
4254     (setq select (if (stringp select) (string-to-number select) select))
4255     (if (or (null select) (zerop select))
4256         select
4257       (if (and (not (zerop scored)) (<= (abs select) scored))
4258           (progn
4259             (setq articles (sort scored-list '<))
4260             (setq number (length articles)))
4261         (setq articles (copy-sequence articles)))
4262
4263       (when (< (abs select) number)
4264         (if (< select 0)
4265             ;; Select the N oldest articles.
4266             (setcdr (nthcdr (1- (abs select)) articles) nil)
4267           ;; Select the N most recent articles.
4268           (setq articles (nthcdr (- number select) articles))))
4269       (setq gnus-newsgroup-unselected
4270             (gnus-sorted-intersection
4271              gnus-newsgroup-unreads
4272              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4273       articles)))
4274
4275 (defun gnus-killed-articles (killed articles)
4276   (let (out)
4277     (while articles
4278       (when (inline (gnus-member-of-range (car articles) killed))
4279         (push (car articles) out))
4280       (setq articles (cdr articles)))
4281     out))
4282
4283 (defun gnus-uncompress-marks (marks)
4284   "Uncompress the mark ranges in MARKS."
4285   (let ((uncompressed '(score bookmark))
4286         out)
4287     (while marks
4288       (if (memq (caar marks) uncompressed)
4289           (push (car marks) out)
4290         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4291       (setq marks (cdr marks)))
4292     out))
4293
4294 (defun gnus-adjust-marked-articles (info)
4295   "Set all article lists and remove all marks that are no longer valid."
4296   (let* ((marked-lists (gnus-info-marks info))
4297          (active (gnus-active (gnus-info-group info)))
4298          (min (car active))
4299          (max (cdr active))
4300          (types gnus-article-mark-lists)
4301          (uncompressed '(score bookmark killed))
4302          marks var articles article mark)
4303
4304     (while marked-lists
4305       (setq marks (pop marked-lists))
4306       (set (setq var (intern (format "gnus-newsgroup-%s"
4307                                      (car (rassq (setq mark (car marks))
4308                                                  types)))))
4309            (if (memq (car marks) uncompressed) (cdr marks)
4310              (gnus-uncompress-range (cdr marks))))
4311
4312       (setq articles (symbol-value var))
4313
4314       ;; All articles have to be subsets of the active articles.
4315       (cond
4316        ;; Adjust "simple" lists.
4317        ((memq mark '(tick dormant expire reply save))
4318         (while articles
4319           (when (or (< (setq article (pop articles)) min) (> article max))
4320             (set var (delq article (symbol-value var))))))
4321        ;; Adjust assocs.
4322        ((memq mark uncompressed)
4323         (when (not (listp (cdr (symbol-value var))))
4324           (set var (list (symbol-value var))))
4325         (when (not (listp (cdr articles)))
4326           (setq articles (list articles)))
4327         (while articles
4328           (when (or (not (consp (setq article (pop articles))))
4329                     (< (car article) min)
4330                     (> (car article) max))
4331             (set var (delq article (symbol-value var))))))))))
4332
4333 (defun gnus-update-missing-marks (missing)
4334   "Go through the list of MISSING articles and remove them from the mark lists."
4335   (when missing
4336     (let ((types gnus-article-mark-lists)
4337           var m)
4338       ;; Go through all types.
4339       (while types
4340         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4341         (when (symbol-value var)
4342           ;; This list has articles.  So we delete all missing articles
4343           ;; from it.
4344           (setq m missing)
4345           (while m
4346             (set var (delq (pop m) (symbol-value var)))))))))
4347
4348 (defun gnus-update-marks ()
4349   "Enter the various lists of marked articles into the newsgroup info list."
4350   (let ((types gnus-article-mark-lists)
4351         (info (gnus-get-info gnus-newsgroup-name))
4352         (uncompressed '(score bookmark killed))
4353         type list newmarked symbol delta-marks)
4354     (when info
4355       ;; Add all marks lists to the list of marks lists.
4356       (while (setq type (pop types))
4357         (setq list (symbol-value
4358                     (setq symbol
4359                           (intern (format "gnus-newsgroup-%s"
4360                                           (car type))))))
4361
4362         (when list
4363           ;; Get rid of the entries of the articles that have the
4364           ;; default score.
4365           (when (and (eq (cdr type) 'score)
4366                      gnus-save-score
4367                      list)
4368             (let* ((arts list)
4369                    (prev (cons nil list))
4370                    (all prev))
4371               (while arts
4372                 (if (or (not (consp (car arts)))
4373                         (= (cdar arts) gnus-summary-default-score))
4374                     (setcdr prev (cdr arts))
4375                   (setq prev arts))
4376                 (setq arts (cdr arts)))
4377               (setq list (cdr all)))))
4378
4379         (unless (memq (cdr type) uncompressed)
4380           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4381        
4382         (when (gnus-check-backend-function
4383                'request-set-mark gnus-newsgroup-name)
4384           ;; uncompressed:s are not proper flags (they are cons cells)
4385           ;; cache is a internal gnus flag
4386           (unless (memq (cdr type) (cons 'cache uncompressed))
4387             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4388                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4389                    (add (gnus-remove-from-range
4390                          (gnus-copy-sequence list) old)))
4391               (when add
4392                 (push (list add 'add (list (cdr type))) delta-marks))
4393               (when del
4394                 (push (list del 'del (list (cdr type))) delta-marks)))))
4395           
4396         (when list
4397           (push (cons (cdr type) list) newmarked)))
4398
4399       (when delta-marks
4400         (unless (gnus-check-group gnus-newsgroup-name)
4401           (error "Can't open server for %s" gnus-newsgroup-name))
4402         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4403           
4404       ;; Enter these new marks into the info of the group.
4405       (if (nthcdr 3 info)
4406           (setcar (nthcdr 3 info) newmarked)
4407         ;; Add the marks lists to the end of the info.
4408         (when newmarked
4409           (setcdr (nthcdr 2 info) (list newmarked))))
4410
4411       ;; Cut off the end of the info if there's nothing else there.
4412       (let ((i 5))
4413         (while (and (> i 2)
4414                     (not (nth i info)))
4415           (when (nthcdr (decf i) info)
4416             (setcdr (nthcdr i info) nil)))))))
4417
4418 (defun gnus-set-mode-line (where)
4419   "This function sets the mode line of the article or summary buffers.
4420 If WHERE is `summary', the summary mode line format will be used."
4421   ;; Is this mode line one we keep updated?
4422   (when (and (memq where gnus-updated-mode-lines)
4423              (symbol-value
4424               (intern (format "gnus-%s-mode-line-format-spec" where))))
4425     (let (mode-string)
4426       (save-excursion
4427         ;; We evaluate this in the summary buffer since these
4428         ;; variables are buffer-local to that buffer.
4429         (set-buffer gnus-summary-buffer)
4430         ;; We bind all these variables that are used in the `eval' form
4431         ;; below.
4432         (let* ((mformat (symbol-value
4433                          (intern
4434                           (format "gnus-%s-mode-line-format-spec" where))))
4435                (gnus-tmp-group-name gnus-newsgroup-name)
4436                (gnus-tmp-article-number (or gnus-current-article 0))
4437                (gnus-tmp-unread gnus-newsgroup-unreads)
4438                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4439                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4440                (gnus-tmp-unread-and-unselected
4441                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4442                             (zerop gnus-tmp-unselected))
4443                        "")
4444                       ((zerop gnus-tmp-unselected)
4445                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4446                       (t (format "{%d(+%d) more}"
4447                                  gnus-tmp-unread-and-unticked
4448                                  gnus-tmp-unselected))))
4449                (gnus-tmp-subject
4450                 (if (and gnus-current-headers
4451                          (vectorp gnus-current-headers))
4452                     (gnus-mode-string-quote
4453                      (mail-header-subject gnus-current-headers))
4454                   ""))
4455                bufname-length max-len
4456                gnus-tmp-header);; passed as argument to any user-format-funcs
4457           (setq mode-string (eval mformat))
4458           (setq bufname-length (if (string-match "%b" mode-string)
4459                                    (- (length
4460                                        (buffer-name
4461                                         (if (eq where 'summary)
4462                                             nil
4463                                           (get-buffer gnus-article-buffer))))
4464                                       2)
4465                                  0))
4466           (setq max-len (max 4 (if gnus-mode-non-string-length
4467                                    (- (window-width)
4468                                       gnus-mode-non-string-length
4469                                       bufname-length)
4470                                  (length mode-string))))
4471           ;; We might have to chop a bit of the string off...
4472           (when (> (length mode-string) max-len)
4473             (setq mode-string
4474                   (concat (truncate-string-to-width mode-string (- max-len 3))
4475                           "...")))
4476           ;; Pad the mode string a bit.
4477           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4478       ;; Update the mode line.
4479       (setq mode-line-buffer-identification
4480             (gnus-mode-line-buffer-identification (list mode-string)))
4481       (set-buffer-modified-p t))))
4482
4483 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4484   "Go through the HEADERS list and add all Xrefs to a hash table.
4485 The resulting hash table is returned, or nil if no Xrefs were found."
4486   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4487          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4488          (xref-hashtb (gnus-make-hashtable))
4489          start group entry number xrefs header)
4490     (while headers
4491       (setq header (pop headers))
4492       (when (and (setq xrefs (mail-header-xref header))
4493                  (not (memq (setq number (mail-header-number header))
4494                             unreads)))
4495         (setq start 0)
4496         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4497           (setq start (match-end 0))
4498           (setq group (if prefix
4499                           (concat prefix (substring xrefs (match-beginning 1)
4500                                                     (match-end 1)))
4501                         (substring xrefs (match-beginning 1) (match-end 1))))
4502           (setq number
4503                 (string-to-int (substring xrefs (match-beginning 2)
4504                                           (match-end 2))))
4505           (if (setq entry (gnus-gethash group xref-hashtb))
4506               (setcdr entry (cons number (cdr entry)))
4507             (gnus-sethash group (cons number nil) xref-hashtb)))))
4508     (and start xref-hashtb)))
4509
4510 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4511   "Look through all the headers and mark the Xrefs as read."
4512   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4513         name entry info xref-hashtb idlist method nth4)
4514     (save-excursion
4515       (set-buffer gnus-group-buffer)
4516       (when (setq xref-hashtb
4517                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4518         (mapatoms
4519          (lambda (group)
4520            (unless (string= from-newsgroup (setq name (symbol-name group)))
4521              (setq idlist (symbol-value group))
4522              ;; Dead groups are not updated.
4523              (and (prog1
4524                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4525                             info (nth 2 entry))
4526                     (when (stringp (setq nth4 (gnus-info-method info)))
4527                       (setq nth4 (gnus-server-to-method nth4))))
4528                   ;; Only do the xrefs if the group has the same
4529                   ;; select method as the group we have just read.
4530                   (or (gnus-methods-equal-p
4531                        nth4 (gnus-find-method-for-group from-newsgroup))
4532                       virtual
4533                       (equal nth4 (setq method (gnus-find-method-for-group
4534                                                 from-newsgroup)))
4535                       (and (equal (car nth4) (car method))
4536                            (equal (nth 1 nth4) (nth 1 method))))
4537                   gnus-use-cross-reference
4538                   (or (not (eq gnus-use-cross-reference t))
4539                       virtual
4540                       ;; Only do cross-references on subscribed
4541                       ;; groups, if that is what is wanted.
4542                       (<= (gnus-info-level info) gnus-level-subscribed))
4543                   (gnus-group-make-articles-read name idlist))))
4544          xref-hashtb)))))
4545
4546 (defun gnus-compute-read-articles (group articles)
4547   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4548          (info (nth 2 entry))
4549          (active (gnus-active group))
4550          ninfo)
4551     (when entry
4552       ;; First peel off all invalid article numbers.
4553       (when active
4554         (let ((ids articles)
4555               id first)
4556           (while (setq id (pop ids))
4557             (when (and first (> id (cdr active)))
4558               ;; We'll end up in this situation in one particular
4559               ;; obscure situation.  If you re-scan a group and get
4560               ;; a new article that is cross-posted to a different
4561               ;; group that has not been re-scanned, you might get
4562               ;; crossposted article that has a higher number than
4563               ;; Gnus believes possible.  So we re-activate this
4564               ;; group as well.  This might mean doing the
4565               ;; crossposting thingy will *increase* the number
4566               ;; of articles in some groups.  Tsk, tsk.
4567               (setq active (or (gnus-activate-group group) active)))
4568             (when (or (> id (cdr active))
4569                       (< id (car active)))
4570               (setq articles (delq id articles))))))
4571       ;; If the read list is nil, we init it.
4572       (if (and active
4573                (null (gnus-info-read info))
4574                (> (car active) 1))
4575           (setq ninfo (cons 1 (1- (car active))))
4576         (setq ninfo (gnus-info-read info)))
4577       ;; Then we add the read articles to the range.
4578       (gnus-add-to-range
4579        ninfo (setq articles (sort articles '<))))))
4580
4581 (defun gnus-group-make-articles-read (group articles)
4582   "Update the info of GROUP to say that ARTICLES are read."
4583   (let* ((num 0)
4584          (entry (gnus-gethash group gnus-newsrc-hashtb))
4585          (info (nth 2 entry))
4586          (active (gnus-active group))
4587          range)
4588     (when entry
4589       (setq range (gnus-compute-read-articles group articles))
4590       (save-excursion
4591         (set-buffer gnus-group-buffer)
4592         (gnus-undo-register
4593           `(progn
4594              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4595              (gnus-info-set-read ',info ',(gnus-info-read info))
4596              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4597              (gnus-group-update-group ,group t))))
4598       ;; Add the read articles to the range.
4599       (gnus-info-set-read info range)
4600       ;; Then we have to re-compute how many unread
4601       ;; articles there are in this group.
4602       (when active
4603         (cond
4604          ((not range)
4605           (setq num (- (1+ (cdr active)) (car active))))
4606          ((not (listp (cdr range)))
4607           (setq num (- (cdr active) (- (1+ (cdr range))
4608                                        (car range)))))
4609          (t
4610           (while range
4611             (if (numberp (car range))
4612                 (setq num (1+ num))
4613               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4614             (setq range (cdr range)))
4615           (setq num (- (cdr active) num))))
4616         ;; Update the number of unread articles.
4617         (setcar entry num)
4618         ;; Update the group buffer.
4619         (gnus-group-update-group group t)))))
4620
4621 (defvar gnus-newsgroup-none-id 0)
4622
4623 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4624   (let ((cur nntp-server-buffer)
4625         (dependencies
4626          (or dependencies
4627              (save-excursion (set-buffer gnus-summary-buffer)
4628                              gnus-newsgroup-dependencies)))
4629         headers id end ref
4630         (mail-parse-charset gnus-newsgroup-charset)
4631         (mail-parse-ignored-charsets 
4632          (save-excursion (condition-case nil
4633                              (set-buffer gnus-summary-buffer)
4634                            (error))
4635                          gnus-newsgroup-ignored-charsets)))
4636     (save-excursion
4637       (set-buffer nntp-server-buffer)
4638       ;; Translate all TAB characters into SPACE characters.
4639       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4640       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4641       (gnus-run-hooks 'gnus-parse-headers-hook)
4642       (let ((case-fold-search t)
4643             in-reply-to header p lines chars)
4644         (goto-char (point-min))
4645         ;; Search to the beginning of the next header.  Error messages
4646         ;; do not begin with 2 or 3.
4647         (while (re-search-forward "^[23][0-9]+ " nil t)
4648           (setq id nil
4649                 ref nil)
4650           ;; This implementation of this function, with nine
4651           ;; search-forwards instead of the one re-search-forward and
4652           ;; a case (which basically was the old function) is actually
4653           ;; about twice as fast, even though it looks messier.  You
4654           ;; can't have everything, I guess.  Speed and elegance
4655           ;; doesn't always go hand in hand.
4656           (setq
4657            header
4658            (vector
4659             ;; Number.
4660             (prog1
4661                 (read cur)
4662               (end-of-line)
4663               (setq p (point))
4664               (narrow-to-region (point)
4665                                 (or (and (search-forward "\n.\n" nil t)
4666                                          (- (point) 2))
4667                                     (point))))
4668             ;; Subject.
4669             (progn
4670               (goto-char p)
4671               (if (search-forward "\nsubject: " nil t)
4672                   (funcall gnus-decode-encoded-word-function
4673                            (nnheader-header-value))
4674                 "(none)"))
4675             ;; From.
4676             (progn
4677               (goto-char p)
4678               (if (search-forward "\nfrom: " nil t)
4679                   (funcall gnus-decode-encoded-word-function
4680                            (nnheader-header-value))
4681                 "(nobody)"))
4682             ;; Date.
4683             (progn
4684               (goto-char p)
4685               (if (search-forward "\ndate: " nil t)
4686                   (nnheader-header-value) ""))
4687             ;; Message-ID.
4688             (progn
4689               (goto-char p)
4690               (setq id (if (re-search-forward
4691                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4692                            ;; We do it this way to make sure the Message-ID
4693                            ;; is (somewhat) syntactically valid.
4694                            (buffer-substring (match-beginning 1)
4695                                              (match-end 1))
4696                          ;; If there was no message-id, we just fake one
4697                          ;; to make subsequent routines simpler.
4698                          (nnheader-generate-fake-message-id))))
4699             ;; References.
4700             (progn
4701               (goto-char p)
4702               (if (search-forward "\nreferences: " nil t)
4703                   (progn
4704                     (setq end (point))
4705                     (prog1
4706                         (nnheader-header-value)
4707                       (setq ref
4708                             (buffer-substring
4709                              (progn
4710                                (end-of-line)
4711                                (search-backward ">" end t)
4712                                (1+ (point)))
4713                              (progn
4714                                (search-backward "<" end t)
4715                                (point))))))
4716                 ;; Get the references from the in-reply-to header if there
4717                 ;; were no references and the in-reply-to header looks
4718                 ;; promising.
4719                 (if (and (search-forward "\nin-reply-to: " nil t)
4720                          (setq in-reply-to (nnheader-header-value))
4721                          (string-match "<[^>]+>" in-reply-to))
4722                     (let (ref2)
4723                       (setq ref (substring in-reply-to (match-beginning 0)
4724                                            (match-end 0)))
4725                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4726                         (setq ref2 (substring in-reply-to (match-beginning 0)
4727                                               (match-end 0)))
4728                         (when (> (length ref2) (length ref))
4729                           (setq ref ref2)))
4730                       ref)
4731                   (setq ref nil))))
4732             ;; Chars.
4733             (progn
4734               (goto-char p)
4735               (if (search-forward "\nchars: " nil t)
4736                   (if (numberp (setq chars (ignore-errors (read cur))))
4737                       chars 0)
4738                 0))
4739             ;; Lines.
4740             (progn
4741               (goto-char p)
4742               (if (search-forward "\nlines: " nil t)
4743                   (if (numberp (setq lines (ignore-errors (read cur))))
4744                       lines 0)
4745                 0))
4746             ;; Xref.
4747             (progn
4748               (goto-char p)
4749               (and (search-forward "\nxref: " nil t)
4750                    (nnheader-header-value)))
4751             ;; Extra.
4752             (when gnus-extra-headers
4753               (let ((extra gnus-extra-headers)
4754                     out)
4755                 (while extra
4756                   (goto-char p)
4757                   (when (search-forward
4758                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4759                     (push (cons (car extra) (nnheader-header-value))
4760                           out))
4761                   (pop extra))
4762                 out))))
4763           (when (equal id ref)
4764             (setq ref nil))
4765
4766           (when gnus-alter-header-function
4767             (funcall gnus-alter-header-function header)
4768             (setq id (mail-header-id header)
4769                   ref (gnus-parent-id (mail-header-references header))))
4770
4771           (when (setq header
4772                       (gnus-dependencies-add-header
4773                        header dependencies force-new))
4774             (push header headers))
4775           (goto-char (point-max))
4776           (widen))
4777         (nreverse headers)))))
4778
4779 ;; Goes through the xover lines and returns a list of vectors
4780 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4781                                                   force-new dependencies
4782                                                   group also-fetch-heads)
4783   "Parse the news overview data in the server buffer, and return a
4784 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4785   ;; Get the Xref when the users reads the articles since most/some
4786   ;; NNTP servers do not include Xrefs when using XOVER.
4787   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4788   (let ((mail-parse-charset gnus-newsgroup-charset)
4789         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4790         (cur nntp-server-buffer)
4791         (dependencies (or dependencies gnus-newsgroup-dependencies))
4792         number headers header)
4793     (save-excursion
4794       (set-buffer nntp-server-buffer)
4795       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4796       ;; Allow the user to mangle the headers before parsing them.
4797       (gnus-run-hooks 'gnus-parse-headers-hook)
4798       (goto-char (point-min))
4799       (while (not (eobp))
4800         (condition-case ()
4801             (while (and sequence (not (eobp)))
4802               (setq number (read cur))
4803               (while (and sequence
4804                           (< (car sequence) number))
4805                 (setq sequence (cdr sequence)))
4806               (and sequence
4807                    (eq number (car sequence))
4808                    (progn
4809                      (setq sequence (cdr sequence))
4810                      (setq header (inline
4811                                     (gnus-nov-parse-line
4812                                      number dependencies force-new))))
4813                    (push header headers))
4814               (forward-line 1))
4815           (error
4816            (gnus-error 4 "Strange nov line (%d)"
4817                        (count-lines (point-min) (point)))))
4818         (forward-line 1))
4819       ;; A common bug in inn is that if you have posted an article and
4820       ;; then retrieves the active file, it will answer correctly --
4821       ;; the new article is included.  However, a NOV entry for the
4822       ;; article may not have been generated yet, so this may fail.
4823       ;; We work around this problem by retrieving the last few
4824       ;; headers using HEAD.
4825       (if (or (not also-fetch-heads)
4826               (not sequence))
4827           ;; We (probably) got all the headers.
4828           (nreverse headers)
4829         (let ((gnus-nov-is-evil t))
4830           (nconc
4831            (nreverse headers)
4832            (when (gnus-retrieve-headers sequence group)
4833              (gnus-get-newsgroup-headers))))))))
4834
4835 (defun gnus-article-get-xrefs ()
4836   "Fill in the Xref value in `gnus-current-headers', if necessary.
4837 This is meant to be called in `gnus-article-internal-prepare-hook'."
4838   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4839                                  gnus-current-headers)))
4840     (or (not gnus-use-cross-reference)
4841         (not headers)
4842         (and (mail-header-xref headers)
4843              (not (string= (mail-header-xref headers) "")))
4844         (let ((case-fold-search t)
4845               xref)
4846           (save-restriction
4847             (nnheader-narrow-to-headers)
4848             (goto-char (point-min))
4849             (when (or (and (not (eobp))
4850                            (eq (downcase (char-after)) ?x)
4851                            (looking-at "Xref:"))
4852                       (search-forward "\nXref:" nil t))
4853               (goto-char (1+ (match-end 0)))
4854               (setq xref (buffer-substring (point)
4855                                            (progn (end-of-line) (point))))
4856               (mail-header-set-xref headers xref)))))))
4857
4858 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4859   "Find article ID and insert the summary line for that article.
4860 OLD-HEADER can either be a header or a line number to insert
4861 the subject line on."
4862   (let* ((line (and (numberp old-header) old-header))
4863          (old-header (and (vectorp old-header) old-header))
4864          (header (cond ((and old-header use-old-header)
4865                         old-header)
4866                        ((and (numberp id)
4867                              (gnus-number-to-header id))
4868                         (gnus-number-to-header id))
4869                        (t
4870                         (gnus-read-header id))))
4871          (number (and (numberp id) id))
4872          d)
4873     (when header
4874       ;; Rebuild the thread that this article is part of and go to the
4875       ;; article we have fetched.
4876       (when (and (not gnus-show-threads)
4877                  old-header)
4878         (when (and number
4879                    (setq d (gnus-data-find (mail-header-number old-header))))
4880           (goto-char (gnus-data-pos d))
4881           (gnus-data-remove
4882            number
4883            (- (gnus-point-at-bol)
4884               (prog1
4885                   (1+ (gnus-point-at-eol))
4886                 (gnus-delete-line))))))
4887       (when old-header
4888         (mail-header-set-number header (mail-header-number old-header)))
4889       (setq gnus-newsgroup-sparse
4890             (delq (setq number (mail-header-number header))
4891                   gnus-newsgroup-sparse))
4892       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4893       (push number gnus-newsgroup-limit)
4894       (gnus-rebuild-thread (mail-header-id header) line)
4895       (gnus-summary-goto-subject number nil t))
4896     (when (and (numberp number)
4897                (> number 0))
4898       ;; We have to update the boundaries even if we can't fetch the
4899       ;; article if ID is a number -- so that the next `P' or `N'
4900       ;; command will fetch the previous (or next) article even
4901       ;; if the one we tried to fetch this time has been canceled.
4902       (when (> number gnus-newsgroup-end)
4903         (setq gnus-newsgroup-end number))
4904       (when (< number gnus-newsgroup-begin)
4905         (setq gnus-newsgroup-begin number))
4906       (setq gnus-newsgroup-unselected
4907             (delq number gnus-newsgroup-unselected)))
4908     ;; Report back a success?
4909     (and header (mail-header-number header))))
4910
4911 ;;; Process/prefix in the summary buffer
4912
4913 (defun gnus-summary-work-articles (n)
4914   "Return a list of articles to be worked upon.
4915 The prefix argument, the list of process marked articles, and the
4916 current article will be taken into consideration."
4917   (save-excursion
4918     (set-buffer gnus-summary-buffer)
4919     (cond
4920      (n
4921       ;; A numerical prefix has been given.
4922       (setq n (prefix-numeric-value n))
4923       (let ((backward (< n 0))
4924             (n (abs (prefix-numeric-value n)))
4925             articles article)
4926         (save-excursion
4927           (while
4928               (and (> n 0)
4929                    (push (setq article (gnus-summary-article-number))
4930                          articles)
4931                    (if backward
4932                        (gnus-summary-find-prev nil article)
4933                      (gnus-summary-find-next nil article)))
4934             (decf n)))
4935         (nreverse articles)))
4936      ((and (gnus-region-active-p) (mark))
4937       (message "region active")
4938       ;; Work on the region between point and mark.
4939       (let ((max (max (point) (mark)))
4940             articles article)
4941         (save-excursion
4942           (goto-char (min (min (point) (mark))))
4943           (while
4944               (and
4945                (push (setq article (gnus-summary-article-number)) articles)
4946                (gnus-summary-find-next nil article)
4947                (< (point) max)))
4948           (nreverse articles))))
4949      (gnus-newsgroup-processable
4950       ;; There are process-marked articles present.
4951       ;; Save current state.
4952       (gnus-summary-save-process-mark)
4953       ;; Return the list.
4954       (reverse gnus-newsgroup-processable))
4955      (t
4956       ;; Just return the current article.
4957       (list (gnus-summary-article-number))))))
4958
4959 (defmacro gnus-summary-iterate (arg &rest forms)
4960   "Iterate over the process/prefixed articles and do FORMS.
4961 ARG is the interactive prefix given to the command.  FORMS will be
4962 executed with point over the summary line of the articles."
4963   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4964     `(let ((,articles (gnus-summary-work-articles ,arg)))
4965        (while ,articles
4966          (gnus-summary-goto-subject (car ,articles))
4967          ,@forms
4968          (pop ,articles)))))
4969
4970 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4971 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4972
4973 (defun gnus-summary-save-process-mark ()
4974   "Push the current set of process marked articles on the stack."
4975   (interactive)
4976   (push (copy-sequence gnus-newsgroup-processable)
4977         gnus-newsgroup-process-stack))
4978
4979 (defun gnus-summary-kill-process-mark ()
4980   "Push the current set of process marked articles on the stack and unmark."
4981   (interactive)
4982   (gnus-summary-save-process-mark)
4983   (gnus-summary-unmark-all-processable))
4984
4985 (defun gnus-summary-yank-process-mark ()
4986   "Pop the last process mark state off the stack and restore it."
4987   (interactive)
4988   (unless gnus-newsgroup-process-stack
4989     (error "Empty mark stack"))
4990   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4991
4992 (defun gnus-summary-process-mark-set (set)
4993   "Make SET into the current process marked articles."
4994   (gnus-summary-unmark-all-processable)
4995   (while set
4996     (gnus-summary-set-process-mark (pop set))))
4997
4998 ;;; Searching and stuff
4999
5000 (defun gnus-summary-search-group (&optional backward use-level)
5001   "Search for next unread newsgroup.
5002 If optional argument BACKWARD is non-nil, search backward instead."
5003   (save-excursion
5004     (set-buffer gnus-group-buffer)
5005     (when (gnus-group-search-forward
5006            backward nil (if use-level (gnus-group-group-level) nil))
5007       (gnus-group-group-name))))
5008
5009 (defun gnus-summary-best-group (&optional exclude-group)
5010   "Find the name of the best unread group.
5011 If EXCLUDE-GROUP, do not go to this group."
5012   (save-excursion
5013     (set-buffer gnus-group-buffer)
5014     (save-excursion
5015       (gnus-group-best-unread-group exclude-group))))
5016
5017 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5018   (if backward (gnus-summary-find-prev)
5019     (let* ((dummy (gnus-summary-article-intangible-p))
5020            (article (or article (gnus-summary-article-number)))
5021            (arts (gnus-data-find-list article))
5022            result)
5023       (when (and (not dummy)
5024                  (or (not gnus-summary-check-current)
5025                      (not unread)
5026                      (not (gnus-data-unread-p (car arts)))))
5027         (setq arts (cdr arts)))
5028       (when (setq result
5029                   (if unread
5030                       (progn
5031                         (while arts
5032                           (when (or (and undownloaded
5033                                          (eq gnus-undownloaded-mark
5034                                              (gnus-data-mark (car arts))))
5035                                     (gnus-data-unread-p (car arts)))
5036                             (setq result (car arts)
5037                                   arts nil))
5038                           (setq arts (cdr arts)))
5039                         result)
5040                     (car arts)))
5041         (goto-char (gnus-data-pos result))
5042         (gnus-data-number result)))))
5043
5044 (defun gnus-summary-find-prev (&optional unread article)
5045   (let* ((eobp (eobp))
5046          (article (or article (gnus-summary-article-number)))
5047          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5048          result)
5049     (when (and (not eobp)
5050                (or (not gnus-summary-check-current)
5051                    (not unread)
5052                    (not (gnus-data-unread-p (car arts)))))
5053       (setq arts (cdr arts)))
5054     (when (setq result
5055                 (if unread
5056                     (progn
5057                       (while arts
5058                         (when (gnus-data-unread-p (car arts))
5059                           (setq result (car arts)
5060                                 arts nil))
5061                         (setq arts (cdr arts)))
5062                       result)
5063                   (car arts)))
5064       (goto-char (gnus-data-pos result))
5065       (gnus-data-number result))))
5066
5067 (defun gnus-summary-find-subject (subject &optional unread backward article)
5068   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5069          (article (or article (gnus-summary-article-number)))
5070          (articles (gnus-data-list backward))
5071          (arts (gnus-data-find-list article articles))
5072          result)
5073     (when (or (not gnus-summary-check-current)
5074               (not unread)
5075               (not (gnus-data-unread-p (car arts))))
5076       (setq arts (cdr arts)))
5077     (while arts
5078       (and (or (not unread)
5079                (gnus-data-unread-p (car arts)))
5080            (vectorp (gnus-data-header (car arts)))
5081            (gnus-subject-equal
5082             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5083            (setq result (car arts)
5084                  arts nil))
5085       (setq arts (cdr arts)))
5086     (and result
5087          (goto-char (gnus-data-pos result))
5088          (gnus-data-number result))))
5089
5090 (defun gnus-summary-search-forward (&optional unread subject backward)
5091   "Search forward for an article.
5092 If UNREAD, look for unread articles.  If SUBJECT, look for
5093 articles with that subject.  If BACKWARD, search backward instead."
5094   (cond (subject (gnus-summary-find-subject subject unread backward))
5095         (backward (gnus-summary-find-prev unread))
5096         (t (gnus-summary-find-next unread))))
5097
5098 (defun gnus-recenter (&optional n)
5099   "Center point in window and redisplay frame.
5100 Also do horizontal recentering."
5101   (interactive "P")
5102   (when (and gnus-auto-center-summary
5103              (not (eq gnus-auto-center-summary 'vertical)))
5104     (gnus-horizontal-recenter))
5105   (recenter n))
5106
5107 (defun gnus-summary-recenter ()
5108   "Center point in the summary window.
5109 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5110 displayed, no centering will be performed."
5111   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5112   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5113   (let* ((top (cond ((< (window-height) 4) 0)
5114                     ((< (window-height) 7) 1)
5115                     (t (if (numberp gnus-auto-center-summary)
5116                            gnus-auto-center-summary
5117                          2))))
5118          (height (1- (window-height)))
5119          (bottom (save-excursion (goto-char (point-max))
5120                                  (forward-line (- height))
5121                                  (point)))
5122          (window (get-buffer-window (current-buffer))))
5123     ;; The user has to want it.
5124     (when gnus-auto-center-summary
5125       (when (get-buffer-window gnus-article-buffer)
5126         ;; Only do recentering when the article buffer is displayed,
5127         ;; Set the window start to either `bottom', which is the biggest
5128         ;; possible valid number, or the second line from the top,
5129         ;; whichever is the least.
5130         (set-window-start
5131          window (min bottom (save-excursion
5132                               (forward-line (- top)) (point)))
5133          t))
5134       ;; Do horizontal recentering while we're at it.
5135       (when (and (get-buffer-window (current-buffer) t)
5136                  (not (eq gnus-auto-center-summary 'vertical)))
5137         (let ((selected (selected-window)))
5138           (select-window (get-buffer-window (current-buffer) t))
5139           (gnus-summary-position-point)
5140           (gnus-horizontal-recenter)
5141           (select-window selected))))))
5142
5143 (defun gnus-summary-jump-to-group (newsgroup)
5144   "Move point to NEWSGROUP in group mode buffer."
5145   ;; Keep update point of group mode buffer if visible.
5146   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5147       (save-window-excursion
5148         ;; Take care of tree window mode.
5149         (when (get-buffer-window gnus-group-buffer)
5150           (pop-to-buffer gnus-group-buffer))
5151         (gnus-group-jump-to-group newsgroup))
5152     (save-excursion
5153       ;; Take care of tree window mode.
5154       (if (get-buffer-window gnus-group-buffer)
5155           (pop-to-buffer gnus-group-buffer)
5156         (set-buffer gnus-group-buffer))
5157       (gnus-group-jump-to-group newsgroup))))
5158
5159 ;; This function returns a list of article numbers based on the
5160 ;; difference between the ranges of read articles in this group and
5161 ;; the range of active articles.
5162 (defun gnus-list-of-unread-articles (group)
5163   (let* ((read (gnus-info-read (gnus-get-info group)))
5164          (active (or (gnus-active group) (gnus-activate-group group)))
5165          (last (cdr active))
5166          first nlast unread)
5167     ;; If none are read, then all are unread.
5168     (if (not read)
5169         (setq first (car active))
5170       ;; If the range of read articles is a single range, then the
5171       ;; first unread article is the article after the last read
5172       ;; article.  Sounds logical, doesn't it?
5173       (if (and (not (listp (cdr read)))
5174                (or (< (car read) (car active))
5175                    (progn (setq read (list read))
5176                           nil)))
5177           (setq first (max (car active) (1+ (cdr read))))
5178         ;; `read' is a list of ranges.
5179         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5180                                   (caar read)))
5181                   1)
5182           (setq first (car active)))
5183         (while read
5184           (when first
5185             (while (< first nlast)
5186               (push first unread)
5187               (setq first (1+ first))))
5188           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5189           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5190           (setq read (cdr read)))))
5191     ;; And add the last unread articles.
5192     (while (<= first last)
5193       (push first unread)
5194       (setq first (1+ first)))
5195     ;; Return the list of unread articles.
5196     (delq 0 (nreverse unread))))
5197
5198 (defun gnus-list-of-read-articles (group)
5199   "Return a list of unread, unticked and non-dormant articles."
5200   (let* ((info (gnus-get-info group))
5201          (marked (gnus-info-marks info))
5202          (active (gnus-active group)))
5203     (and info active
5204          (gnus-set-difference
5205           (gnus-sorted-complement
5206            (gnus-uncompress-range active)
5207            (gnus-list-of-unread-articles group))
5208           (append
5209            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5210            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5211
5212 ;; Various summary commands
5213
5214 (defun gnus-summary-select-article-buffer ()
5215   "Reconfigure windows to show article buffer."
5216   (interactive)
5217   (if (not (gnus-buffer-live-p gnus-article-buffer))
5218       (error "There is no article buffer for this summary buffer")
5219     (gnus-configure-windows 'article)
5220     (select-window (get-buffer-window gnus-article-buffer))))
5221
5222 (defun gnus-summary-universal-argument (arg)
5223   "Perform any operation on all articles that are process/prefixed."
5224   (interactive "P")
5225   (let ((articles (gnus-summary-work-articles arg))
5226         func article)
5227     (if (eq
5228          (setq
5229           func
5230           (key-binding
5231            (read-key-sequence
5232             (substitute-command-keys
5233              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5234          'undefined)
5235         (gnus-error 1 "Undefined key")
5236       (save-excursion
5237         (while articles
5238           (gnus-summary-goto-subject (setq article (pop articles)))
5239           (let (gnus-newsgroup-processable)
5240             (command-execute func))
5241           (gnus-summary-remove-process-mark article)))))
5242   (gnus-summary-position-point))
5243
5244 (defun gnus-summary-toggle-truncation (&optional arg)
5245   "Toggle truncation of summary lines.
5246 With arg, turn line truncation on iff arg is positive."
5247   (interactive "P")
5248   (setq truncate-lines
5249         (if (null arg) (not truncate-lines)
5250           (> (prefix-numeric-value arg) 0)))
5251   (redraw-display))
5252
5253 (defun gnus-summary-reselect-current-group (&optional all rescan)
5254   "Exit and then reselect the current newsgroup.
5255 The prefix argument ALL means to select all articles."
5256   (interactive "P")
5257   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5258     (error "Ephemeral groups can't be reselected"))
5259   (let ((current-subject (gnus-summary-article-number))
5260         (group gnus-newsgroup-name))
5261     (setq gnus-newsgroup-begin nil)
5262     (gnus-summary-exit)
5263     ;; We have to adjust the point of group mode buffer because
5264     ;; point was moved to the next unread newsgroup by exiting.
5265     (gnus-summary-jump-to-group group)
5266     (when rescan
5267       (save-excursion
5268         (gnus-group-get-new-news-this-group 1)))
5269     (gnus-group-read-group all t)
5270     (gnus-summary-goto-subject current-subject nil t)))
5271
5272 (defun gnus-summary-rescan-group (&optional all)
5273   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5274   (interactive "P")
5275   (gnus-summary-reselect-current-group all t))
5276
5277 (defun gnus-summary-update-info (&optional non-destructive)
5278   (save-excursion
5279     (let ((group gnus-newsgroup-name))
5280       (when group
5281         (when gnus-newsgroup-kill-headers
5282           (setq gnus-newsgroup-killed
5283                 (gnus-compress-sequence
5284                  (nconc
5285                   (gnus-set-sorted-intersection
5286                    (gnus-uncompress-range gnus-newsgroup-killed)
5287                    (setq gnus-newsgroup-unselected
5288                          (sort gnus-newsgroup-unselected '<)))
5289                   (setq gnus-newsgroup-unreads
5290                         (sort gnus-newsgroup-unreads '<)))
5291                  t)))
5292         (unless (listp (cdr gnus-newsgroup-killed))
5293           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5294         (let ((headers gnus-newsgroup-headers))
5295           ;; Set the new ranges of read articles.
5296           (save-excursion
5297             (set-buffer gnus-group-buffer)
5298             (gnus-undo-force-boundary))
5299           (gnus-update-read-articles
5300            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5301           ;; Set the current article marks.
5302           (let ((gnus-newsgroup-scored
5303                  (if (and (not gnus-save-score)
5304                           (not non-destructive))
5305                      nil
5306                    gnus-newsgroup-scored)))
5307             (save-excursion
5308               (gnus-update-marks)))
5309           ;; Do the cross-ref thing.
5310           (when gnus-use-cross-reference
5311             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5312           ;; Do not switch windows but change the buffer to work.
5313           (set-buffer gnus-group-buffer)
5314           (unless (gnus-ephemeral-group-p group)
5315             (gnus-group-update-group group)))))))
5316
5317 (defun gnus-summary-save-newsrc (&optional force)
5318   "Save the current number of read/marked articles in the dribble buffer.
5319 The dribble buffer will then be saved.
5320 If FORCE (the prefix), also save the .newsrc file(s)."
5321   (interactive "P")
5322   (gnus-summary-update-info t)
5323   (if force
5324       (gnus-save-newsrc-file)
5325     (gnus-dribble-save)))
5326
5327 (defun gnus-summary-exit (&optional temporary)
5328   "Exit reading current newsgroup, and then return to group selection mode.
5329 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5330   (interactive)
5331   (gnus-set-global-variables)
5332   (when (gnus-buffer-live-p gnus-article-buffer)
5333     (save-excursion
5334       (set-buffer gnus-article-buffer)
5335       (mm-destroy-parts gnus-article-mime-handles)))
5336   (gnus-kill-save-kill-buffer)
5337   (gnus-async-halt-prefetch)
5338   (let* ((group gnus-newsgroup-name)
5339          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5340          (mode major-mode)
5341          (group-point nil)
5342          (buf (current-buffer)))
5343     (unless quit-config
5344       ;; Do adaptive scoring, and possibly save score files.
5345       (when gnus-newsgroup-adaptive
5346         (gnus-score-adaptive))
5347       (when gnus-use-scoring
5348         (gnus-score-save)))
5349     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5350     ;; If we have several article buffers, we kill them at exit.
5351     (unless gnus-single-article-buffer
5352       (gnus-kill-buffer gnus-original-article-buffer)
5353       (setq gnus-article-current nil))
5354     (when gnus-use-cache
5355       (gnus-cache-possibly-remove-articles)
5356       (gnus-cache-save-buffers))
5357     (gnus-async-prefetch-remove-group group)
5358     (when gnus-suppress-duplicates
5359       (gnus-dup-enter-articles))
5360     (when gnus-use-trees
5361       (gnus-tree-close group))
5362     (when gnus-use-cache
5363       (gnus-cache-write-active))
5364     ;; Remove entries for this group.
5365     (nnmail-purge-split-history (gnus-group-real-name group))
5366     ;; Make all changes in this group permanent.
5367     (unless quit-config
5368       (gnus-run-hooks 'gnus-exit-group-hook)
5369       (gnus-summary-update-info))
5370     (gnus-close-group group)
5371     ;; Make sure where we were, and go to next newsgroup.
5372     (set-buffer gnus-group-buffer)
5373     (unless quit-config
5374       (gnus-group-jump-to-group group))
5375     (gnus-run-hooks 'gnus-summary-exit-hook)
5376     (unless (or quit-config
5377                 ;; If this group has disappeared from the summary
5378                 ;; buffer, don't skip forwards.
5379                 (not (string= group (gnus-group-group-name))))
5380       (gnus-group-next-unread-group 1))
5381     (setq group-point (point))
5382     (if temporary
5383         nil                             ;Nothing to do.
5384       ;; If we have several article buffers, we kill them at exit.
5385       (unless gnus-single-article-buffer
5386         (gnus-kill-buffer gnus-article-buffer)
5387         (gnus-kill-buffer gnus-original-article-buffer)
5388         (setq gnus-article-current nil))
5389       (set-buffer buf)
5390       (if (not gnus-kill-summary-on-exit)
5391           (gnus-deaden-summary)
5392         ;; We set all buffer-local variables to nil.  It is unclear why
5393         ;; this is needed, but if we don't, buffer-local variables are
5394         ;; not garbage-collected, it seems.  This would the lead to en
5395         ;; ever-growing Emacs.
5396         (gnus-summary-clear-local-variables)
5397         (when (get-buffer gnus-article-buffer)
5398           (bury-buffer gnus-article-buffer))
5399         ;; We clear the global counterparts of the buffer-local
5400         ;; variables as well, just to be on the safe side.
5401         (set-buffer gnus-group-buffer)
5402         (gnus-summary-clear-local-variables)
5403         ;; Return to group mode buffer.
5404         (when (eq mode 'gnus-summary-mode)
5405           (gnus-kill-buffer buf)))
5406       (setq gnus-current-select-method gnus-select-method)
5407       (pop-to-buffer gnus-group-buffer)
5408       (if (not quit-config)
5409           (progn
5410             (goto-char group-point)
5411             (gnus-configure-windows 'group 'force))
5412         (gnus-handle-ephemeral-exit quit-config))
5413       ;; Clear the current group name.
5414       (unless quit-config
5415         (setq gnus-newsgroup-name nil)))))
5416
5417 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5418 (defun gnus-summary-exit-no-update (&optional no-questions)
5419   "Quit reading current newsgroup without updating read article info."
5420   (interactive)
5421   (let* ((group gnus-newsgroup-name)
5422          (quit-config (gnus-group-quit-config group)))
5423     (when (or no-questions
5424               gnus-expert-user
5425               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5426       (gnus-async-halt-prefetch)
5427       (mapcar 'funcall
5428               (delq 'gnus-summary-expire-articles
5429                     (copy-sequence gnus-summary-prepare-exit-hook)))
5430       (when (gnus-buffer-live-p gnus-article-buffer)
5431         (save-excursion
5432           (set-buffer gnus-article-buffer)
5433           (mm-destroy-parts gnus-article-mime-handles)))
5434       ;; If we have several article buffers, we kill them at exit.
5435       (unless gnus-single-article-buffer
5436         (gnus-kill-buffer gnus-article-buffer)
5437         (gnus-kill-buffer gnus-original-article-buffer)
5438         (setq gnus-article-current nil))
5439       (if (not gnus-kill-summary-on-exit)
5440           (gnus-deaden-summary)
5441         (gnus-close-group group)
5442         (gnus-summary-clear-local-variables)
5443         (set-buffer gnus-group-buffer)
5444         (gnus-summary-clear-local-variables)
5445         (when (get-buffer gnus-summary-buffer)
5446           (kill-buffer gnus-summary-buffer)))
5447       (unless gnus-single-article-buffer
5448         (setq gnus-article-current nil))
5449       (when gnus-use-trees
5450         (gnus-tree-close group))
5451       (gnus-async-prefetch-remove-group group)
5452       (when (get-buffer gnus-article-buffer)
5453         (bury-buffer gnus-article-buffer))
5454       ;; Return to the group buffer.
5455       (gnus-configure-windows 'group 'force)
5456       ;; Clear the current group name.
5457       (setq gnus-newsgroup-name nil)
5458       (when (equal (gnus-group-group-name) group)
5459         (gnus-group-next-unread-group 1))
5460       (when quit-config
5461         (gnus-handle-ephemeral-exit quit-config)))))
5462
5463 (defun gnus-handle-ephemeral-exit (quit-config)
5464   "Handle movement when leaving an ephemeral group.
5465 The state which existed when entering the ephemeral is reset."
5466   (if (not (buffer-name (car quit-config)))
5467       (gnus-configure-windows 'group 'force)
5468     (set-buffer (car quit-config))
5469     (cond ((eq major-mode 'gnus-summary-mode)
5470            (gnus-set-global-variables))
5471           ((eq major-mode 'gnus-article-mode)
5472            (save-excursion
5473              ;; The `gnus-summary-buffer' variable may point
5474              ;; to the old summary buffer when using a single
5475              ;; article buffer.
5476              (unless (gnus-buffer-live-p gnus-summary-buffer)
5477                (set-buffer gnus-group-buffer))
5478              (set-buffer gnus-summary-buffer)
5479              (gnus-set-global-variables))))
5480     (if (or (eq (cdr quit-config) 'article)
5481             (eq (cdr quit-config) 'pick))
5482         (progn
5483           ;; The current article may be from the ephemeral group
5484           ;; thus it is best that we reload this article
5485           (gnus-summary-show-article)
5486           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5487               (gnus-configure-windows 'pick 'force)
5488             (gnus-configure-windows (cdr quit-config) 'force)))
5489       (gnus-configure-windows (cdr quit-config) 'force))
5490     (when (eq major-mode 'gnus-summary-mode)
5491       (gnus-summary-next-subject 1 nil t)
5492       (gnus-summary-recenter)
5493       (gnus-summary-position-point))))
5494
5495 ;;; Dead summaries.
5496
5497 (defvar gnus-dead-summary-mode-map nil)
5498
5499 (unless gnus-dead-summary-mode-map
5500   (setq gnus-dead-summary-mode-map (make-keymap))
5501   (suppress-keymap gnus-dead-summary-mode-map)
5502   (substitute-key-definition
5503    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5504   (let ((keys '("\C-d" "\r" "\177" [delete])))
5505     (while keys
5506       (define-key gnus-dead-summary-mode-map
5507         (pop keys) 'gnus-summary-wake-up-the-dead))))
5508
5509 (defvar gnus-dead-summary-mode nil
5510   "Minor mode for Gnus summary buffers.")
5511
5512 (defun gnus-dead-summary-mode (&optional arg)
5513   "Minor mode for Gnus summary buffers."
5514   (interactive "P")
5515   (when (eq major-mode 'gnus-summary-mode)
5516     (make-local-variable 'gnus-dead-summary-mode)
5517     (setq gnus-dead-summary-mode
5518           (if (null arg) (not gnus-dead-summary-mode)
5519             (> (prefix-numeric-value arg) 0)))
5520     (when gnus-dead-summary-mode
5521       (gnus-add-minor-mode
5522        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5523
5524 (defun gnus-deaden-summary ()
5525   "Make the current summary buffer into a dead summary buffer."
5526   ;; Kill any previous dead summary buffer.
5527   (when (and gnus-dead-summary
5528              (buffer-name gnus-dead-summary))
5529     (save-excursion
5530       (set-buffer gnus-dead-summary)
5531       (when gnus-dead-summary-mode
5532         (kill-buffer (current-buffer)))))
5533   ;; Make this the current dead summary.
5534   (setq gnus-dead-summary (current-buffer))
5535   (gnus-dead-summary-mode 1)
5536   (let ((name (buffer-name)))
5537     (when (string-match "Summary" name)
5538       (rename-buffer
5539        (concat (substring name 0 (match-beginning 0)) "Dead "
5540                (substring name (match-beginning 0)))
5541        t))))
5542
5543 (defun gnus-kill-or-deaden-summary (buffer)
5544   "Kill or deaden the summary BUFFER."
5545   (save-excursion
5546     (when (and (buffer-name buffer)
5547                (not gnus-single-article-buffer))
5548       (save-excursion
5549         (set-buffer buffer)
5550         (gnus-kill-buffer gnus-article-buffer)
5551         (gnus-kill-buffer gnus-original-article-buffer)))
5552     (cond (gnus-kill-summary-on-exit
5553            (when (and gnus-use-trees
5554                       (gnus-buffer-exists-p buffer))
5555              (save-excursion
5556                (set-buffer buffer)
5557                (gnus-tree-close gnus-newsgroup-name)))
5558            (gnus-kill-buffer buffer))
5559           ((gnus-buffer-exists-p buffer)
5560            (save-excursion
5561              (set-buffer buffer)
5562              (gnus-deaden-summary))))))
5563
5564 (defun gnus-summary-wake-up-the-dead (&rest args)
5565   "Wake up the dead summary buffer."
5566   (interactive)
5567   (gnus-dead-summary-mode -1)
5568   (let ((name (buffer-name)))
5569     (when (string-match "Dead " name)
5570       (rename-buffer
5571        (concat (substring name 0 (match-beginning 0))
5572                (substring name (match-end 0)))
5573        t)))
5574   (gnus-message 3 "This dead summary is now alive again"))
5575
5576 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5577 (defun gnus-summary-fetch-faq (&optional faq-dir)
5578   "Fetch the FAQ for the current group.
5579 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5580 in."
5581   (interactive
5582    (list
5583     (when current-prefix-arg
5584       (completing-read
5585        "Faq dir: " (and (listp gnus-group-faq-directory)
5586                         (mapcar (lambda (file) (list file))
5587                                 gnus-group-faq-directory))))))
5588   (let (gnus-faq-buffer)
5589     (when (setq gnus-faq-buffer
5590                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5591       (gnus-configure-windows 'summary-faq))))
5592
5593 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5594 (defun gnus-summary-describe-group (&optional force)
5595   "Describe the current newsgroup."
5596   (interactive "P")
5597   (gnus-group-describe-group force gnus-newsgroup-name))
5598
5599 (defun gnus-summary-describe-briefly ()
5600   "Describe summary mode commands briefly."
5601   (interactive)
5602   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5603
5604 ;; Walking around group mode buffer from summary mode.
5605
5606 (defun gnus-summary-next-group (&optional no-article target-group backward)
5607   "Exit current newsgroup and then select next unread newsgroup.
5608 If prefix argument NO-ARTICLE is non-nil, no article is selected
5609 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5610 previous group instead."
5611   (interactive "P")
5612   ;; Stop pre-fetching.
5613   (gnus-async-halt-prefetch)
5614   (let ((current-group gnus-newsgroup-name)
5615         (current-buffer (current-buffer))
5616         entered)
5617     ;; First we semi-exit this group to update Xrefs and all variables.
5618     ;; We can't do a real exit, because the window conf must remain
5619     ;; the same in case the user is prompted for info, and we don't
5620     ;; want the window conf to change before that...
5621     (gnus-summary-exit t)
5622     (while (not entered)
5623       ;; Then we find what group we are supposed to enter.
5624       (set-buffer gnus-group-buffer)
5625       (gnus-group-jump-to-group current-group)
5626       (setq target-group
5627             (or target-group
5628                 (if (eq gnus-keep-same-level 'best)
5629                     (gnus-summary-best-group gnus-newsgroup-name)
5630                   (gnus-summary-search-group backward gnus-keep-same-level))))
5631       (if (not target-group)
5632           ;; There are no further groups, so we return to the group
5633           ;; buffer.
5634           (progn
5635             (gnus-message 5 "Returning to the group buffer")
5636             (setq entered t)
5637             (when (gnus-buffer-live-p current-buffer)
5638               (set-buffer current-buffer)
5639               (gnus-summary-exit))
5640             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5641         ;; We try to enter the target group.
5642         (gnus-group-jump-to-group target-group)
5643         (let ((unreads (gnus-group-group-unread)))
5644           (if (and (or (eq t unreads)
5645                        (and unreads (not (zerop unreads))))
5646                    (gnus-summary-read-group
5647                     target-group nil no-article
5648                     (and (buffer-name current-buffer) current-buffer)
5649                     nil backward))
5650               (setq entered t)
5651             (setq current-group target-group
5652                   target-group nil)))))))
5653
5654 (defun gnus-summary-prev-group (&optional no-article)
5655   "Exit current newsgroup and then select previous unread newsgroup.
5656 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5657   (interactive "P")
5658   (gnus-summary-next-group no-article nil t))
5659
5660 ;; Walking around summary lines.
5661
5662 (defun gnus-summary-first-subject (&optional unread undownloaded)
5663   "Go to the first unread subject.
5664 If UNREAD is non-nil, go to the first unread article.
5665 Returns the article selected or nil if there are no unread articles."
5666   (interactive "P")
5667   (prog1
5668       (cond
5669        ;; Empty summary.
5670        ((null gnus-newsgroup-data)
5671         (gnus-message 3 "No articles in the group")
5672         nil)
5673        ;; Pick the first article.
5674        ((not unread)
5675         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5676         (gnus-data-number (car gnus-newsgroup-data)))
5677        ;; No unread articles.
5678        ((null gnus-newsgroup-unreads)
5679         (gnus-message 3 "No more unread articles")
5680         nil)
5681        ;; Find the first unread article.
5682        (t
5683         (let ((data gnus-newsgroup-data))
5684           (while (and data
5685                       (and (not (and undownloaded
5686                                      (eq gnus-undownloaded-mark
5687                                          (gnus-data-mark (car data)))))
5688                            (not (gnus-data-unread-p (car data)))))
5689             (setq data (cdr data)))
5690           (when data
5691             (goto-char (gnus-data-pos (car data)))
5692             (gnus-data-number (car data))))))
5693     (gnus-summary-position-point)))
5694
5695 (defun gnus-summary-next-subject (n &optional unread dont-display)
5696   "Go to next N'th summary line.
5697 If N is negative, go to the previous N'th subject line.
5698 If UNREAD is non-nil, only unread articles are selected.
5699 The difference between N and the actual number of steps taken is
5700 returned."
5701   (interactive "p")
5702   (let ((backward (< n 0))
5703         (n (abs n)))
5704     (while (and (> n 0)
5705                 (if backward
5706                     (gnus-summary-find-prev unread)
5707                   (gnus-summary-find-next unread)))
5708       (unless (zerop (setq n (1- n)))
5709         (gnus-summary-show-thread)))
5710     (when (/= 0 n)
5711       (gnus-message 7 "No more%s articles"
5712                     (if unread " unread" "")))
5713     (unless dont-display
5714       (gnus-summary-recenter)
5715       (gnus-summary-position-point))
5716     n))
5717
5718 (defun gnus-summary-next-unread-subject (n)
5719   "Go to next N'th unread summary line."
5720   (interactive "p")
5721   (gnus-summary-next-subject n t))
5722
5723 (defun gnus-summary-prev-subject (n &optional unread)
5724   "Go to previous N'th summary line.
5725 If optional argument UNREAD is non-nil, only unread article is selected."
5726   (interactive "p")
5727   (gnus-summary-next-subject (- n) unread))
5728
5729 (defun gnus-summary-prev-unread-subject (n)
5730   "Go to previous N'th unread summary line."
5731   (interactive "p")
5732   (gnus-summary-next-subject (- n) t))
5733
5734 (defun gnus-summary-goto-subject (article &optional force silent)
5735   "Go the subject line of ARTICLE.
5736 If FORCE, also allow jumping to articles not currently shown."
5737   (interactive "nArticle number: ")
5738   (let ((b (point))
5739         (data (gnus-data-find article)))
5740     ;; We read in the article if we have to.
5741     (and (not data)
5742          force
5743          (gnus-summary-insert-subject
5744           article
5745           (if (or (numberp force) (vectorp force)) force)
5746           t)
5747          (setq data (gnus-data-find article)))
5748     (goto-char b)
5749     (if (not data)
5750         (progn
5751           (unless silent
5752             (gnus-message 3 "Can't find article %d" article))
5753           nil)
5754       (goto-char (gnus-data-pos data))
5755       (gnus-summary-position-point)
5756       article)))
5757
5758 ;; Walking around summary lines with displaying articles.
5759
5760 (defun gnus-summary-expand-window (&optional arg)
5761   "Make the summary buffer take up the entire Emacs frame.
5762 Given a prefix, will force an `article' buffer configuration."
5763   (interactive "P")
5764   (if arg
5765       (gnus-configure-windows 'article 'force)
5766     (gnus-configure-windows 'summary 'force)))
5767
5768 (defun gnus-summary-display-article (article &optional all-header)
5769   "Display ARTICLE in article buffer."
5770   (gnus-set-global-variables)
5771   (if (null article)
5772       nil
5773     (prog1
5774         (if gnus-summary-display-article-function
5775             (funcall gnus-summary-display-article-function article all-header)
5776           (gnus-article-prepare article all-header))
5777       (gnus-run-hooks 'gnus-select-article-hook)
5778       (when (and gnus-current-article
5779                  (not (zerop gnus-current-article)))
5780         (gnus-summary-goto-subject gnus-current-article))
5781       (gnus-summary-recenter)
5782       (when (and gnus-use-trees gnus-show-threads)
5783         (gnus-possibly-generate-tree article)
5784         (gnus-highlight-selected-tree article))
5785       ;; Successfully display article.
5786       (gnus-article-set-window-start
5787        (cdr (assq article gnus-newsgroup-bookmarks))))))
5788
5789 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5790   "Select the current article.
5791 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5792 non-nil, the article will be re-fetched even if it already present in
5793 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5794 be displayed."
5795   ;; Make sure we are in the summary buffer to work around bbdb bug.
5796   (unless (eq major-mode 'gnus-summary-mode)
5797     (set-buffer gnus-summary-buffer))
5798   (let ((article (or article (gnus-summary-article-number)))
5799         (all-headers (not (not all-headers))) ;Must be T or NIL.
5800         gnus-summary-display-article-function)
5801     (and (not pseudo)
5802          (gnus-summary-article-pseudo-p article)
5803          (error "This is a pseudo-article"))
5804     (save-excursion
5805       (set-buffer gnus-summary-buffer)
5806       (if (or (and gnus-single-article-buffer
5807                    (or (null gnus-current-article)
5808                        (null gnus-article-current)
5809                        (null (get-buffer gnus-article-buffer))
5810                        (not (eq article (cdr gnus-article-current)))
5811                        (not (equal (car gnus-article-current)
5812                                    gnus-newsgroup-name))))
5813               (and (not gnus-single-article-buffer)
5814                    (or (null gnus-current-article)
5815                        (not (eq gnus-current-article article))))
5816               force)
5817           ;; The requested article is different from the current article.
5818           (progn
5819             (gnus-summary-display-article article all-headers)
5820             (when (or all-headers gnus-show-all-headers)
5821               (gnus-article-show-all-headers))
5822             (gnus-article-set-window-start
5823              (cdr (assq article gnus-newsgroup-bookmarks)))
5824             article)
5825         (when (or all-headers gnus-show-all-headers)
5826           (gnus-article-show-all-headers))
5827         'old))))
5828
5829 (defun gnus-summary-set-current-mark (&optional current-mark)
5830   "Obsolete function."
5831   nil)
5832
5833 (defun gnus-summary-next-article (&optional unread subject backward push)
5834   "Select the next article.
5835 If UNREAD, only unread articles are selected.
5836 If SUBJECT, only articles with SUBJECT are selected.
5837 If BACKWARD, the previous article is selected instead of the next."
5838   (interactive "P")
5839   (cond
5840    ;; Is there such an article?
5841    ((and (gnus-summary-search-forward unread subject backward)
5842          (or (gnus-summary-display-article (gnus-summary-article-number))
5843              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5844     (gnus-summary-position-point))
5845    ;; If not, we try the first unread, if that is wanted.
5846    ((and subject
5847          gnus-auto-select-same
5848          (gnus-summary-first-unread-article))
5849     (gnus-summary-position-point)
5850     (gnus-message 6 "Wrapped"))
5851    ;; Try to get next/previous article not displayed in this group.
5852    ((and gnus-auto-extend-newsgroup
5853          (not unread) (not subject))
5854     (gnus-summary-goto-article
5855      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5856      nil (count-lines (point-min) (point))))
5857    ;; Go to next/previous group.
5858    (t
5859     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5860       (gnus-summary-jump-to-group gnus-newsgroup-name))
5861     (let ((cmd last-command-char)
5862           (point
5863            (save-excursion
5864              (set-buffer gnus-group-buffer)
5865              (point)))
5866           (group
5867            (if (eq gnus-keep-same-level 'best)
5868                (gnus-summary-best-group gnus-newsgroup-name)
5869              (gnus-summary-search-group backward gnus-keep-same-level))))
5870       ;; For some reason, the group window gets selected.  We change
5871       ;; it back.
5872       (select-window (get-buffer-window (current-buffer)))
5873       ;; Select next unread newsgroup automagically.
5874       (cond
5875        ((or (not gnus-auto-select-next)
5876             (not cmd))
5877         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5878        ((or (eq gnus-auto-select-next 'quietly)
5879             (and (eq gnus-auto-select-next 'slightly-quietly)
5880                  push)
5881             (and (eq gnus-auto-select-next 'almost-quietly)
5882                  (gnus-summary-last-article-p)))
5883         ;; Select quietly.
5884         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5885             (gnus-summary-exit)
5886           (gnus-message 7 "No more%s articles (%s)..."
5887                         (if unread " unread" "")
5888                         (if group (concat "selecting " group)
5889                           "exiting"))
5890           (gnus-summary-next-group nil group backward)))
5891        (t
5892         (when (gnus-key-press-event-p last-input-event)
5893           (gnus-summary-walk-group-buffer
5894            gnus-newsgroup-name cmd unread backward point))))))))
5895
5896 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5897   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5898                       (?\C-p (gnus-group-prev-unread-group 1))))
5899         (cursor-in-echo-area t)
5900         keve key group ended)
5901     (save-excursion
5902       (set-buffer gnus-group-buffer)
5903       (goto-char start)
5904       (setq group
5905             (if (eq gnus-keep-same-level 'best)
5906                 (gnus-summary-best-group gnus-newsgroup-name)
5907               (gnus-summary-search-group backward gnus-keep-same-level))))
5908     (while (not ended)
5909       (gnus-message
5910        5 "No more%s articles%s" (if unread " unread" "")
5911        (if (and group
5912                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5913            (format " (Type %s for %s [%s])"
5914                    (single-key-description cmd) group
5915                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5916          (format " (Type %s to exit %s)"
5917                  (single-key-description cmd)
5918                  gnus-newsgroup-name)))
5919       ;; Confirm auto selection.
5920       (setq key (car (setq keve (gnus-read-event-char))))
5921       (setq ended t)
5922       (cond
5923        ((assq key keystrokes)
5924         (let ((obuf (current-buffer)))
5925           (switch-to-buffer gnus-group-buffer)
5926           (when group
5927             (gnus-group-jump-to-group group))
5928           (eval (cadr (assq key keystrokes)))
5929           (setq group (gnus-group-group-name))
5930           (switch-to-buffer obuf))
5931         (setq ended nil))
5932        ((equal key cmd)
5933         (if (or (not group)
5934                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5935             (gnus-summary-exit)
5936           (gnus-summary-next-group nil group backward)))
5937        (t
5938         (push (cdr keve) unread-command-events))))))
5939
5940 (defun gnus-summary-next-unread-article ()
5941   "Select unread article after current one."
5942   (interactive)
5943   (gnus-summary-next-article
5944    (or (not (eq gnus-summary-goto-unread 'never))
5945        (gnus-summary-last-article-p (gnus-summary-article-number)))
5946    (and gnus-auto-select-same
5947         (gnus-summary-article-subject))))
5948
5949 (defun gnus-summary-prev-article (&optional unread subject)
5950   "Select the article after the current one.
5951 If UNREAD is non-nil, only unread articles are selected."
5952   (interactive "P")
5953   (gnus-summary-next-article unread subject t))
5954
5955 (defun gnus-summary-prev-unread-article ()
5956   "Select unread article before current one."
5957   (interactive)
5958   (gnus-summary-prev-article
5959    (or (not (eq gnus-summary-goto-unread 'never))
5960        (gnus-summary-first-article-p (gnus-summary-article-number)))
5961    (and gnus-auto-select-same
5962         (gnus-summary-article-subject))))
5963
5964 (defun gnus-summary-next-page (&optional lines circular)
5965   "Show next page of the selected article.
5966 If at the end of the current article, select the next article.
5967 LINES says how many lines should be scrolled up.
5968
5969 If CIRCULAR is non-nil, go to the start of the article instead of
5970 selecting the next article when reaching the end of the current
5971 article."
5972   (interactive "P")
5973   (setq gnus-summary-buffer (current-buffer))
5974   (gnus-set-global-variables)
5975   (let ((article (gnus-summary-article-number))
5976         (article-window (get-buffer-window gnus-article-buffer t))
5977         endp)
5978     ;; If the buffer is empty, we have no article.
5979     (unless article
5980       (error "No article to select"))
5981     (gnus-configure-windows 'article)
5982     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5983         (if (and (eq gnus-summary-goto-unread 'never)
5984                  (not (gnus-summary-last-article-p article)))
5985             (gnus-summary-next-article)
5986           (gnus-summary-next-unread-article))
5987       (if (or (null gnus-current-article)
5988               (null gnus-article-current)
5989               (/= article (cdr gnus-article-current))
5990               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5991           ;; Selected subject is different from current article's.
5992           (gnus-summary-display-article article)
5993         (when article-window
5994           (gnus-eval-in-buffer-window gnus-article-buffer
5995             (setq endp (gnus-article-next-page lines)))
5996           (when endp
5997             (cond (circular
5998                    (gnus-summary-beginning-of-article))
5999                   (lines
6000                    (gnus-message 3 "End of message"))
6001                   ((null lines)
6002                    (if (and (eq gnus-summary-goto-unread 'never)
6003                             (not (gnus-summary-last-article-p article)))
6004                        (gnus-summary-next-article)
6005                      (gnus-summary-next-unread-article))))))))
6006     (gnus-summary-recenter)
6007     (gnus-summary-position-point)))
6008
6009 (defun gnus-summary-prev-page (&optional lines move)
6010   "Show previous page of selected article.
6011 Argument LINES specifies lines to be scrolled down.
6012 If MOVE, move to the previous unread article if point is at
6013 the beginning of the buffer."
6014   (interactive "P")
6015   (let ((article (gnus-summary-article-number))
6016         (article-window (get-buffer-window gnus-article-buffer t))
6017         endp)
6018     (gnus-configure-windows 'article)
6019     (if (or (null gnus-current-article)
6020             (null gnus-article-current)
6021             (/= article (cdr gnus-article-current))
6022             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6023         ;; Selected subject is different from current article's.
6024         (gnus-summary-display-article article)
6025       (gnus-summary-recenter)
6026       (when article-window
6027         (gnus-eval-in-buffer-window gnus-article-buffer
6028           (setq endp (gnus-article-prev-page lines)))
6029         (when (and move endp)
6030           (cond (lines
6031                  (gnus-message 3 "Beginning of message"))
6032                 ((null lines)
6033                  (if (and (eq gnus-summary-goto-unread 'never)
6034                           (not (gnus-summary-first-article-p article)))
6035                      (gnus-summary-prev-article)
6036                    (gnus-summary-prev-unread-article))))))))
6037   (gnus-summary-position-point))
6038
6039 (defun gnus-summary-prev-page-or-article (&optional lines)
6040   "Show previous page of selected article.
6041 Argument LINES specifies lines to be scrolled down.
6042 If at the beginning of the article, go to the next article."
6043   (interactive "P")
6044   (gnus-summary-prev-page lines t))
6045
6046 (defun gnus-summary-scroll-up (lines)
6047   "Scroll up (or down) one line current article.
6048 Argument LINES specifies lines to be scrolled up (or down if negative)."
6049   (interactive "p")
6050   (gnus-configure-windows 'article)
6051   (gnus-summary-show-thread)
6052   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6053     (gnus-eval-in-buffer-window gnus-article-buffer
6054       (cond ((> lines 0)
6055              (when (gnus-article-next-page lines)
6056                (gnus-message 3 "End of message")))
6057             ((< lines 0)
6058              (gnus-article-prev-page (- lines))))))
6059   (gnus-summary-recenter)
6060   (gnus-summary-position-point))
6061
6062 (defun gnus-summary-scroll-down (lines)
6063   "Scroll down (or up) one line current article.
6064 Argument LINES specifies lines to be scrolled down (or up if negative)."
6065   (interactive "p")
6066   (gnus-summary-scroll-up (- lines)))
6067
6068 (defun gnus-summary-next-same-subject ()
6069   "Select next article which has the same subject as current one."
6070   (interactive)
6071   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6072
6073 (defun gnus-summary-prev-same-subject ()
6074   "Select previous article which has the same subject as current one."
6075   (interactive)
6076   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6077
6078 (defun gnus-summary-next-unread-same-subject ()
6079   "Select next unread article which has the same subject as current one."
6080   (interactive)
6081   (gnus-summary-next-article t (gnus-summary-article-subject)))
6082
6083 (defun gnus-summary-prev-unread-same-subject ()
6084   "Select previous unread article which has the same subject as current one."
6085   (interactive)
6086   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6087
6088 (defun gnus-summary-first-unread-article ()
6089   "Select the first unread article.
6090 Return nil if there are no unread articles."
6091   (interactive)
6092   (prog1
6093       (when (gnus-summary-first-subject t)
6094         (gnus-summary-show-thread)
6095         (gnus-summary-first-subject t)
6096         (gnus-summary-display-article (gnus-summary-article-number)))
6097     (gnus-summary-position-point)))
6098
6099 (defun gnus-summary-first-unread-subject ()
6100   "Place the point on the subject line of the first unread article.
6101 Return nil if there are no unread articles."
6102   (interactive)
6103   (prog1
6104       (when (gnus-summary-first-subject t)
6105         (gnus-summary-show-thread)
6106         (gnus-summary-first-subject t))
6107     (gnus-summary-position-point)))
6108
6109 (defun gnus-summary-first-article ()
6110   "Select the first article.
6111 Return nil if there are no articles."
6112   (interactive)
6113   (prog1
6114       (when (gnus-summary-first-subject)
6115         (gnus-summary-show-thread)
6116         (gnus-summary-first-subject)
6117         (gnus-summary-display-article (gnus-summary-article-number)))
6118     (gnus-summary-position-point)))
6119
6120 (defun gnus-summary-best-unread-article ()
6121   "Select the unread article with the highest score."
6122   (interactive)
6123   (let ((best -1000000)
6124         (data gnus-newsgroup-data)
6125         article score)
6126     (while data
6127       (and (gnus-data-unread-p (car data))
6128            (> (setq score
6129                     (gnus-summary-article-score (gnus-data-number (car data))))
6130               best)
6131            (setq best score
6132                  article (gnus-data-number (car data))))
6133       (setq data (cdr data)))
6134     (prog1
6135         (if article
6136             (gnus-summary-goto-article article)
6137           (error "No unread articles"))
6138       (gnus-summary-position-point))))
6139
6140 (defun gnus-summary-last-subject ()
6141   "Go to the last displayed subject line in the group."
6142   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6143     (when article
6144       (gnus-summary-goto-subject article))))
6145
6146 (defun gnus-summary-goto-article (article &optional all-headers force)
6147   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6148 If ALL-HEADERS is non-nil, no header lines are hidden.
6149 If FORCE, go to the article even if it isn't displayed.  If FORCE
6150 is a number, it is the line the article is to be displayed on."
6151   (interactive
6152    (list
6153     (completing-read
6154      "Article number or Message-ID: "
6155      (mapcar (lambda (number) (list (int-to-string number)))
6156              gnus-newsgroup-limit))
6157     current-prefix-arg
6158     t))
6159   (prog1
6160       (if (and (stringp article)
6161                (string-match "@" article))
6162           (gnus-summary-refer-article article)
6163         (when (stringp article)
6164           (setq article (string-to-number article)))
6165         (if (gnus-summary-goto-subject article force)
6166             (gnus-summary-display-article article all-headers)
6167           (gnus-message 4 "Couldn't go to article %s" article) nil))
6168     (gnus-summary-position-point)))
6169
6170 (defun gnus-summary-goto-last-article ()
6171   "Go to the previously read article."
6172   (interactive)
6173   (prog1
6174       (when gnus-last-article
6175         (gnus-summary-goto-article gnus-last-article nil t))
6176     (gnus-summary-position-point)))
6177
6178 (defun gnus-summary-pop-article (number)
6179   "Pop one article off the history and go to the previous.
6180 NUMBER articles will be popped off."
6181   (interactive "p")
6182   (let (to)
6183     (setq gnus-newsgroup-history
6184           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6185     (if to
6186         (gnus-summary-goto-article (car to) nil t)
6187       (error "Article history empty")))
6188   (gnus-summary-position-point))
6189
6190 ;; Summary commands and functions for limiting the summary buffer.
6191
6192 (defun gnus-summary-limit-to-articles (n)
6193   "Limit the summary buffer to the next N articles.
6194 If not given a prefix, use the process marked articles instead."
6195   (interactive "P")
6196   (prog1
6197       (let ((articles (gnus-summary-work-articles n)))
6198         (setq gnus-newsgroup-processable nil)
6199         (gnus-summary-limit articles))
6200     (gnus-summary-position-point)))
6201
6202 (defun gnus-summary-pop-limit (&optional total)
6203   "Restore the previous limit.
6204 If given a prefix, remove all limits."
6205   (interactive "P")
6206   (when total
6207     (setq gnus-newsgroup-limits
6208           (list (mapcar (lambda (h) (mail-header-number h))
6209                         gnus-newsgroup-headers))))
6210   (unless gnus-newsgroup-limits
6211     (error "No limit to pop"))
6212   (prog1
6213       (gnus-summary-limit nil 'pop)
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-limit-to-subject (subject &optional header)
6217   "Limit the summary buffer to articles that have subjects that match a regexp."
6218   (interactive "sLimit to subject (regexp): ")
6219   (unless header
6220     (setq header "subject"))
6221   (when (not (equal "" subject))
6222     (prog1
6223         (let ((articles (gnus-summary-find-matching
6224                          (or header "subject") subject 'all)))
6225           (unless articles
6226             (error "Found no matches for \"%s\"" subject))
6227           (gnus-summary-limit articles))
6228       (gnus-summary-position-point))))
6229
6230 (defun gnus-summary-limit-to-author (from)
6231   "Limit the summary buffer to articles that have authors that match a regexp."
6232   (interactive "sLimit to author (regexp): ")
6233   (gnus-summary-limit-to-subject from "from"))
6234
6235 (defun gnus-summary-limit-to-age (age &optional younger-p)
6236   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6237 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6238 articles that are younger than AGE days."
6239   (interactive
6240    (let ((younger current-prefix-arg)
6241          (days-got nil)
6242          days)
6243      (while (not days-got)
6244        (setq days (if younger
6245                       (read-string "Limit to articles within (in days): ")
6246                     (read-string "Limit to articles old than (in days): ")))
6247        (when (> (length days) 0)
6248          (setq days (read days)))
6249        (if (numberp days)
6250            (setq days-got t)
6251          (message "Please enter a number.")
6252          (sleep-for 1)))
6253      (list days younger)))
6254   (prog1
6255       (let ((data gnus-newsgroup-data)
6256             (cutoff (days-to-time age))
6257             articles d date is-younger)
6258         (while (setq d (pop data))
6259           (when (and (vectorp (gnus-data-header d))
6260                      (setq date (mail-header-date (gnus-data-header d))))
6261             (setq is-younger (time-less-p
6262                               (time-since (condition-case ()
6263                                               (date-to-time date)
6264                                             (error '(0 0))))
6265                               cutoff))
6266             (when (if younger-p
6267                       is-younger
6268                     (not is-younger))
6269               (push (gnus-data-number d) articles))))
6270         (gnus-summary-limit (nreverse articles)))
6271     (gnus-summary-position-point)))
6272
6273 (defun gnus-summary-limit-to-extra (header regexp)
6274   "Limit the summary buffer to articles that match an 'extra' header."
6275   (interactive
6276    (let ((header
6277           (intern
6278            (gnus-completing-read
6279             (symbol-name (car gnus-extra-headers))      
6280             "Limit extra header:"       
6281             (mapcar (lambda (x) 
6282                       (cons (symbol-name x) x))
6283                     gnus-extra-headers)
6284             nil                 
6285             t))))
6286      (list header
6287            (read-string (format "Limit to header %s (regexp): " header)))))
6288   (when (not (equal "" regexp))
6289     (prog1
6290         (let ((articles (gnus-summary-find-matching
6291                          (cons 'extra header) regexp 'all)))
6292           (unless articles
6293             (error "Found no matches for \"%s\"" regexp))
6294           (gnus-summary-limit articles))
6295       (gnus-summary-position-point))))
6296
6297 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6298 (make-obsolete
6299  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6300
6301 (defun gnus-summary-limit-to-unread (&optional all)
6302   "Limit the summary buffer to articles that are not marked as read.
6303 If ALL is non-nil, limit strictly to unread articles."
6304   (interactive "P")
6305   (if all
6306       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6307     (gnus-summary-limit-to-marks
6308      ;; Concat all the marks that say that an article is read and have
6309      ;; those removed.
6310      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6311            gnus-killed-mark gnus-kill-file-mark
6312            gnus-low-score-mark gnus-expirable-mark
6313            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6314            gnus-duplicate-mark gnus-souped-mark)
6315      'reverse)))
6316
6317 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6318 (make-obsolete 'gnus-summary-delete-marked-with
6319                'gnus-summary-limit-exlude-marks)
6320
6321 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6322   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6323 If REVERSE, limit the summary buffer to articles that are marked
6324 with MARKS.  MARKS can either be a string of marks or a list of marks.
6325 Returns how many articles were removed."
6326   (interactive "sMarks: ")
6327   (gnus-summary-limit-to-marks marks t))
6328
6329 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6330   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6331 If REVERSE (the prefix), limit the summary buffer to articles that are
6332 not marked with MARKS.  MARKS can either be a string of marks or a
6333 list of marks.
6334 Returns how many articles were removed."
6335   (interactive "sMarks: \nP")
6336   (prog1
6337       (let ((data gnus-newsgroup-data)
6338             (marks (if (listp marks) marks
6339                      (append marks nil))) ; Transform to list.
6340             articles)
6341         (while data
6342           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6343                   (memq (gnus-data-mark (car data)) marks))
6344             (push (gnus-data-number (car data)) articles))
6345           (setq data (cdr data)))
6346         (gnus-summary-limit articles))
6347     (gnus-summary-position-point)))
6348
6349 (defun gnus-summary-limit-to-score (&optional score)
6350   "Limit to articles with score at or above SCORE."
6351   (interactive "P")
6352   (setq score (if score
6353                   (prefix-numeric-value score)
6354                 (or gnus-summary-default-score 0)))
6355   (let ((data gnus-newsgroup-data)
6356         articles)
6357     (while data
6358       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6359                 score)
6360         (push (gnus-data-number (car data)) articles))
6361       (setq data (cdr data)))
6362     (prog1
6363         (gnus-summary-limit articles)
6364       (gnus-summary-position-point))))
6365
6366 (defun gnus-summary-limit-include-thread (id)
6367   "Display all the hidden articles that in the current thread."
6368   (interactive (list (mail-header-id (gnus-summary-article-header))))
6369   (let ((articles (gnus-articles-in-thread
6370                    (gnus-id-to-thread (gnus-root-id id)))))
6371     (prog1
6372         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6373       (gnus-summary-position-point))))
6374
6375 (defun gnus-summary-limit-include-dormant ()
6376   "Display all the hidden articles that are marked as dormant.
6377 Note that this command only works on a subset of the articles currently
6378 fetched for this group."
6379   (interactive)
6380   (unless gnus-newsgroup-dormant
6381     (error "There are no dormant articles in this group"))
6382   (prog1
6383       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6384     (gnus-summary-position-point)))
6385
6386 (defun gnus-summary-limit-exclude-dormant ()
6387   "Hide all dormant articles."
6388   (interactive)
6389   (prog1
6390       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6391     (gnus-summary-position-point)))
6392
6393 (defun gnus-summary-limit-exclude-childless-dormant ()
6394   "Hide all dormant articles that have no children."
6395   (interactive)
6396   (let ((data (gnus-data-list t))
6397         articles d children)
6398     ;; Find all articles that are either not dormant or have
6399     ;; children.
6400     (while (setq d (pop data))
6401       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6402                 (and (setq children
6403                            (gnus-article-children (gnus-data-number d)))
6404                      (let (found)
6405                        (while children
6406                          (when (memq (car children) articles)
6407                            (setq children nil
6408                                  found t))
6409                          (pop children))
6410                        found)))
6411         (push (gnus-data-number d) articles)))
6412     ;; Do the limiting.
6413     (prog1
6414         (gnus-summary-limit articles)
6415       (gnus-summary-position-point))))
6416
6417 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6418   "Mark all unread excluded articles as read.
6419 If ALL, mark even excluded ticked and dormants as read."
6420   (interactive "P")
6421   (let ((articles (gnus-sorted-complement
6422                    (sort
6423                     (mapcar (lambda (h) (mail-header-number h))
6424                             gnus-newsgroup-headers)
6425                     '<)
6426                    (sort gnus-newsgroup-limit '<)))
6427         article)
6428     (setq gnus-newsgroup-unreads
6429           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6430     (if all
6431         (setq gnus-newsgroup-dormant nil
6432               gnus-newsgroup-marked nil
6433               gnus-newsgroup-reads
6434               (nconc
6435                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6436                gnus-newsgroup-reads))
6437       (while (setq article (pop articles))
6438         (unless (or (memq article gnus-newsgroup-dormant)
6439                     (memq article gnus-newsgroup-marked))
6440           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6441
6442 (defun gnus-summary-limit (articles &optional pop)
6443   (if pop
6444       ;; We pop the previous limit off the stack and use that.
6445       (setq articles (car gnus-newsgroup-limits)
6446             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6447     ;; We use the new limit, so we push the old limit on the stack.
6448     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6449   ;; Set the limit.
6450   (setq gnus-newsgroup-limit articles)
6451   (let ((total (length gnus-newsgroup-data))
6452         (data (gnus-data-find-list (gnus-summary-article-number)))
6453         (gnus-summary-mark-below nil)   ; Inhibit this.
6454         found)
6455     ;; This will do all the work of generating the new summary buffer
6456     ;; according to the new limit.
6457     (gnus-summary-prepare)
6458     ;; Hide any threads, possibly.
6459     (and gnus-show-threads
6460          gnus-thread-hide-subtree
6461          (gnus-summary-hide-all-threads))
6462     ;; Try to return to the article you were at, or one in the
6463     ;; neighborhood.
6464     (when data
6465       ;; We try to find some article after the current one.
6466       (while data
6467         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6468           (setq data nil
6469                 found t))
6470         (setq data (cdr data))))
6471     (unless found
6472       ;; If there is no data, that means that we were after the last
6473       ;; article.  The same goes when we can't find any articles
6474       ;; after the current one.
6475       (goto-char (point-max))
6476       (gnus-summary-find-prev))
6477     (gnus-set-mode-line 'summary)
6478     ;; We return how many articles were removed from the summary
6479     ;; buffer as a result of the new limit.
6480     (- total (length gnus-newsgroup-data))))
6481
6482 (defsubst gnus-invisible-cut-children (threads)
6483   (let ((num 0))
6484     (while threads
6485       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6486         (incf num))
6487       (pop threads))
6488     (< num 2)))
6489
6490 (defsubst gnus-cut-thread (thread)
6491   "Go forwards in the thread until we find an article that we want to display."
6492   (when (or (eq gnus-fetch-old-headers 'some)
6493             (eq gnus-fetch-old-headers 'invisible)
6494             (numberp gnus-fetch-old-headers)
6495             (eq gnus-build-sparse-threads 'some)
6496             (eq gnus-build-sparse-threads 'more))
6497     ;; Deal with old-fetched headers and sparse threads.
6498     (while (and
6499             thread
6500             (or
6501              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6502              (gnus-summary-article-ancient-p
6503               (mail-header-number (car thread))))
6504             (if (or (<= (length (cdr thread)) 1)
6505                     (eq gnus-fetch-old-headers 'invisible))
6506                 (setq gnus-newsgroup-limit
6507                       (delq (mail-header-number (car thread))
6508                             gnus-newsgroup-limit)
6509                       thread (cadr thread))
6510               (when (gnus-invisible-cut-children (cdr thread))
6511                 (let ((th (cdr thread)))
6512                   (while th
6513                     (if (memq (mail-header-number (caar th))
6514                               gnus-newsgroup-limit)
6515                         (setq thread (car th)
6516                               th nil)
6517                       (setq th (cdr th))))))))))
6518   thread)
6519
6520 (defun gnus-cut-threads (threads)
6521   "Cut off all uninteresting articles from the beginning of threads."
6522   (when (or (eq gnus-fetch-old-headers 'some)
6523             (eq gnus-fetch-old-headers 'invisible)
6524             (numberp gnus-fetch-old-headers)
6525             (eq gnus-build-sparse-threads 'some)
6526             (eq gnus-build-sparse-threads 'more))
6527     (let ((th threads))
6528       (while th
6529         (setcar th (gnus-cut-thread (car th)))
6530         (setq th (cdr th)))))
6531   ;; Remove nixed out threads.
6532   (delq nil threads))
6533
6534 (defun gnus-summary-initial-limit (&optional show-if-empty)
6535   "Figure out what the initial limit is supposed to be on group entry.
6536 This entails weeding out unwanted dormants, low-scored articles,
6537 fetch-old-headers verbiage, and so on."
6538   ;; Most groups have nothing to remove.
6539   (if (or gnus-inhibit-limiting
6540           (and (null gnus-newsgroup-dormant)
6541                (not (eq gnus-fetch-old-headers 'some))
6542                (not (numberp gnus-fetch-old-headers))
6543                (not (eq gnus-fetch-old-headers 'invisible))
6544                (null gnus-summary-expunge-below)
6545                (not (eq gnus-build-sparse-threads 'some))
6546                (not (eq gnus-build-sparse-threads 'more))
6547                (null gnus-thread-expunge-below)
6548                (not gnus-use-nocem)))
6549       ()                                ; Do nothing.
6550     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6551     (setq gnus-newsgroup-limit nil)
6552     (mapatoms
6553      (lambda (node)
6554        (unless (car (symbol-value node))
6555          ;; These threads have no parents -- they are roots.
6556          (let ((nodes (cdr (symbol-value node)))
6557                thread)
6558            (while nodes
6559              (if (and gnus-thread-expunge-below
6560                       (< (gnus-thread-total-score (car nodes))
6561                          gnus-thread-expunge-below))
6562                  (gnus-expunge-thread (pop nodes))
6563                (setq thread (pop nodes))
6564                (gnus-summary-limit-children thread))))))
6565      gnus-newsgroup-dependencies)
6566     ;; If this limitation resulted in an empty group, we might
6567     ;; pop the previous limit and use it instead.
6568     (when (and (not gnus-newsgroup-limit)
6569                show-if-empty)
6570       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6571     gnus-newsgroup-limit))
6572
6573 (defun gnus-summary-limit-children (thread)
6574   "Return 1 if this subthread is visible and 0 if it is not."
6575   ;; First we get the number of visible children to this thread.  This
6576   ;; is done by recursing down the thread using this function, so this
6577   ;; will really go down to a leaf article first, before slowly
6578   ;; working its way up towards the root.
6579   (when thread
6580     (let ((children
6581            (if (cdr thread)
6582                (apply '+ (mapcar 'gnus-summary-limit-children
6583                                  (cdr thread)))
6584              0))
6585           (number (mail-header-number (car thread)))
6586           score)
6587       (if (and
6588            (not (memq number gnus-newsgroup-marked))
6589            (or
6590             ;; If this article is dormant and has absolutely no visible
6591             ;; children, then this article isn't visible.
6592             (and (memq number gnus-newsgroup-dormant)
6593                  (zerop children))
6594             ;; If this is "fetch-old-headered" and there is no
6595             ;; visible children, then we don't want this article.
6596             (and (or (eq gnus-fetch-old-headers 'some)
6597                      (numberp gnus-fetch-old-headers))
6598                  (gnus-summary-article-ancient-p number)
6599                  (zerop children))
6600             ;; If this is "fetch-old-headered" and `invisible', then
6601             ;; we don't want this article.
6602             (and (eq gnus-fetch-old-headers 'invisible)
6603                  (gnus-summary-article-ancient-p number))
6604             ;; If this is a sparsely inserted article with no children,
6605             ;; we don't want it.
6606             (and (eq gnus-build-sparse-threads 'some)
6607                  (gnus-summary-article-sparse-p number)
6608                  (zerop children))
6609             ;; If we use expunging, and this article is really
6610             ;; low-scored, then we don't want this article.
6611             (when (and gnus-summary-expunge-below
6612                        (< (setq score
6613                                 (or (cdr (assq number gnus-newsgroup-scored))
6614                                     gnus-summary-default-score))
6615                           gnus-summary-expunge-below))
6616               ;; We increase the expunge-tally here, but that has
6617               ;; nothing to do with the limits, really.
6618               (incf gnus-newsgroup-expunged-tally)
6619               ;; We also mark as read here, if that's wanted.
6620               (when (and gnus-summary-mark-below
6621                          (< score gnus-summary-mark-below))
6622                 (setq gnus-newsgroup-unreads
6623                       (delq number gnus-newsgroup-unreads))
6624                 (if gnus-newsgroup-auto-expire
6625                     (push number gnus-newsgroup-expirable)
6626                   (push (cons number gnus-low-score-mark)
6627                         gnus-newsgroup-reads)))
6628               t)
6629             ;; Check NoCeM things.
6630             (if (and gnus-use-nocem
6631                      (gnus-nocem-unwanted-article-p
6632                       (mail-header-id (car thread))))
6633                 (progn
6634                   (setq gnus-newsgroup-unreads
6635                         (delq number gnus-newsgroup-unreads))
6636                   t))))
6637           ;; Nope, invisible article.
6638           0
6639         ;; Ok, this article is to be visible, so we add it to the limit
6640         ;; and return 1.
6641         (push number gnus-newsgroup-limit)
6642         1))))
6643
6644 (defun gnus-expunge-thread (thread)
6645   "Mark all articles in THREAD as read."
6646   (let* ((number (mail-header-number (car thread))))
6647     (incf gnus-newsgroup-expunged-tally)
6648     ;; We also mark as read here, if that's wanted.
6649     (setq gnus-newsgroup-unreads
6650           (delq number gnus-newsgroup-unreads))
6651     (if gnus-newsgroup-auto-expire
6652         (push number gnus-newsgroup-expirable)
6653       (push (cons number gnus-low-score-mark)
6654             gnus-newsgroup-reads)))
6655   ;; Go recursively through all subthreads.
6656   (mapcar 'gnus-expunge-thread (cdr thread)))
6657
6658 ;; Summary article oriented commands
6659
6660 (defun gnus-summary-refer-parent-article (n)
6661   "Refer parent article N times.
6662 If N is negative, go to ancestor -N instead.
6663 The difference between N and the number of articles fetched is returned."
6664   (interactive "p")
6665   (let ((skip 1)
6666         error header ref)
6667     (when (not (natnump n))
6668       (setq skip (abs n)
6669             n 1))
6670     (while (and (> n 0)
6671                 (not error))
6672       (setq header (gnus-summary-article-header))
6673       (if (and (eq (mail-header-number header)
6674                    (cdr gnus-article-current))
6675                (equal gnus-newsgroup-name
6676                       (car gnus-article-current)))
6677           ;; If we try to find the parent of the currently
6678           ;; displayed article, then we take a look at the actual
6679           ;; References header, since this is slightly more
6680           ;; reliable than the References field we got from the
6681           ;; server.
6682           (save-excursion
6683             (set-buffer gnus-original-article-buffer)
6684             (nnheader-narrow-to-headers)
6685             (unless (setq ref (message-fetch-field "references"))
6686               (setq ref (message-fetch-field "in-reply-to")))
6687             (widen))
6688         (setq ref
6689               ;; It's not the current article, so we take a bet on
6690               ;; the value we got from the server.
6691               (mail-header-references header)))
6692       (if (and ref
6693                (not (equal ref "")))
6694           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6695             (gnus-message 1 "Couldn't find parent"))
6696         (gnus-message 1 "No references in article %d"
6697                       (gnus-summary-article-number))
6698         (setq error t))
6699       (decf n))
6700     (gnus-summary-position-point)
6701     n))
6702
6703 (defun gnus-summary-refer-references ()
6704   "Fetch all articles mentioned in the References header.
6705 Return the number of articles fetched."
6706   (interactive)
6707   (let ((ref (mail-header-references (gnus-summary-article-header)))
6708         (current (gnus-summary-article-number))
6709         (n 0))
6710     (if (or (not ref)
6711             (equal ref ""))
6712         (error "No References in the current article")
6713       ;; For each Message-ID in the References header...
6714       (while (string-match "<[^>]*>" ref)
6715         (incf n)
6716         ;; ... fetch that article.
6717         (gnus-summary-refer-article
6718          (prog1 (match-string 0 ref)
6719            (setq ref (substring ref (match-end 0))))))
6720       (gnus-summary-goto-subject current)
6721       (gnus-summary-position-point)
6722       n)))
6723
6724 (defun gnus-summary-refer-thread (&optional limit)
6725   "Fetch all articles in the current thread.
6726 If LIMIT (the numerical prefix), fetch that many old headers instead
6727 of what's specified by the `gnus-refer-thread-limit' variable."
6728   (interactive "P")
6729   (let ((id (mail-header-id (gnus-summary-article-header)))
6730         (limit (if limit (prefix-numeric-value limit)
6731                  gnus-refer-thread-limit)))
6732     ;; We want to fetch LIMIT *old* headers, but we also have to
6733     ;; re-fetch all the headers in the current buffer, because many of
6734     ;; them may be undisplayed.  So we adjust LIMIT.
6735     (when (numberp limit)
6736       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6737     (unless (eq gnus-fetch-old-headers 'invisible)
6738       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6739       ;; Retrieve the headers and read them in.
6740       (if (eq (gnus-retrieve-headers
6741                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6742               'nov)
6743           (gnus-build-all-threads)
6744         (error "Can't fetch thread from backends that don't support NOV"))
6745       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6746     (gnus-summary-limit-include-thread id)))
6747
6748 (defun gnus-summary-refer-article (message-id)
6749   "Fetch an article specified by MESSAGE-ID."
6750   (interactive "sMessage-ID: ")
6751   (when (and (stringp message-id)
6752              (not (zerop (length message-id))))
6753     ;; Construct the correct Message-ID if necessary.
6754     ;; Suggested by tale@pawl.rpi.edu.
6755     (unless (string-match "^<" message-id)
6756       (setq message-id (concat "<" message-id)))
6757     (unless (string-match ">$" message-id)
6758       (setq message-id (concat message-id ">")))
6759     (let* ((header (gnus-id-to-header message-id))
6760            (sparse (and header
6761                         (gnus-summary-article-sparse-p
6762                          (mail-header-number header))
6763                         (memq (mail-header-number header)
6764                               gnus-newsgroup-limit)))
6765            number)
6766       (cond
6767        ;; If the article is present in the buffer we just go to it.
6768        ((and header
6769              (or (not (gnus-summary-article-sparse-p
6770                        (mail-header-number header)))
6771                  sparse))
6772         (prog1
6773             (gnus-summary-goto-article
6774              (mail-header-number header) nil t)
6775           (when sparse
6776             (gnus-summary-update-article (mail-header-number header)))))
6777        (t
6778         ;; We fetch the article.
6779         (catch 'found
6780           (dolist (gnus-override-method (gnus-refer-article-methods))
6781             (gnus-check-server gnus-override-method)
6782             ;; Fetch the header, and display the article.
6783             (when (setq number (gnus-summary-insert-subject message-id))
6784               (gnus-summary-select-article nil nil nil number)
6785               (throw 'found t)))
6786           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6787
6788 (defun gnus-refer-article-methods ()
6789   "Return a list of referrable methods."
6790   (cond
6791    ;; No method, so we default to current and native.
6792    ((null gnus-refer-article-method)
6793     (list gnus-current-select-method gnus-select-method))
6794    ;; Current.
6795    ((eq 'current gnus-refer-article-method)
6796     (list gnus-current-select-method))
6797    ;; List of select methods.
6798    ((not (stringp (cadr gnus-refer-article-method)))
6799     (let (out)
6800       (dolist (method gnus-refer-article-method)
6801         (push (if (eq 'current method)
6802                   gnus-current-select-method
6803                 method)
6804               out))
6805       (nreverse out)))
6806    ;; One single select method.
6807    (t
6808     (list gnus-refer-article-method))))
6809
6810 (defun gnus-summary-edit-parameters ()
6811   "Edit the group parameters of the current group."
6812   (interactive)
6813   (gnus-group-edit-group gnus-newsgroup-name 'params))
6814
6815 (defun gnus-summary-customize-parameters ()
6816   "Customize the group parameters of the current group."
6817   (interactive)
6818   (gnus-group-customize gnus-newsgroup-name))
6819
6820 (defun gnus-summary-enter-digest-group (&optional force)
6821   "Enter an nndoc group based on the current article.
6822 If FORCE, force a digest interpretation.  If not, try
6823 to guess what the document format is."
6824   (interactive "P")
6825   (let ((conf gnus-current-window-configuration))
6826     (save-excursion
6827       (gnus-summary-select-article))
6828     (setq gnus-current-window-configuration conf)
6829     (let* ((name (format "%s-%d"
6830                          (gnus-group-prefixed-name
6831                           gnus-newsgroup-name (list 'nndoc ""))
6832                          (save-excursion
6833                            (set-buffer gnus-summary-buffer)
6834                            gnus-current-article)))
6835            (ogroup gnus-newsgroup-name)
6836            (params (append (gnus-info-params (gnus-get-info ogroup))
6837                            (list (cons 'to-group ogroup))
6838                            (list (cons 'save-article-group ogroup))))
6839            (case-fold-search t)
6840            (buf (current-buffer))
6841            dig to-address)
6842       (save-excursion
6843         (set-buffer gnus-original-article-buffer)
6844         ;; Have the digest group inherit the main mail address of
6845         ;; the parent article.
6846         (when (setq to-address (or (message-fetch-field "reply-to")
6847                                    (message-fetch-field "from")))
6848           (setq params (append (list (cons 'to-address to-address)))))
6849         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6850         (insert-buffer-substring gnus-original-article-buffer)
6851         ;; Remove lines that may lead nndoc to misinterpret the
6852         ;; document type.
6853         (narrow-to-region
6854          (goto-char (point-min))
6855          (or (search-forward "\n\n" nil t) (point)))
6856         (goto-char (point-min))
6857         (delete-matching-lines "^Path:\\|^From ")
6858         (widen))
6859       (unwind-protect
6860           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6861                     (gnus-newsgroup-ephemeral-ignored-charsets
6862                      gnus-newsgroup-ignored-charsets))
6863                 (gnus-group-read-ephemeral-group
6864                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6865                               (nndoc-article-type
6866                                ,(if force 'mbox 'guess))) t))
6867               ;; Make all postings to this group go to the parent group.
6868               (nconc (gnus-info-params (gnus-get-info name))
6869                      params)
6870             ;; Couldn't select this doc group.
6871             (switch-to-buffer buf)
6872             (gnus-set-global-variables)
6873             (gnus-configure-windows 'summary)
6874             (gnus-message 3 "Article couldn't be entered?"))
6875         (kill-buffer dig)))))
6876
6877 (defun gnus-summary-read-document (n)
6878   "Open a new group based on the current article(s).
6879 This will allow you to read digests and other similar
6880 documents as newsgroups.
6881 Obeys the standard process/prefix convention."
6882   (interactive "P")
6883   (let* ((articles (gnus-summary-work-articles n))
6884          (ogroup gnus-newsgroup-name)
6885          (params (append (gnus-info-params (gnus-get-info ogroup))
6886                          (list (cons 'to-group ogroup))))
6887          article group egroup groups vgroup)
6888     (while (setq article (pop articles))
6889       (setq group (format "%s-%d" gnus-newsgroup-name article))
6890       (gnus-summary-remove-process-mark article)
6891       (when (gnus-summary-display-article article)
6892         (save-excursion
6893           (with-temp-buffer
6894             (insert-buffer-substring gnus-original-article-buffer)
6895             ;; Remove some headers that may lead nndoc to make
6896             ;; the wrong guess.
6897             (message-narrow-to-head)
6898             (goto-char (point-min))
6899             (delete-matching-lines "^\\(Path\\):\\|^From ")
6900             (widen)
6901             (if (setq egroup
6902                       (gnus-group-read-ephemeral-group
6903                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6904                                      (nndoc-article-type guess))
6905                        t nil t))
6906                 (progn
6907                   ;; Make all postings to this group go to the parent group.
6908                   (nconc (gnus-info-params (gnus-get-info egroup))
6909                          params)
6910                   (push egroup groups))
6911               ;; Couldn't select this doc group.
6912               (gnus-error 3 "Article couldn't be entered"))))))
6913     ;; Now we have selected all the documents.
6914     (cond
6915      ((not groups)
6916       (error "None of the articles could be interpreted as documents"))
6917      ((gnus-group-read-ephemeral-group
6918        (setq vgroup (format
6919                      "nnvirtual:%s-%s" gnus-newsgroup-name
6920                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6921        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6922        t
6923        (cons (current-buffer) 'summary)))
6924      (t
6925       (error "Couldn't select virtual nndoc group")))))
6926
6927 (defun gnus-summary-isearch-article (&optional regexp-p)
6928   "Do incremental search forward on the current article.
6929 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6930   (interactive "P")
6931   (gnus-summary-select-article)
6932   (gnus-configure-windows 'article)
6933   (gnus-eval-in-buffer-window gnus-article-buffer
6934     (save-restriction
6935       (widen)
6936       (isearch-forward regexp-p))))
6937
6938 (defun gnus-summary-search-article-forward (regexp &optional backward)
6939   "Search for an article containing REGEXP forward.
6940 If BACKWARD, search backward instead."
6941   (interactive
6942    (list (read-string
6943           (format "Search article %s (regexp%s): "
6944                   (if current-prefix-arg "backward" "forward")
6945                   (if gnus-last-search-regexp
6946                       (concat ", default " gnus-last-search-regexp)
6947                     "")))
6948          current-prefix-arg))
6949   (if (string-equal regexp "")
6950       (setq regexp (or gnus-last-search-regexp ""))
6951     (setq gnus-last-search-regexp regexp))
6952   (if (gnus-summary-search-article regexp backward)
6953       (gnus-summary-show-thread)
6954     (error "Search failed: \"%s\"" regexp)))
6955
6956 (defun gnus-summary-search-article-backward (regexp)
6957   "Search for an article containing REGEXP backward."
6958   (interactive
6959    (list (read-string
6960           (format "Search article backward (regexp%s): "
6961                   (if gnus-last-search-regexp
6962                       (concat ", default " gnus-last-search-regexp)
6963                     "")))))
6964   (gnus-summary-search-article-forward regexp 'backward))
6965
6966 (defun gnus-summary-search-article (regexp &optional backward)
6967   "Search for an article containing REGEXP.
6968 Optional argument BACKWARD means do search for backward.
6969 `gnus-select-article-hook' is not called during the search."
6970   ;; We have to require this here to make sure that the following
6971   ;; dynamic binding isn't shadowed by autoloading.
6972   (require 'gnus-async)
6973   (require 'gnus-art)
6974   (let ((gnus-select-article-hook nil)  ;Disable hook.
6975         (gnus-article-prepare-hook nil)
6976         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6977         (gnus-use-article-prefetch nil)
6978         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6979         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6980         (sum (current-buffer))
6981         (gnus-display-mime-function nil)
6982         (found nil)
6983         point)
6984     (gnus-save-hidden-threads
6985       (gnus-summary-select-article)
6986       (set-buffer gnus-article-buffer)
6987       (goto-char (window-point (get-buffer-window (current-buffer))))
6988       (when backward
6989         (forward-line -1))
6990       (while (not found)
6991         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6992         (if (if backward
6993                 (re-search-backward regexp nil t)
6994               (re-search-forward regexp nil t))
6995             ;; We found the regexp.
6996             (progn
6997               (setq found 'found)
6998               (beginning-of-line)
6999               (set-window-start
7000                (get-buffer-window (current-buffer))
7001                (point))
7002               (forward-line 1)
7003               (set-window-point
7004                (get-buffer-window (current-buffer))
7005                (point))
7006               (set-buffer sum)
7007               (setq point (point)))
7008           ;; We didn't find it, so we go to the next article.
7009           (set-buffer sum)
7010           (setq found 'not)
7011           (while (eq found 'not)
7012             (if (not (if backward (gnus-summary-find-prev)
7013                        (gnus-summary-find-next)))
7014                 ;; No more articles.
7015                 (setq found t)
7016               ;; Select the next article and adjust point.
7017               (unless (gnus-summary-article-sparse-p
7018                        (gnus-summary-article-number))
7019                 (setq found nil)
7020                 (gnus-summary-select-article)
7021                 (set-buffer gnus-article-buffer)
7022                 (widen)
7023                 (goto-char (if backward (point-max) (point-min))))))))
7024       (gnus-message 7 ""))
7025     ;; Return whether we found the regexp.
7026     (when (eq found 'found)
7027       (goto-char point)
7028       (gnus-summary-show-thread)
7029       (gnus-summary-goto-subject gnus-current-article)
7030       (gnus-summary-position-point)
7031       t)))
7032
7033 (defun gnus-summary-find-matching (header regexp &optional backward unread
7034                                           not-case-fold)
7035   "Return a list of all articles that match REGEXP on HEADER.
7036 The search stars on the current article and goes forwards unless
7037 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7038 If UNREAD is non-nil, only unread articles will
7039 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7040 in the comparisons."
7041   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7042                 (gnus-data-find-list
7043                  (gnus-summary-article-number) (gnus-data-list backward))))
7044         (case-fold-search (not not-case-fold))
7045         articles d func)
7046     (if (consp header)
7047         (if (eq (car header) 'extra)
7048             (setq func
7049                   `(lambda (h)
7050                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7051                          "")))
7052           (error "%s is an invalid header" header))
7053       (unless (fboundp (intern (concat "mail-header-" header)))
7054         (error "%s is not a valid header" header))
7055       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7056     (while data
7057       (setq d (car data))
7058       (and (or (not unread)             ; We want all articles...
7059                (gnus-data-unread-p d))  ; Or just unreads.
7060            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7061            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7062            (push (gnus-data-number d) articles)) ; Success!
7063       (setq data (cdr data)))
7064     (nreverse articles)))
7065
7066 (defun gnus-summary-execute-command (header regexp command &optional backward)
7067   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7068 If HEADER is an empty string (or nil), the match is done on the entire
7069 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7070   (interactive
7071    (list (let ((completion-ignore-case t))
7072            (completing-read
7073             "Header name: "
7074             (mapcar (lambda (string) (list string))
7075                     '("Number" "Subject" "From" "Lines" "Date"
7076                       "Message-ID" "Xref" "References" "Body"))
7077             nil 'require-match))
7078          (read-string "Regexp: ")
7079          (read-key-sequence "Command: ")
7080          current-prefix-arg))
7081   (when (equal header "Body")
7082     (setq header ""))
7083   ;; Hidden thread subtrees must be searched as well.
7084   (gnus-summary-show-all-threads)
7085   ;; We don't want to change current point nor window configuration.
7086   (save-excursion
7087     (save-window-excursion
7088       (gnus-message 6 "Executing %s..." (key-description command))
7089       ;; We'd like to execute COMMAND interactively so as to give arguments.
7090       (gnus-execute header regexp
7091                     `(call-interactively ',(key-binding command))
7092                     backward)
7093       (gnus-message 6 "Executing %s...done" (key-description command)))))
7094
7095 (defun gnus-summary-beginning-of-article ()
7096   "Scroll the article back to the beginning."
7097   (interactive)
7098   (gnus-summary-select-article)
7099   (gnus-configure-windows 'article)
7100   (gnus-eval-in-buffer-window gnus-article-buffer
7101     (widen)
7102     (goto-char (point-min))
7103     (when gnus-page-broken
7104       (gnus-narrow-to-page))))
7105
7106 (defun gnus-summary-end-of-article ()
7107   "Scroll to the end of the article."
7108   (interactive)
7109   (gnus-summary-select-article)
7110   (gnus-configure-windows 'article)
7111   (gnus-eval-in-buffer-window gnus-article-buffer
7112     (widen)
7113     (goto-char (point-max))
7114     (recenter -3)
7115     (when gnus-page-broken
7116       (gnus-narrow-to-page))))
7117
7118 (defun gnus-summary-print-article (&optional filename n)
7119   "Generate and print a PostScript image of the N next (mail) articles.
7120
7121 If N is negative, print the N previous articles.  If N is nil and articles
7122 have been marked with the process mark, print these instead.
7123
7124 If the optional first argument FILENAME is nil, send the image to the
7125 printer.  If FILENAME is a string, save the PostScript image in a file with
7126 that name.  If FILENAME is a number, prompt the user for the name of the file
7127 to save in."
7128   (interactive (list (ps-print-preprint current-prefix-arg)
7129                      current-prefix-arg))
7130   (dolist (article (gnus-summary-work-articles n))
7131     (gnus-summary-select-article nil nil 'pseudo article)
7132     (gnus-eval-in-buffer-window gnus-article-buffer
7133       (let ((buffer (generate-new-buffer " *print*")))
7134         (unwind-protect
7135             (progn
7136               (copy-to-buffer buffer (point-min) (point-max))
7137               (set-buffer buffer)
7138               (gnus-article-delete-invisible-text)
7139               (let ((ps-left-header
7140                      (list
7141                       (concat "("
7142                               (mail-header-subject gnus-current-headers) ")")
7143                       (concat "("
7144                               (mail-header-from gnus-current-headers) ")")))
7145                     (ps-right-header
7146                      (list
7147                       "/pagenumberstring load"
7148                       (concat "("
7149                               (mail-header-date gnus-current-headers) ")"))))
7150                 (gnus-run-hooks 'gnus-ps-print-hook)
7151                 (save-excursion
7152                   (ps-print-buffer-with-faces filename))))
7153           (kill-buffer buffer))))))
7154
7155 (defun gnus-summary-show-article (&optional arg)
7156   "Force re-fetching of the current article.
7157 If ARG (the prefix) is a number, show the article with the charset 
7158 defined in `gnus-summary-show-article-charset-alist', or the charset
7159 inputed.
7160 If ARG (the prefix) is non-nil and not a number, show the raw article 
7161 without any article massaging functions being run."
7162   (interactive "P")
7163   (cond 
7164    ((numberp arg)
7165     (let ((gnus-newsgroup-charset 
7166            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7167                (read-coding-system "Charset: ")))
7168           (gnus-newsgroup-ignored-charsets 'gnus-all))
7169       (gnus-summary-select-article nil 'force)))
7170    ((not arg)
7171     ;; Select the article the normal way.
7172     (gnus-summary-select-article nil 'force))
7173    (t
7174     ;; We have to require this here to make sure that the following
7175     ;; dynamic binding isn't shadowed by autoloading.
7176     (require 'gnus-async)
7177     (require 'gnus-art)
7178     ;; Bind the article treatment functions to nil.
7179     (let ((gnus-have-all-headers t)
7180           gnus-article-prepare-hook
7181           gnus-article-decode-hook
7182           gnus-display-mime-function
7183           gnus-break-pages)
7184       ;; Destroy any MIME parts.
7185       (when (gnus-buffer-live-p gnus-article-buffer)
7186         (save-excursion
7187           (set-buffer gnus-article-buffer)
7188           (mm-destroy-parts gnus-article-mime-handles)))
7189       (gnus-summary-select-article nil 'force))))
7190   (gnus-summary-goto-subject gnus-current-article)
7191   (gnus-summary-position-point))
7192
7193 (defun gnus-summary-verbose-headers (&optional arg)
7194   "Toggle permanent full header display.
7195 If ARG is a positive number, turn header display on.
7196 If ARG is a negative number, turn header display off."
7197   (interactive "P")
7198   (setq gnus-show-all-headers
7199         (cond ((or (not (numberp arg))
7200                    (zerop arg))
7201                (not gnus-show-all-headers))
7202               ((natnump arg)
7203                t)))
7204   (gnus-summary-show-article))
7205
7206 (defun gnus-summary-toggle-header (&optional arg)
7207   "Show the headers if they are hidden, or hide them if they are shown.
7208 If ARG is a positive number, show the entire header.
7209 If ARG is a negative number, hide the unwanted header lines."
7210   (interactive "P")
7211   (save-excursion
7212     (set-buffer gnus-article-buffer)
7213     (save-restriction
7214       (let* ((buffer-read-only nil)
7215              (inhibit-point-motion-hooks t)
7216              hidden e)
7217         (setq hidden
7218               (if (numberp arg)
7219                   (>= arg 0)
7220                 (save-restriction 
7221                   (article-narrow-to-head)
7222                   (gnus-article-hidden-text-p 'headers))))
7223         (goto-char (point-min))
7224         (when (search-forward "\n\n" nil t)
7225           (delete-region (point-min) (1- (point))))
7226         (goto-char (point-min))
7227         (save-excursion
7228           (set-buffer gnus-original-article-buffer)
7229           (goto-char (point-min))
7230           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7231         (insert-buffer-substring gnus-original-article-buffer 1 e)
7232         (save-restriction
7233           (narrow-to-region (point-min) (point))
7234           (article-decode-encoded-words)
7235           (if  hidden
7236               (let ((gnus-treat-hide-headers nil)
7237                     (gnus-treat-hide-boring-headers nil))
7238                 (gnus-treat-article 'head))
7239             (gnus-treat-article 'head)))))))
7240
7241 (defun gnus-summary-show-all-headers ()
7242   "Make all header lines visible."
7243   (interactive)
7244   (gnus-article-show-all-headers))
7245
7246 (defun gnus-summary-caesar-message (&optional arg)
7247   "Caesar rotate the current article by 13.
7248 The numerical prefix specifies how many places to rotate each letter
7249 forward."
7250   (interactive "P")
7251   (gnus-summary-select-article)
7252   (let ((mail-header-separator ""))
7253     (gnus-eval-in-buffer-window gnus-article-buffer
7254       (save-restriction
7255         (widen)
7256         (let ((start (window-start))
7257               buffer-read-only)
7258           (message-caesar-buffer-body arg)
7259           (set-window-start (get-buffer-window (current-buffer)) start))))))
7260
7261 (defun gnus-summary-stop-page-breaking ()
7262   "Stop page breaking in the current article."
7263   (interactive)
7264   (gnus-summary-select-article)
7265   (gnus-eval-in-buffer-window gnus-article-buffer
7266     (widen)
7267     (when (gnus-visual-p 'page-marker)
7268       (let ((buffer-read-only nil))
7269         (gnus-remove-text-with-property 'gnus-prev)
7270         (gnus-remove-text-with-property 'gnus-next))
7271       (setq gnus-page-broken nil))))
7272
7273 (defun gnus-summary-move-article (&optional n to-newsgroup
7274                                             select-method action)
7275   "Move the current article to a different newsgroup.
7276 If N is a positive number, move the N next articles.
7277 If N is a negative number, move the N previous articles.
7278 If N is nil and any articles have been marked with the process mark,
7279 move those articles instead.
7280 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7281 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7282 re-spool using this method.
7283
7284 For this function to work, both the current newsgroup and the
7285 newsgroup that you want to move to have to support the `request-move'
7286 and `request-accept' functions.
7287
7288 ACTION can be either `move' (the default), `crosspost' or `copy'."
7289   (interactive "P")
7290   (unless action
7291     (setq action 'move))
7292   ;; Disable marking as read.
7293   (let (gnus-mark-article-hook)
7294     (save-window-excursion
7295       (gnus-summary-select-article)))
7296   ;; Check whether the source group supports the required functions.
7297   (cond ((and (eq action 'move)
7298               (not (gnus-check-backend-function
7299                     'request-move-article gnus-newsgroup-name)))
7300          (error "The current group does not support article moving"))
7301         ((and (eq action 'crosspost)
7302               (not (gnus-check-backend-function
7303                     'request-replace-article gnus-newsgroup-name)))
7304          (error "The current group does not support article editing")))
7305   (let ((articles (gnus-summary-work-articles n))
7306         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7307         (names '((move "Move" "Moving")
7308                  (copy "Copy" "Copying")
7309                  (crosspost "Crosspost" "Crossposting")))
7310         (copy-buf (save-excursion
7311                     (nnheader-set-temp-buffer " *copy article*")))
7312         art-group to-method new-xref article to-groups)
7313     (unless (assq action names)
7314       (error "Unknown action %s" action))
7315     ;; Read the newsgroup name.
7316     (when (and (not to-newsgroup)
7317                (not select-method))
7318       (setq to-newsgroup
7319             (gnus-read-move-group-name
7320              (cadr (assq action names))
7321              (symbol-value (intern (format "gnus-current-%s-group" action)))
7322              articles prefix))
7323       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7324     (setq to-method (or select-method
7325                         (gnus-group-name-to-method to-newsgroup)))
7326     ;; Check the method we are to move this article to...
7327     (unless (gnus-check-backend-function
7328              'request-accept-article (car to-method))
7329       (error "%s does not support article copying" (car to-method)))
7330     (unless (gnus-check-server to-method)
7331       (error "Can't open server %s" (car to-method)))
7332     (gnus-message 6 "%s to %s: %s..."
7333                   (caddr (assq action names))
7334                   (or (car select-method) to-newsgroup) articles)
7335     (while articles
7336       (setq article (pop articles))
7337       (setq
7338        art-group
7339        (cond
7340         ;; Move the article.
7341         ((eq action 'move)
7342          ;; Remove this article from future suppression.
7343          (gnus-dup-unsuppress-article article)
7344          (gnus-request-move-article
7345           article                       ; Article to move
7346           gnus-newsgroup-name           ; From newsgroup
7347           (nth 1 (gnus-find-method-for-group
7348                   gnus-newsgroup-name)) ; Server
7349           (list 'gnus-request-accept-article
7350                 to-newsgroup (list 'quote select-method)
7351                 (not articles) t)       ; Accept form
7352           (not articles)))              ; Only save nov last time
7353         ;; Copy the article.
7354         ((eq action 'copy)
7355          (save-excursion
7356            (set-buffer copy-buf)
7357            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7358              (gnus-request-accept-article
7359               to-newsgroup select-method (not articles) t))))
7360         ;; Crosspost the article.
7361         ((eq action 'crosspost)
7362          (let ((xref (message-tokenize-header
7363                       (mail-header-xref (gnus-summary-article-header article))
7364                       " ")))
7365            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7366                                   ":" article))
7367            (unless xref
7368              (setq xref (list (system-name))))
7369            (setq new-xref
7370                  (concat
7371                   (mapconcat 'identity
7372                              (delete "Xref:" (delete new-xref xref))
7373                              " ")
7374                   " " new-xref))
7375            (save-excursion
7376              (set-buffer copy-buf)
7377              ;; First put the article in the destination group.
7378              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7379              (when (consp (setq art-group
7380                                 (gnus-request-accept-article
7381                                  to-newsgroup select-method (not articles))))
7382                (setq new-xref (concat new-xref " " (car art-group)
7383                                       ":" (cdr art-group)))
7384                ;; Now we have the new Xrefs header, so we insert
7385                ;; it and replace the new article.
7386                (nnheader-replace-header "Xref" new-xref)
7387                (gnus-request-replace-article
7388                 (cdr art-group) to-newsgroup (current-buffer))
7389                art-group))))))
7390       (cond
7391        ((not art-group)
7392         (gnus-message 1 "Couldn't %s article %s: %s"
7393                       (cadr (assq action names)) article
7394                       (nnheader-get-report (car to-method))))
7395        ((and (eq art-group 'junk)
7396              (eq action 'move))
7397         (gnus-summary-mark-article article gnus-canceled-mark)
7398         (gnus-message 4 "Deleted article %s" article))
7399        (t
7400         (let* ((pto-group (gnus-group-prefixed-name
7401                            (car art-group) to-method))
7402                (entry
7403                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7404                (info (nth 2 entry))
7405                (to-group (gnus-info-group info))
7406                to-marks)
7407           ;; Update the group that has been moved to.
7408           (when (and info
7409                      (memq action '(move copy)))
7410             (unless (member to-group to-groups)
7411               (push to-group to-groups))
7412
7413             (unless (memq article gnus-newsgroup-unreads)
7414               (push 'read to-marks)
7415               (gnus-info-set-read
7416                info (gnus-add-to-range (gnus-info-read info)
7417                                        (list (cdr art-group)))))
7418
7419             ;; See whether the article is to be put in the cache.
7420             (let ((marks gnus-article-mark-lists)
7421                   (to-article (cdr art-group)))
7422
7423               ;; Enter the article into the cache in the new group,
7424               ;; if that is required.
7425               (when gnus-use-cache
7426                 (gnus-cache-possibly-enter-article
7427                  to-group to-article
7428                  (memq article gnus-newsgroup-marked)
7429                  (memq article gnus-newsgroup-dormant)
7430                  (memq article gnus-newsgroup-unreads)))
7431
7432               (when gnus-preserve-marks 
7433                 ;; Copy any marks over to the new group.
7434                 (when (and (equal to-group gnus-newsgroup-name)
7435                            (not (memq article gnus-newsgroup-unreads)))
7436                   ;; Mark this article as read in this group.
7437                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7438                   (setcdr (gnus-active to-group) to-article)
7439                   (setcdr gnus-newsgroup-active to-article))
7440
7441                 (while marks
7442                   (when (memq article (symbol-value
7443                                        (intern (format "gnus-newsgroup-%s"
7444                                                        (caar marks)))))
7445                     (push (cdar marks) to-marks)
7446                     ;; If the other group is the same as this group,
7447                     ;; then we have to add the mark to the list.
7448                     (when (equal to-group gnus-newsgroup-name)
7449                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7450                            (cons to-article
7451                                  (symbol-value
7452                                   (intern (format "gnus-newsgroup-%s"
7453                                                   (caar marks)))))))
7454                     ;; Copy the marks to other group.
7455                     (gnus-add-marked-articles
7456                      to-group (cdar marks) (list to-article) info))
7457                   (setq marks (cdr marks)))
7458
7459                 (gnus-request-set-mark to-group (list (list (list to-article)
7460                                                             'set
7461                                                             to-marks))))
7462
7463               (gnus-dribble-enter
7464                (concat "(gnus-group-set-info '"
7465                        (gnus-prin1-to-string (gnus-get-info to-group))
7466                        ")"))))
7467
7468           ;; Update the Xref header in this article to point to
7469           ;; the new crossposted article we have just created.
7470           (when (eq action 'crosspost)
7471             (save-excursion
7472               (set-buffer copy-buf)
7473               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7474               (nnheader-replace-header "Xref" new-xref)
7475               (gnus-request-replace-article
7476                article gnus-newsgroup-name (current-buffer)))))
7477
7478         ;;;!!!Why is this necessary?
7479         (set-buffer gnus-summary-buffer)
7480
7481         (gnus-summary-goto-subject article)
7482         (when (eq action 'move)
7483           (gnus-summary-mark-article article gnus-canceled-mark))))
7484       (gnus-summary-remove-process-mark article))
7485     ;; Re-activate all groups that have been moved to.
7486     (while to-groups
7487       (save-excursion
7488         (set-buffer gnus-group-buffer)
7489         (when (gnus-group-goto-group (car to-groups) t)
7490           (gnus-group-get-new-news-this-group 1 t))
7491         (pop to-groups)))
7492
7493     (gnus-kill-buffer copy-buf)
7494     (gnus-summary-position-point)
7495     (gnus-set-mode-line 'summary)))
7496
7497 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7498   "Move the current article to a different newsgroup.
7499 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7500 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7501 re-spool using this method."
7502   (interactive "P")
7503   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7504
7505 (defun gnus-summary-crosspost-article (&optional n)
7506   "Crosspost the current article to some other group."
7507   (interactive "P")
7508   (gnus-summary-move-article n nil nil 'crosspost))
7509
7510 (defcustom gnus-summary-respool-default-method nil
7511   "Default method for respooling an article.
7512 If nil, use to the current newsgroup method."
7513   :type '(choice (gnus-select-method :value (nnml ""))
7514                  (const nil))
7515   :group 'gnus-summary-mail)
7516
7517 (defun gnus-summary-respool-article (&optional n method)
7518   "Respool the current article.
7519 The article will be squeezed through the mail spooling process again,
7520 which means that it will be put in some mail newsgroup or other
7521 depending on `nnmail-split-methods'.
7522 If N is a positive number, respool the N next articles.
7523 If N is a negative number, respool the N previous articles.
7524 If N is nil and any articles have been marked with the process mark,
7525 respool those articles instead.
7526
7527 Respooling can be done both from mail groups and \"real\" newsgroups.
7528 In the former case, the articles in question will be moved from the
7529 current group into whatever groups they are destined to.  In the
7530 latter case, they will be copied into the relevant groups."
7531   (interactive
7532    (list current-prefix-arg
7533          (let* ((methods (gnus-methods-using 'respool))
7534                 (methname
7535                  (symbol-name (or gnus-summary-respool-default-method
7536                                   (car (gnus-find-method-for-group
7537                                         gnus-newsgroup-name)))))
7538                 (method
7539                  (gnus-completing-read
7540                   methname "What backend do you want to use when respooling?"
7541                   methods nil t nil 'gnus-mail-method-history))
7542                 ms)
7543            (cond
7544             ((zerop (length (setq ms (gnus-servers-using-backend
7545                                       (intern method)))))
7546              (list (intern method) ""))
7547             ((= 1 (length ms))
7548              (car ms))
7549             (t
7550              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7551                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7552                            ms-alist))))))))
7553   (unless method
7554     (error "No method given for respooling"))
7555   (if (assoc (symbol-name
7556               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7557              (gnus-methods-using 'respool))
7558       (gnus-summary-move-article n nil method)
7559     (gnus-summary-copy-article n nil method)))
7560
7561 (defun gnus-summary-import-article (file)
7562   "Import an arbitrary file into a mail newsgroup."
7563   (interactive "fImport file: ")
7564   (let ((group gnus-newsgroup-name)
7565         (now (current-time))
7566         atts lines)
7567     (unless (gnus-check-backend-function 'request-accept-article group)
7568       (error "%s does not support article importing" group))
7569     (or (file-readable-p file)
7570         (not (file-regular-p file))
7571         (error "Can't read %s" file))
7572     (save-excursion
7573       (set-buffer (gnus-get-buffer-create " *import file*"))
7574       (erase-buffer)
7575       (insert-file-contents file)
7576       (goto-char (point-min))
7577       (unless (nnheader-article-p)
7578         ;; This doesn't look like an article, so we fudge some headers.
7579         (setq atts (file-attributes file)
7580               lines (count-lines (point-min) (point-max)))
7581         (insert "From: " (read-string "From: ") "\n"
7582                 "Subject: " (read-string "Subject: ") "\n"
7583                 "Date: " (message-make-date (nth 5 atts))
7584                 "\n"
7585                 "Message-ID: " (message-make-message-id) "\n"
7586                 "Lines: " (int-to-string lines) "\n"
7587                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7588       (gnus-request-accept-article group nil t)
7589       (kill-buffer (current-buffer)))))
7590
7591 (defun gnus-summary-article-posted-p ()
7592   "Say whether the current (mail) article is available from news as well.
7593 This will be the case if the article has both been mailed and posted."
7594   (interactive)
7595   (let ((id (mail-header-references (gnus-summary-article-header)))
7596         (gnus-override-method (car (gnus-refer-article-methods))))
7597     (if (gnus-request-head id "")
7598         (gnus-message 2 "The current message was found on %s"
7599                       gnus-override-method)
7600       (gnus-message 2 "The current message couldn't be found on %s"
7601                     gnus-override-method)
7602       nil)))
7603
7604 (defun gnus-summary-expire-articles (&optional now)
7605   "Expire all articles that are marked as expirable in the current group."
7606   (interactive)
7607   (when (gnus-check-backend-function
7608          'request-expire-articles gnus-newsgroup-name)
7609     ;; This backend supports expiry.
7610     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7611            (expirable (if total
7612                           (progn
7613                             ;; We need to update the info for
7614                             ;; this group for `gnus-list-of-read-articles'
7615                             ;; to give us the right answer.
7616                             (gnus-run-hooks 'gnus-exit-group-hook)
7617                             (gnus-summary-update-info)
7618                             (gnus-list-of-read-articles gnus-newsgroup-name))
7619                         (setq gnus-newsgroup-expirable
7620                               (sort gnus-newsgroup-expirable '<))))
7621            (expiry-wait (if now 'immediate
7622                           (gnus-group-find-parameter
7623                            gnus-newsgroup-name 'expiry-wait)))
7624            es)
7625       (when expirable
7626         ;; There are expirable articles in this group, so we run them
7627         ;; through the expiry process.
7628         (gnus-message 6 "Expiring articles...")
7629         (unless (gnus-check-group gnus-newsgroup-name)
7630           (error "Can't open server for %s" gnus-newsgroup-name))
7631         ;; The list of articles that weren't expired is returned.
7632         (save-excursion
7633           (if expiry-wait
7634               (let ((nnmail-expiry-wait-function nil)
7635                     (nnmail-expiry-wait expiry-wait))
7636                 (setq es (gnus-request-expire-articles
7637                           expirable gnus-newsgroup-name)))
7638             (setq es (gnus-request-expire-articles
7639                       expirable gnus-newsgroup-name))))
7640         (unless total
7641           (setq gnus-newsgroup-expirable es))
7642         ;; We go through the old list of expirable, and mark all
7643         ;; really expired articles as nonexistent.
7644         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7645           (let ((gnus-use-cache nil))
7646             (while expirable
7647               (unless (memq (car expirable) es)
7648                 (when (gnus-data-find (car expirable))
7649                   (gnus-summary-mark-article
7650                    (car expirable) gnus-canceled-mark)))
7651               (setq expirable (cdr expirable)))))
7652         (gnus-message 6 "Expiring articles...done")))))
7653
7654 (defun gnus-summary-expire-articles-now ()
7655   "Expunge all expirable articles in the current group.
7656 This means that *all* articles that are marked as expirable will be
7657 deleted forever, right now."
7658   (interactive)
7659   (or gnus-expert-user
7660       (gnus-yes-or-no-p
7661        "Are you really, really, really sure you want to delete all these messages? ")
7662       (error "Phew!"))
7663   (gnus-summary-expire-articles t))
7664
7665 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7666 (defun gnus-summary-delete-article (&optional n)
7667   "Delete the N next (mail) articles.
7668 This command actually deletes articles.  This is not a marking
7669 command.  The article will disappear forever from your life, never to
7670 return.
7671 If N is negative, delete backwards.
7672 If N is nil and articles have been marked with the process mark,
7673 delete these instead."
7674   (interactive "P")
7675   (unless (gnus-check-backend-function 'request-expire-articles
7676                                        gnus-newsgroup-name)
7677     (error "The current newsgroup does not support article deletion"))
7678   ;; Compute the list of articles to delete.
7679   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7680         not-deleted)
7681     (if (and gnus-novice-user
7682              (not (gnus-yes-or-no-p
7683                    (format "Do you really want to delete %s forever? "
7684                            (if (> (length articles) 1)
7685                                (format "these %s articles" (length articles))
7686                              "this article")))))
7687         ()
7688       ;; Delete the articles.
7689       (setq not-deleted (gnus-request-expire-articles
7690                          articles gnus-newsgroup-name 'force))
7691       (while articles
7692         (gnus-summary-remove-process-mark (car articles))
7693         ;; The backend might not have been able to delete the article
7694         ;; after all.
7695         (unless (memq (car articles) not-deleted)
7696           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7697         (setq articles (cdr articles)))
7698       (when not-deleted
7699         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7700     (gnus-summary-position-point)
7701     (gnus-set-mode-line 'summary)
7702     not-deleted))
7703
7704 (defun gnus-summary-edit-article (&optional force)
7705   "Edit the current article.
7706 This will have permanent effect only in mail groups.
7707 If FORCE is non-nil, allow editing of articles even in read-only
7708 groups."
7709   (interactive "P")
7710   (save-excursion
7711     (set-buffer gnus-summary-buffer)
7712     (let ((mail-parse-charset gnus-newsgroup-charset)
7713           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7714       (gnus-set-global-variables)
7715       (when (and (not force)
7716                  (gnus-group-read-only-p))
7717         (error "The current newsgroup does not support article editing"))
7718       (gnus-summary-show-article t)
7719       (gnus-article-edit-article
7720        'mime-to-mml
7721        `(lambda (no-highlight)
7722           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7723                 (mail-parse-ignored-charsets 
7724                  ',gnus-newsgroup-ignored-charsets))
7725             (mml-to-mime)
7726             (gnus-summary-edit-article-done
7727              ,(or (mail-header-references gnus-current-headers) "")
7728              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7729
7730 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7731
7732 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7733                                                  no-highlight)
7734   "Make edits to the current article permanent."
7735   (interactive)
7736   ;; Replace the article.
7737   (let ((buf (current-buffer)))
7738     (with-temp-buffer
7739       (insert-buffer-substring buf)
7740       (if (and (not read-only)
7741                (not (gnus-request-replace-article
7742                      (cdr gnus-article-current) (car gnus-article-current)
7743                      (current-buffer) t)))
7744           (error "Couldn't replace article")
7745         ;; Update the summary buffer.
7746         (if (and references
7747                  (equal (message-tokenize-header references " ")
7748                         (message-tokenize-header
7749                          (or (message-fetch-field "references") "") " ")))
7750             ;; We only have to update this line.
7751             (save-excursion
7752               (save-restriction
7753                 (message-narrow-to-head)
7754                 (let ((head (buffer-string))
7755                       header)
7756                   (with-temp-buffer
7757                     (insert (format "211 %d Article retrieved.\n"
7758                                     (cdr gnus-article-current)))
7759                     (insert head)
7760                     (insert ".\n")
7761                     (let ((nntp-server-buffer (current-buffer)))
7762                       (setq header (car (gnus-get-newsgroup-headers
7763                                          (save-excursion
7764                                            (set-buffer gnus-summary-buffer)
7765                                            gnus-newsgroup-dependencies)
7766                                          t))))
7767                     (save-excursion
7768                       (set-buffer gnus-summary-buffer)
7769                       (gnus-data-set-header
7770                        (gnus-data-find (cdr gnus-article-current))
7771                        header)
7772                       (gnus-summary-update-article-line
7773                        (cdr gnus-article-current) header))))))
7774           ;; Update threads.
7775           (set-buffer (or buffer gnus-summary-buffer))
7776           (gnus-summary-update-article (cdr gnus-article-current)))
7777         ;; Prettify the article buffer again.
7778         (unless no-highlight
7779           (save-excursion
7780             (set-buffer gnus-article-buffer)
7781             ;;;!!! Fix this -- article should be rehighlighted.
7782             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7783             (set-buffer gnus-original-article-buffer)
7784             (gnus-request-article
7785              (cdr gnus-article-current)
7786              (car gnus-article-current) (current-buffer))))
7787         ;; Prettify the summary buffer line.
7788         (when (gnus-visual-p 'summary-highlight 'highlight)
7789           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7790
7791 (defun gnus-summary-edit-wash (key)
7792   "Perform editing command KEY in the article buffer."
7793   (interactive
7794    (list
7795     (progn
7796       (message "%s" (concat (this-command-keys) "- "))
7797       (read-char))))
7798   (message "")
7799   (gnus-summary-edit-article)
7800   (execute-kbd-macro (concat (this-command-keys) key))
7801   (gnus-article-edit-done))
7802
7803 ;;; Respooling
7804
7805 (defun gnus-summary-respool-query (&optional silent trace)
7806   "Query where the respool algorithm would put this article."
7807   (interactive)
7808   (let (gnus-mark-article-hook)
7809     (gnus-summary-select-article)
7810     (save-excursion
7811       (set-buffer gnus-original-article-buffer)
7812       (save-restriction
7813         (message-narrow-to-head)
7814         (let ((groups (nnmail-article-group 'identity trace)))
7815           (unless silent
7816             (if groups
7817                 (message "This message would go to %s"
7818                          (mapconcat 'car groups ", "))
7819               (message "This message would go to no groups"))
7820             groups))))))
7821
7822 (defun gnus-summary-respool-trace ()
7823   "Trace where the respool algorithm would put this article.
7824 Display a buffer showing all fancy splitting patterns which matched."
7825   (interactive)
7826   (gnus-summary-respool-query nil t))
7827
7828 ;; Summary marking commands.
7829
7830 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7831   "Mark articles which has the same subject as read, and then select the next.
7832 If UNMARK is positive, remove any kind of mark.
7833 If UNMARK is negative, tick articles."
7834   (interactive "P")
7835   (when unmark
7836     (setq unmark (prefix-numeric-value unmark)))
7837   (let ((count
7838          (gnus-summary-mark-same-subject
7839           (gnus-summary-article-subject) unmark)))
7840     ;; Select next unread article.  If auto-select-same mode, should
7841     ;; select the first unread article.
7842     (gnus-summary-next-article t (and gnus-auto-select-same
7843                                       (gnus-summary-article-subject)))
7844     (gnus-message 7 "%d article%s marked as %s"
7845                   count (if (= count 1) " is" "s are")
7846                   (if unmark "unread" "read"))))
7847
7848 (defun gnus-summary-kill-same-subject (&optional unmark)
7849   "Mark articles which has the same subject as read.
7850 If UNMARK is positive, remove any kind of mark.
7851 If UNMARK is negative, tick articles."
7852   (interactive "P")
7853   (when unmark
7854     (setq unmark (prefix-numeric-value unmark)))
7855   (let ((count
7856          (gnus-summary-mark-same-subject
7857           (gnus-summary-article-subject) unmark)))
7858     ;; If marked as read, go to next unread subject.
7859     (when (null unmark)
7860       ;; Go to next unread subject.
7861       (gnus-summary-next-subject 1 t))
7862     (gnus-message 7 "%d articles are marked as %s"
7863                   count (if unmark "unread" "read"))))
7864
7865 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7866   "Mark articles with same SUBJECT as read, and return marked number.
7867 If optional argument UNMARK is positive, remove any kinds of marks.
7868 If optional argument UNMARK is negative, mark articles as unread instead."
7869   (let ((count 1))
7870     (save-excursion
7871       (cond
7872        ((null unmark)                   ; Mark as read.
7873         (while (and
7874                 (progn
7875                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7876                   (gnus-summary-show-thread) t)
7877                 (gnus-summary-find-subject subject))
7878           (setq count (1+ count))))
7879        ((> unmark 0)                    ; Tick.
7880         (while (and
7881                 (progn
7882                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7883                   (gnus-summary-show-thread) t)
7884                 (gnus-summary-find-subject subject))
7885           (setq count (1+ count))))
7886        (t                               ; Mark as unread.
7887         (while (and
7888                 (progn
7889                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7890                   (gnus-summary-show-thread) t)
7891                 (gnus-summary-find-subject subject))
7892           (setq count (1+ count)))))
7893       (gnus-set-mode-line 'summary)
7894       ;; Return the number of marked articles.
7895       count)))
7896
7897 (defun gnus-summary-mark-as-processable (n &optional unmark)
7898   "Set the process mark on the next N articles.
7899 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7900 the process mark instead.  The difference between N and the actual
7901 number of articles marked is returned."
7902   (interactive "p")
7903   (let ((backward (< n 0))
7904         (n (abs n)))
7905     (while (and
7906             (> n 0)
7907             (if unmark
7908                 (gnus-summary-remove-process-mark
7909                  (gnus-summary-article-number))
7910               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7911             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7912       (setq n (1- n)))
7913     (when (/= 0 n)
7914       (gnus-message 7 "No more articles"))
7915     (gnus-summary-recenter)
7916     (gnus-summary-position-point)
7917     n))
7918
7919 (defun gnus-summary-unmark-as-processable (n)
7920   "Remove the process mark from the next N articles.
7921 If N is negative, unmark backward instead.  The difference between N and
7922 the actual number of articles unmarked is returned."
7923   (interactive "p")
7924   (gnus-summary-mark-as-processable n t))
7925
7926 (defun gnus-summary-unmark-all-processable ()
7927   "Remove the process mark from all articles."
7928   (interactive)
7929   (save-excursion
7930     (while gnus-newsgroup-processable
7931       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7932   (gnus-summary-position-point))
7933
7934 (defun gnus-summary-mark-as-expirable (n)
7935   "Mark N articles forward as expirable.
7936 If N is negative, mark backward instead.  The difference between N and
7937 the actual number of articles marked is returned."
7938   (interactive "p")
7939   (gnus-summary-mark-forward n gnus-expirable-mark))
7940
7941 (defun gnus-summary-mark-article-as-replied (article)
7942   "Mark ARTICLE replied and update the summary line."
7943   (push article gnus-newsgroup-replied)
7944   (let ((buffer-read-only nil))
7945     (when (gnus-summary-goto-subject article nil t)
7946       (gnus-summary-update-secondary-mark article))))
7947
7948 (defun gnus-summary-set-bookmark (article)
7949   "Set a bookmark in current article."
7950   (interactive (list (gnus-summary-article-number)))
7951   (when (or (not (get-buffer gnus-article-buffer))
7952             (not gnus-current-article)
7953             (not gnus-article-current)
7954             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7955     (error "No current article selected"))
7956   ;; Remove old bookmark, if one exists.
7957   (let ((old (assq article gnus-newsgroup-bookmarks)))
7958     (when old
7959       (setq gnus-newsgroup-bookmarks
7960             (delq old gnus-newsgroup-bookmarks))))
7961   ;; Set the new bookmark, which is on the form
7962   ;; (article-number . line-number-in-body).
7963   (push
7964    (cons article
7965          (save-excursion
7966            (set-buffer gnus-article-buffer)
7967            (count-lines
7968             (min (point)
7969                  (save-excursion
7970                    (goto-char (point-min))
7971                    (search-forward "\n\n" nil t)
7972                    (point)))
7973             (point))))
7974    gnus-newsgroup-bookmarks)
7975   (gnus-message 6 "A bookmark has been added to the current article."))
7976
7977 (defun gnus-summary-remove-bookmark (article)
7978   "Remove the bookmark from the current article."
7979   (interactive (list (gnus-summary-article-number)))
7980   ;; Remove old bookmark, if one exists.
7981   (let ((old (assq article gnus-newsgroup-bookmarks)))
7982     (if old
7983         (progn
7984           (setq gnus-newsgroup-bookmarks
7985                 (delq old gnus-newsgroup-bookmarks))
7986           (gnus-message 6 "Removed bookmark."))
7987       (gnus-message 6 "No bookmark in current article."))))
7988
7989 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7990 (defun gnus-summary-mark-as-dormant (n)
7991   "Mark N articles forward as dormant.
7992 If N is negative, mark backward instead.  The difference between N and
7993 the actual number of articles marked is returned."
7994   (interactive "p")
7995   (gnus-summary-mark-forward n gnus-dormant-mark))
7996
7997 (defun gnus-summary-set-process-mark (article)
7998   "Set the process mark on ARTICLE and update the summary line."
7999   (setq gnus-newsgroup-processable
8000         (cons article
8001               (delq article gnus-newsgroup-processable)))
8002   (when (gnus-summary-goto-subject article)
8003     (gnus-summary-show-thread)
8004     (gnus-summary-goto-subject article)
8005     (gnus-summary-update-secondary-mark article)))
8006
8007 (defun gnus-summary-remove-process-mark (article)
8008   "Remove the process mark from ARTICLE and update the summary line."
8009   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8010   (when (gnus-summary-goto-subject article)
8011     (gnus-summary-show-thread)
8012     (gnus-summary-goto-subject article)
8013     (gnus-summary-update-secondary-mark article)))
8014
8015 (defun gnus-summary-set-saved-mark (article)
8016   "Set the process mark on ARTICLE and update the summary line."
8017   (push article gnus-newsgroup-saved)
8018   (when (gnus-summary-goto-subject article)
8019     (gnus-summary-update-secondary-mark article)))
8020
8021 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8022   "Mark N articles as read forwards.
8023 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8024 The difference between N and the actual number of articles marked is
8025 returned.
8026 Iff NO-EXPIRE, auto-expiry will be inhibited."
8027   (interactive "p")
8028   (gnus-summary-show-thread)
8029   (let ((backward (< n 0))
8030         (gnus-summary-goto-unread
8031          (and gnus-summary-goto-unread
8032               (not (eq gnus-summary-goto-unread 'never))
8033               (not (memq mark (list gnus-unread-mark
8034                                     gnus-ticked-mark gnus-dormant-mark)))))
8035         (n (abs n))
8036         (mark (or mark gnus-del-mark)))
8037     (while (and (> n 0)
8038                 (gnus-summary-mark-article nil mark no-expire)
8039                 (zerop (gnus-summary-next-subject
8040                         (if backward -1 1)
8041                         (and gnus-summary-goto-unread
8042                              (not (eq gnus-summary-goto-unread 'never)))
8043                         t)))
8044       (setq n (1- n)))
8045     (when (/= 0 n)
8046       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8047     (gnus-summary-recenter)
8048     (gnus-summary-position-point)
8049     (gnus-set-mode-line 'summary)
8050     n))
8051
8052 (defun gnus-summary-mark-article-as-read (mark)
8053   "Mark the current article quickly as read with MARK."
8054   (let ((article (gnus-summary-article-number)))
8055     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8056     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8057     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8058     (push (cons article mark) gnus-newsgroup-reads)
8059     ;; Possibly remove from cache, if that is used.
8060     (when gnus-use-cache
8061       (gnus-cache-enter-remove-article article))
8062     ;; Allow the backend to change the mark.
8063     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8064     ;; Check for auto-expiry.
8065     (when (and gnus-newsgroup-auto-expire
8066                (memq mark gnus-auto-expirable-marks))
8067       (setq mark gnus-expirable-mark)
8068       ;; Let the backend know about the mark change.
8069       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8070       (push article gnus-newsgroup-expirable))
8071     ;; Set the mark in the buffer.
8072     (gnus-summary-update-mark mark 'unread)
8073     t))
8074
8075 (defun gnus-summary-mark-article-as-unread (mark)
8076   "Mark the current article quickly as unread with MARK."
8077   (let* ((article (gnus-summary-article-number))
8078          (old-mark (gnus-summary-article-mark article)))
8079     ;; Allow the backend to change the mark.
8080     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8081     (if (eq mark old-mark)
8082         t
8083       (if (<= article 0)
8084           (progn
8085             (gnus-error 1 "Can't mark negative article numbers")
8086             nil)
8087         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8088         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8089         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8090         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8091         (cond ((= mark gnus-ticked-mark)
8092                (push article gnus-newsgroup-marked))
8093               ((= mark gnus-dormant-mark)
8094                (push article gnus-newsgroup-dormant))
8095               (t
8096                (push article gnus-newsgroup-unreads)))
8097         (gnus-pull article gnus-newsgroup-reads)
8098
8099         ;; See whether the article is to be put in the cache.
8100         (and gnus-use-cache
8101              (vectorp (gnus-summary-article-header article))
8102              (save-excursion
8103                (gnus-cache-possibly-enter-article
8104                 gnus-newsgroup-name article
8105                 (= mark gnus-ticked-mark)
8106                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8107
8108         ;; Fix the mark.
8109         (gnus-summary-update-mark mark 'unread)
8110         t))))
8111
8112 (defun gnus-summary-mark-article (&optional article mark no-expire)
8113   "Mark ARTICLE with MARK.  MARK can be any character.
8114 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8115 `??' (dormant) and `?E' (expirable).
8116 If MARK is nil, then the default character `?r' is used.
8117 If ARTICLE is nil, then the article on the current line will be
8118 marked.
8119 Iff NO-EXPIRE, auto-expiry will be inhibited."
8120   ;; The mark might be a string.
8121   (when (stringp mark)
8122     (setq mark (aref mark 0)))
8123   ;; If no mark is given, then we check auto-expiring.
8124   (when (null mark)
8125     (setq mark gnus-del-mark))
8126   (when (and (not no-expire)
8127              gnus-newsgroup-auto-expire
8128              (memq mark gnus-auto-expirable-marks))
8129     (setq mark gnus-expirable-mark))
8130   (let ((article (or article (gnus-summary-article-number)))
8131         (old-mark (gnus-summary-article-mark article)))
8132     ;; Allow the backend to change the mark.
8133     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8134     (if (eq mark old-mark)
8135         t
8136       (unless article
8137         (error "No article on current line"))
8138       (if (not (if (or (= mark gnus-unread-mark)
8139                        (= mark gnus-ticked-mark)
8140                        (= mark gnus-dormant-mark))
8141                    (gnus-mark-article-as-unread article mark)
8142                  (gnus-mark-article-as-read article mark)))
8143           t
8144         ;; See whether the article is to be put in the cache.
8145         (and gnus-use-cache
8146              (not (= mark gnus-canceled-mark))
8147              (vectorp (gnus-summary-article-header article))
8148              (save-excursion
8149                (gnus-cache-possibly-enter-article
8150                 gnus-newsgroup-name article
8151                 (= mark gnus-ticked-mark)
8152                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8153
8154         (when (gnus-summary-goto-subject article nil t)
8155           (let ((buffer-read-only nil))
8156             (gnus-summary-show-thread)
8157             ;; Fix the mark.
8158             (gnus-summary-update-mark mark 'unread)
8159             t))))))
8160
8161 (defun gnus-summary-update-secondary-mark (article)
8162   "Update the secondary (read, process, cache) mark."
8163   (gnus-summary-update-mark
8164    (cond ((memq article gnus-newsgroup-processable)
8165           gnus-process-mark)
8166          ((memq article gnus-newsgroup-cached)
8167           gnus-cached-mark)
8168          ((memq article gnus-newsgroup-replied)
8169           gnus-replied-mark)
8170          ((memq article gnus-newsgroup-saved)
8171           gnus-saved-mark)
8172          (t gnus-unread-mark))
8173    'replied)
8174   (when (gnus-visual-p 'summary-highlight 'highlight)
8175     (gnus-run-hooks 'gnus-summary-update-hook))
8176   t)
8177
8178 (defun gnus-summary-update-mark (mark type)
8179   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8180         (buffer-read-only nil))
8181     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8182     (when forward
8183       (when (looking-at "\r")
8184         (incf forward))
8185       (when (<= (+ forward (point)) (point-max))
8186         ;; Go to the right position on the line.
8187         (goto-char (+ forward (point)))
8188         ;; Replace the old mark with the new mark.
8189         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8190         ;; Optionally update the marks by some user rule.
8191         (when (eq type 'unread)
8192           (gnus-data-set-mark
8193            (gnus-data-find (gnus-summary-article-number)) mark)
8194           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8195
8196 (defun gnus-mark-article-as-read (article &optional mark)
8197   "Enter ARTICLE in the pertinent lists and remove it from others."
8198   ;; Make the article expirable.
8199   (let ((mark (or mark gnus-del-mark)))
8200     (if (= mark gnus-expirable-mark)
8201         (push article gnus-newsgroup-expirable)
8202       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8203     ;; Remove from unread and marked lists.
8204     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8205     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8206     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8207     (push (cons article mark) gnus-newsgroup-reads)
8208     ;; Possibly remove from cache, if that is used.
8209     (when gnus-use-cache
8210       (gnus-cache-enter-remove-article article))
8211     t))
8212
8213 (defun gnus-mark-article-as-unread (article &optional mark)
8214   "Enter ARTICLE in the pertinent lists and remove it from others."
8215   (let ((mark (or mark gnus-ticked-mark)))
8216     (if (<= article 0)
8217         (progn
8218           (gnus-error 1 "Can't mark negative article numbers")
8219           nil)
8220       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8221             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8222             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8223             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8224
8225       ;; Unsuppress duplicates?
8226       (when gnus-suppress-duplicates
8227         (gnus-dup-unsuppress-article article))
8228
8229       (cond ((= mark gnus-ticked-mark)
8230              (push article gnus-newsgroup-marked))
8231             ((= mark gnus-dormant-mark)
8232              (push article gnus-newsgroup-dormant))
8233             (t
8234              (push article gnus-newsgroup-unreads)))
8235       (gnus-pull article gnus-newsgroup-reads)
8236       t)))
8237
8238 (defalias 'gnus-summary-mark-as-unread-forward
8239   'gnus-summary-tick-article-forward)
8240 (make-obsolete 'gnus-summary-mark-as-unread-forward
8241                'gnus-summary-tick-article-forward)
8242 (defun gnus-summary-tick-article-forward (n)
8243   "Tick N articles forwards.
8244 If N is negative, tick backwards instead.
8245 The difference between N and the number of articles ticked is returned."
8246   (interactive "p")
8247   (gnus-summary-mark-forward n gnus-ticked-mark))
8248
8249 (defalias 'gnus-summary-mark-as-unread-backward
8250   'gnus-summary-tick-article-backward)
8251 (make-obsolete 'gnus-summary-mark-as-unread-backward
8252                'gnus-summary-tick-article-backward)
8253 (defun gnus-summary-tick-article-backward (n)
8254   "Tick N articles backwards.
8255 The difference between N and the number of articles ticked is returned."
8256   (interactive "p")
8257   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8258
8259 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8260 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8261 (defun gnus-summary-tick-article (&optional article clear-mark)
8262   "Mark current article as unread.
8263 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8264 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8265   (interactive)
8266   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8267                                        gnus-ticked-mark)))
8268
8269 (defun gnus-summary-mark-as-read-forward (n)
8270   "Mark N articles as read forwards.
8271 If N is negative, mark backwards instead.
8272 The difference between N and the actual number of articles marked is
8273 returned."
8274   (interactive "p")
8275   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8276
8277 (defun gnus-summary-mark-as-read-backward (n)
8278   "Mark the N articles as read backwards.
8279 The difference between N and the actual number of articles marked is
8280 returned."
8281   (interactive "p")
8282   (gnus-summary-mark-forward
8283    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8284
8285 (defun gnus-summary-mark-as-read (&optional article mark)
8286   "Mark current article as read.
8287 ARTICLE specifies the article to be marked as read.
8288 MARK specifies a string to be inserted at the beginning of the line."
8289   (gnus-summary-mark-article article mark))
8290
8291 (defun gnus-summary-clear-mark-forward (n)
8292   "Clear marks from N articles forward.
8293 If N is negative, clear backward instead.
8294 The difference between N and the number of marks cleared is returned."
8295   (interactive "p")
8296   (gnus-summary-mark-forward n gnus-unread-mark))
8297
8298 (defun gnus-summary-clear-mark-backward (n)
8299   "Clear marks from N articles backward.
8300 The difference between N and the number of marks cleared is returned."
8301   (interactive "p")
8302   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8303
8304 (defun gnus-summary-mark-unread-as-read ()
8305   "Intended to be used by `gnus-summary-mark-article-hook'."
8306   (when (memq gnus-current-article gnus-newsgroup-unreads)
8307     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8308
8309 (defun gnus-summary-mark-read-and-unread-as-read ()
8310   "Intended to be used by `gnus-summary-mark-article-hook'."
8311   (let ((mark (gnus-summary-article-mark)))
8312     (when (or (gnus-unread-mark-p mark)
8313               (gnus-read-mark-p mark))
8314       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8315
8316 (defun gnus-summary-mark-region-as-read (point mark all)
8317   "Mark all unread articles between point and mark as read.
8318 If given a prefix, mark all articles between point and mark as read,
8319 even ticked and dormant ones."
8320   (interactive "r\nP")
8321   (save-excursion
8322     (let (article)
8323       (goto-char point)
8324       (beginning-of-line)
8325       (while (and
8326               (< (point) mark)
8327               (progn
8328                 (when (or all
8329                           (memq (setq article (gnus-summary-article-number))
8330                                 gnus-newsgroup-unreads))
8331                   (gnus-summary-mark-article article gnus-del-mark))
8332                 t)
8333               (gnus-summary-find-next))))))
8334
8335 (defun gnus-summary-mark-below (score mark)
8336   "Mark articles with score less than SCORE with MARK."
8337   (interactive "P\ncMark: ")
8338   (setq score (if score
8339                   (prefix-numeric-value score)
8340                 (or gnus-summary-default-score 0)))
8341   (save-excursion
8342     (set-buffer gnus-summary-buffer)
8343     (goto-char (point-min))
8344     (while
8345         (progn
8346           (and (< (gnus-summary-article-score) score)
8347                (gnus-summary-mark-article nil mark))
8348           (gnus-summary-find-next)))))
8349
8350 (defun gnus-summary-kill-below (&optional score)
8351   "Mark articles with score below SCORE as read."
8352   (interactive "P")
8353   (gnus-summary-mark-below score gnus-killed-mark))
8354
8355 (defun gnus-summary-clear-above (&optional score)
8356   "Clear all marks from articles with score above SCORE."
8357   (interactive "P")
8358   (gnus-summary-mark-above score gnus-unread-mark))
8359
8360 (defun gnus-summary-tick-above (&optional score)
8361   "Tick all articles with score above SCORE."
8362   (interactive "P")
8363   (gnus-summary-mark-above score gnus-ticked-mark))
8364
8365 (defun gnus-summary-mark-above (score mark)
8366   "Mark articles with score over SCORE with MARK."
8367   (interactive "P\ncMark: ")
8368   (setq score (if score
8369                   (prefix-numeric-value score)
8370                 (or gnus-summary-default-score 0)))
8371   (save-excursion
8372     (set-buffer gnus-summary-buffer)
8373     (goto-char (point-min))
8374     (while (and (progn
8375                   (when (> (gnus-summary-article-score) score)
8376                     (gnus-summary-mark-article nil mark))
8377                   t)
8378                 (gnus-summary-find-next)))))
8379
8380 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8381 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8382 (defun gnus-summary-limit-include-expunged (&optional no-error)
8383   "Display all the hidden articles that were expunged for low scores."
8384   (interactive)
8385   (let ((buffer-read-only nil))
8386     (let ((scored gnus-newsgroup-scored)
8387           headers h)
8388       (while scored
8389         (unless (gnus-summary-goto-subject (caar scored))
8390           (and (setq h (gnus-summary-article-header (caar scored)))
8391                (< (cdar scored) gnus-summary-expunge-below)
8392                (push h headers)))
8393         (setq scored (cdr scored)))
8394       (if (not headers)
8395           (when (not no-error)
8396             (error "No expunged articles hidden"))
8397         (goto-char (point-min))
8398         (gnus-summary-prepare-unthreaded (nreverse headers))
8399         (goto-char (point-min))
8400         (gnus-summary-position-point)
8401         t))))
8402
8403 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8404   "Mark all unread articles in this newsgroup as read.
8405 If prefix argument ALL is non-nil, ticked and dormant articles will
8406 also be marked as read.
8407 If QUIETLY is non-nil, no questions will be asked.
8408 If TO-HERE is non-nil, it should be a point in the buffer.  All
8409 articles before this point will be marked as read.
8410 Note that this function will only catch up the unread article
8411 in the current summary buffer limitation.
8412 The number of articles marked as read is returned."
8413   (interactive "P")
8414   (prog1
8415       (save-excursion
8416         (when (or quietly
8417                   (not gnus-interactive-catchup) ;Without confirmation?
8418                   gnus-expert-user
8419                   (gnus-y-or-n-p
8420                    (if all
8421                        "Mark absolutely all articles as read? "
8422                      "Mark all unread articles as read? ")))
8423           (if (and not-mark
8424                    (not gnus-newsgroup-adaptive)
8425                    (not gnus-newsgroup-auto-expire)
8426                    (not gnus-suppress-duplicates)
8427                    (or (not gnus-use-cache)
8428                        (eq gnus-use-cache 'passive)))
8429               (progn
8430                 (when all
8431                   (setq gnus-newsgroup-marked nil
8432                         gnus-newsgroup-dormant nil))
8433                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8434             ;; We actually mark all articles as canceled, which we
8435             ;; have to do when using auto-expiry or adaptive scoring.
8436             (gnus-summary-show-all-threads)
8437             (when (gnus-summary-first-subject (not all) t)
8438               (while (and
8439                       (if to-here (< (point) to-here) t)
8440                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8441                       (gnus-summary-find-next (not all) nil nil t))))
8442             (gnus-set-mode-line 'summary))
8443           t))
8444     (gnus-summary-position-point)))
8445
8446 (defun gnus-summary-catchup-to-here (&optional all)
8447   "Mark all unticked articles before the current one as read.
8448 If ALL is non-nil, also mark ticked and dormant articles as read."
8449   (interactive "P")
8450   (save-excursion
8451     (gnus-save-hidden-threads
8452       (let ((beg (point)))
8453         ;; We check that there are unread articles.
8454         (when (or all (gnus-summary-find-prev))
8455           (gnus-summary-catchup all t beg)))))
8456   (gnus-summary-position-point))
8457
8458 (defun gnus-summary-catchup-all (&optional quietly)
8459   "Mark all articles in this newsgroup as read."
8460   (interactive "P")
8461   (gnus-summary-catchup t quietly))
8462
8463 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8464   "Mark all unread articles in this group as read, then exit.
8465 If prefix argument ALL is non-nil, all articles are marked as read."
8466   (interactive "P")
8467   (when (gnus-summary-catchup all quietly nil 'fast)
8468     ;; Select next newsgroup or exit.
8469     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8470              (eq gnus-auto-select-next 'quietly))
8471         (gnus-summary-next-group nil)
8472       (gnus-summary-exit))))
8473
8474 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8475   "Mark all articles in this newsgroup as read, and then exit."
8476   (interactive "P")
8477   (gnus-summary-catchup-and-exit t quietly))
8478
8479 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8480   "Mark all articles in this group as read and select the next group.
8481 If given a prefix, mark all articles, unread as well as ticked, as
8482 read."
8483   (interactive "P")
8484   (save-excursion
8485     (gnus-summary-catchup all))
8486   (gnus-summary-next-group))
8487
8488 ;; Thread-based commands.
8489
8490 (defun gnus-summary-articles-in-thread (&optional article)
8491   "Return a list of all articles in the current thread.
8492 If ARTICLE is non-nil, return all articles in the thread that starts
8493 with that article."
8494   (let* ((article (or article (gnus-summary-article-number)))
8495          (data (gnus-data-find-list article))
8496          (top-level (gnus-data-level (car data)))
8497          (top-subject
8498           (cond ((null gnus-thread-operation-ignore-subject)
8499                  (gnus-simplify-subject-re
8500                   (mail-header-subject (gnus-data-header (car data)))))
8501                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8502                  (gnus-simplify-subject-fuzzy
8503                   (mail-header-subject (gnus-data-header (car data)))))
8504                 (t nil)))
8505          (end-point (save-excursion
8506                       (if (gnus-summary-go-to-next-thread)
8507                           (point) (point-max))))
8508          articles)
8509     (while (and data
8510                 (< (gnus-data-pos (car data)) end-point))
8511       (when (or (not top-subject)
8512                 (string= top-subject
8513                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8514                              (gnus-simplify-subject-fuzzy
8515                               (mail-header-subject
8516                                (gnus-data-header (car data))))
8517                            (gnus-simplify-subject-re
8518                             (mail-header-subject
8519                              (gnus-data-header (car data)))))))
8520         (push (gnus-data-number (car data)) articles))
8521       (unless (and (setq data (cdr data))
8522                    (> (gnus-data-level (car data)) top-level))
8523         (setq data nil)))
8524     ;; Return the list of articles.
8525     (nreverse articles)))
8526
8527 (defun gnus-summary-rethread-current ()
8528   "Rethread the thread the current article is part of."
8529   (interactive)
8530   (let* ((gnus-show-threads t)
8531          (article (gnus-summary-article-number))
8532          (id (mail-header-id (gnus-summary-article-header)))
8533          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8534     (unless id
8535       (error "No article on the current line"))
8536     (gnus-rebuild-thread id)
8537     (gnus-summary-goto-subject article)))
8538
8539 (defun gnus-summary-reparent-thread ()
8540   "Make the current article child of the marked (or previous) article.
8541
8542 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8543 is non-nil or the Subject: of both articles are the same."
8544   (interactive)
8545   (unless (not (gnus-group-read-only-p))
8546     (error "The current newsgroup does not support article editing"))
8547   (unless (<= (length gnus-newsgroup-processable) 1)
8548     (error "No more than one article may be marked"))
8549   (save-window-excursion
8550     (let ((gnus-article-buffer " *reparent*")
8551           (current-article (gnus-summary-article-number))
8552           ;; First grab the marked article, otherwise one line up.
8553           (parent-article (if (not (null gnus-newsgroup-processable))
8554                               (car gnus-newsgroup-processable)
8555                             (save-excursion
8556                               (if (eq (forward-line -1) 0)
8557                                   (gnus-summary-article-number)
8558                                 (error "Beginning of summary buffer"))))))
8559       (unless (not (eq current-article parent-article))
8560         (error "An article may not be self-referential"))
8561       (let ((message-id (mail-header-id
8562                          (gnus-summary-article-header parent-article))))
8563         (unless (and message-id (not (equal message-id "")))
8564           (error "No message-id in desired parent"))
8565         (gnus-with-article current-article
8566           (goto-char (point-min))
8567           (if (re-search-forward "^References: " nil t)
8568               (progn
8569                 (re-search-forward "^[^ \t]" nil t)
8570                 (forward-line -1)
8571                 (end-of-line)
8572                 (insert " " message-id))
8573             (insert "References: " message-id "\n")))
8574         (set-buffer gnus-summary-buffer)
8575         (gnus-summary-unmark-all-processable)
8576         (gnus-summary-update-article current-article)
8577         (gnus-summary-rethread-current)
8578         (gnus-message 3 "Article %d is now the child of article %d"
8579                       current-article parent-article)))))
8580
8581 (defun gnus-summary-toggle-threads (&optional arg)
8582   "Toggle showing conversation threads.
8583 If ARG is positive number, turn showing conversation threads on."
8584   (interactive "P")
8585   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8586     (setq gnus-show-threads
8587           (if (null arg) (not gnus-show-threads)
8588             (> (prefix-numeric-value arg) 0)))
8589     (gnus-summary-prepare)
8590     (gnus-summary-goto-subject current)
8591     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8592     (gnus-summary-position-point)))
8593
8594 (defun gnus-summary-show-all-threads ()
8595   "Show all threads."
8596   (interactive)
8597   (save-excursion
8598     (let ((buffer-read-only nil))
8599       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8600   (gnus-summary-position-point))
8601
8602 (defun gnus-summary-show-thread ()
8603   "Show thread subtrees.
8604 Returns nil if no thread was there to be shown."
8605   (interactive)
8606   (let ((buffer-read-only nil)
8607         (orig (point))
8608         ;; first goto end then to beg, to have point at beg after let
8609         (end (progn (end-of-line) (point)))
8610         (beg (progn (beginning-of-line) (point))))
8611     (prog1
8612         ;; Any hidden lines here?
8613         (search-forward "\r" end t)
8614       (subst-char-in-region beg end ?\^M ?\n t)
8615       (goto-char orig)
8616       (gnus-summary-position-point))))
8617
8618 (defun gnus-summary-hide-all-threads ()
8619   "Hide all thread subtrees."
8620   (interactive)
8621   (save-excursion
8622     (goto-char (point-min))
8623     (gnus-summary-hide-thread)
8624     (while (zerop (gnus-summary-next-thread 1 t))
8625       (gnus-summary-hide-thread)))
8626   (gnus-summary-position-point))
8627
8628 (defun gnus-summary-hide-thread ()
8629   "Hide thread subtrees.
8630 Returns nil if no threads were there to be hidden."
8631   (interactive)
8632   (let ((buffer-read-only nil)
8633         (start (point))
8634         (article (gnus-summary-article-number)))
8635     (goto-char start)
8636     ;; Go forward until either the buffer ends or the subthread
8637     ;; ends.
8638     (when (and (not (eobp))
8639                (or (zerop (gnus-summary-next-thread 1 t))
8640                    (goto-char (point-max))))
8641       (prog1
8642           (if (and (> (point) start)
8643                    (search-backward "\n" start t))
8644               (progn
8645                 (subst-char-in-region start (point) ?\n ?\^M)
8646                 (gnus-summary-goto-subject article))
8647             (goto-char start)
8648             nil)))))
8649
8650 (defun gnus-summary-go-to-next-thread (&optional previous)
8651   "Go to the same level (or less) next thread.
8652 If PREVIOUS is non-nil, go to previous thread instead.
8653 Return the article number moved to, or nil if moving was impossible."
8654   (let ((level (gnus-summary-thread-level))
8655         (way (if previous -1 1))
8656         (beg (point)))
8657     (forward-line way)
8658     (while (and (not (eobp))
8659                 (< level (gnus-summary-thread-level)))
8660       (forward-line way))
8661     (if (eobp)
8662         (progn
8663           (goto-char beg)
8664           nil)
8665       (setq beg (point))
8666       (prog1
8667           (gnus-summary-article-number)
8668         (goto-char beg)))))
8669
8670 (defun gnus-summary-next-thread (n &optional silent)
8671   "Go to the same level next N'th thread.
8672 If N is negative, search backward instead.
8673 Returns the difference between N and the number of skips actually
8674 done.
8675
8676 If SILENT, don't output messages."
8677   (interactive "p")
8678   (let ((backward (< n 0))
8679         (n (abs n)))
8680     (while (and (> n 0)
8681                 (gnus-summary-go-to-next-thread backward))
8682       (decf n))
8683     (unless silent
8684       (gnus-summary-position-point))
8685     (when (and (not silent) (/= 0 n))
8686       (gnus-message 7 "No more threads"))
8687     n))
8688
8689 (defun gnus-summary-prev-thread (n)
8690   "Go to the same level previous N'th thread.
8691 Returns the difference between N and the number of skips actually
8692 done."
8693   (interactive "p")
8694   (gnus-summary-next-thread (- n)))
8695
8696 (defun gnus-summary-go-down-thread ()
8697   "Go down one level in the current thread."
8698   (let ((children (gnus-summary-article-children)))
8699     (when children
8700       (gnus-summary-goto-subject (car children)))))
8701
8702 (defun gnus-summary-go-up-thread ()
8703   "Go up one level in the current thread."
8704   (let ((parent (gnus-summary-article-parent)))
8705     (when parent
8706       (gnus-summary-goto-subject parent))))
8707
8708 (defun gnus-summary-down-thread (n)
8709   "Go down thread N steps.
8710 If N is negative, go up instead.
8711 Returns the difference between N and how many steps down that were
8712 taken."
8713   (interactive "p")
8714   (let ((up (< n 0))
8715         (n (abs n)))
8716     (while (and (> n 0)
8717                 (if up (gnus-summary-go-up-thread)
8718                   (gnus-summary-go-down-thread)))
8719       (setq n (1- n)))
8720     (gnus-summary-position-point)
8721     (when (/= 0 n)
8722       (gnus-message 7 "Can't go further"))
8723     n))
8724
8725 (defun gnus-summary-up-thread (n)
8726   "Go up thread N steps.
8727 If N is negative, go up instead.
8728 Returns the difference between N and how many steps down that were
8729 taken."
8730   (interactive "p")
8731   (gnus-summary-down-thread (- n)))
8732
8733 (defun gnus-summary-top-thread ()
8734   "Go to the top of the thread."
8735   (interactive)
8736   (while (gnus-summary-go-up-thread))
8737   (gnus-summary-article-number))
8738
8739 (defun gnus-summary-kill-thread (&optional unmark)
8740   "Mark articles under current thread as read.
8741 If the prefix argument is positive, remove any kinds of marks.
8742 If the prefix argument is negative, tick articles instead."
8743   (interactive "P")
8744   (when unmark
8745     (setq unmark (prefix-numeric-value unmark)))
8746   (let ((articles (gnus-summary-articles-in-thread)))
8747     (save-excursion
8748       ;; Expand the thread.
8749       (gnus-summary-show-thread)
8750       ;; Mark all the articles.
8751       (while articles
8752         (gnus-summary-goto-subject (car articles))
8753         (cond ((null unmark)
8754                (gnus-summary-mark-article-as-read gnus-killed-mark))
8755               ((> unmark 0)
8756                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8757               (t
8758                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8759         (setq articles (cdr articles))))
8760     ;; Hide killed subtrees.
8761     (and (null unmark)
8762          gnus-thread-hide-killed
8763          (gnus-summary-hide-thread))
8764     ;; If marked as read, go to next unread subject.
8765     (when (null unmark)
8766       ;; Go to next unread subject.
8767       (gnus-summary-next-subject 1 t)))
8768   (gnus-set-mode-line 'summary))
8769
8770 ;; Summary sorting commands
8771
8772 (defun gnus-summary-sort-by-number (&optional reverse)
8773   "Sort the summary buffer by article number.
8774 Argument REVERSE means reverse order."
8775   (interactive "P")
8776   (gnus-summary-sort 'number reverse))
8777
8778 (defun gnus-summary-sort-by-author (&optional reverse)
8779   "Sort the summary buffer by author name alphabetically.
8780 If case-fold-search is non-nil, case of letters is ignored.
8781 Argument REVERSE means reverse order."
8782   (interactive "P")
8783   (gnus-summary-sort 'author reverse))
8784
8785 (defun gnus-summary-sort-by-subject (&optional reverse)
8786   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8787 If case-fold-search is non-nil, case of letters is ignored.
8788 Argument REVERSE means reverse order."
8789   (interactive "P")
8790   (gnus-summary-sort 'subject reverse))
8791
8792 (defun gnus-summary-sort-by-date (&optional reverse)
8793   "Sort the summary buffer by date.
8794 Argument REVERSE means reverse order."
8795   (interactive "P")
8796   (gnus-summary-sort 'date reverse))
8797
8798 (defun gnus-summary-sort-by-score (&optional reverse)
8799   "Sort the summary buffer by score.
8800 Argument REVERSE means reverse order."
8801   (interactive "P")
8802   (gnus-summary-sort 'score reverse))
8803
8804 (defun gnus-summary-sort-by-lines (&optional reverse)
8805   "Sort the summary buffer by the number of lines.
8806 Argument REVERSE means reverse order."
8807   (interactive "P")
8808   (gnus-summary-sort 'lines reverse))
8809
8810 (defun gnus-summary-sort-by-chars (&optional reverse)
8811   "Sort the summary buffer by article length.
8812 Argument REVERSE means reverse order."
8813   (interactive "P")
8814   (gnus-summary-sort 'chars reverse))   
8815
8816 (defun gnus-summary-sort (predicate reverse)
8817   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8818   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8819          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8820          (gnus-thread-sort-functions
8821           (if (not reverse)
8822               thread
8823             `(lambda (t1 t2)
8824                (,thread t2 t1))))
8825          (gnus-article-sort-functions
8826           (if (not reverse)
8827               article
8828             `(lambda (t1 t2)
8829                (,article t2 t1))))
8830          (buffer-read-only)
8831          (gnus-summary-prepare-hook nil))
8832     ;; We do the sorting by regenerating the threads.
8833     (gnus-summary-prepare)
8834     ;; Hide subthreads if needed.
8835     (when (and gnus-show-threads gnus-thread-hide-subtree)
8836       (gnus-summary-hide-all-threads))))
8837
8838 ;; Summary saving commands.
8839
8840 (defun gnus-summary-save-article (&optional n not-saved)
8841   "Save the current article using the default saver function.
8842 If N is a positive number, save the N next articles.
8843 If N is a negative number, save the N previous articles.
8844 If N is nil and any articles have been marked with the process mark,
8845 save those articles instead.
8846 The variable `gnus-default-article-saver' specifies the saver function."
8847   (interactive "P")
8848   (let* ((articles (gnus-summary-work-articles n))
8849          (save-buffer (save-excursion
8850                         (nnheader-set-temp-buffer " *Gnus Save*")))
8851          (num (length articles))
8852          header file)
8853     (dolist (article articles)
8854       (setq header (gnus-summary-article-header article))
8855       (if (not (vectorp header))
8856           ;; This is a pseudo-article.
8857           (if (assq 'name header)
8858               (gnus-copy-file (cdr (assq 'name header)))
8859             (gnus-message 1 "Article %d is unsaveable" article))
8860         ;; This is a real article.
8861         (save-window-excursion
8862           (gnus-summary-select-article t nil nil article))
8863         (save-excursion
8864           (set-buffer save-buffer)
8865           (erase-buffer)
8866           (insert-buffer-substring gnus-original-article-buffer))
8867         (setq file (gnus-article-save save-buffer file num))
8868         (gnus-summary-remove-process-mark article)
8869         (unless not-saved
8870           (gnus-summary-set-saved-mark article))))
8871     (gnus-kill-buffer save-buffer)
8872     (gnus-summary-position-point)
8873     (gnus-set-mode-line 'summary)
8874     n))
8875
8876 (defun gnus-summary-pipe-output (&optional arg)
8877   "Pipe the current article to a subprocess.
8878 If N is a positive number, pipe the N next articles.
8879 If N is a negative number, pipe the N previous articles.
8880 If N is nil and any articles have been marked with the process mark,
8881 pipe those articles instead."
8882   (interactive "P")
8883   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8884     (gnus-summary-save-article arg t))
8885   (gnus-configure-windows 'pipe))
8886
8887 (defun gnus-summary-save-article-mail (&optional arg)
8888   "Append the current article to an mail file.
8889 If N is a positive number, save the N next articles.
8890 If N is a negative number, save the N previous articles.
8891 If N is nil and any articles have been marked with the process mark,
8892 save those articles instead."
8893   (interactive "P")
8894   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8895     (gnus-summary-save-article arg)))
8896
8897 (defun gnus-summary-save-article-rmail (&optional arg)
8898   "Append the current article to an rmail file.
8899 If N is a positive number, save the N next articles.
8900 If N is a negative number, save the N previous articles.
8901 If N is nil and any articles have been marked with the process mark,
8902 save those articles instead."
8903   (interactive "P")
8904   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8905     (gnus-summary-save-article arg)))
8906
8907 (defun gnus-summary-save-article-file (&optional arg)
8908   "Append the current article to a file.
8909 If N is a positive number, save the N next articles.
8910 If N is a negative number, save the N previous articles.
8911 If N is nil and any articles have been marked with the process mark,
8912 save those articles instead."
8913   (interactive "P")
8914   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8915     (gnus-summary-save-article arg)))
8916
8917 (defun gnus-summary-write-article-file (&optional arg)
8918   "Write the current article to a file, deleting the previous file.
8919 If N is a positive number, save the N next articles.
8920 If N is a negative number, save the N previous articles.
8921 If N is nil and any articles have been marked with the process mark,
8922 save those articles instead."
8923   (interactive "P")
8924   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8925     (gnus-summary-save-article arg)))
8926
8927 (defun gnus-summary-save-article-body-file (&optional arg)
8928   "Append the current article body to a file.
8929 If N is a positive number, save the N next articles.
8930 If N is a negative number, save the N previous articles.
8931 If N is nil and any articles have been marked with the process mark,
8932 save those articles instead."
8933   (interactive "P")
8934   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8935     (gnus-summary-save-article arg)))
8936
8937 (defun gnus-summary-pipe-message (program)
8938   "Pipe the current article through PROGRAM."
8939   (interactive "sProgram: ")
8940   (gnus-summary-select-article)
8941   (let ((mail-header-separator ""))
8942     (gnus-eval-in-buffer-window gnus-article-buffer
8943       (save-restriction
8944         (widen)
8945         (let ((start (window-start))
8946               buffer-read-only)
8947           (message-pipe-buffer-body program)
8948           (set-window-start (get-buffer-window (current-buffer)) start))))))
8949
8950 (defun gnus-get-split-value (methods)
8951   "Return a value based on the split METHODS."
8952   (let (split-name method result match)
8953     (when methods
8954       (save-excursion
8955         (set-buffer gnus-original-article-buffer)
8956         (save-restriction
8957           (nnheader-narrow-to-headers)
8958           (while methods
8959             (goto-char (point-min))
8960             (setq method (pop methods))
8961             (setq match (car method))
8962             (when (cond
8963                    ((stringp match)
8964                     ;; Regular expression.
8965                     (ignore-errors
8966                       (re-search-forward match nil t)))
8967                    ((gnus-functionp match)
8968                     ;; Function.
8969                     (save-restriction
8970                       (widen)
8971                       (setq result (funcall match gnus-newsgroup-name))))
8972                    ((consp match)
8973                     ;; Form.
8974                     (save-restriction
8975                       (widen)
8976                       (setq result (eval match)))))
8977               (setq split-name (append (cdr method) split-name))
8978               (cond ((stringp result)
8979                      (push (expand-file-name
8980                             result gnus-article-save-directory)
8981                            split-name))
8982                     ((consp result)
8983                      (setq split-name (append result split-name)))))))))
8984     (nreverse split-name)))
8985
8986 (defun gnus-valid-move-group-p (group)
8987   (and (boundp group)
8988        (symbol-name group)
8989        (symbol-value group)
8990        (gnus-get-function (gnus-find-method-for-group
8991                            (symbol-name group)) 'request-accept-article t)))
8992
8993 (defun gnus-read-move-group-name (prompt default articles prefix)
8994   "Read a group name."
8995   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8996          (minibuffer-confirm-incomplete nil) ; XEmacs
8997          (prom
8998           (format "%s %s to:"
8999                   prompt
9000                   (if (> (length articles) 1)
9001                       (format "these %d articles" (length articles))
9002                     "this article")))
9003          (to-newsgroup
9004           (cond
9005            ((null split-name)
9006             (gnus-completing-read default prom
9007                                   gnus-active-hashtb
9008                                   'gnus-valid-move-group-p
9009                                   nil prefix
9010                                   'gnus-group-history))
9011            ((= 1 (length split-name))
9012             (gnus-completing-read (car split-name) prom
9013                                   gnus-active-hashtb
9014                                   'gnus-valid-move-group-p
9015                                   nil nil
9016                                   'gnus-group-history))
9017            (t
9018             (gnus-completing-read nil prom
9019                                   (mapcar (lambda (el) (list el))
9020                                           (nreverse split-name))
9021                                   nil nil nil
9022                                   'gnus-group-history)))))
9023     (when to-newsgroup
9024       (if (or (string= to-newsgroup "")
9025               (string= to-newsgroup prefix))
9026           (setq to-newsgroup default))
9027       (unless to-newsgroup
9028         (error "No group name entered"))
9029       (or (gnus-active to-newsgroup)
9030           (gnus-activate-group to-newsgroup)
9031           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9032                                      to-newsgroup))
9033               (or (and (gnus-request-create-group
9034                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9035                        (gnus-activate-group
9036                         to-newsgroup nil nil
9037                         (gnus-group-name-to-method to-newsgroup))
9038                        (gnus-subscribe-group to-newsgroup))
9039                   (error "Couldn't create group %s" to-newsgroup)))
9040           (error "No such group: %s" to-newsgroup)))
9041     to-newsgroup))
9042
9043 (defun gnus-summary-save-parts (type dir n reverse)
9044   "Save parts matching TYPE to DIR.
9045 If REVERSE, save parts that do not match TYPE."
9046   (interactive
9047    (list (read-string "Save parts of type: " "image/.*")
9048          (read-file-name "Save to directory: " t nil t)
9049          current-prefix-arg))
9050   (gnus-summary-iterate n
9051     (let ((gnus-display-mime-function nil)
9052           (gnus-inhibit-treatment t))
9053       (gnus-summary-select-article))
9054     (save-excursion
9055       (set-buffer gnus-article-buffer)
9056       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9057         (when handles
9058           (gnus-summary-save-parts-1 type dir handles reverse)
9059           (mm-destroy-parts handles))))))
9060
9061 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9062   (if (stringp (car handle))
9063       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9064               (cdr handle))
9065     (when (if reverse
9066               (not (string-match type (mm-handle-media-type handle)))
9067             (string-match type (mm-handle-media-type handle)))
9068       (let ((file (expand-file-name
9069                    (file-name-nondirectory
9070                     (or
9071                      (mail-content-type-get
9072                       (mm-handle-disposition handle) 'filename)
9073                      (concat gnus-newsgroup-name "." gnus-current-article)))
9074                    dir)))
9075         (unless (file-exists-p file)
9076           (mm-save-part-to-file handle file))))))
9077
9078 ;; Summary extract commands
9079
9080 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9081   (let ((buffer-read-only nil)
9082         (article (gnus-summary-article-number))
9083         after-article b e)
9084     (unless (gnus-summary-goto-subject article)
9085       (error "No such article: %d" article))
9086     (gnus-summary-position-point)
9087     ;; If all commands are to be bunched up on one line, we collect
9088     ;; them here.
9089     (unless gnus-view-pseudos-separately
9090       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9091             files action)
9092         (while ps
9093           (setq action (cdr (assq 'action (car ps))))
9094           (setq files (list (cdr (assq 'name (car ps)))))
9095           (while (and ps (cdr ps)
9096                       (string= (or action "1")
9097                                (or (cdr (assq 'action (cadr ps))) "2")))
9098             (push (cdr (assq 'name (cadr ps))) files)
9099             (setcdr ps (cddr ps)))
9100           (when files
9101             (when (not (string-match "%s" action))
9102               (push " " files))
9103             (push " " files)
9104             (when (assq 'execute (car ps))
9105               (setcdr (assq 'execute (car ps))
9106                       (funcall (if (string-match "%s" action)
9107                                    'format 'concat)
9108                                action
9109                                (mapconcat
9110                                 (lambda (f)
9111                                   (if (equal f " ")
9112                                       f
9113                                     (mm-quote-arg f)))
9114                                 files " ")))))
9115           (setq ps (cdr ps)))))
9116     (if (and gnus-view-pseudos (not not-view))
9117         (while pslist
9118           (when (assq 'execute (car pslist))
9119             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9120                                   (eq gnus-view-pseudos 'not-confirm)))
9121           (setq pslist (cdr pslist)))
9122       (save-excursion
9123         (while pslist
9124           (setq after-article (or (cdr (assq 'article (car pslist)))
9125                                   (gnus-summary-article-number)))
9126           (gnus-summary-goto-subject after-article)
9127           (forward-line 1)
9128           (setq b (point))
9129           (insert "    " (file-name-nondirectory
9130                           (cdr (assq 'name (car pslist))))
9131                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9132           (setq e (point))
9133           (forward-line -1)             ; back to `b'
9134           (gnus-add-text-properties
9135            b (1- e) (list 'gnus-number gnus-reffed-article-number
9136                           gnus-mouse-face-prop gnus-mouse-face))
9137           (gnus-data-enter
9138            after-article gnus-reffed-article-number
9139            gnus-unread-mark b (car pslist) 0 (- e b))
9140           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9141           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9142           (setq pslist (cdr pslist)))))))
9143
9144 (defun gnus-pseudos< (p1 p2)
9145   (let ((c1 (cdr (assq 'action p1)))
9146         (c2 (cdr (assq 'action p2))))
9147     (and c1 c2 (string< c1 c2))))
9148
9149 (defun gnus-request-pseudo-article (props)
9150   (cond ((assq 'execute props)
9151          (gnus-execute-command (cdr (assq 'execute props)))))
9152   (let ((gnus-current-article (gnus-summary-article-number)))
9153     (gnus-run-hooks 'gnus-mark-article-hook)))
9154
9155 (defun gnus-execute-command (command &optional automatic)
9156   (save-excursion
9157     (gnus-article-setup-buffer)
9158     (set-buffer gnus-article-buffer)
9159     (setq buffer-read-only nil)
9160     (let ((command (if automatic command
9161                      (read-string "Command: " (cons command 0)))))
9162       (erase-buffer)
9163       (insert "$ " command "\n\n")
9164       (if gnus-view-pseudo-asynchronously
9165           (start-process "gnus-execute" (current-buffer) shell-file-name
9166                          shell-command-switch command)
9167         (call-process shell-file-name nil t nil
9168                       shell-command-switch command)))))
9169
9170 ;; Summary kill commands.
9171
9172 (defun gnus-summary-edit-global-kill (article)
9173   "Edit the \"global\" kill file."
9174   (interactive (list (gnus-summary-article-number)))
9175   (gnus-group-edit-global-kill article))
9176
9177 (defun gnus-summary-edit-local-kill ()
9178   "Edit a local kill file applied to the current newsgroup."
9179   (interactive)
9180   (setq gnus-current-headers (gnus-summary-article-header))
9181   (gnus-group-edit-local-kill
9182    (gnus-summary-article-number) gnus-newsgroup-name))
9183
9184 ;;; Header reading.
9185
9186 (defun gnus-read-header (id &optional header)
9187   "Read the headers of article ID and enter them into the Gnus system."
9188   (let ((group gnus-newsgroup-name)
9189         (gnus-override-method
9190          (or
9191           gnus-override-method
9192           (and (gnus-news-group-p gnus-newsgroup-name)
9193                (car (gnus-refer-article-methods)))))
9194         where)
9195     ;; First we check to see whether the header in question is already
9196     ;; fetched.
9197     (if (stringp id)
9198         ;; This is a Message-ID.
9199         (setq header (or header (gnus-id-to-header id)))
9200       ;; This is an article number.
9201       (setq header (or header (gnus-summary-article-header id))))
9202     (if (and header
9203              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9204         ;; We have found the header.
9205         header
9206       ;; If this is a sparse article, we have to nix out its
9207       ;; previous entry in the thread hashtb.
9208       (when (and header
9209                  (gnus-summary-article-sparse-p (mail-header-number header)))
9210         (let* ((parent (gnus-parent-id (mail-header-references header)))
9211                (thread (and parent (gnus-id-to-thread parent))))
9212           (when thread
9213             (delq (assq header thread) thread))))
9214       ;; We have to really fetch the header to this article.
9215       (save-excursion
9216         (set-buffer nntp-server-buffer)
9217         (when (setq where (gnus-request-head id group))
9218           (nnheader-fold-continuation-lines)
9219           (goto-char (point-max))
9220           (insert ".\n")
9221           (goto-char (point-min))
9222           (insert "211 ")
9223           (princ (cond
9224                   ((numberp id) id)
9225                   ((cdr where) (cdr where))
9226                   (header (mail-header-number header))
9227                   (t gnus-reffed-article-number))
9228                  (current-buffer))
9229           (insert " Article retrieved.\n"))
9230         (if (or (not where)
9231                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9232             ()                          ; Malformed head.
9233           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9234             (when (and (stringp id)
9235                        (not (string= (gnus-group-real-name group)
9236                                      (car where))))
9237               ;; If we fetched by Message-ID and the article came
9238               ;; from a different group, we fudge some bogus article
9239               ;; numbers for this article.
9240               (mail-header-set-number header gnus-reffed-article-number))
9241             (save-excursion
9242               (set-buffer gnus-summary-buffer)
9243               (decf gnus-reffed-article-number)
9244               (gnus-remove-header (mail-header-number header))
9245               (push header gnus-newsgroup-headers)
9246               (setq gnus-current-headers header)
9247               (push (mail-header-number header) gnus-newsgroup-limit)))
9248           header)))))
9249
9250 (defun gnus-remove-header (number)
9251   "Remove header NUMBER from `gnus-newsgroup-headers'."
9252   (if (and gnus-newsgroup-headers
9253            (= number (mail-header-number (car gnus-newsgroup-headers))))
9254       (pop gnus-newsgroup-headers)
9255     (let ((headers gnus-newsgroup-headers))
9256       (while (and (cdr headers)
9257                   (not (= number (mail-header-number (cadr headers)))))
9258         (pop headers))
9259       (when (cdr headers)
9260         (setcdr headers (cddr headers))))))
9261
9262 ;;;
9263 ;;; summary highlights
9264 ;;;
9265
9266 (defun gnus-highlight-selected-summary ()
9267   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9268   ;; Highlight selected article in summary buffer
9269   (when gnus-summary-selected-face
9270     (save-excursion
9271       (let* ((beg (progn (beginning-of-line) (point)))
9272              (end (progn (end-of-line) (point)))
9273              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9274              (from (if (get-text-property beg gnus-mouse-face-prop)
9275                        beg
9276                      (or (next-single-property-change
9277                           beg gnus-mouse-face-prop nil end)
9278                          beg)))
9279              (to
9280               (if (= from end)
9281                   (- from 2)
9282                 (or (next-single-property-change
9283                      from gnus-mouse-face-prop nil end)
9284                     end))))
9285         ;; If no mouse-face prop on line we will have to = from = end,
9286         ;; so we highlight the entire line instead.
9287         (when (= (+ to 2) from)
9288           (setq from beg)
9289           (setq to end))
9290         (if gnus-newsgroup-selected-overlay
9291             ;; Move old overlay.
9292             (gnus-move-overlay
9293              gnus-newsgroup-selected-overlay from to (current-buffer))
9294           ;; Create new overlay.
9295           (gnus-overlay-put
9296            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9297            'face gnus-summary-selected-face))))))
9298
9299 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9300 (defun gnus-summary-highlight-line ()
9301   "Highlight current line according to `gnus-summary-highlight'."
9302   (let* ((list gnus-summary-highlight)
9303          (p (point))
9304          (end (progn (end-of-line) (point)))
9305          ;; now find out where the line starts and leave point there.
9306          (beg (progn (beginning-of-line) (point)))
9307          (article (gnus-summary-article-number))
9308          (score (or (cdr (assq (or article gnus-current-article)
9309                                gnus-newsgroup-scored))
9310                     gnus-summary-default-score 0))
9311          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9312          (inhibit-read-only t))
9313     ;; Eval the cars of the lists until we find a match.
9314     (let ((default gnus-summary-default-score))
9315       (while (and list
9316                   (not (eval (caar list))))
9317         (setq list (cdr list))))
9318     (let ((face (cdar list)))
9319       (unless (eq face (get-text-property beg 'face))
9320         (gnus-put-text-property-excluding-characters-with-faces
9321          beg end 'face
9322          (setq face (if (boundp face) (symbol-value face) face)))
9323         (when gnus-summary-highlight-line-function
9324           (funcall gnus-summary-highlight-line-function article face))))
9325     (goto-char p)))
9326
9327 (defun gnus-update-read-articles (group unread &optional compute)
9328   "Update the list of read articles in GROUP."
9329   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9330          (entry (gnus-gethash group gnus-newsrc-hashtb))
9331          (info (nth 2 entry))
9332          (prev 1)
9333          (unread (sort (copy-sequence unread) '<))
9334          read)
9335     (if (or (not info) (not active))
9336         ;; There is no info on this group if it was, in fact,
9337         ;; killed.  Gnus stores no information on killed groups, so
9338         ;; there's nothing to be done.
9339         ;; One could store the information somewhere temporarily,
9340         ;; perhaps...  Hmmm...
9341         ()
9342       ;; Remove any negative articles numbers.
9343       (while (and unread (< (car unread) 0))
9344         (setq unread (cdr unread)))
9345       ;; Remove any expired article numbers
9346       (while (and unread (< (car unread) (car active)))
9347         (setq unread (cdr unread)))
9348       ;; Compute the ranges of read articles by looking at the list of
9349       ;; unread articles.
9350       (while unread
9351         (when (/= (car unread) prev)
9352           (push (if (= prev (1- (car unread))) prev
9353                   (cons prev (1- (car unread))))
9354                 read))
9355         (setq prev (1+ (car unread)))
9356         (setq unread (cdr unread)))
9357       (when (<= prev (cdr active))
9358         (push (cons prev (cdr active)) read))
9359       (setq read (if (> (length read) 1) (nreverse read) read))
9360       (if compute
9361           read
9362         (save-excursion
9363           (set-buffer gnus-group-buffer)
9364           (gnus-undo-register
9365             `(progn
9366                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9367                (gnus-info-set-read ',info ',(gnus-info-read info))
9368                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9369                (gnus-group-update-group ,group t))))
9370         ;; Propagate the read marks to the backend.
9371         (if (gnus-check-backend-function 'request-set-mark group)
9372             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9373                   (add (gnus-remove-from-range read (gnus-info-read info))))
9374               (when (or add del)
9375                 (unless (gnus-check-group group)
9376                   (error "Can't open server for %s" group))
9377                 (gnus-request-set-mark
9378                  group (delq nil (list (if add (list add 'add '(read)))
9379                                        (if del (list del 'del '(read)))))))))
9380         ;; Enter this list into the group info.
9381         (gnus-info-set-read info read)
9382         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9383         (gnus-get-unread-articles-in-group info (gnus-active group))
9384         t))))
9385
9386 (defun gnus-offer-save-summaries ()
9387   "Offer to save all active summary buffers."
9388   (save-excursion
9389     (let ((buflist (buffer-list))
9390           buffers bufname)
9391       ;; Go through all buffers and find all summaries.
9392       (while buflist
9393         (and (setq bufname (buffer-name (car buflist)))
9394              (string-match "Summary" bufname)
9395              (save-excursion
9396                (set-buffer bufname)
9397                ;; We check that this is, indeed, a summary buffer.
9398                (and (eq major-mode 'gnus-summary-mode)
9399                     ;; Also make sure this isn't bogus.
9400                     gnus-newsgroup-prepared
9401                     ;; Also make sure that this isn't a dead summary buffer.
9402                     (not gnus-dead-summary-mode)))
9403              (push bufname buffers))
9404         (setq buflist (cdr buflist)))
9405       ;; Go through all these summary buffers and offer to save them.
9406       (when buffers
9407         (map-y-or-n-p
9408          "Update summary buffer %s? "
9409          (lambda (buf)
9410            (switch-to-buffer buf)
9411            (gnus-summary-exit))
9412          buffers)))))
9413
9414 (defun gnus-summary-setup-default-charset ()
9415   "Setup newsgroup default charset."
9416   (if (equal gnus-newsgroup-name "nndraft:drafts")
9417       (setq gnus-newsgroup-charset nil)
9418     (let* ((name (and gnus-newsgroup-name
9419                       (gnus-group-real-name gnus-newsgroup-name)))
9420            (ignored-charsets 
9421             (or gnus-newsgroup-ephemeral-ignored-charsets
9422                 (append
9423                  (and gnus-newsgroup-name
9424                       (or (gnus-group-find-parameter gnus-newsgroup-name
9425                                                      'ignored-charsets t)
9426                           (let ((alist gnus-group-ignored-charsets-alist)
9427                                 elem (charsets nil))
9428                             (while (setq elem (pop alist))
9429                               (when (and name
9430                                          (string-match (car elem) name))
9431                                 (setq alist nil
9432                                       charsets (cdr elem))))
9433                             charsets)))
9434                  gnus-newsgroup-ignored-charsets))))
9435       (setq gnus-newsgroup-charset
9436             (or gnus-newsgroup-ephemeral-charset
9437                 (and gnus-newsgroup-name
9438                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9439                          (let ((alist gnus-group-charset-alist)
9440                                elem charset)
9441                            (while (setq elem (pop alist))
9442                              (when (and name
9443                                         (string-match (car elem) name))
9444                                (setq alist nil
9445                                      charset (cadr elem))))
9446                            charset)))
9447                 gnus-default-charset))
9448       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9449            ignored-charsets))))
9450
9451 ;;;
9452 ;;; Mime Commands
9453 ;;;
9454
9455 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9456   "Display the current article buffer fully MIME-buttonized.
9457 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9458 treated as multipart/mixed."
9459   (interactive "P")
9460   (require 'gnus-art)
9461   (let ((gnus-unbuttonized-mime-types nil)
9462         (gnus-mime-display-multipart-as-mixed show-all-parts))
9463     (gnus-summary-show-article)))
9464
9465 (defun gnus-summary-repair-multipart (article)
9466   "Add a Content-Type header to a multipart article without one."
9467   (interactive (list (gnus-summary-article-number)))
9468   (gnus-with-article article
9469     (message-narrow-to-head)
9470     (goto-char (point-max))
9471     (widen)
9472     (when (search-forward "\n--" nil t)
9473       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9474         (message-narrow-to-head)
9475         (message-remove-header "Mime-Version")
9476         (message-remove-header "Content-Type")
9477         (goto-char (point-max))
9478         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9479                         separator))
9480         (insert "Mime-Version: 1.0\n")
9481         (widen))))
9482   (let (gnus-mark-article-hook)
9483     (gnus-summary-select-article t t nil article)))
9484
9485 (defun gnus-summary-toggle-display-buttonized ()
9486   "Toggle the buttonizing of the article buffer."
9487   (interactive)
9488   (require 'gnus-art)
9489   (if (setq gnus-inhibit-mime-unbuttonizing
9490             (not gnus-inhibit-mime-unbuttonizing))
9491       (let ((gnus-unbuttonized-mime-types nil))
9492         (gnus-summary-show-article))
9493     (gnus-summary-show-article)))
9494
9495 ;;;
9496 ;;; with article
9497 ;;;
9498
9499 (defmacro gnus-with-article (article &rest forms)
9500   "Select ARTICLE and perform FORMS in the original article buffer.
9501 Then replace the article with the result."
9502   `(progn
9503      ;; We don't want the article to be marked as read.
9504      (let (gnus-mark-article-hook)
9505        (gnus-summary-select-article t t nil ,article))
9506      (set-buffer gnus-original-article-buffer)
9507      ,@forms
9508      (if (not (gnus-check-backend-function
9509                'request-replace-article (car gnus-article-current)))
9510          (gnus-message 5 "Read-only group; not replacing")
9511        (unless (gnus-request-replace-article
9512                 ,article (car gnus-article-current)
9513                 (current-buffer) t)
9514          (error "Couldn't replace article")))
9515      ;; The cache and backlog have to be flushed somewhat.
9516      (when gnus-keep-backlog
9517        (gnus-backlog-remove-article
9518         (car gnus-article-current) (cdr gnus-article-current)))
9519      (when gnus-use-cache
9520        (gnus-cache-update-article
9521         (car gnus-article-current) (cdr gnus-article-current)))))
9522
9523 (put 'gnus-with-article 'lisp-indent-function 1)
9524 (put 'gnus-with-article 'edebug-form-spec '(form body))
9525
9526 ;;;
9527 ;;; Generic summary marking commands
9528 ;;;
9529
9530 (defvar gnus-summary-marking-alist
9531   '((read gnus-del-mark "d")
9532     (unread gnus-unread-mark "u")
9533     (ticked gnus-ticked-mark "!")
9534     (dormant gnus-dormant-mark "?")
9535     (expirable gnus-expirable-mark "e"))
9536   "An alist of names/marks/keystrokes.")
9537
9538 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9539 (defvar gnus-summary-mark-map)
9540
9541 (defun gnus-summary-make-all-marking-commands ()
9542   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9543   (dolist (elem gnus-summary-marking-alist)
9544     (apply 'gnus-summary-make-marking-command elem)))
9545
9546 (defun gnus-summary-make-marking-command (name mark keystroke)
9547   (let ((map (make-sparse-keymap)))
9548     (define-key gnus-summary-generic-mark-map keystroke map)
9549     (dolist (lway `((next "next" next nil "n")
9550                     (next-unread "next unread" next t "N")
9551                     (prev "previous" prev nil "p")
9552                     (prev-unread "previous unread" prev t "P")
9553                     (nomove "" nil nil ,keystroke)))
9554       (let ((func (gnus-summary-make-marking-command-1
9555                    mark (car lway) lway name)))
9556         (setq func (eval func))
9557         (define-key map (nth 4 lway) func)))))
9558       
9559 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9560   `(defun ,(intern
9561             (format "gnus-summary-put-mark-as-%s%s"
9562                     name (if (eq way 'nomove)
9563                              ""
9564                            (concat "-" (symbol-name way)))))
9565      (n)
9566      ,(format
9567        "Mark the current article as %s%s.
9568 If N, the prefix, then repeat N times.
9569 If N is negative, move in reverse order.
9570 The difference between N and the actual number of articles marked is
9571 returned."
9572        name (cadr lway))
9573      (interactive "p")
9574      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9575     
9576 (defun gnus-summary-generic-mark (n mark move unread)
9577   "Mark N articles with MARK."
9578   (unless (eq major-mode 'gnus-summary-mode)
9579     (error "This command can only be used in the summary buffer"))
9580   (gnus-summary-show-thread)
9581   (let ((nummove
9582          (cond
9583           ((eq move 'next) 1)
9584           ((eq move 'prev) -1)
9585           (t 0))))
9586     (if (zerop nummove)
9587         (setq n 1)
9588       (when (< n 0)
9589         (setq n (abs n)
9590               nummove (* -1 nummove))))
9591     (while (and (> n 0)
9592                 (gnus-summary-mark-article nil mark)
9593                 (zerop (gnus-summary-next-subject nummove unread t)))
9594       (setq n (1- n)))
9595     (when (/= 0 n)
9596       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9597     (gnus-summary-recenter)
9598     (gnus-summary-position-point)
9599     (gnus-set-mode-line 'summary)
9600     n))
9601
9602 (gnus-summary-make-all-marking-commands)
9603
9604 (gnus-ems-redefine)
9605
9606 (provide 'gnus-sum)
9607
9608 (run-hooks 'gnus-sum-load-hook)
9609
9610 ;;; gnus-sum.el ends here