2000-12-01 Christopher Splinter <chris@splinter.inka.de>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40 (autoload 'gnus-cache-write-active "gnus-cache")
41 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
42
43 (defcustom gnus-kill-summary-on-exit t
44   "*If non-nil, kill the summary buffer when you exit from it.
45 If nil, the summary will become a \"*Dead Summary*\" buffer, and
46 it will be killed sometime later."
47   :group 'gnus-summary-exit
48   :type 'boolean)
49
50 (defcustom gnus-fetch-old-headers nil
51   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
52 If an unread article in the group refers to an older, already read (or
53 just marked as read) article, the old article will not normally be
54 displayed in the Summary buffer.  If this variable is non-nil, Gnus
55 will attempt to grab the headers to the old articles, and thereby
56 build complete threads.  If it has the value `some', only enough
57 headers to connect otherwise loose threads will be displayed.  This
58 variable can also be a number.  In that case, no more than that number
59 of old headers will be fetched.  If it has the value `invisible', all
60 old headers will be fetched, but none will be displayed.
61
62 The server has to support NOV for any of this to work."
63   :group 'gnus-thread
64   :type '(choice (const :tag "off" nil)
65                  (const some)
66                  number
67                  (sexp :menu-tag "other" t)))
68
69 (defcustom gnus-refer-thread-limit 200
70   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
71 If t, fetch all the available old headers."
72   :group 'gnus-thread
73   :type '(choice number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-summary-make-false-root 'adopt
77   "*nil means that Gnus won't gather loose threads.
78 If the root of a thread has expired or been read in a previous
79 session, the information necessary to build a complete thread has been
80 lost.  Instead of having many small sub-threads from this original thread
81 scattered all over the summary buffer, Gnus can gather them.
82
83 If non-nil, Gnus will try to gather all loose sub-threads from an
84 original thread into one large thread.
85
86 If this variable is non-nil, it should be one of `none', `adopt',
87 `dummy' or `empty'.
88
89 If this variable is `none', Gnus will not make a false root, but just
90 present the sub-threads after another.
91 If this variable is `dummy', Gnus will create a dummy root that will
92 have all the sub-threads as children.
93 If this variable is `adopt', Gnus will make one of the \"children\"
94 the parent and mark all the step-children as such.
95 If this variable is `empty', the \"children\" are printed with empty
96 subject fields.  (Or rather, they will be printed with a string
97 given by the `gnus-summary-same-subject' variable.)"
98   :group 'gnus-thread
99   :type '(choice (const :tag "off" nil)
100                  (const none)
101                  (const dummy)
102                  (const adopt)
103                  (const empty)))
104
105 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
106   "*A regexp to match subjects to be excluded from loose thread gathering.
107 As loose thread gathering is done on subjects only, that means that
108 there can be many false gatherings performed.  By rooting out certain
109 common subjects, gathering might become saner."
110   :group 'gnus-thread
111   :type 'regexp)
112
113 (defcustom gnus-summary-gather-subject-limit nil
114   "*Maximum length of subject comparisons when gathering loose threads.
115 Use nil to compare full subjects.  Setting this variable to a low
116 number will help gather threads that have been corrupted by
117 newsreaders chopping off subject lines, but it might also mean that
118 unrelated articles that have subject that happen to begin with the
119 same few characters will be incorrectly gathered.
120
121 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
122 comparing subjects."
123   :group 'gnus-thread
124   :type '(choice (const :tag "off" nil)
125                  (const fuzzy)
126                  (sexp :menu-tag "on" t)))
127
128 (defcustom gnus-simplify-subject-functions nil
129   "List of functions taking a string argument that simplify subjects.
130 The functions are applied recursively.
131
132 Useful functions to put in this list include: `gnus-simplify-subject-re',
133 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
134   :group 'gnus-thread
135   :type '(repeat function))
136
137 (defcustom gnus-simplify-ignored-prefixes nil
138   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
139   :group 'gnus-thread
140   :type '(choice (const :tag "off" nil)
141                  regexp))
142
143 (defcustom gnus-build-sparse-threads nil
144   "*If non-nil, fill in the gaps in threads.
145 If `some', only fill in the gaps that are needed to tie loose threads
146 together.  If `more', fill in all leaf nodes that Gnus can find.  If
147 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
148   :group 'gnus-thread
149   :type '(choice (const :tag "off" nil)
150                  (const some)
151                  (const more)
152                  (sexp :menu-tag "all" t)))
153
154 (defcustom gnus-summary-thread-gathering-function
155   'gnus-gather-threads-by-subject
156   "*Function used for gathering loose threads.
157 There are two pre-defined functions: `gnus-gather-threads-by-subject',
158 which only takes Subjects into consideration; and
159 `gnus-gather-threads-by-references', which compared the References
160 headers of the articles to find matches."
161   :group 'gnus-thread
162   :type '(radio (function-item gnus-gather-threads-by-subject)
163                 (function-item gnus-gather-threads-by-references)
164                 (function :tag "other")))
165
166 (defcustom gnus-summary-same-subject ""
167   "*String indicating that the current article has the same subject as the previous.
168 This variable will only be used if the value of
169 `gnus-summary-make-false-root' is `empty'."
170   :group 'gnus-summary-format
171   :type 'string)
172
173 (defcustom gnus-summary-goto-unread t
174   "*If t, many commands will go to the next unread article.
175 This applies to marking commands as well as other commands that
176 \"naturally\" select the next article, like, for instance, `SPC' at
177 the end of an article.
178
179 If nil, the marking commands do NOT go to the next unread article
180 (they go to the next article instead).  If `never', commands that
181 usually go to the next unread article, will go to the next article,
182 whether it is read or not."
183   :group 'gnus-summary-marks
184   :link '(custom-manual "(gnus)Setting Marks")
185   :type '(choice (const :tag "off" nil)
186                  (const never)
187                  (sexp :menu-tag "on" t)))
188
189 (defcustom gnus-summary-default-score 0
190   "*Default article score level.
191 All scores generated by the score files will be added to this score.
192 If this variable is nil, scoring will be disabled."
193   :group 'gnus-score-default
194   :type '(choice (const :tag "disable")
195                  integer))
196
197 (defcustom gnus-summary-zcore-fuzz 0
198   "*Fuzziness factor for the zcore in the summary buffer.
199 Articles with scores closer than this to `gnus-summary-default-score'
200 will not be marked."
201   :group 'gnus-summary-format
202   :type 'integer)
203
204 (defcustom gnus-simplify-subject-fuzzy-regexp nil
205   "*Strings to be removed when doing fuzzy matches.
206 This can either be a regular expression or list of regular expressions
207 that will be removed from subject strings if fuzzy subject
208 simplification is selected."
209   :group 'gnus-thread
210   :type '(repeat regexp))
211
212 (defcustom gnus-show-threads t
213   "*If non-nil, display threads in summary mode."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-subtree nil
218   "*If non-nil, hide all threads initially.
219 If threads are hidden, you have to run the command
220 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
221 to expose hidden threads."
222   :group 'gnus-thread
223   :type 'boolean)
224
225 (defcustom gnus-thread-hide-killed t
226   "*If non-nil, hide killed threads automatically."
227   :group 'gnus-thread
228   :type 'boolean)
229
230 (defcustom gnus-thread-ignore-subject t
231   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
232 If nil, articles that have different subjects from their parents will
233 start separate threads."
234   :group 'gnus-thread
235   :type 'boolean)
236
237 (defcustom gnus-thread-operation-ignore-subject t
238   "*If non-nil, subjects will be ignored when doing thread commands.
239 This affects commands like `gnus-summary-kill-thread' and
240 `gnus-summary-lower-thread'.
241
242 If this variable is nil, articles in the same thread with different
243 subjects will not be included in the operation in question.  If this
244 variable is `fuzzy', only articles that have subjects that are fuzzily
245 equal will be included."
246   :group 'gnus-thread
247   :type '(choice (const :tag "off" nil)
248                  (const fuzzy)
249                  (sexp :tag "on" t)))
250
251 (defcustom gnus-thread-indent-level 4
252   "*Number that says how much each sub-thread should be indented."
253   :group 'gnus-thread
254   :type 'integer)
255
256 (defcustom gnus-auto-extend-newsgroup t
257   "*If non-nil, extend newsgroup forward and backward when requested."
258   :group 'gnus-summary-choose
259   :type 'boolean)
260
261 (defcustom gnus-auto-select-first t
262   "*If nil, don't select the first unread article when entering a group.
263 If this variable is `best', select the highest-scored unread article
264 in the group.  If t, select the first unread article.
265
266 This variable can also be a function to place point on a likely
267 subject line.  Useful values include `gnus-summary-first-unread-subject',
268 `gnus-summary-first-unread-article' and
269 `gnus-summary-best-unread-article'.
270
271 If you want to prevent automatic selection of the first unread article
272 in some newsgroups, set the variable to nil in
273 `gnus-select-group-hook'."
274   :group 'gnus-group-select
275   :type '(choice (const :tag "none" nil)
276                  (const best)
277                  (sexp :menu-tag "first" t)
278                  (function-item gnus-summary-first-unread-subject)
279                  (function-item gnus-summary-first-unread-article)
280                  (function-item gnus-summary-best-unread-article)))
281
282 (defcustom gnus-auto-select-next t
283   "*If non-nil, offer to go to the next group from the end of the previous.
284 If the value is t and the next newsgroup is empty, Gnus will exit
285 summary mode and go back to group mode.  If the value is neither nil
286 nor t, Gnus will select the following unread newsgroup.  In
287 particular, if the value is the symbol `quietly', the next unread
288 newsgroup will be selected without any confirmation, and if it is
289 `almost-quietly', the next group will be selected without any
290 confirmation if you are located on the last article in the group.
291 Finally, if this variable is `slightly-quietly', the `Z n' command
292 will go to the next group without confirmation."
293   :group 'gnus-summary-maneuvering
294   :type '(choice (const :tag "off" nil)
295                  (const quietly)
296                  (const almost-quietly)
297                  (const slightly-quietly)
298                  (sexp :menu-tag "on" t)))
299
300 (defcustom gnus-auto-select-same nil
301   "*If non-nil, select the next article with the same subject.
302 If there are no more articles with the same subject, go to
303 the first unread article."
304   :group 'gnus-summary-maneuvering
305   :type 'boolean)
306
307 (defcustom gnus-summary-check-current nil
308   "*If non-nil, consider the current article when moving.
309 The \"unread\" movement commands will stay on the same line if the
310 current article is unread."
311   :group 'gnus-summary-maneuvering
312   :type 'boolean)
313
314 (defcustom gnus-auto-center-summary t
315   "*If non-nil, always center the current summary buffer.
316 In particular, if `vertical' do only vertical recentering.  If non-nil
317 and non-`vertical', do both horizontal and vertical recentering."
318   :group 'gnus-summary-maneuvering
319   :type '(choice (const :tag "none" nil)
320                  (const vertical)
321                  (integer :tag "height")
322                  (sexp :menu-tag "both" t)))
323
324 (defcustom gnus-show-all-headers nil
325   "*If non-nil, don't hide any headers."
326   :group 'gnus-article-hiding
327   :group 'gnus-article-headers
328   :type 'boolean)
329
330 (defcustom gnus-summary-ignore-duplicates nil
331   "*If non-nil, ignore articles with identical Message-ID headers."
332   :group 'gnus-summary
333   :type 'boolean)
334
335 (defcustom gnus-single-article-buffer t
336   "*If non-nil, display all articles in the same buffer.
337 If nil, each group will get its own article buffer."
338   :group 'gnus-article-various
339   :type 'boolean)
340
341 (defcustom gnus-break-pages t
342   "*If non-nil, do page breaking on articles.
343 The page delimiter is specified by the `gnus-page-delimiter'
344 variable."
345   :group 'gnus-article-various
346   :type 'boolean)
347
348 (defcustom gnus-move-split-methods nil
349   "*Variable used to suggest where articles are to be moved to.
350 It uses the same syntax as the `gnus-split-methods' variable.
351 However, whereas `gnus-split-methods' specifies file names as targets,
352 this variable specifies group names."
353   :group 'gnus-summary-mail
354   :type '(repeat (choice (list :value (fun) function)
355                          (cons :value ("" "") regexp (repeat string))
356                          (sexp :value nil))))
357
358 (defcustom gnus-unread-mark ?  ;Whitespace
359   "*Mark used for unread articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-ticked-mark ?!
364   "*Mark used for ticked articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-dormant-mark ??
369   "*Mark used for dormant articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-del-mark ?r
374   "*Mark used for del'd articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-read-mark ?R
379   "*Mark used for read articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-expirable-mark ?E
384   "*Mark used for expirable articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-killed-mark ?K
389   "*Mark used for killed articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-souped-mark ?F
394   "*Mark used for killed articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-kill-file-mark ?X
399   "*Mark used for articles killed by kill files."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-low-score-mark ?Y
404   "*Mark used for articles with a low score."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-catchup-mark ?C
409   "*Mark used for articles that are caught up."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-replied-mark ?A
414   "*Mark used for articles that have been replied to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-cached-mark ?*
419   "*Mark used for articles that are in the cache."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-saved-mark ?S
424   "*Mark used for articles that have been saved to."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-ancient-mark ?O
429   "*Mark used for ancient articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-sparse-mark ?Q
434   "*Mark used for sparsely reffed articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-canceled-mark ?G
439   "*Mark used for canceled articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-duplicate-mark ?M
444   "*Mark used for duplicate articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-undownloaded-mark ?@
449   "*Mark used for articles that weren't downloaded."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-downloadable-mark ?%
454   "*Mark used for articles that are to be downloaded."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-unsendable-mark ?=
459   "*Mark used for articles that won't be sent."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-score-over-mark ?+
464   "*Score mark used for articles with high scores."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-score-below-mark ?-
469   "*Score mark used for articles with low scores."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-empty-thread-mark ?  ;Whitespace
474   "*There is no thread under the article."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-not-empty-thread-mark ?=
479   "*There is a thread under the article."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-view-pseudo-asynchronously nil
484   "*If non-nil, Gnus will view pseudo-articles asynchronously."
485   :group 'gnus-extract-view
486   :type 'boolean)
487
488 (defcustom gnus-auto-expirable-marks
489   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
490         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
491         gnus-souped-mark gnus-duplicate-mark)
492   "*The list of marks converted into expiration if a group is auto-expirable."
493   :group 'gnus-summary
494   :type '(repeat character))
495
496 (defcustom gnus-inhibit-user-auto-expire t
497   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
498   :group 'gnus-summary
499   :type 'boolean)
500
501 (defcustom gnus-view-pseudos nil
502   "*If `automatic', pseudo-articles will be viewed automatically.
503 If `not-confirm', pseudos will be viewed automatically, and the user
504 will not be asked to confirm the command."
505   :group 'gnus-extract-view
506   :type '(choice (const :tag "off" nil)
507                  (const automatic)
508                  (const not-confirm)))
509
510 (defcustom gnus-view-pseudos-separately t
511   "*If non-nil, one pseudo-article will be created for each file to be viewed.
512 If nil, all files that use the same viewing command will be given as a
513 list of parameters to that command."
514   :group 'gnus-extract-view
515   :type 'boolean)
516
517 (defcustom gnus-insert-pseudo-articles t
518   "*If non-nil, insert pseudo-articles when decoding articles."
519   :group 'gnus-extract-view
520   :type 'boolean)
521
522 (defcustom gnus-summary-dummy-line-format
523   "  %(:                          :%) %S\n"
524   "*The format specification for the dummy roots in the summary buffer.
525 It works along the same lines as a normal formatting string,
526 with some simple extensions.
527
528 %S  The subject"
529   :group 'gnus-threading
530   :type 'string)
531
532 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
533   "*The format specification for the summary mode line.
534 It works along the same lines as a normal formatting string,
535 with some simple extensions:
536
537 %G  Group name
538 %p  Unprefixed group name
539 %A  Current article number
540 %z  Current article score
541 %V  Gnus version
542 %U  Number of unread articles in the group
543 %e  Number of unselected articles in the group
544 %Z  A string with unread/unselected article counts
545 %g  Shortish group name
546 %S  Subject of the current article
547 %u  User-defined spec
548 %s  Current score file name
549 %d  Number of dormant articles
550 %r  Number of articles that have been marked as read in this session
551 %E  Number of articles expunged by the score files"
552   :group 'gnus-summary-format
553   :type 'string)
554
555 (defcustom gnus-list-identifiers nil
556   "Regexp that matches list identifiers to be removed from subject.
557 This can also be a list of regexps."
558   :group 'gnus-summary-format
559   :group 'gnus-article-hiding
560   :type '(choice (const :tag "none" nil)
561                  (regexp :value ".*")
562                  (repeat :value (".*") regexp)))
563
564 (defcustom gnus-summary-mark-below 0
565   "*Mark all articles with a score below this variable as read.
566 This variable is local to each summary buffer and usually set by the
567 score file."
568   :group 'gnus-score-default
569   :type 'integer)
570
571 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
572   "*List of functions used for sorting articles in the summary buffer.
573 This variable is only used when not using a threaded display."
574   :group 'gnus-summary-sort
575   :type '(repeat (choice (function-item gnus-article-sort-by-number)
576                          (function-item gnus-article-sort-by-author)
577                          (function-item gnus-article-sort-by-subject)
578                          (function-item gnus-article-sort-by-date)
579                          (function-item gnus-article-sort-by-score)
580                          (function :tag "other"))))
581
582 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
583   "*List of functions used for sorting threads in the summary buffer.
584 By default, threads are sorted by article number.
585
586 Each function takes two threads and return non-nil if the first thread
587 should be sorted before the other.  If you use more than one function,
588 the primary sort function should be the last.  You should probably
589 always include `gnus-thread-sort-by-number' in the list of sorting
590 functions -- preferably first.
591
592 Ready-made functions include `gnus-thread-sort-by-number',
593 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
594 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
595 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
596   :group 'gnus-summary-sort
597   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
598                          (function-item gnus-thread-sort-by-author)
599                          (function-item gnus-thread-sort-by-subject)
600                          (function-item gnus-thread-sort-by-date)
601                          (function-item gnus-thread-sort-by-score)
602                          (function-item gnus-thread-sort-by-total-score)
603                          (function :tag "other"))))
604
605 (defcustom gnus-thread-score-function '+
606   "*Function used for calculating the total score of a thread.
607
608 The function is called with the scores of the article and each
609 subthread and should then return the score of the thread.
610
611 Some functions you can use are `+', `max', or `min'."
612   :group 'gnus-summary-sort
613   :type 'function)
614
615 (defcustom gnus-summary-expunge-below nil
616   "All articles that have a score less than this variable will be expunged.
617 This variable is local to the summary buffers."
618   :group 'gnus-score-default
619   :type '(choice (const :tag "off" nil)
620                  integer))
621
622 (defcustom gnus-thread-expunge-below nil
623   "All threads that have a total score less than this variable will be expunged.
624 See `gnus-thread-score-function' for en explanation of what a
625 \"thread score\" is.
626
627 This variable is local to the summary buffers."
628   :group 'gnus-threading
629   :group 'gnus-score-default
630   :type '(choice (const :tag "off" nil)
631                  integer))
632
633 (defcustom gnus-summary-mode-hook nil
634   "*A hook for Gnus summary mode.
635 This hook is run before any variables are set in the summary buffer."
636   :group 'gnus-summary-various
637   :type 'hook)
638
639 (defcustom gnus-summary-menu-hook nil
640   "*Hook run after the creation of the summary mode menu."
641   :group 'gnus-summary-visual
642   :type 'hook)
643
644 (defcustom gnus-summary-exit-hook nil
645   "*A hook called on exit from the summary buffer.
646 It will be called with point in the group buffer."
647   :group 'gnus-summary-exit
648   :type 'hook)
649
650 (defcustom gnus-summary-prepare-hook nil
651   "*A hook called after the summary buffer has been generated.
652 If you want to modify the summary buffer, you can use this hook."
653   :group 'gnus-summary-various
654   :type 'hook)
655
656 (defcustom gnus-summary-prepared-hook nil
657   "*A hook called as the last thing after the summary buffer has been generated."
658   :group 'gnus-summary-various
659   :type 'hook)
660
661 (defcustom gnus-summary-generate-hook nil
662   "*A hook run just before generating the summary buffer.
663 This hook is commonly used to customize threading variables and the
664 like."
665   :group 'gnus-summary-various
666   :type 'hook)
667
668 (defcustom gnus-select-group-hook nil
669   "*A hook called when a newsgroup is selected.
670
671 If you'd like to simplify subjects like the
672 `gnus-summary-next-same-subject' command does, you can use the
673 following hook:
674
675  (setq gnus-select-group-hook
676       (list
677         (lambda ()
678           (mapcar (lambda (header)
679                      (mail-header-set-subject
680                       header
681                       (gnus-simplify-subject
682                        (mail-header-subject header) 're-only)))
683                   gnus-newsgroup-headers))))"
684   :group 'gnus-group-select
685   :type 'hook)
686
687 (defcustom gnus-select-article-hook nil
688   "*A hook called when an article is selected."
689   :group 'gnus-summary-choose
690   :type 'hook)
691
692 (defcustom gnus-visual-mark-article-hook
693   (list 'gnus-highlight-selected-summary)
694   "*Hook run after selecting an article in the summary buffer.
695 It is meant to be used for highlighting the article in some way.  It
696 is not run if `gnus-visual' is nil."
697   :group 'gnus-summary-visual
698   :type 'hook)
699
700 (defcustom gnus-parse-headers-hook nil
701   "*A hook called before parsing the headers."
702   :group 'gnus-various
703   :type 'hook)
704
705 (defcustom gnus-exit-group-hook nil
706   "*A hook called when exiting summary mode.
707 This hook is not called from the non-updating exit commands like `Q'."
708   :group 'gnus-various
709   :type 'hook)
710
711 (defcustom gnus-summary-update-hook
712   (list 'gnus-summary-highlight-line)
713   "*A hook called when a summary line is changed.
714 The hook will not be called if `gnus-visual' is nil.
715
716 The default function `gnus-summary-highlight-line' will
717 highlight the line according to the `gnus-summary-highlight'
718 variable."
719   :group 'gnus-summary-visual
720   :type 'hook)
721
722 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
723   "*A hook called when an article is selected for the first time.
724 The hook is intended to mark an article as read (or unread)
725 automatically when it is selected."
726   :group 'gnus-summary-choose
727   :type 'hook)
728
729 (defcustom gnus-group-no-more-groups-hook nil
730   "*A hook run when returning to group mode having no more (unread) groups."
731   :group 'gnus-group-select
732   :type 'hook)
733
734 (defcustom gnus-ps-print-hook nil
735   "*A hook run before ps-printing something from Gnus."
736   :group 'gnus-summary
737   :type 'hook)
738
739 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
740   "Face used for highlighting the current article in the summary buffer."
741   :group 'gnus-summary-visual
742   :type 'face)
743
744 (defcustom gnus-summary-highlight
745   '(((= mark gnus-canceled-mark)
746      . gnus-summary-cancelled-face)
747     ((and (> score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-high-ticked-face)
751     ((and (< score default)
752           (or (= mark gnus-dormant-mark)
753               (= mark gnus-ticked-mark)))
754      . gnus-summary-low-ticked-face)
755     ((or (= mark gnus-dormant-mark)
756          (= mark gnus-ticked-mark))
757      . gnus-summary-normal-ticked-face)
758     ((and (> score default) (= mark gnus-ancient-mark))
759      . gnus-summary-high-ancient-face)
760     ((and (< score default) (= mark gnus-ancient-mark))
761      . gnus-summary-low-ancient-face)
762     ((= mark gnus-ancient-mark)
763      . gnus-summary-normal-ancient-face)
764     ((and (> score default) (= mark gnus-unread-mark))
765      . gnus-summary-high-unread-face)
766     ((and (< score default) (= mark gnus-unread-mark))
767      . gnus-summary-low-unread-face)
768     ((= mark gnus-unread-mark)
769      . gnus-summary-normal-unread-face)
770     ((and (> score default) (memq mark (list gnus-downloadable-mark
771                                              gnus-undownloaded-mark)))
772      . gnus-summary-high-unread-face)
773     ((and (< score default) (memq mark (list gnus-downloadable-mark
774                                              gnus-undownloaded-mark)))
775      . gnus-summary-low-unread-face)
776     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
777      . gnus-summary-normal-unread-face)
778     ((> score default)
779      . gnus-summary-high-read-face)
780     ((< score default)
781      . gnus-summary-low-read-face)
782     (t
783      . gnus-summary-normal-read-face))
784   "*Controls the highlighting of summary buffer lines.
785
786 A list of (FORM . FACE) pairs.  When deciding how a a particular
787 summary line should be displayed, each form is evaluated.  The content
788 of the face field after the first true form is used.  You can change
789 how those summary lines are displayed, by editing the face field.
790
791 You can use the following variables in the FORM field.
792
793 score:   The articles score
794 default: The default article score.
795 below:   The score below which articles are automatically marked as read.
796 mark:    The articles mark."
797   :group 'gnus-summary-visual
798   :type '(repeat (cons (sexp :tag "Form" nil)
799                        face)))
800
801 (defcustom gnus-alter-header-function nil
802   "Function called to allow alteration of article header structures.
803 The function is called with one parameter, the article header vector,
804 which it may alter in any way.")
805
806 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
807   "Variable that says which function should be used to decode a string with encoded words.")
808
809 (defcustom gnus-extra-headers nil
810   "*Extra headers to parse."
811   :group 'gnus-summary
812   :type '(repeat symbol))
813
814 (defcustom gnus-ignored-from-addresses
815   (and user-mail-address (regexp-quote user-mail-address))
816   "*Regexp of From headers that may be suppressed in favor of To headers."
817   :group 'gnus-summary
818   :type 'regexp)
819
820 (defcustom gnus-group-charset-alist
821   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
822     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
823     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
824     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
825     ("^relcom\\>" koi8-r)
826     ("^fido7\\>" koi8-r)
827     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
828     ("^israel\\>" iso-8859-1)
829     ("^han\\>" euc-kr)
830     ("^alt.chinese.text.big5\\>" chinese-big5)
831     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
832     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
833     (".*" iso-8859-1))
834   "Alist of regexps (to match group names) and default charsets to be used when reading."
835   :type '(repeat (list (regexp :tag "Group")
836                        (symbol :tag "Charset")))
837   :group 'gnus-charset)
838
839 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
840   "List of charsets that should be ignored.
841 When these charsets are used in the \"charset\" parameter, the
842 default charset will be used instead."
843   :type '(repeat symbol)
844   :group 'gnus-charset)
845
846 (defcustom gnus-group-ignored-charsets-alist
847   '(("alt\\.chinese\\.text" iso-8859-1))
848   "Alist of regexps (to match group names) and charsets that should be ignored.
849 When these charsets are used in the \"charset\" parameter, the
850 default charset will be used instead."
851   :type '(repeat (cons (regexp :tag "Group")
852                        (repeat symbol)))
853   :group 'gnus-charset)
854
855 (defcustom gnus-group-highlight-words-alist nil
856   "Alist of group regexps and highlight regexps.
857 This variable uses the same syntax as `gnus-emphasis-alist'."
858   :type '(repeat (cons (regexp :tag "Group")
859                        (repeat (list (regexp :tag "Highlight regexp")
860                                      (number :tag "Group for entire word" 0)
861                                      (number :tag "Group for displayed part" 0)
862                                      (symbol :tag "Face"
863                                              gnus-emphasis-highlight-words)))))
864   :group 'gnus-summary-visual)
865
866 (defcustom gnus-summary-show-article-charset-alist
867   nil
868   "Alist of number and charset.
869 The article will be shown with the charset corresponding to the
870 numbered argument.
871 For example: ((1 . cn-gb-2312) (2 . big5))."
872   :type '(repeat (cons (number :tag "Argument" 1)
873                        (symbol :tag "Charset")))
874   :group 'gnus-charset)
875
876 (defcustom gnus-preserve-marks t
877   "Whether marks are preserved when moving, copying and respooling messages."
878   :type 'boolean
879   :group 'gnus-summary-marks)
880
881 (defcustom gnus-alter-articles-to-read-function nil
882   "Function to be called to alter the list of articles to be selected."
883   :type 'function
884   :group 'gnus-summary)
885
886 (defcustom gnus-orphan-score nil
887   "*All orphans get this score added.  Set in the score file."
888   :group 'gnus-score-default
889   :type '(choice (const nil)
890                  integer))
891
892 (defcustom gnus-summary-save-parts-default-mime "image/.*"
893   "*A regexp to match MIME parts when saving multiple parts of a message
894 with gnus-summary-save-parts (X m). This regexp will be used by default
895 when prompting the user for which type of files to save."
896   :group 'gnus-summary
897   :type 'regexp)
898
899
900 ;;; Internal variables
901
902 (defvar gnus-article-mime-handles nil)
903 (defvar gnus-article-decoded-p nil)
904 (defvar gnus-article-charset nil)
905 (defvar gnus-article-ignored-charsets nil)
906 (defvar gnus-scores-exclude-files nil)
907 (defvar gnus-page-broken nil)
908 (defvar gnus-inhibit-mime-unbuttonizing nil)
909
910 (defvar gnus-original-article nil)
911 (defvar gnus-article-internal-prepare-hook nil)
912 (defvar gnus-newsgroup-process-stack nil)
913
914 (defvar gnus-thread-indent-array nil)
915 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
916 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
917   "Function called to sort the articles within a thread after it has been gathered together.")
918
919 (defvar gnus-summary-save-parts-type-history nil)
920 (defvar gnus-summary-save-parts-last-directory nil)
921
922 ;; Avoid highlighting in kill files.
923 (defvar gnus-summary-inhibit-highlight nil)
924 (defvar gnus-newsgroup-selected-overlay nil)
925 (defvar gnus-inhibit-limiting nil)
926 (defvar gnus-newsgroup-adaptive-score-file nil)
927 (defvar gnus-current-score-file nil)
928 (defvar gnus-current-move-group nil)
929 (defvar gnus-current-copy-group nil)
930 (defvar gnus-current-crosspost-group nil)
931
932 (defvar gnus-newsgroup-dependencies nil)
933 (defvar gnus-newsgroup-adaptive nil)
934 (defvar gnus-summary-display-article-function nil)
935 (defvar gnus-summary-highlight-line-function nil
936   "Function called after highlighting a summary line.")
937
938 (defvar gnus-summary-line-format-alist
939   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
940     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
941     (?s gnus-tmp-subject-or-nil ?s)
942     (?n gnus-tmp-name ?s)
943     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
944         ?s)
945     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
946             gnus-tmp-from) ?s)
947     (?F gnus-tmp-from ?s)
948     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
949     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
950     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
951     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
952     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
953     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
954     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
955     (?L gnus-tmp-lines ?d)
956     (?I gnus-tmp-indentation ?s)
957     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
958     (?R gnus-tmp-replied ?c)
959     (?\[ gnus-tmp-opening-bracket ?c)
960     (?\] gnus-tmp-closing-bracket ?c)
961     (?\> (make-string gnus-tmp-level ? ) ?s)
962     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
963     (?i gnus-tmp-score ?d)
964     (?z gnus-tmp-score-char ?c)
965     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
966     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
967     (?U gnus-tmp-unread ?c)
968     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
969     (?t (gnus-summary-number-of-articles-in-thread
970          (and (boundp 'thread) (car thread)) gnus-tmp-level)
971         ?d)
972     (?e (gnus-summary-number-of-articles-in-thread
973          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
974         ?c)
975     (?u gnus-tmp-user-defined ?s)
976     (?P (gnus-pick-line-number) ?d))
977   "An alist of format specifications that can appear in summary lines.
978 These are paired with what variables they correspond with, along with
979 the type of the variable (string, integer, character, etc).")
980
981 (defvar gnus-summary-dummy-line-format-alist
982   `((?S gnus-tmp-subject ?s)
983     (?N gnus-tmp-number ?d)
984     (?u gnus-tmp-user-defined ?s)))
985
986 (defvar gnus-summary-mode-line-format-alist
987   `((?G gnus-tmp-group-name ?s)
988     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
989     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
990     (?A gnus-tmp-article-number ?d)
991     (?Z gnus-tmp-unread-and-unselected ?s)
992     (?V gnus-version ?s)
993     (?U gnus-tmp-unread-and-unticked ?d)
994     (?S gnus-tmp-subject ?s)
995     (?e gnus-tmp-unselected ?d)
996     (?u gnus-tmp-user-defined ?s)
997     (?d (length gnus-newsgroup-dormant) ?d)
998     (?t (length gnus-newsgroup-marked) ?d)
999     (?r (length gnus-newsgroup-reads) ?d)
1000     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1001     (?E gnus-newsgroup-expunged-tally ?d)
1002     (?s (gnus-current-score-file-nondirectory) ?s)))
1003
1004 (defvar gnus-last-search-regexp nil
1005   "Default regexp for article search command.")
1006
1007 (defvar gnus-last-shell-command nil
1008   "Default shell command on article.")
1009
1010 (defvar gnus-newsgroup-begin nil)
1011 (defvar gnus-newsgroup-end nil)
1012 (defvar gnus-newsgroup-last-rmail nil)
1013 (defvar gnus-newsgroup-last-mail nil)
1014 (defvar gnus-newsgroup-last-folder nil)
1015 (defvar gnus-newsgroup-last-file nil)
1016 (defvar gnus-newsgroup-auto-expire nil)
1017 (defvar gnus-newsgroup-active nil)
1018
1019 (defvar gnus-newsgroup-data nil)
1020 (defvar gnus-newsgroup-data-reverse nil)
1021 (defvar gnus-newsgroup-limit nil)
1022 (defvar gnus-newsgroup-limits nil)
1023
1024 (defvar gnus-newsgroup-unreads nil
1025   "List of unread articles in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-unselected nil
1028   "List of unselected unread articles in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-reads nil
1031   "Alist of read articles and article marks in the current newsgroup.")
1032
1033 (defvar gnus-newsgroup-expunged-tally nil)
1034
1035 (defvar gnus-newsgroup-marked nil
1036   "List of ticked articles in the current newsgroup (a subset of unread art).")
1037
1038 (defvar gnus-newsgroup-killed nil
1039   "List of ranges of articles that have been through the scoring process.")
1040
1041 (defvar gnus-newsgroup-cached nil
1042   "List of articles that come from the article cache.")
1043
1044 (defvar gnus-newsgroup-saved nil
1045   "List of articles that have been saved.")
1046
1047 (defvar gnus-newsgroup-kill-headers nil)
1048
1049 (defvar gnus-newsgroup-replied nil
1050   "List of articles that have been replied to in the current newsgroup.")
1051
1052 (defvar gnus-newsgroup-expirable nil
1053   "List of articles in the current newsgroup that can be expired.")
1054
1055 (defvar gnus-newsgroup-processable nil
1056   "List of articles in the current newsgroup that can be processed.")
1057
1058 (defvar gnus-newsgroup-downloadable nil
1059   "List of articles in the current newsgroup that can be processed.")
1060
1061 (defvar gnus-newsgroup-undownloaded nil
1062   "List of articles in the current newsgroup that haven't been downloaded..")
1063
1064 (defvar gnus-newsgroup-unsendable nil
1065   "List of articles in the current newsgroup that won't be sent.")
1066
1067 (defvar gnus-newsgroup-bookmarks nil
1068   "List of articles in the current newsgroup that have bookmarks.")
1069
1070 (defvar gnus-newsgroup-dormant nil
1071   "List of dormant articles in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-scored nil
1074   "List of scored articles in the current newsgroup.")
1075
1076 (defvar gnus-newsgroup-headers nil
1077   "List of article headers in the current newsgroup.")
1078
1079 (defvar gnus-newsgroup-threads nil)
1080
1081 (defvar gnus-newsgroup-prepared nil
1082   "Whether the current group has been prepared properly.")
1083
1084 (defvar gnus-newsgroup-ancient nil
1085   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1086
1087 (defvar gnus-newsgroup-sparse nil)
1088
1089 (defvar gnus-current-article nil)
1090 (defvar gnus-article-current nil)
1091 (defvar gnus-current-headers nil)
1092 (defvar gnus-have-all-headers nil)
1093 (defvar gnus-last-article nil)
1094 (defvar gnus-newsgroup-history nil)
1095 (defvar gnus-newsgroup-charset nil)
1096 (defvar gnus-newsgroup-ephemeral-charset nil)
1097 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1098
1099 (defvar gnus-article-before-search nil)
1100
1101 (defconst gnus-summary-local-variables
1102   '(gnus-newsgroup-name
1103     gnus-newsgroup-begin gnus-newsgroup-end
1104     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1105     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1106     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1107     gnus-newsgroup-unselected gnus-newsgroup-marked
1108     gnus-newsgroup-reads gnus-newsgroup-saved
1109     gnus-newsgroup-replied gnus-newsgroup-expirable
1110     gnus-newsgroup-processable gnus-newsgroup-killed
1111     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1112     gnus-newsgroup-unsendable
1113     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1114     gnus-newsgroup-headers gnus-newsgroup-threads
1115     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1116     gnus-current-article gnus-current-headers gnus-have-all-headers
1117     gnus-last-article gnus-article-internal-prepare-hook
1118     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1119     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1120     gnus-thread-expunge-below
1121     gnus-score-alist gnus-current-score-file
1122     (gnus-summary-expunge-below . global)
1123     (gnus-summary-mark-below . global)
1124     (gnus-orphan-score . global)
1125     gnus-newsgroup-active gnus-scores-exclude-files
1126     gnus-newsgroup-history gnus-newsgroup-ancient
1127     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1128     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1129     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1130     (gnus-newsgroup-expunged-tally . 0)
1131     gnus-cache-removable-articles gnus-newsgroup-cached
1132     gnus-newsgroup-data gnus-newsgroup-data-reverse
1133     gnus-newsgroup-limit gnus-newsgroup-limits
1134     gnus-newsgroup-charset)
1135   "Variables that are buffer-local to the summary buffers.")
1136
1137 (defvar gnus-newsgroup-variables nil
1138   "Variables that have separate values in the newsgroups.")
1139
1140 ;; Byte-compiler warning.
1141 (defvar gnus-article-mode-map)
1142
1143 ;; MIME stuff.
1144
1145 (defvar gnus-decode-encoded-word-methods
1146   '(mail-decode-encoded-word-string)
1147   "List of methods used to decode encoded words.
1148
1149 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1150 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1151 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1152 whose names match REGEXP.
1153
1154 For example:
1155 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1156  mail-decode-encoded-word-string
1157  (\"chinese\" . rfc1843-decode-string))")
1158
1159 (defvar gnus-decode-encoded-word-methods-cache nil)
1160
1161 (defun gnus-multi-decode-encoded-word-string (string)
1162   "Apply the functions from `gnus-encoded-word-methods' that match."
1163   (unless (and gnus-decode-encoded-word-methods-cache
1164                (eq gnus-newsgroup-name
1165                    (car gnus-decode-encoded-word-methods-cache)))
1166     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1167     (mapcar (lambda (x)
1168               (if (symbolp x)
1169                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1170                 (if (and gnus-newsgroup-name
1171                          (string-match (car x) gnus-newsgroup-name))
1172                     (nconc gnus-decode-encoded-word-methods-cache
1173                            (list (cdr x))))))
1174           gnus-decode-encoded-word-methods))
1175   (let ((xlist gnus-decode-encoded-word-methods-cache))
1176     (pop xlist)
1177     (while xlist
1178       (setq string (funcall (pop xlist) string))))
1179   string)
1180
1181 ;; Subject simplification.
1182
1183 (defun gnus-simplify-whitespace (str)
1184   "Remove excessive whitespace from STR."
1185   (let ((mystr str))
1186     ;; Multiple spaces.
1187     (while (string-match "[ \t][ \t]+" mystr)
1188       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1189                           " "
1190                           (substring mystr (match-end 0)))))
1191     ;; Leading spaces.
1192     (when (string-match "^[ \t]+" mystr)
1193       (setq mystr (substring mystr (match-end 0))))
1194     ;; Trailing spaces.
1195     (when (string-match "[ \t]+$" mystr)
1196       (setq mystr (substring mystr 0 (match-beginning 0))))
1197     mystr))
1198
1199 (defsubst gnus-simplify-subject-re (subject)
1200   "Remove \"Re:\" from subject lines."
1201   (if (string-match "^[Rr][Ee]: *" subject)
1202       (substring subject (match-end 0))
1203     subject))
1204
1205 (defun gnus-simplify-subject (subject &optional re-only)
1206   "Remove `Re:' and words in parentheses.
1207 If RE-ONLY is non-nil, strip leading `Re:'s only."
1208   (let ((case-fold-search t))           ;Ignore case.
1209     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1210     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1211       (setq subject (substring subject (match-end 0))))
1212     ;; Remove uninteresting prefixes.
1213     (when (and (not re-only)
1214                gnus-simplify-ignored-prefixes
1215                (string-match gnus-simplify-ignored-prefixes subject))
1216       (setq subject (substring subject (match-end 0))))
1217     ;; Remove words in parentheses from end.
1218     (unless re-only
1219       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1220         (setq subject (substring subject 0 (match-beginning 0)))))
1221     ;; Return subject string.
1222     subject))
1223
1224 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1225 ;; all whitespace.
1226 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1227   (goto-char (point-min))
1228   (while (re-search-forward regexp nil t)
1229     (replace-match (or newtext ""))))
1230
1231 (defun gnus-simplify-buffer-fuzzy ()
1232   "Simplify string in the buffer fuzzily.
1233 The string in the accessible portion of the current buffer is simplified.
1234 It is assumed to be a single-line subject.
1235 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1236 matter is removed.  Additional things can be deleted by setting
1237 `gnus-simplify-subject-fuzzy-regexp'."
1238   (let ((case-fold-search t)
1239         (modified-tick))
1240     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1241
1242     (while (not (eq modified-tick (buffer-modified-tick)))
1243       (setq modified-tick (buffer-modified-tick))
1244       (cond
1245        ((listp gnus-simplify-subject-fuzzy-regexp)
1246         (mapcar 'gnus-simplify-buffer-fuzzy-step
1247                 gnus-simplify-subject-fuzzy-regexp))
1248        (gnus-simplify-subject-fuzzy-regexp
1249         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1250       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1251       (gnus-simplify-buffer-fuzzy-step
1252        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1253       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1254
1255     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1256     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1257     (gnus-simplify-buffer-fuzzy-step " $")
1258     (gnus-simplify-buffer-fuzzy-step "^ +")))
1259
1260 (defun gnus-simplify-subject-fuzzy (subject)
1261   "Simplify a subject string fuzzily.
1262 See `gnus-simplify-buffer-fuzzy' for details."
1263   (save-excursion
1264     (gnus-set-work-buffer)
1265     (let ((case-fold-search t))
1266       ;; Remove uninteresting prefixes.
1267       (when (and gnus-simplify-ignored-prefixes
1268                  (string-match gnus-simplify-ignored-prefixes subject))
1269         (setq subject (substring subject (match-end 0))))
1270       (insert subject)
1271       (inline (gnus-simplify-buffer-fuzzy))
1272       (buffer-string))))
1273
1274 (defsubst gnus-simplify-subject-fully (subject)
1275   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1276   (cond
1277    (gnus-simplify-subject-functions
1278     (gnus-map-function gnus-simplify-subject-functions subject))
1279    ((null gnus-summary-gather-subject-limit)
1280     (gnus-simplify-subject-re subject))
1281    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1282     (gnus-simplify-subject-fuzzy subject))
1283    ((numberp gnus-summary-gather-subject-limit)
1284     (gnus-limit-string (gnus-simplify-subject-re subject)
1285                        gnus-summary-gather-subject-limit))
1286    (t
1287     subject)))
1288
1289 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1290   "Check whether two subjects are equal.
1291 If optional argument simple-first is t, first argument is already
1292 simplified."
1293   (cond
1294    ((null simple-first)
1295     (equal (gnus-simplify-subject-fully s1)
1296            (gnus-simplify-subject-fully s2)))
1297    (t
1298     (equal s1
1299            (gnus-simplify-subject-fully s2)))))
1300
1301 (defun gnus-summary-bubble-group ()
1302   "Increase the score of the current group.
1303 This is a handy function to add to `gnus-summary-exit-hook' to
1304 increase the score of each group you read."
1305   (gnus-group-add-score gnus-newsgroup-name))
1306
1307 \f
1308 ;;;
1309 ;;; Gnus summary mode
1310 ;;;
1311
1312 (put 'gnus-summary-mode 'mode-class 'special)
1313
1314 (when t
1315   ;; Non-orthogonal keys
1316
1317   (gnus-define-keys gnus-summary-mode-map
1318     " " gnus-summary-next-page
1319     "\177" gnus-summary-prev-page
1320     [delete] gnus-summary-prev-page
1321     [backspace] gnus-summary-prev-page
1322     "\r" gnus-summary-scroll-up
1323     "\M-\r" gnus-summary-scroll-down
1324     "n" gnus-summary-next-unread-article
1325     "p" gnus-summary-prev-unread-article
1326     "N" gnus-summary-next-article
1327     "P" gnus-summary-prev-article
1328     "\M-\C-n" gnus-summary-next-same-subject
1329     "\M-\C-p" gnus-summary-prev-same-subject
1330     "\M-n" gnus-summary-next-unread-subject
1331     "\M-p" gnus-summary-prev-unread-subject
1332     "." gnus-summary-first-unread-article
1333     "," gnus-summary-best-unread-article
1334     "\M-s" gnus-summary-search-article-forward
1335     "\M-r" gnus-summary-search-article-backward
1336     "<" gnus-summary-beginning-of-article
1337     ">" gnus-summary-end-of-article
1338     "j" gnus-summary-goto-article
1339     "^" gnus-summary-refer-parent-article
1340     "\M-^" gnus-summary-refer-article
1341     "u" gnus-summary-tick-article-forward
1342     "!" gnus-summary-tick-article-forward
1343     "U" gnus-summary-tick-article-backward
1344     "d" gnus-summary-mark-as-read-forward
1345     "D" gnus-summary-mark-as-read-backward
1346     "E" gnus-summary-mark-as-expirable
1347     "\M-u" gnus-summary-clear-mark-forward
1348     "\M-U" gnus-summary-clear-mark-backward
1349     "k" gnus-summary-kill-same-subject-and-select
1350     "\C-k" gnus-summary-kill-same-subject
1351     "\M-\C-k" gnus-summary-kill-thread
1352     "\M-\C-l" gnus-summary-lower-thread
1353     "e" gnus-summary-edit-article
1354     "#" gnus-summary-mark-as-processable
1355     "\M-#" gnus-summary-unmark-as-processable
1356     "\M-\C-t" gnus-summary-toggle-threads
1357     "\M-\C-s" gnus-summary-show-thread
1358     "\M-\C-h" gnus-summary-hide-thread
1359     "\M-\C-f" gnus-summary-next-thread
1360     "\M-\C-b" gnus-summary-prev-thread
1361     [(meta down)] gnus-summary-next-thread
1362     [(meta up)] gnus-summary-prev-thread
1363     "\M-\C-u" gnus-summary-up-thread
1364     "\M-\C-d" gnus-summary-down-thread
1365     "&" gnus-summary-execute-command
1366     "c" gnus-summary-catchup-and-exit
1367     "\C-w" gnus-summary-mark-region-as-read
1368     "\C-t" gnus-summary-toggle-truncation
1369     "?" gnus-summary-mark-as-dormant
1370     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1371     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1372     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1373     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1374     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1375     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1376     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1377     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1378     "=" gnus-summary-expand-window
1379     "\C-x\C-s" gnus-summary-reselect-current-group
1380     "\M-g" gnus-summary-rescan-group
1381     "w" gnus-summary-stop-page-breaking
1382     "\C-c\C-r" gnus-summary-caesar-message
1383     "f" gnus-summary-followup
1384     "F" gnus-summary-followup-with-original
1385     "C" gnus-summary-cancel-article
1386     "r" gnus-summary-reply
1387     "R" gnus-summary-reply-with-original
1388     "\C-c\C-f" gnus-summary-mail-forward
1389     "o" gnus-summary-save-article
1390     "\C-o" gnus-summary-save-article-mail
1391     "|" gnus-summary-pipe-output
1392     "\M-k" gnus-summary-edit-local-kill
1393     "\M-K" gnus-summary-edit-global-kill
1394     ;; "V" gnus-version
1395     "\C-c\C-d" gnus-summary-describe-group
1396     "q" gnus-summary-exit
1397     "Q" gnus-summary-exit-no-update
1398     "\C-c\C-i" gnus-info-find-node
1399     gnus-mouse-2 gnus-mouse-pick-article
1400     "m" gnus-summary-mail-other-window
1401     "a" gnus-summary-post-news
1402     "x" gnus-summary-limit-to-unread
1403     "s" gnus-summary-isearch-article
1404     "t" gnus-summary-toggle-header
1405     "g" gnus-summary-show-article
1406     "l" gnus-summary-goto-last-article
1407     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1408     "\C-d" gnus-summary-enter-digest-group
1409     "\M-\C-d" gnus-summary-read-document
1410     "\M-\C-e" gnus-summary-edit-parameters
1411     "\M-\C-a" gnus-summary-customize-parameters
1412     "\C-c\C-b" gnus-bug
1413     "*" gnus-cache-enter-article
1414     "\M-*" gnus-cache-remove-article
1415     "\M-&" gnus-summary-universal-argument
1416     "\C-l" gnus-recenter
1417     "I" gnus-summary-increase-score
1418     "L" gnus-summary-lower-score
1419     "\M-i" gnus-symbolic-argument
1420     "h" gnus-summary-select-article-buffer
1421
1422     "b" gnus-article-view-part
1423     "\M-t" gnus-summary-toggle-display-buttonized
1424
1425     "V" gnus-summary-score-map
1426     "X" gnus-uu-extract-map
1427     "S" gnus-summary-send-map)
1428
1429   ;; Sort of orthogonal keymap
1430   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1431     "t" gnus-summary-tick-article-forward
1432     "!" gnus-summary-tick-article-forward
1433     "d" gnus-summary-mark-as-read-forward
1434     "r" gnus-summary-mark-as-read-forward
1435     "c" gnus-summary-clear-mark-forward
1436     " " gnus-summary-clear-mark-forward
1437     "e" gnus-summary-mark-as-expirable
1438     "x" gnus-summary-mark-as-expirable
1439     "?" gnus-summary-mark-as-dormant
1440     "b" gnus-summary-set-bookmark
1441     "B" gnus-summary-remove-bookmark
1442     "#" gnus-summary-mark-as-processable
1443     "\M-#" gnus-summary-unmark-as-processable
1444     "S" gnus-summary-limit-include-expunged
1445     "C" gnus-summary-catchup
1446     "H" gnus-summary-catchup-to-here
1447     "\C-c" gnus-summary-catchup-all
1448     "k" gnus-summary-kill-same-subject-and-select
1449     "K" gnus-summary-kill-same-subject
1450     "P" gnus-uu-mark-map)
1451
1452   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1453     "c" gnus-summary-clear-above
1454     "u" gnus-summary-tick-above
1455     "m" gnus-summary-mark-above
1456     "k" gnus-summary-kill-below)
1457
1458   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1459     "/" gnus-summary-limit-to-subject
1460     "n" gnus-summary-limit-to-articles
1461     "w" gnus-summary-pop-limit
1462     "s" gnus-summary-limit-to-subject
1463     "a" gnus-summary-limit-to-author
1464     "u" gnus-summary-limit-to-unread
1465     "m" gnus-summary-limit-to-marks
1466     "M" gnus-summary-limit-exclude-marks
1467     "v" gnus-summary-limit-to-score
1468     "*" gnus-summary-limit-include-cached
1469     "D" gnus-summary-limit-include-dormant
1470     "T" gnus-summary-limit-include-thread
1471     "d" gnus-summary-limit-exclude-dormant
1472     "t" gnus-summary-limit-to-age
1473     "x" gnus-summary-limit-to-extra
1474     "E" gnus-summary-limit-include-expunged
1475     "c" gnus-summary-limit-exclude-childless-dormant
1476     "C" gnus-summary-limit-mark-excluded-as-read)
1477
1478   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1479     "n" gnus-summary-next-unread-article
1480     "p" gnus-summary-prev-unread-article
1481     "N" gnus-summary-next-article
1482     "P" gnus-summary-prev-article
1483     "\C-n" gnus-summary-next-same-subject
1484     "\C-p" gnus-summary-prev-same-subject
1485     "\M-n" gnus-summary-next-unread-subject
1486     "\M-p" gnus-summary-prev-unread-subject
1487     "f" gnus-summary-first-unread-article
1488     "b" gnus-summary-best-unread-article
1489     "j" gnus-summary-goto-article
1490     "g" gnus-summary-goto-subject
1491     "l" gnus-summary-goto-last-article
1492     "o" gnus-summary-pop-article)
1493
1494   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1495     "k" gnus-summary-kill-thread
1496     "l" gnus-summary-lower-thread
1497     "i" gnus-summary-raise-thread
1498     "T" gnus-summary-toggle-threads
1499     "t" gnus-summary-rethread-current
1500     "^" gnus-summary-reparent-thread
1501     "s" gnus-summary-show-thread
1502     "S" gnus-summary-show-all-threads
1503     "h" gnus-summary-hide-thread
1504     "H" gnus-summary-hide-all-threads
1505     "n" gnus-summary-next-thread
1506     "p" gnus-summary-prev-thread
1507     "u" gnus-summary-up-thread
1508     "o" gnus-summary-top-thread
1509     "d" gnus-summary-down-thread
1510     "#" gnus-uu-mark-thread
1511     "\M-#" gnus-uu-unmark-thread)
1512
1513   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1514     "g" gnus-summary-prepare
1515     "c" gnus-summary-insert-cached-articles)
1516
1517   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1518     "c" gnus-summary-catchup-and-exit
1519     "C" gnus-summary-catchup-all-and-exit
1520     "E" gnus-summary-exit-no-update
1521     "Q" gnus-summary-exit
1522     "Z" gnus-summary-exit
1523     "n" gnus-summary-catchup-and-goto-next-group
1524     "R" gnus-summary-reselect-current-group
1525     "G" gnus-summary-rescan-group
1526     "N" gnus-summary-next-group
1527     "s" gnus-summary-save-newsrc
1528     "P" gnus-summary-prev-group)
1529
1530   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1531     " " gnus-summary-next-page
1532     "n" gnus-summary-next-page
1533     "\177" gnus-summary-prev-page
1534     [delete] gnus-summary-prev-page
1535     "p" gnus-summary-prev-page
1536     "\r" gnus-summary-scroll-up
1537     "\M-\r" gnus-summary-scroll-down
1538     "<" gnus-summary-beginning-of-article
1539     ">" gnus-summary-end-of-article
1540     "b" gnus-summary-beginning-of-article
1541     "e" gnus-summary-end-of-article
1542     "^" gnus-summary-refer-parent-article
1543     "r" gnus-summary-refer-parent-article
1544     "D" gnus-summary-enter-digest-group
1545     "R" gnus-summary-refer-references
1546     "T" gnus-summary-refer-thread
1547     "g" gnus-summary-show-article
1548     "s" gnus-summary-isearch-article
1549     "P" gnus-summary-print-article
1550     "M" gnus-mailing-list-insinuate
1551     "t" gnus-article-babel)
1552
1553   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1554     "b" gnus-article-add-buttons
1555     "B" gnus-article-add-buttons-to-head
1556     "o" gnus-article-treat-overstrike
1557     "e" gnus-article-emphasize
1558     "w" gnus-article-fill-cited-article
1559     "Q" gnus-article-fill-long-lines
1560     "C" gnus-article-capitalize-sentences
1561     "c" gnus-article-remove-cr
1562     "q" gnus-article-de-quoted-unreadable
1563     "6" gnus-article-de-base64-unreadable
1564     "Z" gnus-article-decode-HZ
1565     "h" gnus-article-wash-html
1566     "s" gnus-summary-force-verify-and-decrypt
1567     "f" gnus-article-display-x-face
1568     "l" gnus-summary-stop-page-breaking
1569     "r" gnus-summary-caesar-message
1570     "t" gnus-summary-toggle-header
1571     "v" gnus-summary-verbose-headers
1572     "H" gnus-article-strip-headers-in-body
1573     "p" gnus-article-verify-x-pgp-sig
1574     "d" gnus-article-treat-dumbquotes)
1575
1576   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1577     "a" gnus-article-hide
1578     "h" gnus-article-hide-headers
1579     "b" gnus-article-hide-boring-headers
1580     "s" gnus-article-hide-signature
1581     "c" gnus-article-hide-citation
1582     "C" gnus-article-hide-citation-in-followups
1583     "l" gnus-article-hide-list-identifiers
1584     "p" gnus-article-hide-pgp
1585     "B" gnus-article-strip-banner
1586     "P" gnus-article-hide-pem
1587     "\C-c" gnus-article-hide-citation-maybe)
1588
1589   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1590     "a" gnus-article-highlight
1591     "h" gnus-article-highlight-headers
1592     "c" gnus-article-highlight-citation
1593     "s" gnus-article-highlight-signature)
1594
1595   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1596     "w" gnus-article-decode-mime-words
1597     "c" gnus-article-decode-charset
1598     "v" gnus-mime-view-all-parts
1599     "b" gnus-article-view-part)
1600
1601   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1602     "z" gnus-article-date-ut
1603     "u" gnus-article-date-ut
1604     "l" gnus-article-date-local
1605     "e" gnus-article-date-lapsed
1606     "o" gnus-article-date-original
1607     "i" gnus-article-date-iso8601
1608     "s" gnus-article-date-user)
1609
1610   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1611     "t" gnus-article-remove-trailing-blank-lines
1612     "l" gnus-article-strip-leading-blank-lines
1613     "m" gnus-article-strip-multiple-blank-lines
1614     "a" gnus-article-strip-blank-lines
1615     "A" gnus-article-strip-all-blank-lines
1616     "s" gnus-article-strip-leading-space
1617     "e" gnus-article-strip-trailing-space)
1618
1619   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1620     "v" gnus-version
1621     "f" gnus-summary-fetch-faq
1622     "d" gnus-summary-describe-group
1623     "h" gnus-summary-describe-briefly
1624     "i" gnus-info-find-node)
1625
1626   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1627     "e" gnus-summary-expire-articles
1628     "\M-\C-e" gnus-summary-expire-articles-now
1629     "\177" gnus-summary-delete-article
1630     [delete] gnus-summary-delete-article
1631     [backspace] gnus-summary-delete-article
1632     "m" gnus-summary-move-article
1633     "r" gnus-summary-respool-article
1634     "w" gnus-summary-edit-article
1635     "c" gnus-summary-copy-article
1636     "B" gnus-summary-crosspost-article
1637     "q" gnus-summary-respool-query
1638     "t" gnus-summary-respool-trace
1639     "i" gnus-summary-import-article
1640     "p" gnus-summary-article-posted-p)
1641
1642   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1643     "o" gnus-summary-save-article
1644     "m" gnus-summary-save-article-mail
1645     "F" gnus-summary-write-article-file
1646     "r" gnus-summary-save-article-rmail
1647     "f" gnus-summary-save-article-file
1648     "b" gnus-summary-save-article-body-file
1649     "h" gnus-summary-save-article-folder
1650     "v" gnus-summary-save-article-vm
1651     "p" gnus-summary-pipe-output
1652     "s" gnus-soup-add-article)
1653
1654   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1655     "b" gnus-summary-display-buttonized
1656     "m" gnus-summary-repair-multipart
1657     "v" gnus-article-view-part
1658     "o" gnus-article-save-part
1659     "c" gnus-article-copy-part
1660     "C" gnus-article-view-part-as-charset
1661     "e" gnus-article-externalize-part
1662     "E" gnus-article-encrypt-body
1663     "i" gnus-article-inline-part
1664     "|" gnus-article-pipe-part))
1665
1666 (defun gnus-summary-make-menu-bar ()
1667   (gnus-turn-off-edit-menu 'summary)
1668
1669   (unless (boundp 'gnus-summary-misc-menu)
1670
1671     (easy-menu-define
1672      gnus-summary-kill-menu gnus-summary-mode-map ""
1673      (cons
1674       "Score"
1675       (nconc
1676        (list
1677         ["Enter score..." gnus-summary-score-entry t]
1678         ["Customize" gnus-score-customize t])
1679        (gnus-make-score-map 'increase)
1680        (gnus-make-score-map 'lower)
1681        '(("Mark"
1682           ["Kill below" gnus-summary-kill-below t]
1683           ["Mark above" gnus-summary-mark-above t]
1684           ["Tick above" gnus-summary-tick-above t]
1685           ["Clear above" gnus-summary-clear-above t])
1686          ["Current score" gnus-summary-current-score t]
1687          ["Set score" gnus-summary-set-score t]
1688          ["Switch current score file..." gnus-score-change-score-file t]
1689          ["Set mark below..." gnus-score-set-mark-below t]
1690          ["Set expunge below..." gnus-score-set-expunge-below t]
1691          ["Edit current score file" gnus-score-edit-current-scores t]
1692          ["Edit score file" gnus-score-edit-file t]
1693          ["Trace score" gnus-score-find-trace t]
1694          ["Find words" gnus-score-find-favourite-words t]
1695          ["Rescore buffer" gnus-summary-rescore t]
1696          ["Increase score..." gnus-summary-increase-score t]
1697          ["Lower score..." gnus-summary-lower-score t]))))
1698
1699     ;; Define both the Article menu in the summary buffer and the equivalent
1700     ;; Commands menu in the article buffer here for consistency.
1701     (let ((innards
1702            '(("Hide"
1703               ["All" gnus-article-hide t]
1704               ["Headers" gnus-article-hide-headers t]
1705               ["Signature" gnus-article-hide-signature t]
1706               ["Citation" gnus-article-hide-citation t]
1707               ["List identifiers" gnus-article-hide-list-identifiers t]
1708               ["PGP" gnus-article-hide-pgp t]
1709               ["Banner" gnus-article-strip-banner t]
1710               ["Boring headers" gnus-article-hide-boring-headers t])
1711              ("Highlight"
1712               ["All" gnus-article-highlight t]
1713               ["Headers" gnus-article-highlight-headers t]
1714               ["Signature" gnus-article-highlight-signature t]
1715               ["Citation" gnus-article-highlight-citation t])
1716              ("MIME"
1717               ["Words" gnus-article-decode-mime-words t]
1718               ["Charset" gnus-article-decode-charset t]
1719               ["QP" gnus-article-de-quoted-unreadable t]
1720               ["Base64" gnus-article-de-base64-unreadable t]
1721               ["View all" gnus-mime-view-all-parts t]
1722               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1723               ["Encrypt body" gnus-article-encrypt-body t])
1724              ("Date"
1725               ["Local" gnus-article-date-local t]
1726               ["ISO8601" gnus-article-date-iso8601 t]
1727               ["UT" gnus-article-date-ut t]
1728               ["Original" gnus-article-date-original t]
1729               ["Lapsed" gnus-article-date-lapsed t]
1730               ["User-defined" gnus-article-date-user t])
1731              ("Washing"
1732               ("Remove Blanks"
1733                ["Leading" gnus-article-strip-leading-blank-lines t]
1734                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1735                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1736                ["All of the above" gnus-article-strip-blank-lines t]
1737                ["All" gnus-article-strip-all-blank-lines t]
1738                ["Leading space" gnus-article-strip-leading-space t]
1739                ["Trailing space" gnus-article-strip-trailing-space t])
1740               ["Overstrike" gnus-article-treat-overstrike t]
1741               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1742               ["Emphasis" gnus-article-emphasize t]
1743               ["Word wrap" gnus-article-fill-cited-article t]
1744               ["Fill long lines" gnus-article-fill-long-lines t]
1745               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1746               ["CR" gnus-article-remove-cr t]
1747               ["Show X-Face" gnus-article-display-x-face t]
1748               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1749               ["Base64" gnus-article-de-base64-unreadable t]
1750               ["Rot 13" gnus-summary-caesar-message t]
1751               ["Unix pipe" gnus-summary-pipe-message t]
1752               ["Add buttons" gnus-article-add-buttons t]
1753               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1754               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1755               ["Verbose header" gnus-summary-verbose-headers t]
1756               ["Toggle header" gnus-summary-toggle-header t]
1757               ["Html" gnus-article-wash-html t]
1758               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1759               ["HZ" gnus-article-decode-HZ t])
1760              ("Output"
1761               ["Save in default format" gnus-summary-save-article t]
1762               ["Save in file" gnus-summary-save-article-file t]
1763               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1764               ["Save in MH folder" gnus-summary-save-article-folder t]
1765               ["Save in VM folder" gnus-summary-save-article-vm t]
1766               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1767               ["Save body in file" gnus-summary-save-article-body-file t]
1768               ["Pipe through a filter" gnus-summary-pipe-output t]
1769               ["Add to SOUP packet" gnus-soup-add-article t]
1770               ["Print" gnus-summary-print-article t])
1771              ("Backend"
1772               ["Respool article..." gnus-summary-respool-article t]
1773               ["Move article..." gnus-summary-move-article
1774                (gnus-check-backend-function
1775                 'request-move-article gnus-newsgroup-name)]
1776               ["Copy article..." gnus-summary-copy-article t]
1777               ["Crosspost article..." gnus-summary-crosspost-article
1778                (gnus-check-backend-function
1779                 'request-replace-article gnus-newsgroup-name)]
1780               ["Import file..." gnus-summary-import-article t]
1781               ["Check if posted" gnus-summary-article-posted-p t]
1782               ["Edit article" gnus-summary-edit-article
1783                (not (gnus-group-read-only-p))]
1784               ["Delete article" gnus-summary-delete-article
1785                (gnus-check-backend-function
1786                 'request-expire-articles gnus-newsgroup-name)]
1787               ["Query respool" gnus-summary-respool-query t]
1788               ["Trace respool" gnus-summary-respool-trace t]
1789               ["Delete expirable articles" gnus-summary-expire-articles-now
1790                (gnus-check-backend-function
1791                 'request-expire-articles gnus-newsgroup-name)])
1792              ("Extract"
1793               ["Uudecode" gnus-uu-decode-uu t]
1794               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1795               ["Unshar" gnus-uu-decode-unshar t]
1796               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1797               ["Save" gnus-uu-decode-save t]
1798               ["Binhex" gnus-uu-decode-binhex t]
1799               ["Postscript" gnus-uu-decode-postscript t])
1800              ("Cache"
1801               ["Enter article" gnus-cache-enter-article t]
1802               ["Remove article" gnus-cache-remove-article t])
1803              ["Translate" gnus-article-babel t]
1804              ["Select article buffer" gnus-summary-select-article-buffer t]
1805              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1806              ["Isearch article..." gnus-summary-isearch-article t]
1807              ["Beginning of the article" gnus-summary-beginning-of-article t]
1808              ["End of the article" gnus-summary-end-of-article t]
1809              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1810              ["Fetch referenced articles" gnus-summary-refer-references t]
1811              ["Fetch current thread" gnus-summary-refer-thread t]
1812              ["Fetch article with id..." gnus-summary-refer-article t]
1813              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1814              ["Redisplay" gnus-summary-show-article t])))
1815       (easy-menu-define
1816        gnus-summary-article-menu gnus-summary-mode-map ""
1817        (cons "Article" innards))
1818
1819       (easy-menu-define
1820        gnus-article-commands-menu gnus-article-mode-map ""
1821        (cons "Commands" innards)))
1822
1823     (easy-menu-define
1824      gnus-summary-thread-menu gnus-summary-mode-map ""
1825      '("Threads"
1826        ["Toggle threading" gnus-summary-toggle-threads t]
1827        ["Hide threads" gnus-summary-hide-all-threads t]
1828        ["Show threads" gnus-summary-show-all-threads t]
1829        ["Hide thread" gnus-summary-hide-thread t]
1830        ["Show thread" gnus-summary-show-thread t]
1831        ["Go to next thread" gnus-summary-next-thread t]
1832        ["Go to previous thread" gnus-summary-prev-thread t]
1833        ["Go down thread" gnus-summary-down-thread t]
1834        ["Go up thread" gnus-summary-up-thread t]
1835        ["Top of thread" gnus-summary-top-thread t]
1836        ["Mark thread as read" gnus-summary-kill-thread t]
1837        ["Lower thread score" gnus-summary-lower-thread t]
1838        ["Raise thread score" gnus-summary-raise-thread t]
1839        ["Rethread current" gnus-summary-rethread-current t]))
1840
1841     (easy-menu-define
1842      gnus-summary-post-menu gnus-summary-mode-map ""
1843      '("Post"
1844        ["Post an article" gnus-summary-post-news t]
1845        ["Followup" gnus-summary-followup t]
1846        ["Followup and yank" gnus-summary-followup-with-original t]
1847        ["Supersede article" gnus-summary-supersede-article t]
1848        ["Cancel article" gnus-summary-cancel-article t]
1849        ["Reply" gnus-summary-reply t]
1850        ["Reply and yank" gnus-summary-reply-with-original t]
1851        ["Wide reply" gnus-summary-wide-reply t]
1852        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1853        ["Mail forward" gnus-summary-mail-forward t]
1854        ["Post forward" gnus-summary-post-forward t]
1855        ["Digest and mail" gnus-uu-digest-mail-forward t]
1856        ["Digest and post" gnus-uu-digest-post-forward t]
1857        ["Resend message" gnus-summary-resend-message t]
1858        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1859        ["Send a mail" gnus-summary-mail-other-window t]
1860        ["Uuencode and post" gnus-uu-post-news t]
1861        ["Followup via news" gnus-summary-followup-to-mail t]
1862        ["Followup via news and yank"
1863         gnus-summary-followup-to-mail-with-original t]
1864        ;;("Draft"
1865        ;;["Send" gnus-summary-send-draft t]
1866        ;;["Send bounced" gnus-resend-bounced-mail t])
1867        ))
1868
1869     (easy-menu-define
1870      gnus-summary-misc-menu gnus-summary-mode-map ""
1871      '("Misc"
1872        ("Mark Read"
1873         ["Mark as read" gnus-summary-mark-as-read-forward t]
1874         ["Mark same subject and select"
1875          gnus-summary-kill-same-subject-and-select t]
1876         ["Mark same subject" gnus-summary-kill-same-subject t]
1877         ["Catchup" gnus-summary-catchup t]
1878         ["Catchup all" gnus-summary-catchup-all t]
1879         ["Catchup to here" gnus-summary-catchup-to-here t]
1880         ["Catchup region" gnus-summary-mark-region-as-read t]
1881         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1882        ("Mark Various"
1883         ["Tick" gnus-summary-tick-article-forward t]
1884         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1885         ["Remove marks" gnus-summary-clear-mark-forward t]
1886         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1887         ["Set bookmark" gnus-summary-set-bookmark t]
1888         ["Remove bookmark" gnus-summary-remove-bookmark t])
1889        ("Mark Limit"
1890         ["Marks..." gnus-summary-limit-to-marks t]
1891         ["Subject..." gnus-summary-limit-to-subject t]
1892         ["Author..." gnus-summary-limit-to-author t]
1893         ["Age..." gnus-summary-limit-to-age t]
1894         ["Extra..." gnus-summary-limit-to-extra t]
1895         ["Score" gnus-summary-limit-to-score t]
1896         ["Unread" gnus-summary-limit-to-unread t]
1897         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1898         ["Articles" gnus-summary-limit-to-articles t]
1899         ["Pop limit" gnus-summary-pop-limit t]
1900         ["Show dormant" gnus-summary-limit-include-dormant t]
1901         ["Hide childless dormant"
1902          gnus-summary-limit-exclude-childless-dormant t]
1903         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1904         ["Hide marked" gnus-summary-limit-exclude-marks t]
1905         ["Show expunged" gnus-summary-show-all-expunged t])
1906        ("Process Mark"
1907         ["Set mark" gnus-summary-mark-as-processable t]
1908         ["Remove mark" gnus-summary-unmark-as-processable t]
1909         ["Remove all marks" gnus-summary-unmark-all-processable t]
1910         ["Mark above" gnus-uu-mark-over t]
1911         ["Mark series" gnus-uu-mark-series t]
1912         ["Mark region" gnus-uu-mark-region t]
1913         ["Unmark region" gnus-uu-unmark-region t]
1914         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1915         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1916         ["Mark all" gnus-uu-mark-all t]
1917         ["Mark buffer" gnus-uu-mark-buffer t]
1918         ["Mark sparse" gnus-uu-mark-sparse t]
1919         ["Mark thread" gnus-uu-mark-thread t]
1920         ["Unmark thread" gnus-uu-unmark-thread t]
1921         ("Process Mark Sets"
1922          ["Kill" gnus-summary-kill-process-mark t]
1923          ["Yank" gnus-summary-yank-process-mark
1924           gnus-newsgroup-process-stack]
1925          ["Save" gnus-summary-save-process-mark t]))
1926        ("Scroll article"
1927         ["Page forward" gnus-summary-next-page t]
1928         ["Page backward" gnus-summary-prev-page t]
1929         ["Line forward" gnus-summary-scroll-up t])
1930        ("Move"
1931         ["Next unread article" gnus-summary-next-unread-article t]
1932         ["Previous unread article" gnus-summary-prev-unread-article t]
1933         ["Next article" gnus-summary-next-article t]
1934         ["Previous article" gnus-summary-prev-article t]
1935         ["Next unread subject" gnus-summary-next-unread-subject t]
1936         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1937         ["Next article same subject" gnus-summary-next-same-subject t]
1938         ["Previous article same subject" gnus-summary-prev-same-subject t]
1939         ["First unread article" gnus-summary-first-unread-article t]
1940         ["Best unread article" gnus-summary-best-unread-article t]
1941         ["Go to subject number..." gnus-summary-goto-subject t]
1942         ["Go to article number..." gnus-summary-goto-article t]
1943         ["Go to the last article" gnus-summary-goto-last-article t]
1944         ["Pop article off history" gnus-summary-pop-article t])
1945        ("Sort"
1946         ["Sort by number" gnus-summary-sort-by-number t]
1947         ["Sort by author" gnus-summary-sort-by-author t]
1948         ["Sort by subject" gnus-summary-sort-by-subject t]
1949         ["Sort by date" gnus-summary-sort-by-date t]
1950         ["Sort by score" gnus-summary-sort-by-score t]
1951         ["Sort by lines" gnus-summary-sort-by-lines t]
1952         ["Sort by characters" gnus-summary-sort-by-chars t])
1953        ("Help"
1954         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1955         ["Describe group" gnus-summary-describe-group t]
1956         ["Read manual" gnus-info-find-node t])
1957        ("Modes"
1958         ["Pick and read" gnus-pick-mode t]
1959         ["Binary" gnus-binary-mode t])
1960        ("Regeneration"
1961         ["Regenerate" gnus-summary-prepare t]
1962         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1963         ["Toggle threading" gnus-summary-toggle-threads t])
1964        ["Filter articles..." gnus-summary-execute-command t]
1965        ["Run command on subjects..." gnus-summary-universal-argument t]
1966        ["Search articles forward..." gnus-summary-search-article-forward t]
1967        ["Search articles backward..." gnus-summary-search-article-backward t]
1968        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1969        ["Expand window" gnus-summary-expand-window t]
1970        ["Expire expirable articles" gnus-summary-expire-articles
1971         (gnus-check-backend-function
1972          'request-expire-articles gnus-newsgroup-name)]
1973        ["Edit local kill file" gnus-summary-edit-local-kill t]
1974        ["Edit main kill file" gnus-summary-edit-global-kill t]
1975        ["Edit group parameters" gnus-summary-edit-parameters t]
1976        ["Customize group parameters" gnus-summary-customize-parameters t]
1977        ["Send a bug report" gnus-bug t]
1978        ("Exit"
1979         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1980         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1981         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1982         ["Exit group" gnus-summary-exit t]
1983         ["Exit group without updating" gnus-summary-exit-no-update t]
1984         ["Exit and goto next group" gnus-summary-next-group t]
1985         ["Exit and goto prev group" gnus-summary-prev-group t]
1986         ["Reselect group" gnus-summary-reselect-current-group t]
1987         ["Rescan group" gnus-summary-rescan-group t]
1988         ["Update dribble" gnus-summary-save-newsrc t])))
1989
1990     (gnus-run-hooks 'gnus-summary-menu-hook)))
1991
1992 (defun gnus-score-set-default (var value)
1993   "A version of set that updates the GNU Emacs menu-bar."
1994   (set var value)
1995   ;; It is the message that forces the active status to be updated.
1996   (message ""))
1997
1998 (defun gnus-make-score-map (type)
1999   "Make a summary score map of type TYPE."
2000   (if t
2001       nil
2002     (let ((headers '(("author" "from" string)
2003                      ("subject" "subject" string)
2004                      ("article body" "body" string)
2005                      ("article head" "head" string)
2006                      ("xref" "xref" string)
2007                      ("extra header" "extra" string)
2008                      ("lines" "lines" number)
2009                      ("followups to author" "followup" string)))
2010           (types '((number ("less than" <)
2011                            ("greater than" >)
2012                            ("equal" =))
2013                    (string ("substring" s)
2014                            ("exact string" e)
2015                            ("fuzzy string" f)
2016                            ("regexp" r))))
2017           (perms '(("temporary" (current-time-string))
2018                    ("permanent" nil)
2019                    ("immediate" now)))
2020           header)
2021       (list
2022        (apply
2023         'nconc
2024         (list
2025          (if (eq type 'lower)
2026              "Lower score"
2027            "Increase score"))
2028         (let (outh)
2029           (while headers
2030             (setq header (car headers))
2031             (setq outh
2032                   (cons
2033                    (apply
2034                     'nconc
2035                     (list (car header))
2036                     (let ((ts (cdr (assoc (nth 2 header) types)))
2037                           outt)
2038                       (while ts
2039                         (setq outt
2040                               (cons
2041                                (apply
2042                                 'nconc
2043                                 (list (caar ts))
2044                                 (let ((ps perms)
2045                                       outp)
2046                                   (while ps
2047                                     (setq outp
2048                                           (cons
2049                                            (vector
2050                                             (caar ps)
2051                                             (list
2052                                              'gnus-summary-score-entry
2053                                              (nth 1 header)
2054                                              (if (or (string= (nth 1 header)
2055                                                               "head")
2056                                                      (string= (nth 1 header)
2057                                                               "body"))
2058                                                  ""
2059                                                (list 'gnus-summary-header
2060                                                      (nth 1 header)))
2061                                              (list 'quote (nth 1 (car ts)))
2062                                              (list 'gnus-score-delta-default
2063                                                    nil)
2064                                              (nth 1 (car ps))
2065                                              t)
2066                                             t)
2067                                            outp))
2068                                     (setq ps (cdr ps)))
2069                                   (list (nreverse outp))))
2070                                outt))
2071                         (setq ts (cdr ts)))
2072                       (list (nreverse outt))))
2073                    outh))
2074             (setq headers (cdr headers)))
2075           (list (nreverse outh))))))))
2076
2077 \f
2078
2079 (defun gnus-summary-mode (&optional group)
2080   "Major mode for reading articles.
2081
2082 All normal editing commands are switched off.
2083 \\<gnus-summary-mode-map>
2084 Each line in this buffer represents one article.  To read an
2085 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2086 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2087 respectively.
2088
2089 You can also post articles and send mail from this buffer.  To
2090 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2091 of an article, type `\\[gnus-summary-reply]'.
2092
2093 There are approx. one gazillion commands you can execute in this
2094 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2095
2096 The following commands are available:
2097
2098 \\{gnus-summary-mode-map}"
2099   (interactive)
2100   (when (gnus-visual-p 'summary-menu 'menu)
2101     (gnus-summary-make-menu-bar))
2102   (kill-all-local-variables)
2103   (gnus-summary-make-local-variables)
2104   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2105     (gnus-summary-make-local-variables))
2106   (gnus-make-thread-indent-array)
2107   (gnus-simplify-mode-line)
2108   (setq major-mode 'gnus-summary-mode)
2109   (setq mode-name "Summary")
2110   (make-local-variable 'minor-mode-alist)
2111   (use-local-map gnus-summary-mode-map)
2112   (buffer-disable-undo)
2113   (setq buffer-read-only t)             ;Disable modification
2114   (setq truncate-lines t)
2115   (setq selective-display t)
2116   (setq selective-display-ellipses t)   ;Display `...'
2117   (gnus-summary-set-display-table)
2118   (gnus-set-default-directory)
2119   (setq gnus-newsgroup-name group)
2120   (make-local-variable 'gnus-summary-line-format)
2121   (make-local-variable 'gnus-summary-line-format-spec)
2122   (make-local-variable 'gnus-summary-dummy-line-format)
2123   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2124   (make-local-variable 'gnus-summary-mark-positions)
2125   (make-local-hook 'pre-command-hook)
2126   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2127   (gnus-run-hooks 'gnus-summary-mode-hook)
2128   (mm-enable-multibyte-mule4)
2129   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2130   (gnus-update-summary-mark-positions))
2131
2132 (defun gnus-summary-make-local-variables ()
2133   "Make all the local summary buffer variables."
2134   (let (global)
2135     (dolist (local gnus-summary-local-variables)
2136       (if (consp local)
2137           (progn
2138             (if (eq (cdr local) 'global)
2139                 ;; Copy the global value of the variable.
2140                 (setq global (symbol-value (car local)))
2141               ;; Use the value from the list.
2142               (setq global (eval (cdr local))))
2143             (set (make-local-variable (car local)) global))
2144         ;; Simple nil-valued local variable.
2145         (set (make-local-variable local) nil)))))
2146
2147 (defun gnus-summary-clear-local-variables ()
2148   (let ((locals gnus-summary-local-variables))
2149     (while locals
2150       (if (consp (car locals))
2151           (and (vectorp (caar locals))
2152                (set (caar locals) nil))
2153         (and (vectorp (car locals))
2154              (set (car locals) nil)))
2155       (setq locals (cdr locals)))))
2156
2157 ;; Summary data functions.
2158
2159 (defmacro gnus-data-number (data)
2160   `(car ,data))
2161
2162 (defmacro gnus-data-set-number (data number)
2163   `(setcar ,data ,number))
2164
2165 (defmacro gnus-data-mark (data)
2166   `(nth 1 ,data))
2167
2168 (defmacro gnus-data-set-mark (data mark)
2169   `(setcar (nthcdr 1 ,data) ,mark))
2170
2171 (defmacro gnus-data-pos (data)
2172   `(nth 2 ,data))
2173
2174 (defmacro gnus-data-set-pos (data pos)
2175   `(setcar (nthcdr 2 ,data) ,pos))
2176
2177 (defmacro gnus-data-header (data)
2178   `(nth 3 ,data))
2179
2180 (defmacro gnus-data-set-header (data header)
2181   `(setf (nth 3 ,data) ,header))
2182
2183 (defmacro gnus-data-level (data)
2184   `(nth 4 ,data))
2185
2186 (defmacro gnus-data-unread-p (data)
2187   `(= (nth 1 ,data) gnus-unread-mark))
2188
2189 (defmacro gnus-data-read-p (data)
2190   `(/= (nth 1 ,data) gnus-unread-mark))
2191
2192 (defmacro gnus-data-pseudo-p (data)
2193   `(consp (nth 3 ,data)))
2194
2195 (defmacro gnus-data-find (number)
2196   `(assq ,number gnus-newsgroup-data))
2197
2198 (defmacro gnus-data-find-list (number &optional data)
2199   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2200      (memq (assq ,number bdata)
2201            bdata)))
2202
2203 (defmacro gnus-data-make (number mark pos header level)
2204   `(list ,number ,mark ,pos ,header ,level))
2205
2206 (defun gnus-data-enter (after-article number mark pos header level offset)
2207   (let ((data (gnus-data-find-list after-article)))
2208     (unless data
2209       (error "No such article: %d" after-article))
2210     (setcdr data (cons (gnus-data-make number mark pos header level)
2211                        (cdr data)))
2212     (setq gnus-newsgroup-data-reverse nil)
2213     (gnus-data-update-list (cddr data) offset)))
2214
2215 (defun gnus-data-enter-list (after-article list &optional offset)
2216   (when list
2217     (let ((data (and after-article (gnus-data-find-list after-article)))
2218           (ilist list))
2219       (if (not (or data
2220                    after-article))
2221           (let ((odata gnus-newsgroup-data))
2222             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2223             (when offset
2224               (gnus-data-update-list odata offset)))
2225         ;; Find the last element in the list to be spliced into the main
2226         ;; list.
2227         (while (cdr list)
2228           (setq list (cdr list)))
2229         (if (not data)
2230             (progn
2231               (setcdr list gnus-newsgroup-data)
2232               (setq gnus-newsgroup-data ilist)
2233               (when offset
2234                 (gnus-data-update-list (cdr list) offset)))
2235           (setcdr list (cdr data))
2236           (setcdr data ilist)
2237           (when offset
2238             (gnus-data-update-list (cdr list) offset))))
2239       (setq gnus-newsgroup-data-reverse nil))))
2240
2241 (defun gnus-data-remove (article &optional offset)
2242   (let ((data gnus-newsgroup-data))
2243     (if (= (gnus-data-number (car data)) article)
2244         (progn
2245           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2246                 gnus-newsgroup-data-reverse nil)
2247           (when offset
2248             (gnus-data-update-list gnus-newsgroup-data offset)))
2249       (while (cdr data)
2250         (when (= (gnus-data-number (cadr data)) article)
2251           (setcdr data (cddr data))
2252           (when offset
2253             (gnus-data-update-list (cdr data) offset))
2254           (setq data nil
2255                 gnus-newsgroup-data-reverse nil))
2256         (setq data (cdr data))))))
2257
2258 (defmacro gnus-data-list (backward)
2259   `(if ,backward
2260        (or gnus-newsgroup-data-reverse
2261            (setq gnus-newsgroup-data-reverse
2262                  (reverse gnus-newsgroup-data)))
2263      gnus-newsgroup-data))
2264
2265 (defun gnus-data-update-list (data offset)
2266   "Add OFFSET to the POS of all data entries in DATA."
2267   (setq gnus-newsgroup-data-reverse nil)
2268   (while data
2269     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2270     (setq data (cdr data))))
2271
2272 (defun gnus-summary-article-pseudo-p (article)
2273   "Say whether this article is a pseudo article or not."
2274   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2275
2276 (defmacro gnus-summary-article-sparse-p (article)
2277   "Say whether this article is a sparse article or not."
2278   `(memq ,article gnus-newsgroup-sparse))
2279
2280 (defmacro gnus-summary-article-ancient-p (article)
2281   "Say whether this article is a sparse article or not."
2282   `(memq ,article gnus-newsgroup-ancient))
2283
2284 (defun gnus-article-parent-p (number)
2285   "Say whether this article is a parent or not."
2286   (let ((data (gnus-data-find-list number)))
2287     (and (cdr data)                     ; There has to be an article after...
2288          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2289             (gnus-data-level (nth 1 data))))))
2290
2291 (defun gnus-article-children (number)
2292   "Return a list of all children to NUMBER."
2293   (let* ((data (gnus-data-find-list number))
2294          (level (gnus-data-level (car data)))
2295          children)
2296     (setq data (cdr data))
2297     (while (and data
2298                 (= (gnus-data-level (car data)) (1+ level)))
2299       (push (gnus-data-number (car data)) children)
2300       (setq data (cdr data)))
2301     children))
2302
2303 (defmacro gnus-summary-skip-intangible ()
2304   "If the current article is intangible, then jump to a different article."
2305   '(let ((to (get-text-property (point) 'gnus-intangible)))
2306      (and to (gnus-summary-goto-subject to))))
2307
2308 (defmacro gnus-summary-article-intangible-p ()
2309   "Say whether this article is intangible or not."
2310   '(get-text-property (point) 'gnus-intangible))
2311
2312 (defun gnus-article-read-p (article)
2313   "Say whether ARTICLE is read or not."
2314   (not (or (memq article gnus-newsgroup-marked)
2315            (memq article gnus-newsgroup-unreads)
2316            (memq article gnus-newsgroup-unselected)
2317            (memq article gnus-newsgroup-dormant))))
2318
2319 ;; Some summary mode macros.
2320
2321 (defmacro gnus-summary-article-number ()
2322   "The article number of the article on the current line.
2323 If there isn's an article number here, then we return the current
2324 article number."
2325   '(progn
2326      (gnus-summary-skip-intangible)
2327      (or (get-text-property (point) 'gnus-number)
2328          (gnus-summary-last-subject))))
2329
2330 (defmacro gnus-summary-article-header (&optional number)
2331   "Return the header of article NUMBER."
2332   `(gnus-data-header (gnus-data-find
2333                       ,(or number '(gnus-summary-article-number)))))
2334
2335 (defmacro gnus-summary-thread-level (&optional number)
2336   "Return the level of thread that starts with article NUMBER."
2337   `(if (and (eq gnus-summary-make-false-root 'dummy)
2338             (get-text-property (point) 'gnus-intangible))
2339        0
2340      (gnus-data-level (gnus-data-find
2341                        ,(or number '(gnus-summary-article-number))))))
2342
2343 (defmacro gnus-summary-article-mark (&optional number)
2344   "Return the mark of article NUMBER."
2345   `(gnus-data-mark (gnus-data-find
2346                     ,(or number '(gnus-summary-article-number)))))
2347
2348 (defmacro gnus-summary-article-pos (&optional number)
2349   "Return the position of the line of article NUMBER."
2350   `(gnus-data-pos (gnus-data-find
2351                    ,(or number '(gnus-summary-article-number)))))
2352
2353 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2354 (defmacro gnus-summary-article-subject (&optional number)
2355   "Return current subject string or nil if nothing."
2356   `(let ((headers
2357           ,(if number
2358                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2359              '(gnus-data-header (assq (gnus-summary-article-number)
2360                                       gnus-newsgroup-data)))))
2361      (and headers
2362           (vectorp headers)
2363           (mail-header-subject headers))))
2364
2365 (defmacro gnus-summary-article-score (&optional number)
2366   "Return current article score."
2367   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2368                   gnus-newsgroup-scored))
2369        gnus-summary-default-score 0))
2370
2371 (defun gnus-summary-article-children (&optional number)
2372   "Return a list of article numbers that are children of article NUMBER."
2373   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2374          (level (gnus-data-level (car data)))
2375          l children)
2376     (while (and (setq data (cdr data))
2377                 (> (setq l (gnus-data-level (car data))) level))
2378       (and (= (1+ level) l)
2379            (push (gnus-data-number (car data))
2380                  children)))
2381     (nreverse children)))
2382
2383 (defun gnus-summary-article-parent (&optional number)
2384   "Return the article number of the parent of article NUMBER."
2385   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2386                                     (gnus-data-list t)))
2387          (level (gnus-data-level (car data))))
2388     (if (zerop level)
2389         ()                              ; This is a root.
2390       ;; We search until we find an article with a level less than
2391       ;; this one.  That function has to be the parent.
2392       (while (and (setq data (cdr data))
2393                   (not (< (gnus-data-level (car data)) level))))
2394       (and data (gnus-data-number (car data))))))
2395
2396 (defun gnus-unread-mark-p (mark)
2397   "Say whether MARK is the unread mark."
2398   (= mark gnus-unread-mark))
2399
2400 (defun gnus-read-mark-p (mark)
2401   "Say whether MARK is one of the marks that mark as read.
2402 This is all marks except unread, ticked, dormant, and expirable."
2403   (not (or (= mark gnus-unread-mark)
2404            (= mark gnus-ticked-mark)
2405            (= mark gnus-dormant-mark)
2406            (= mark gnus-expirable-mark))))
2407
2408 (defmacro gnus-article-mark (number)
2409   "Return the MARK of article NUMBER.
2410 This macro should only be used when computing the mark the \"first\"
2411 time; i.e., when generating the summary lines.  After that,
2412 `gnus-summary-article-mark' should be used to examine the
2413 marks of articles."
2414   `(cond
2415     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2416     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2417     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2418     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2419     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2420     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2421     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2422     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2423            gnus-ancient-mark))))
2424
2425 ;; Saving hidden threads.
2426
2427 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2428 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2429
2430 (defmacro gnus-save-hidden-threads (&rest forms)
2431   "Save hidden threads, eval FORMS, and restore the hidden threads."
2432   (let ((config (make-symbol "config")))
2433     `(let ((,config (gnus-hidden-threads-configuration)))
2434        (unwind-protect
2435            (save-excursion
2436              ,@forms)
2437          (gnus-restore-hidden-threads-configuration ,config)))))
2438
2439 (defun gnus-data-compute-positions ()
2440   "Compute the positions of all articles."
2441   (setq gnus-newsgroup-data-reverse nil)
2442   (let ((data gnus-newsgroup-data))
2443     (save-excursion
2444       (gnus-save-hidden-threads
2445         (gnus-summary-show-all-threads)
2446         (goto-char (point-min))
2447         (while data
2448           (while (get-text-property (point) 'gnus-intangible)
2449             (forward-line 1))
2450           (gnus-data-set-pos (car data) (+ (point) 3))
2451           (setq data (cdr data))
2452           (forward-line 1))))))
2453
2454 (defun gnus-hidden-threads-configuration ()
2455   "Return the current hidden threads configuration."
2456   (save-excursion
2457     (let (config)
2458       (goto-char (point-min))
2459       (while (search-forward "\r" nil t)
2460         (push (1- (point)) config))
2461       config)))
2462
2463 (defun gnus-restore-hidden-threads-configuration (config)
2464   "Restore hidden threads configuration from CONFIG."
2465   (save-excursion
2466     (let (point buffer-read-only)
2467       (while (setq point (pop config))
2468         (when (and (< point (point-max))
2469                    (goto-char point)
2470                    (eq (char-after) ?\n))
2471           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2472
2473 ;; Various summary mode internalish functions.
2474
2475 (defun gnus-mouse-pick-article (e)
2476   (interactive "e")
2477   (mouse-set-point e)
2478   (gnus-summary-next-page nil t))
2479
2480 (defun gnus-summary-set-display-table ()
2481   "Change the display table.
2482 Odd characters have a tendency to mess
2483 up nicely formatted displays - we make all possible glyphs
2484 display only a single character."
2485
2486   ;; We start from the standard display table, if any.
2487   (let ((table (or (copy-sequence standard-display-table)
2488                    (make-display-table)))
2489         (i 32))
2490     ;; Nix out all the control chars...
2491     (while (>= (setq i (1- i)) 0)
2492       (aset table i [??]))
2493     ;; ... but not newline and cr, of course.  (cr is necessary for the
2494     ;; selective display).
2495     (aset table ?\n nil)
2496     (aset table ?\r nil)
2497     ;; We keep TAB as well.
2498     (aset table ?\t nil)
2499     ;; We nix out any glyphs over 126 that are not set already.
2500     (let ((i 256))
2501       (while (>= (setq i (1- i)) 127)
2502         ;; Only modify if the entry is nil.
2503         (unless (aref table i)
2504           (aset table i [??]))))
2505     (setq buffer-display-table table)))
2506
2507 (defun gnus-summary-setup-buffer (group)
2508   "Initialize summary buffer."
2509   (let ((buffer (concat "*Summary " group "*")))
2510     (if (get-buffer buffer)
2511         (progn
2512           (set-buffer buffer)
2513           (setq gnus-summary-buffer (current-buffer))
2514           (not gnus-newsgroup-prepared))
2515       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2516       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2517       (gnus-summary-mode group)
2518       (when gnus-carpal
2519         (gnus-carpal-setup-buffer 'summary))
2520       (unless gnus-single-article-buffer
2521         (make-local-variable 'gnus-article-buffer)
2522         (make-local-variable 'gnus-article-current)
2523         (make-local-variable 'gnus-original-article-buffer))
2524       (setq gnus-newsgroup-name group)
2525       t)))
2526
2527 (defun gnus-set-global-variables ()
2528   "Set the global equivalents of the buffer-local variables.
2529 They are set to the latest values they had.  These reflect the summary
2530 buffer that was in action when the last article was fetched."
2531   (when (eq major-mode 'gnus-summary-mode)
2532     (setq gnus-summary-buffer (current-buffer))
2533     (let ((name gnus-newsgroup-name)
2534           (marked gnus-newsgroup-marked)
2535           (unread gnus-newsgroup-unreads)
2536           (headers gnus-current-headers)
2537           (data gnus-newsgroup-data)
2538           (summary gnus-summary-buffer)
2539           (article-buffer gnus-article-buffer)
2540           (original gnus-original-article-buffer)
2541           (gac gnus-article-current)
2542           (reffed gnus-reffed-article-number)
2543           (score-file gnus-current-score-file)
2544           (default-charset gnus-newsgroup-charset)
2545           vlist)
2546       (let ((locals gnus-newsgroup-variables))
2547         (while locals
2548           (if (consp (car locals))
2549               (push (eval (caar locals)) vlist)
2550             (push (eval (car locals)) vlist))
2551           (setq locals (cdr locals)))
2552         (setq vlist (nreverse vlist)))
2553       (save-excursion
2554         (set-buffer gnus-group-buffer)
2555         (setq gnus-newsgroup-name name
2556               gnus-newsgroup-marked marked
2557               gnus-newsgroup-unreads unread
2558               gnus-current-headers headers
2559               gnus-newsgroup-data data
2560               gnus-article-current gac
2561               gnus-summary-buffer summary
2562               gnus-article-buffer article-buffer
2563               gnus-original-article-buffer original
2564               gnus-reffed-article-number reffed
2565               gnus-current-score-file score-file
2566               gnus-newsgroup-charset default-charset)
2567         (let ((locals gnus-newsgroup-variables))
2568           (while locals
2569             (if (consp (car locals))
2570                 (set (caar locals) (pop vlist))
2571               (set (car locals) (pop vlist)))
2572             (setq locals (cdr locals))))
2573         ;; The article buffer also has local variables.
2574         (when (gnus-buffer-live-p gnus-article-buffer)
2575           (set-buffer gnus-article-buffer)
2576           (setq gnus-summary-buffer summary))))))
2577
2578 (defun gnus-summary-article-unread-p (article)
2579   "Say whether ARTICLE is unread or not."
2580   (memq article gnus-newsgroup-unreads))
2581
2582 (defun gnus-summary-first-article-p (&optional article)
2583   "Return whether ARTICLE is the first article in the buffer."
2584   (if (not (setq article (or article (gnus-summary-article-number))))
2585       nil
2586     (eq article (caar gnus-newsgroup-data))))
2587
2588 (defun gnus-summary-last-article-p (&optional article)
2589   "Return whether ARTICLE is the last article in the buffer."
2590   (if (not (setq article (or article (gnus-summary-article-number))))
2591       ;; All non-existent numbers are the last article.  :-)
2592       t
2593     (not (cdr (gnus-data-find-list article)))))
2594
2595 (defun gnus-make-thread-indent-array ()
2596   (let ((n 200))
2597     (unless (and gnus-thread-indent-array
2598                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2599       (setq gnus-thread-indent-array (make-vector 201 "")
2600             gnus-thread-indent-array-level gnus-thread-indent-level)
2601       (while (>= n 0)
2602         (aset gnus-thread-indent-array n
2603               (make-string (* n gnus-thread-indent-level) ? ))
2604         (setq n (1- n))))))
2605
2606 (defun gnus-update-summary-mark-positions ()
2607   "Compute where the summary marks are to go."
2608   (save-excursion
2609     (when (gnus-buffer-exists-p gnus-summary-buffer)
2610       (set-buffer gnus-summary-buffer))
2611     (let ((gnus-replied-mark 129)
2612           (gnus-score-below-mark 130)
2613           (gnus-score-over-mark 130)
2614           (gnus-download-mark 131)
2615           (spec gnus-summary-line-format-spec)
2616           gnus-visual pos)
2617       (save-excursion
2618         (gnus-set-work-buffer)
2619         (let ((gnus-summary-line-format-spec spec)
2620               (gnus-newsgroup-downloadable '((0 . t))))
2621           (gnus-summary-insert-line
2622            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2623           (goto-char (point-min))
2624           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2625                                              (- (point) 2)))))
2626           (goto-char (point-min))
2627           (push (cons 'replied (and (search-forward "\201" nil t)
2628                                     (- (point) 2)))
2629                 pos)
2630           (goto-char (point-min))
2631           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2632                 pos)
2633           (goto-char (point-min))
2634           (push (cons 'download
2635                       (and (search-forward "\203" nil t) (- (point) 2)))
2636                 pos)))
2637       (setq gnus-summary-mark-positions pos))))
2638
2639 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2640   "Insert a dummy root in the summary buffer."
2641   (beginning-of-line)
2642   (gnus-add-text-properties
2643    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2644    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2645
2646 (defun gnus-summary-from-or-to-or-newsgroups (header)
2647   (let ((to (cdr (assq 'To (mail-header-extra header))))
2648         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2649         (mail-parse-charset gnus-newsgroup-charset)
2650         (mail-parse-ignored-charsets
2651          (save-excursion (set-buffer gnus-summary-buffer)
2652                          gnus-newsgroup-ignored-charsets)))
2653     (cond
2654      ((and to
2655            gnus-ignored-from-addresses
2656            (string-match gnus-ignored-from-addresses
2657                          (mail-header-from header)))
2658       (concat "-> "
2659               (or (car (funcall gnus-extract-address-components
2660                                 (funcall
2661                                  gnus-decode-encoded-word-function to)))
2662                   (funcall gnus-decode-encoded-word-function to))))
2663      ((and newsgroups
2664            gnus-ignored-from-addresses
2665            (string-match gnus-ignored-from-addresses
2666                          (mail-header-from header)))
2667       (concat "=> " newsgroups))
2668      (t
2669       (or (car (funcall gnus-extract-address-components
2670                         (mail-header-from header)))
2671           (mail-header-from header))))))
2672
2673 (defun gnus-summary-insert-line (gnus-tmp-header
2674                                  gnus-tmp-level gnus-tmp-current
2675                                  gnus-tmp-unread gnus-tmp-replied
2676                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2677                                  &optional gnus-tmp-dummy gnus-tmp-score
2678                                  gnus-tmp-process)
2679   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2680          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2681          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2682          (gnus-tmp-score-char
2683           (if (or (null gnus-summary-default-score)
2684                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2685                       gnus-summary-zcore-fuzz))
2686               ?  ;Whitespace
2687             (if (< gnus-tmp-score gnus-summary-default-score)
2688                 gnus-score-below-mark gnus-score-over-mark)))
2689          (gnus-tmp-replied
2690           (cond (gnus-tmp-process gnus-process-mark)
2691                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2692                  gnus-cached-mark)
2693                 (gnus-tmp-replied gnus-replied-mark)
2694                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2695                  gnus-saved-mark)
2696                 (t gnus-unread-mark)))
2697          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2698          (gnus-tmp-name
2699           (cond
2700            ((string-match "<[^>]+> *$" gnus-tmp-from)
2701             (let ((beg (match-beginning 0)))
2702               (or (and (string-match "^\".+\"" gnus-tmp-from)
2703                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2704                   (substring gnus-tmp-from 0 beg))))
2705            ((string-match "(.+)" gnus-tmp-from)
2706             (substring gnus-tmp-from
2707                        (1+ (match-beginning 0)) (1- (match-end 0))))
2708            (t gnus-tmp-from)))
2709          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2710          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2711          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2712          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2713          (buffer-read-only nil))
2714     (when (string= gnus-tmp-name "")
2715       (setq gnus-tmp-name gnus-tmp-from))
2716     (unless (numberp gnus-tmp-lines)
2717       (setq gnus-tmp-lines 0))
2718     (gnus-put-text-property
2719      (point)
2720      (progn (eval gnus-summary-line-format-spec) (point))
2721      'gnus-number gnus-tmp-number)
2722     (when (gnus-visual-p 'summary-highlight 'highlight)
2723       (forward-line -1)
2724       (gnus-run-hooks 'gnus-summary-update-hook)
2725       (forward-line 1))))
2726
2727 (defun gnus-summary-update-line (&optional dont-update)
2728   "Update summary line after change."
2729   (when (and gnus-summary-default-score
2730              (not gnus-summary-inhibit-highlight))
2731     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2732            (article (gnus-summary-article-number))
2733            (score (gnus-summary-article-score article)))
2734       (unless dont-update
2735         (if (and gnus-summary-mark-below
2736                  (< (gnus-summary-article-score)
2737                     gnus-summary-mark-below))
2738             ;; This article has a low score, so we mark it as read.
2739             (when (memq article gnus-newsgroup-unreads)
2740               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2741           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2742             ;; This article was previously marked as read on account
2743             ;; of a low score, but now it has risen, so we mark it as
2744             ;; unread.
2745             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2746         (gnus-summary-update-mark
2747          (if (or (null gnus-summary-default-score)
2748                  (<= (abs (- score gnus-summary-default-score))
2749                      gnus-summary-zcore-fuzz))
2750              ?  ;Whitespace
2751            (if (< score gnus-summary-default-score)
2752                gnus-score-below-mark gnus-score-over-mark))
2753          'score))
2754       ;; Do visual highlighting.
2755       (when (gnus-visual-p 'summary-highlight 'highlight)
2756         (gnus-run-hooks 'gnus-summary-update-hook)))))
2757
2758 (defvar gnus-tmp-new-adopts nil)
2759
2760 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2761   "Return the number of articles in THREAD.
2762 This may be 0 in some cases -- if none of the articles in
2763 the thread are to be displayed."
2764   (let* ((number
2765           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2766           (cond
2767            ((not (listp thread))
2768             1)
2769            ((and (consp thread) (cdr thread))
2770             (apply
2771              '+ 1 (mapcar
2772                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2773            ((null thread)
2774             1)
2775            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2776             1)
2777            (t 0))))
2778     (when (and level (zerop level) gnus-tmp-new-adopts)
2779       (incf number
2780             (apply '+ (mapcar
2781                        'gnus-summary-number-of-articles-in-thread
2782                        gnus-tmp-new-adopts))))
2783     (if char
2784         (if (> number 1) gnus-not-empty-thread-mark
2785           gnus-empty-thread-mark)
2786       number)))
2787
2788 (defun gnus-summary-set-local-parameters (group)
2789   "Go through the local params of GROUP and set all variable specs in that list."
2790   (let ((params (gnus-group-find-parameter group))
2791         elem)
2792     (while params
2793       (setq elem (car params)
2794             params (cdr params))
2795       (and (consp elem)                 ; Has to be a cons.
2796            (consp (cdr elem))           ; The cdr has to be a list.
2797            (symbolp (car elem))         ; Has to be a symbol in there.
2798            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2799            (ignore-errors               ; So we set it.
2800              (make-local-variable (car elem))
2801              (set (car elem) (eval (nth 1 elem))))))))
2802
2803 (defun gnus-summary-read-group (group &optional show-all no-article
2804                                       kill-buffer no-display backward
2805                                       select-articles)
2806   "Start reading news in newsgroup GROUP.
2807 If SHOW-ALL is non-nil, already read articles are also listed.
2808 If NO-ARTICLE is non-nil, no article is selected initially.
2809 If NO-DISPLAY, don't generate a summary buffer."
2810   (let (result)
2811     (while (and group
2812                 (null (setq result
2813                             (let ((gnus-auto-select-next nil))
2814                               (or (gnus-summary-read-group-1
2815                                    group show-all no-article
2816                                    kill-buffer no-display
2817                                    select-articles)
2818                                   (setq show-all nil
2819                                         select-articles nil)))))
2820                 (eq gnus-auto-select-next 'quietly))
2821       (set-buffer gnus-group-buffer)
2822       ;; The entry function called above goes to the next
2823       ;; group automatically, so we go two groups back
2824       ;; if we are searching for the previous group.
2825       (when backward
2826         (gnus-group-prev-unread-group 2))
2827       (if (not (equal group (gnus-group-group-name)))
2828           (setq group (gnus-group-group-name))
2829         (setq group nil)))
2830     result))
2831
2832 (defun gnus-summary-read-group-1 (group show-all no-article
2833                                         kill-buffer no-display
2834                                         &optional select-articles)
2835   ;; Killed foreign groups can't be entered.
2836   (when (and (not (gnus-group-native-p group))
2837              (not (gnus-gethash group gnus-newsrc-hashtb)))
2838     (error "Dead non-native groups can't be entered"))
2839   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2840   (let* ((new-group (gnus-summary-setup-buffer group))
2841          (quit-config (gnus-group-quit-config group))
2842          (did-select (and new-group (gnus-select-newsgroup
2843                                      group show-all select-articles))))
2844     (cond
2845      ;; This summary buffer exists already, so we just select it.
2846      ((not new-group)
2847       (gnus-set-global-variables)
2848       (when kill-buffer
2849         (gnus-kill-or-deaden-summary kill-buffer))
2850       (gnus-configure-windows 'summary 'force)
2851       (gnus-set-mode-line 'summary)
2852       (gnus-summary-position-point)
2853       (message "")
2854       t)
2855      ;; We couldn't select this group.
2856      ((null did-select)
2857       (when (and (eq major-mode 'gnus-summary-mode)
2858                  (not (equal (current-buffer) kill-buffer)))
2859         (kill-buffer (current-buffer))
2860         (if (not quit-config)
2861             (progn
2862               ;; Update the info -- marks might need to be removed,
2863               ;; for instance.
2864               (gnus-summary-update-info)
2865               (set-buffer gnus-group-buffer)
2866               (gnus-group-jump-to-group group)
2867               (gnus-group-next-unread-group 1))
2868           (gnus-handle-ephemeral-exit quit-config)))
2869       (gnus-message 3 "Can't select group")
2870       nil)
2871      ;; The user did a `C-g' while prompting for number of articles,
2872      ;; so we exit this group.
2873      ((eq did-select 'quit)
2874       (and (eq major-mode 'gnus-summary-mode)
2875            (not (equal (current-buffer) kill-buffer))
2876            (kill-buffer (current-buffer)))
2877       (when kill-buffer
2878         (gnus-kill-or-deaden-summary kill-buffer))
2879       (if (not quit-config)
2880           (progn
2881             (set-buffer gnus-group-buffer)
2882             (gnus-group-jump-to-group group)
2883             (gnus-group-next-unread-group 1)
2884             (gnus-configure-windows 'group 'force))
2885         (gnus-handle-ephemeral-exit quit-config))
2886       ;; Finally signal the quit.
2887       (signal 'quit nil))
2888      ;; The group was successfully selected.
2889      (t
2890       (gnus-set-global-variables)
2891       ;; Save the active value in effect when the group was entered.
2892       (setq gnus-newsgroup-active
2893             (gnus-copy-sequence
2894              (gnus-active gnus-newsgroup-name)))
2895       ;; You can change the summary buffer in some way with this hook.
2896       (gnus-run-hooks 'gnus-select-group-hook)
2897       ;; Set any local variables in the group parameters.
2898       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2899       (gnus-update-format-specifications
2900        nil 'summary 'summary-mode 'summary-dummy)
2901       (gnus-update-summary-mark-positions)
2902       ;; Do score processing.
2903       (when gnus-use-scoring
2904         (gnus-possibly-score-headers))
2905       ;; Check whether to fill in the gaps in the threads.
2906       (when gnus-build-sparse-threads
2907         (gnus-build-sparse-threads))
2908       ;; Find the initial limit.
2909       (if gnus-show-threads
2910           (if show-all
2911               (let ((gnus-newsgroup-dormant nil))
2912                 (gnus-summary-initial-limit show-all))
2913             (gnus-summary-initial-limit show-all))
2914         ;; When untreaded, all articles are always shown.
2915         (setq gnus-newsgroup-limit
2916               (mapcar
2917                (lambda (header) (mail-header-number header))
2918                gnus-newsgroup-headers)))
2919       ;; Generate the summary buffer.
2920       (unless no-display
2921         (gnus-summary-prepare))
2922       (when gnus-use-trees
2923         (gnus-tree-open group)
2924         (setq gnus-summary-highlight-line-function
2925               'gnus-tree-highlight-article))
2926       ;; If the summary buffer is empty, but there are some low-scored
2927       ;; articles or some excluded dormants, we include these in the
2928       ;; buffer.
2929       (when (and (zerop (buffer-size))
2930                  (not no-display))
2931         (cond (gnus-newsgroup-dormant
2932                (gnus-summary-limit-include-dormant))
2933               ((and gnus-newsgroup-scored show-all)
2934                (gnus-summary-limit-include-expunged t))))
2935       ;; Function `gnus-apply-kill-file' must be called in this hook.
2936       (gnus-run-hooks 'gnus-apply-kill-hook)
2937       (if (and (zerop (buffer-size))
2938                (not no-display))
2939           (progn
2940             ;; This newsgroup is empty.
2941             (gnus-summary-catchup-and-exit nil t)
2942             (gnus-message 6 "No unread news")
2943             (when kill-buffer
2944               (gnus-kill-or-deaden-summary kill-buffer))
2945             ;; Return nil from this function.
2946             nil)
2947         ;; Hide conversation thread subtrees.  We cannot do this in
2948         ;; gnus-summary-prepare-hook since kill processing may not
2949         ;; work with hidden articles.
2950         (and gnus-show-threads
2951              gnus-thread-hide-subtree
2952              (gnus-summary-hide-all-threads))
2953         (when kill-buffer
2954           (gnus-kill-or-deaden-summary kill-buffer))
2955         ;; Show first unread article if requested.
2956         (if (and (not no-article)
2957                  (not no-display)
2958                  gnus-newsgroup-unreads
2959                  gnus-auto-select-first)
2960             (progn
2961               (gnus-configure-windows 'summary)
2962               (cond
2963                ((eq gnus-auto-select-first 'best)
2964                 (gnus-summary-best-unread-article))
2965                ((eq gnus-auto-select-first t)
2966                 (gnus-summary-first-unread-article))
2967                ((gnus-functionp gnus-auto-select-first)
2968                 (funcall gnus-auto-select-first))))
2969           ;; Don't select any articles, just move point to the first
2970           ;; article in the group.
2971           (goto-char (point-min))
2972           (gnus-summary-position-point)
2973           (gnus-configure-windows 'summary 'force)
2974           (gnus-set-mode-line 'summary))
2975         (when (get-buffer-window gnus-group-buffer t)
2976           ;; Gotta use windows, because recenter does weird stuff if
2977           ;; the current buffer ain't the displayed window.
2978           (let ((owin (selected-window)))
2979             (select-window (get-buffer-window gnus-group-buffer t))
2980             (when (gnus-group-goto-group group)
2981               (recenter))
2982             (select-window owin)))
2983         ;; Mark this buffer as "prepared".
2984         (setq gnus-newsgroup-prepared t)
2985         (gnus-run-hooks 'gnus-summary-prepared-hook)
2986         t)))))
2987
2988 (defun gnus-summary-prepare ()
2989   "Generate the summary buffer."
2990   (interactive)
2991   (let ((buffer-read-only nil))
2992     (erase-buffer)
2993     (setq gnus-newsgroup-data nil
2994           gnus-newsgroup-data-reverse nil)
2995     (gnus-run-hooks 'gnus-summary-generate-hook)
2996     ;; Generate the buffer, either with threads or without.
2997     (when gnus-newsgroup-headers
2998       (gnus-summary-prepare-threads
2999        (if gnus-show-threads
3000            (gnus-sort-gathered-threads
3001             (funcall gnus-summary-thread-gathering-function
3002                      (gnus-sort-threads
3003                       (gnus-cut-threads (gnus-make-threads)))))
3004          ;; Unthreaded display.
3005          (gnus-sort-articles gnus-newsgroup-headers))))
3006     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3007     ;; Call hooks for modifying summary buffer.
3008     (goto-char (point-min))
3009     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3010
3011 (defsubst gnus-general-simplify-subject (subject)
3012   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3013   (setq subject
3014         (cond
3015          ;; Truncate the subject.
3016          (gnus-simplify-subject-functions
3017           (gnus-map-function gnus-simplify-subject-functions subject))
3018          ((numberp gnus-summary-gather-subject-limit)
3019           (setq subject (gnus-simplify-subject-re subject))
3020           (if (> (length subject) gnus-summary-gather-subject-limit)
3021               (substring subject 0 gnus-summary-gather-subject-limit)
3022             subject))
3023          ;; Fuzzily simplify it.
3024          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3025           (gnus-simplify-subject-fuzzy subject))
3026          ;; Just remove the leading "Re:".
3027          (t
3028           (gnus-simplify-subject-re subject))))
3029
3030   (if (and gnus-summary-gather-exclude-subject
3031            (string-match gnus-summary-gather-exclude-subject subject))
3032       nil                               ; This article shouldn't be gathered
3033     subject))
3034
3035 (defun gnus-summary-simplify-subject-query ()
3036   "Query where the respool algorithm would put this article."
3037   (interactive)
3038   (gnus-summary-select-article)
3039   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3040
3041 (defun gnus-gather-threads-by-subject (threads)
3042   "Gather threads by looking at Subject headers."
3043   (if (not gnus-summary-make-false-root)
3044       threads
3045     (let ((hashtb (gnus-make-hashtable 1024))
3046           (prev threads)
3047           (result threads)
3048           subject hthread whole-subject)
3049       (while threads
3050         (setq subject (gnus-general-simplify-subject
3051                        (setq whole-subject (mail-header-subject
3052                                             (caar threads)))))
3053         (when subject
3054           (if (setq hthread (gnus-gethash subject hashtb))
3055               (progn
3056                 ;; We enter a dummy root into the thread, if we
3057                 ;; haven't done that already.
3058                 (unless (stringp (caar hthread))
3059                   (setcar hthread (list whole-subject (car hthread))))
3060                 ;; We add this new gathered thread to this gathered
3061                 ;; thread.
3062                 (setcdr (car hthread)
3063                         (nconc (cdar hthread) (list (car threads))))
3064                 ;; Remove it from the list of threads.
3065                 (setcdr prev (cdr threads))
3066                 (setq threads prev))
3067             ;; Enter this thread into the hash table.
3068             (gnus-sethash subject threads hashtb)))
3069         (setq prev threads)
3070         (setq threads (cdr threads)))
3071       result)))
3072
3073 (defun gnus-gather-threads-by-references (threads)
3074   "Gather threads by looking at References headers."
3075   (let ((idhashtb (gnus-make-hashtable 1024))
3076         (thhashtb (gnus-make-hashtable 1024))
3077         (prev threads)
3078         (result threads)
3079         ids references id gthread gid entered ref)
3080     (while threads
3081       (when (setq references (mail-header-references (caar threads)))
3082         (setq id (mail-header-id (caar threads))
3083               ids (gnus-split-references references)
3084               entered nil)
3085         (while (setq ref (pop ids))
3086           (setq ids (delete ref ids))
3087           (if (not (setq gid (gnus-gethash ref idhashtb)))
3088               (progn
3089                 (gnus-sethash ref id idhashtb)
3090                 (gnus-sethash id threads thhashtb))
3091             (setq gthread (gnus-gethash gid thhashtb))
3092             (unless entered
3093               ;; We enter a dummy root into the thread, if we
3094               ;; haven't done that already.
3095               (unless (stringp (caar gthread))
3096                 (setcar gthread (list (mail-header-subject (caar gthread))
3097                                       (car gthread))))
3098               ;; We add this new gathered thread to this gathered
3099               ;; thread.
3100               (setcdr (car gthread)
3101                       (nconc (cdar gthread) (list (car threads)))))
3102             ;; Add it into the thread hash table.
3103             (gnus-sethash id gthread thhashtb)
3104             (setq entered t)
3105             ;; Remove it from the list of threads.
3106             (setcdr prev (cdr threads))
3107             (setq threads prev))))
3108       (setq prev threads)
3109       (setq threads (cdr threads)))
3110     result))
3111
3112 (defun gnus-sort-gathered-threads (threads)
3113   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3114   (let ((result threads))
3115     (while threads
3116       (when (stringp (caar threads))
3117         (setcdr (car threads)
3118                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3119       (setq threads (cdr threads)))
3120     result))
3121
3122 (defun gnus-thread-loop-p (root thread)
3123   "Say whether ROOT is in THREAD."
3124   (let ((stack (list thread))
3125         (infloop 0)
3126         th)
3127     (while (setq thread (pop stack))
3128       (setq th (cdr thread))
3129       (while (and th
3130                   (not (eq (caar th) root)))
3131         (pop th))
3132       (if th
3133           ;; We have found a loop.
3134           (let (ref-dep)
3135             (setcdr thread (delq (car th) (cdr thread)))
3136             (if (boundp (setq ref-dep (intern "none"
3137                                               gnus-newsgroup-dependencies)))
3138                 (setcdr (symbol-value ref-dep)
3139                         (nconc (cdr (symbol-value ref-dep))
3140                                (list (car th))))
3141               (set ref-dep (list nil (car th))))
3142             (setq infloop 1
3143                   stack nil))
3144         ;; Push all the subthreads onto the stack.
3145         (push (cdr thread) stack)))
3146     infloop))
3147
3148 (defun gnus-make-threads ()
3149   "Go through the dependency hashtb and find the roots.  Return all threads."
3150   (let (threads)
3151     (while (catch 'infloop
3152              (mapatoms
3153               (lambda (refs)
3154                 ;; Deal with self-referencing References loops.
3155                 (when (and (car (symbol-value refs))
3156                            (not (zerop
3157                                  (apply
3158                                   '+
3159                                   (mapcar
3160                                    (lambda (thread)
3161                                      (gnus-thread-loop-p
3162                                       (car (symbol-value refs)) thread))
3163                                    (cdr (symbol-value refs)))))))
3164                   (setq threads nil)
3165                   (throw 'infloop t))
3166                 (unless (car (symbol-value refs))
3167                   ;; These threads do not refer back to any other articles,
3168                   ;; so they're roots.
3169                   (setq threads (append (cdr (symbol-value refs)) threads))))
3170               gnus-newsgroup-dependencies)))
3171     threads))
3172
3173 ;; Build the thread tree.
3174 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3175   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3176
3177 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3178 if it was already present.
3179
3180 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3181 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3182 Message-IDs will be renamed be renamed to a unique Message-ID before
3183 being entered.
3184
3185 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3186   (let* ((id (mail-header-id header))
3187          (id-dep (and id (intern id dependencies)))
3188          ref ref-dep ref-header)
3189     ;; Enter this `header' in the `dependencies' table.
3190     (cond
3191      ((not id-dep)
3192       (setq header nil))
3193      ;; The first two cases do the normal part: enter a new `header'
3194      ;; in the `dependencies' table.
3195      ((not (boundp id-dep))
3196       (set id-dep (list header)))
3197      ((null (car (symbol-value id-dep)))
3198       (setcar (symbol-value id-dep) header))
3199
3200      ;; From here the `header' was already present in the
3201      ;; `dependencies' table.
3202      (force-new
3203       ;; Overrides an existing entry;
3204       ;; just set the header part of the entry.
3205       (setcar (symbol-value id-dep) header))
3206
3207      ;; Renames the existing `header' to a unique Message-ID.
3208      ((not gnus-summary-ignore-duplicates)
3209       ;; An article with this Message-ID has already been seen.
3210       ;; We rename the Message-ID.
3211       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3212            (list header))
3213       (mail-header-set-id header id))
3214
3215      ;; The last case ignores an existing entry, except it adds any
3216      ;; additional Xrefs (in case the two articles came from different
3217      ;; servers.
3218      ;; Also sets `header' to `nil' meaning that the `dependencies'
3219      ;; table was *not* modified.
3220      (t
3221       (mail-header-set-xref
3222        (car (symbol-value id-dep))
3223        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3224                    "")
3225                (or (mail-header-xref header) "")))
3226       (setq header nil)))
3227
3228     (when header
3229       ;; First check if that we are not creating a References loop.
3230       (setq ref (gnus-parent-id (mail-header-references header)))
3231       (while (and ref
3232                   (setq ref-dep (intern-soft ref dependencies))
3233                   (boundp ref-dep)
3234                   (setq ref-header (car (symbol-value ref-dep))))
3235         (if (string= id ref)
3236             ;; Yuk!  This is a reference loop.  Make the article be a
3237             ;; root article.
3238             (progn
3239               (mail-header-set-references (car (symbol-value id-dep)) "none")
3240               (setq ref nil))
3241           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3242       (setq ref (gnus-parent-id (mail-header-references header)))
3243       (setq ref-dep (intern (or ref "none") dependencies))
3244       (if (boundp ref-dep)
3245           (setcdr (symbol-value ref-dep)
3246                   (nconc (cdr (symbol-value ref-dep))
3247                          (list (symbol-value id-dep))))
3248         (set ref-dep (list nil (symbol-value id-dep)))))
3249     header))
3250
3251 (defun gnus-build-sparse-threads ()
3252   (let ((headers gnus-newsgroup-headers)
3253         (mail-parse-charset gnus-newsgroup-charset)
3254         (gnus-summary-ignore-duplicates t)
3255         header references generation relations
3256         subject child end new-child date)
3257     ;; First we create an alist of generations/relations, where
3258     ;; generations is how much we trust the relation, and the relation
3259     ;; is parent/child.
3260     (gnus-message 7 "Making sparse threads...")
3261     (save-excursion
3262       (nnheader-set-temp-buffer " *gnus sparse threads*")
3263       (while (setq header (pop headers))
3264         (when (and (setq references (mail-header-references header))
3265                    (not (string= references "")))
3266           (insert references)
3267           (setq child (mail-header-id header)
3268                 subject (mail-header-subject header)
3269                 date (mail-header-date header)
3270                 generation 0)
3271           (while (search-backward ">" nil t)
3272             (setq end (1+ (point)))
3273             (when (search-backward "<" nil t)
3274               (setq new-child (buffer-substring (point) end))
3275               (push (list (incf generation)
3276                           child (setq child new-child)
3277                           subject date)
3278                     relations)))
3279           (when child
3280             (push (list (1+ generation) child nil subject) relations))
3281           (erase-buffer)))
3282       (kill-buffer (current-buffer)))
3283     ;; Sort over trustworthiness.
3284     (mapcar
3285      (lambda (relation)
3286        (when (gnus-dependencies-add-header
3287               (make-full-mail-header
3288                gnus-reffed-article-number
3289                (nth 3 relation) "" (or (nth 4 relation) "")
3290                (nth 1 relation)
3291                (or (nth 2 relation) "") 0 0 "")
3292               gnus-newsgroup-dependencies nil)
3293          (push gnus-reffed-article-number gnus-newsgroup-limit)
3294          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3295          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3296                gnus-newsgroup-reads)
3297          (decf gnus-reffed-article-number)))
3298      (sort relations 'car-less-than-car))
3299     (gnus-message 7 "Making sparse threads...done")))
3300
3301 (defun gnus-build-old-threads ()
3302   ;; Look at all the articles that refer back to old articles, and
3303   ;; fetch the headers for the articles that aren't there.  This will
3304   ;; build complete threads - if the roots haven't been expired by the
3305   ;; server, that is.
3306   (let ((mail-parse-charset gnus-newsgroup-charset)
3307         id heads)
3308     (mapatoms
3309      (lambda (refs)
3310        (when (not (car (symbol-value refs)))
3311          (setq heads (cdr (symbol-value refs)))
3312          (while heads
3313            (if (memq (mail-header-number (caar heads))
3314                      gnus-newsgroup-dormant)
3315                (setq heads (cdr heads))
3316              (setq id (symbol-name refs))
3317              (while (and (setq id (gnus-build-get-header id))
3318                          (not (car (gnus-id-to-thread id)))))
3319              (setq heads nil)))))
3320      gnus-newsgroup-dependencies)))
3321
3322 ;; This function has to be called with point after the article number
3323 ;; on the beginning of the line.
3324 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3325   (let ((eol (gnus-point-at-eol))
3326         (buffer (current-buffer))
3327         header)
3328
3329     ;; overview: [num subject from date id refs chars lines misc]
3330     (unwind-protect
3331         (progn
3332           (narrow-to-region (point) eol)
3333           (unless (eobp)
3334             (forward-char))
3335
3336           (setq header
3337                 (make-full-mail-header
3338                  number                 ; number
3339                  (funcall gnus-decode-encoded-word-function
3340                           (nnheader-nov-field)) ; subject
3341                  (funcall gnus-decode-encoded-word-function
3342                           (nnheader-nov-field)) ; from
3343                  (nnheader-nov-field)   ; date
3344                  (nnheader-nov-read-message-id) ; id
3345                  (nnheader-nov-field)   ; refs
3346                  (nnheader-nov-read-integer) ; chars
3347                  (nnheader-nov-read-integer) ; lines
3348                  (unless (eobp)
3349                    (if (looking-at "Xref: ")
3350                        (goto-char (match-end 0)))
3351                    (nnheader-nov-field)) ; Xref
3352                  (nnheader-nov-parse-extra)))) ; extra
3353
3354       (widen))
3355
3356     (when gnus-alter-header-function
3357       (funcall gnus-alter-header-function header))
3358     (gnus-dependencies-add-header header dependencies force-new)))
3359
3360 (defun gnus-build-get-header (id)
3361   "Look through the buffer of NOV lines and find the header to ID.
3362 Enter this line into the dependencies hash table, and return
3363 the id of the parent article (if any)."
3364   (let ((deps gnus-newsgroup-dependencies)
3365         found header)
3366     (prog1
3367         (save-excursion
3368           (set-buffer nntp-server-buffer)
3369           (let ((case-fold-search nil))
3370             (goto-char (point-min))
3371             (while (and (not found)
3372                         (search-forward id nil t))
3373               (beginning-of-line)
3374               (setq found (looking-at
3375                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3376                                    (regexp-quote id))))
3377               (or found (beginning-of-line 2)))
3378             (when found
3379               (beginning-of-line)
3380               (and
3381                (setq header (gnus-nov-parse-line
3382                              (read (current-buffer)) deps))
3383                (gnus-parent-id (mail-header-references header))))))
3384       (when header
3385         (let ((number (mail-header-number header)))
3386           (push number gnus-newsgroup-limit)
3387           (push header gnus-newsgroup-headers)
3388           (if (memq number gnus-newsgroup-unselected)
3389               (progn
3390                 (push number gnus-newsgroup-unreads)
3391                 (setq gnus-newsgroup-unselected
3392                       (delq number gnus-newsgroup-unselected)))
3393             (push number gnus-newsgroup-ancient)))))))
3394
3395 (defun gnus-build-all-threads ()
3396   "Read all the headers."
3397   (let ((gnus-summary-ignore-duplicates t)
3398         (mail-parse-charset gnus-newsgroup-charset)
3399         (dependencies gnus-newsgroup-dependencies)
3400         header article)
3401     (save-excursion
3402       (set-buffer nntp-server-buffer)
3403       (let ((case-fold-search nil))
3404         (goto-char (point-min))
3405         (while (not (eobp))
3406           (ignore-errors
3407             (setq article (read (current-buffer))
3408                   header (gnus-nov-parse-line article dependencies)))
3409           (when header
3410             (save-excursion
3411               (set-buffer gnus-summary-buffer)
3412               (push header gnus-newsgroup-headers)
3413               (if (memq (setq article (mail-header-number header))
3414                         gnus-newsgroup-unselected)
3415                   (progn
3416                     (push article gnus-newsgroup-unreads)
3417                     (setq gnus-newsgroup-unselected
3418                           (delq article gnus-newsgroup-unselected)))
3419                 (push article gnus-newsgroup-ancient)))
3420             (forward-line 1)))))))
3421
3422 (defun gnus-summary-update-article-line (article header)
3423   "Update the line for ARTICLE using HEADERS."
3424   (let* ((id (mail-header-id header))
3425          (thread (gnus-id-to-thread id)))
3426     (unless thread
3427       (error "Article in no thread"))
3428     ;; Update the thread.
3429     (setcar thread header)
3430     (gnus-summary-goto-subject article)
3431     (let* ((datal (gnus-data-find-list article))
3432            (data (car datal))
3433            (length (when (cdr datal)
3434                      (- (gnus-data-pos data)
3435                         (gnus-data-pos (cadr datal)))))
3436            (buffer-read-only nil)
3437            (level (gnus-summary-thread-level)))
3438       (gnus-delete-line)
3439       (gnus-summary-insert-line
3440        header level nil (gnus-article-mark article)
3441        (memq article gnus-newsgroup-replied)
3442        (memq article gnus-newsgroup-expirable)
3443        ;; Only insert the Subject string when it's different
3444        ;; from the previous Subject string.
3445        (if (and
3446             gnus-show-threads
3447             (gnus-subject-equal
3448              (condition-case ()
3449                  (mail-header-subject
3450                   (gnus-data-header
3451                    (cadr
3452                     (gnus-data-find-list
3453                      article
3454                      (gnus-data-list t)))))
3455                ;; Error on the side of excessive subjects.
3456                (error ""))
3457              (mail-header-subject header)))
3458            ""
3459          (mail-header-subject header))
3460        nil (cdr (assq article gnus-newsgroup-scored))
3461        (memq article gnus-newsgroup-processable))
3462       (when length
3463         (gnus-data-update-list
3464          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3465
3466 (defun gnus-summary-update-article (article &optional iheader)
3467   "Update ARTICLE in the summary buffer."
3468   (set-buffer gnus-summary-buffer)
3469   (let* ((header (gnus-summary-article-header article))
3470          (id (mail-header-id header))
3471          (data (gnus-data-find article))
3472          (thread (gnus-id-to-thread id))
3473          (references (mail-header-references header))
3474          (parent
3475           (gnus-id-to-thread
3476            (or (gnus-parent-id
3477                 (when (and references
3478                            (not (equal "" references)))
3479                   references))
3480                "none")))
3481          (buffer-read-only nil)
3482          (old (car thread)))
3483     (when thread
3484       (unless iheader
3485         (setcar thread nil)
3486         (when parent
3487           (delq thread parent)))
3488       (if (gnus-summary-insert-subject id header)
3489           ;; Set the (possibly) new article number in the data structure.
3490           (gnus-data-set-number data (gnus-id-to-article id))
3491         (setcar thread old)
3492         nil))))
3493
3494 (defun gnus-rebuild-thread (id &optional line)
3495   "Rebuild the thread containing ID.
3496 If LINE, insert the rebuilt thread starting on line LINE."
3497   (let ((buffer-read-only nil)
3498         old-pos current thread data)
3499     (if (not gnus-show-threads)
3500         (setq thread (list (car (gnus-id-to-thread id))))
3501       ;; Get the thread this article is part of.
3502       (setq thread (gnus-remove-thread id)))
3503     (setq old-pos (gnus-point-at-bol))
3504     (setq current (save-excursion
3505                     (and (zerop (forward-line -1))
3506                          (gnus-summary-article-number))))
3507     ;; If this is a gathered thread, we have to go some re-gathering.
3508     (when (stringp (car thread))
3509       (let ((subject (car thread))
3510             roots thr)
3511         (setq thread (cdr thread))
3512         (while thread
3513           (unless (memq (setq thr (gnus-id-to-thread
3514                                    (gnus-root-id
3515                                     (mail-header-id (caar thread)))))
3516                         roots)
3517             (push thr roots))
3518           (setq thread (cdr thread)))
3519         ;; We now have all (unique) roots.
3520         (if (= (length roots) 1)
3521             ;; All the loose roots are now one solid root.
3522             (setq thread (car roots))
3523           (setq thread (cons subject (gnus-sort-threads roots))))))
3524     (let (threads)
3525       ;; We then insert this thread into the summary buffer.
3526       (when line
3527         (goto-char (point-min))
3528         (forward-line (1- line)))
3529       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3530         (if gnus-show-threads
3531             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3532           (gnus-summary-prepare-unthreaded thread))
3533         (setq data (nreverse gnus-newsgroup-data))
3534         (setq threads gnus-newsgroup-threads))
3535       ;; We splice the new data into the data structure.
3536       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3537       ;;!!! then we want to insert at the beginning of the buffer.
3538       ;;!!! That happens to be true with Gnus now, but that may
3539       ;;!!! change in the future.  Perhaps.
3540       (gnus-data-enter-list
3541        (if line nil current) data (- (point) old-pos))
3542       (setq gnus-newsgroup-threads
3543             (nconc threads gnus-newsgroup-threads))
3544       (gnus-data-compute-positions))))
3545
3546 (defun gnus-number-to-header (number)
3547   "Return the header for article NUMBER."
3548   (let ((headers gnus-newsgroup-headers))
3549     (while (and headers
3550                 (not (= number (mail-header-number (car headers)))))
3551       (pop headers))
3552     (when headers
3553       (car headers))))
3554
3555 (defun gnus-parent-headers (in-headers &optional generation)
3556   "Return the headers of the GENERATIONeth parent of HEADERS."
3557   (unless generation
3558     (setq generation 1))
3559   (let ((parent t)
3560         (headers in-headers)
3561         references)
3562     (while (and parent
3563                 (not (zerop generation))
3564                 (setq references (mail-header-references headers)))
3565       (setq headers (if (and references
3566                              (setq parent (gnus-parent-id references)))
3567                         (car (gnus-id-to-thread parent))
3568                       nil))
3569       (decf generation))
3570     (and (not (eq headers in-headers))
3571          headers)))
3572
3573 (defun gnus-id-to-thread (id)
3574   "Return the (sub-)thread where ID appears."
3575   (gnus-gethash id gnus-newsgroup-dependencies))
3576
3577 (defun gnus-id-to-article (id)
3578   "Return the article number of ID."
3579   (let ((thread (gnus-id-to-thread id)))
3580     (when (and thread
3581                (car thread))
3582       (mail-header-number (car thread)))))
3583
3584 (defun gnus-id-to-header (id)
3585   "Return the article headers of ID."
3586   (car (gnus-id-to-thread id)))
3587
3588 (defun gnus-article-displayed-root-p (article)
3589   "Say whether ARTICLE is a root(ish) article."
3590   (let ((level (gnus-summary-thread-level article))
3591         (refs (mail-header-references  (gnus-summary-article-header article)))
3592         particle)
3593     (cond
3594      ((null level) nil)
3595      ((zerop level) t)
3596      ((null refs) t)
3597      ((null (gnus-parent-id refs)) t)
3598      ((and (= 1 level)
3599            (null (setq particle (gnus-id-to-article
3600                                  (gnus-parent-id refs))))
3601            (null (gnus-summary-thread-level particle)))))))
3602
3603 (defun gnus-root-id (id)
3604   "Return the id of the root of the thread where ID appears."
3605   (let (last-id prev)
3606     (while (and id (setq prev (car (gnus-id-to-thread id))))
3607       (setq last-id id
3608             id (gnus-parent-id (mail-header-references prev))))
3609     last-id))
3610
3611 (defun gnus-articles-in-thread (thread)
3612   "Return the list of articles in THREAD."
3613   (cons (mail-header-number (car thread))
3614         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3615
3616 (defun gnus-remove-thread (id &optional dont-remove)
3617   "Remove the thread that has ID in it."
3618   (let (headers thread last-id)
3619     ;; First go up in this thread until we find the root.
3620     (setq last-id (gnus-root-id id)
3621           headers (message-flatten-list (gnus-id-to-thread last-id)))
3622     ;; We have now found the real root of this thread.  It might have
3623     ;; been gathered into some loose thread, so we have to search
3624     ;; through the threads to find the thread we wanted.
3625     (let ((threads gnus-newsgroup-threads)
3626           sub)
3627       (while threads
3628         (setq sub (car threads))
3629         (if (stringp (car sub))
3630             ;; This is a gathered thread, so we look at the roots
3631             ;; below it to find whether this article is in this
3632             ;; gathered root.
3633             (progn
3634               (setq sub (cdr sub))
3635               (while sub
3636                 (when (member (caar sub) headers)
3637                   (setq thread (car threads)
3638                         threads nil
3639                         sub nil))
3640                 (setq sub (cdr sub))))
3641           ;; It's an ordinary thread, so we check it.
3642           (when (eq (car sub) (car headers))
3643             (setq thread sub
3644                   threads nil)))
3645         (setq threads (cdr threads)))
3646       ;; If this article is in no thread, then it's a root.
3647       (if thread
3648           (unless dont-remove
3649             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3650         (setq thread (gnus-id-to-thread last-id)))
3651       (when thread
3652         (prog1
3653             thread                      ; We return this thread.
3654           (unless dont-remove
3655             (if (stringp (car thread))
3656                 (progn
3657                   ;; If we use dummy roots, then we have to remove the
3658                   ;; dummy root as well.
3659                   (when (eq gnus-summary-make-false-root 'dummy)
3660                     ;; We go to the dummy root by going to
3661                     ;; the first sub-"thread", and then one line up.
3662                     (gnus-summary-goto-article
3663                      (mail-header-number (caadr thread)))
3664                     (forward-line -1)
3665                     (gnus-delete-line)
3666                     (gnus-data-compute-positions))
3667                   (setq thread (cdr thread))
3668                   (while thread
3669                     (gnus-remove-thread-1 (car thread))
3670                     (setq thread (cdr thread))))
3671               (gnus-remove-thread-1 thread))))))))
3672
3673 (defun gnus-remove-thread-1 (thread)
3674   "Remove the thread THREAD recursively."
3675   (let ((number (mail-header-number (pop thread)))
3676         d)
3677     (setq thread (reverse thread))
3678     (while thread
3679       (gnus-remove-thread-1 (pop thread)))
3680     (when (setq d (gnus-data-find number))
3681       (goto-char (gnus-data-pos d))
3682       (gnus-summary-show-thread)
3683       (gnus-data-remove
3684        number
3685        (- (gnus-point-at-bol)
3686           (prog1
3687               (1+ (gnus-point-at-eol))
3688             (gnus-delete-line)))))))
3689
3690 (defun gnus-sort-threads-1 (threads func)
3691   (sort (mapcar (lambda (thread)
3692                   (cons (car thread)
3693                         (and (cdr thread)
3694                              (gnus-sort-threads-1 (cdr thread) func))))
3695                 threads) func))
3696
3697 (defun gnus-sort-threads (threads)
3698   "Sort THREADS."
3699   (if (not gnus-thread-sort-functions)
3700       threads
3701     (gnus-message 8 "Sorting threads...")
3702     (prog1
3703         (gnus-sort-threads-1 
3704          threads 
3705          (gnus-make-sort-function gnus-thread-sort-functions))
3706       (gnus-message 8 "Sorting threads...done"))))
3707
3708 (defun gnus-sort-articles (articles)
3709   "Sort ARTICLES."
3710   (when gnus-article-sort-functions
3711     (gnus-message 7 "Sorting articles...")
3712     (prog1
3713         (setq gnus-newsgroup-headers
3714               (sort articles (gnus-make-sort-function
3715                               gnus-article-sort-functions)))
3716       (gnus-message 7 "Sorting articles...done"))))
3717
3718 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3719 (defmacro gnus-thread-header (thread)
3720   "Return header of first article in THREAD.
3721 Note that THREAD must never, ever be anything else than a variable -
3722 using some other form will lead to serious barfage."
3723   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3724   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3725   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3726         (vector thread) 2))
3727
3728 (defsubst gnus-article-sort-by-number (h1 h2)
3729   "Sort articles by article number."
3730   (< (mail-header-number h1)
3731      (mail-header-number h2)))
3732
3733 (defun gnus-thread-sort-by-number (h1 h2)
3734   "Sort threads by root article number."
3735   (gnus-article-sort-by-number
3736    (gnus-thread-header h1) (gnus-thread-header h2)))
3737
3738 (defsubst gnus-article-sort-by-lines (h1 h2)
3739   "Sort articles by article Lines header."
3740   (< (mail-header-lines h1)
3741      (mail-header-lines h2)))
3742
3743 (defun gnus-thread-sort-by-lines (h1 h2)
3744   "Sort threads by root article Lines header."
3745   (gnus-article-sort-by-lines
3746    (gnus-thread-header h1) (gnus-thread-header h2)))
3747
3748 (defsubst gnus-article-sort-by-chars (h1 h2)
3749   "Sort articles by octet length."
3750   (< (mail-header-chars h1)
3751      (mail-header-chars h2)))
3752
3753 (defun gnus-thread-sort-by-chars (h1 h2)
3754   "Sort threads by root article octet length."
3755   (gnus-article-sort-by-chars
3756    (gnus-thread-header h1) (gnus-thread-header h2)))
3757
3758 (defsubst gnus-article-sort-by-author (h1 h2)
3759   "Sort articles by root author."
3760   (string-lessp
3761    (let ((extract (funcall
3762                    gnus-extract-address-components
3763                    (mail-header-from h1))))
3764      (or (car extract) (cadr extract) ""))
3765    (let ((extract (funcall
3766                    gnus-extract-address-components
3767                    (mail-header-from h2))))
3768      (or (car extract) (cadr extract) ""))))
3769
3770 (defun gnus-thread-sort-by-author (h1 h2)
3771   "Sort threads by root author."
3772   (gnus-article-sort-by-author
3773    (gnus-thread-header h1)  (gnus-thread-header h2)))
3774
3775 (defsubst gnus-article-sort-by-subject (h1 h2)
3776   "Sort articles by root subject."
3777   (string-lessp
3778    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3779    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3780
3781 (defun gnus-thread-sort-by-subject (h1 h2)
3782   "Sort threads by root subject."
3783   (gnus-article-sort-by-subject
3784    (gnus-thread-header h1) (gnus-thread-header h2)))
3785
3786 (defsubst gnus-article-sort-by-date (h1 h2)
3787   "Sort articles by root article date."
3788   (time-less-p
3789    (gnus-date-get-time (mail-header-date h1))
3790    (gnus-date-get-time (mail-header-date h2))))
3791
3792 (defun gnus-thread-sort-by-date (h1 h2)
3793   "Sort threads by root article date."
3794   (gnus-article-sort-by-date
3795    (gnus-thread-header h1) (gnus-thread-header h2)))
3796
3797 (defsubst gnus-article-sort-by-score (h1 h2)
3798   "Sort articles by root article score.
3799 Unscored articles will be counted as having a score of zero."
3800   (> (or (cdr (assq (mail-header-number h1)
3801                     gnus-newsgroup-scored))
3802          gnus-summary-default-score 0)
3803      (or (cdr (assq (mail-header-number h2)
3804                     gnus-newsgroup-scored))
3805          gnus-summary-default-score 0)))
3806
3807 (defun gnus-thread-sort-by-score (h1 h2)
3808   "Sort threads by root article score."
3809   (gnus-article-sort-by-score
3810    (gnus-thread-header h1) (gnus-thread-header h2)))
3811
3812 (defun gnus-thread-sort-by-total-score (h1 h2)
3813   "Sort threads by the sum of all scores in the thread.
3814 Unscored articles will be counted as having a score of zero."
3815   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3816
3817 (defun gnus-thread-total-score (thread)
3818   ;; This function find the total score of THREAD.
3819   (cond ((null thread)
3820          0)
3821         ((consp thread)
3822          (if (stringp (car thread))
3823              (apply gnus-thread-score-function 0
3824                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3825            (gnus-thread-total-score-1 thread)))
3826         (t
3827          (gnus-thread-total-score-1 (list thread)))))
3828
3829 (defun gnus-thread-total-score-1 (root)
3830   ;; This function find the total score of the thread below ROOT.
3831   (setq root (car root))
3832   (apply gnus-thread-score-function
3833          (or (append
3834               (mapcar 'gnus-thread-total-score
3835                       (cdr (gnus-id-to-thread (mail-header-id root))))
3836               (when (> (mail-header-number root) 0)
3837                 (list (or (cdr (assq (mail-header-number root)
3838                                      gnus-newsgroup-scored))
3839                           gnus-summary-default-score 0))))
3840              (list gnus-summary-default-score)
3841              '(0))))
3842
3843 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3844 (defvar gnus-tmp-prev-subject nil)
3845 (defvar gnus-tmp-false-parent nil)
3846 (defvar gnus-tmp-root-expunged nil)
3847 (defvar gnus-tmp-dummy-line nil)
3848
3849 (defvar gnus-tmp-header)
3850 (defun gnus-extra-header (type &optional header)
3851   "Return the extra header of TYPE."
3852   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3853       ""))
3854
3855 (defun gnus-summary-prepare-threads (threads)
3856   "Prepare summary buffer from THREADS and indentation LEVEL.
3857 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3858 or a straight list of headers."
3859   (gnus-message 7 "Generating summary...")
3860
3861   (setq gnus-newsgroup-threads threads)
3862   (beginning-of-line)
3863
3864   (let ((gnus-tmp-level 0)
3865         (default-score (or gnus-summary-default-score 0))
3866         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3867         thread number subject stack state gnus-tmp-gathered beg-match
3868         new-roots gnus-tmp-new-adopts thread-end
3869         gnus-tmp-header gnus-tmp-unread
3870         gnus-tmp-replied gnus-tmp-subject-or-nil
3871         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3872         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3873         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3874
3875     (setq gnus-tmp-prev-subject nil)
3876
3877     (if (vectorp (car threads))
3878         ;; If this is a straight (sic) list of headers, then a
3879         ;; threaded summary display isn't required, so we just create
3880         ;; an unthreaded one.
3881         (gnus-summary-prepare-unthreaded threads)
3882
3883       ;; Do the threaded display.
3884
3885       (while (or threads stack gnus-tmp-new-adopts new-roots)
3886
3887         (if (and (= gnus-tmp-level 0)
3888                  (or (not stack)
3889                      (= (caar stack) 0))
3890                  (not gnus-tmp-false-parent)
3891                  (or gnus-tmp-new-adopts new-roots))
3892             (if gnus-tmp-new-adopts
3893                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3894                       thread (list (car gnus-tmp-new-adopts))
3895                       gnus-tmp-header (caar thread)
3896                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3897               (when new-roots
3898                 (setq thread (list (car new-roots))
3899                       gnus-tmp-header (caar thread)
3900                       new-roots (cdr new-roots))))
3901
3902           (if threads
3903               ;; If there are some threads, we do them before the
3904               ;; threads on the stack.
3905               (setq thread threads
3906                     gnus-tmp-header (caar thread))
3907             ;; There were no current threads, so we pop something off
3908             ;; the stack.
3909             (setq state (car stack)
3910                   gnus-tmp-level (car state)
3911                   thread (cdr state)
3912                   stack (cdr stack)
3913                   gnus-tmp-header (caar thread))))
3914
3915         (setq gnus-tmp-false-parent nil)
3916         (setq gnus-tmp-root-expunged nil)
3917         (setq thread-end nil)
3918
3919         (if (stringp gnus-tmp-header)
3920             ;; The header is a dummy root.
3921             (cond
3922              ((eq gnus-summary-make-false-root 'adopt)
3923               ;; We let the first article adopt the rest.
3924               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3925                                                (cddar thread)))
3926               (setq gnus-tmp-gathered
3927                     (nconc (mapcar
3928                             (lambda (h) (mail-header-number (car h)))
3929                             (cddar thread))
3930                            gnus-tmp-gathered))
3931               (setq thread (cons (list (caar thread)
3932                                        (cadar thread))
3933                                  (cdr thread)))
3934               (setq gnus-tmp-level -1
3935                     gnus-tmp-false-parent t))
3936              ((eq gnus-summary-make-false-root 'empty)
3937               ;; We print adopted articles with empty subject fields.
3938               (setq gnus-tmp-gathered
3939                     (nconc (mapcar
3940                             (lambda (h) (mail-header-number (car h)))
3941                             (cddar thread))
3942                            gnus-tmp-gathered))
3943               (setq gnus-tmp-level -1))
3944              ((eq gnus-summary-make-false-root 'dummy)
3945               ;; We remember that we probably want to output a dummy
3946               ;; root.
3947               (setq gnus-tmp-dummy-line gnus-tmp-header)
3948               (setq gnus-tmp-prev-subject gnus-tmp-header))
3949              (t
3950               ;; We do not make a root for the gathered
3951               ;; sub-threads at all.
3952               (setq gnus-tmp-level -1)))
3953
3954           (setq number (mail-header-number gnus-tmp-header)
3955                 subject (mail-header-subject gnus-tmp-header))
3956
3957           (cond
3958            ;; If the thread has changed subject, we might want to make
3959            ;; this subthread into a root.
3960            ((and (null gnus-thread-ignore-subject)
3961                  (not (zerop gnus-tmp-level))
3962                  gnus-tmp-prev-subject
3963                  (not (inline
3964                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3965             (setq new-roots (nconc new-roots (list (car thread)))
3966                   thread-end t
3967                   gnus-tmp-header nil))
3968            ;; If the article lies outside the current limit,
3969            ;; then we do not display it.
3970            ((not (memq number gnus-newsgroup-limit))
3971             (setq gnus-tmp-gathered
3972                   (nconc (mapcar
3973                           (lambda (h) (mail-header-number (car h)))
3974                           (cdar thread))
3975                          gnus-tmp-gathered))
3976             (setq gnus-tmp-new-adopts (if (cdar thread)
3977                                           (append gnus-tmp-new-adopts
3978                                                   (cdar thread))
3979                                         gnus-tmp-new-adopts)
3980                   thread-end t
3981                   gnus-tmp-header nil)
3982             (when (zerop gnus-tmp-level)
3983               (setq gnus-tmp-root-expunged t)))
3984            ;; Perhaps this article is to be marked as read?
3985            ((and gnus-summary-mark-below
3986                  (< (or (cdr (assq number gnus-newsgroup-scored))
3987                         default-score)
3988                     gnus-summary-mark-below)
3989                  ;; Don't touch sparse articles.
3990                  (not (gnus-summary-article-sparse-p number))
3991                  (not (gnus-summary-article-ancient-p number)))
3992             (setq gnus-newsgroup-unreads
3993                   (delq number gnus-newsgroup-unreads))
3994             (if gnus-newsgroup-auto-expire
3995                 (push number gnus-newsgroup-expirable)
3996               (push (cons number gnus-low-score-mark)
3997                     gnus-newsgroup-reads))))
3998
3999           (when gnus-tmp-header
4000             ;; We may have an old dummy line to output before this
4001             ;; article.
4002             (when (and gnus-tmp-dummy-line
4003                        (gnus-subject-equal
4004                         gnus-tmp-dummy-line
4005                         (mail-header-subject gnus-tmp-header)))
4006               (gnus-summary-insert-dummy-line
4007                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4008               (setq gnus-tmp-dummy-line nil))
4009
4010             ;; Compute the mark.
4011             (setq gnus-tmp-unread (gnus-article-mark number))
4012
4013             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4014                                   gnus-tmp-header gnus-tmp-level)
4015                   gnus-newsgroup-data)
4016
4017             ;; Actually insert the line.
4018             (setq
4019              gnus-tmp-subject-or-nil
4020              (cond
4021               ((and gnus-thread-ignore-subject
4022                     gnus-tmp-prev-subject
4023                     (not (inline (gnus-subject-equal
4024                                   gnus-tmp-prev-subject subject))))
4025                subject)
4026               ((zerop gnus-tmp-level)
4027                (if (and (eq gnus-summary-make-false-root 'empty)
4028                         (memq number gnus-tmp-gathered)
4029                         gnus-tmp-prev-subject
4030                         (inline (gnus-subject-equal
4031                                  gnus-tmp-prev-subject subject)))
4032                    gnus-summary-same-subject
4033                  subject))
4034               (t gnus-summary-same-subject)))
4035             (if (and (eq gnus-summary-make-false-root 'adopt)
4036                      (= gnus-tmp-level 1)
4037                      (memq number gnus-tmp-gathered))
4038                 (setq gnus-tmp-opening-bracket ?\<
4039                       gnus-tmp-closing-bracket ?\>)
4040               (setq gnus-tmp-opening-bracket ?\[
4041                     gnus-tmp-closing-bracket ?\]))
4042             (setq
4043              gnus-tmp-indentation
4044              (aref gnus-thread-indent-array gnus-tmp-level)
4045              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4046              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4047                                 gnus-summary-default-score 0)
4048              gnus-tmp-score-char
4049              (if (or (null gnus-summary-default-score)
4050                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4051                          gnus-summary-zcore-fuzz))
4052                  ?  ;Whitespace
4053                (if (< gnus-tmp-score gnus-summary-default-score)
4054                    gnus-score-below-mark gnus-score-over-mark))
4055              gnus-tmp-replied
4056              (cond ((memq number gnus-newsgroup-processable)
4057                     gnus-process-mark)
4058                    ((memq number gnus-newsgroup-cached)
4059                     gnus-cached-mark)
4060                    ((memq number gnus-newsgroup-replied)
4061                     gnus-replied-mark)
4062                    ((memq number gnus-newsgroup-saved)
4063                     gnus-saved-mark)
4064                    (t gnus-unread-mark))
4065              gnus-tmp-from (mail-header-from gnus-tmp-header)
4066              gnus-tmp-name
4067              (cond
4068               ((string-match "<[^>]+> *$" gnus-tmp-from)
4069                (setq beg-match (match-beginning 0))
4070                (or (and (string-match "^\".+\"" gnus-tmp-from)
4071                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4072                    (substring gnus-tmp-from 0 beg-match)))
4073               ((string-match "(.+)" gnus-tmp-from)
4074                (substring gnus-tmp-from
4075                           (1+ (match-beginning 0)) (1- (match-end 0))))
4076               (t gnus-tmp-from)))
4077             (when (string= gnus-tmp-name "")
4078               (setq gnus-tmp-name gnus-tmp-from))
4079             (unless (numberp gnus-tmp-lines)
4080               (setq gnus-tmp-lines 0))
4081             (gnus-put-text-property
4082              (point)
4083              (progn (eval gnus-summary-line-format-spec) (point))
4084              'gnus-number number)
4085             (when gnus-visual-p
4086               (forward-line -1)
4087               (gnus-run-hooks 'gnus-summary-update-hook)
4088               (forward-line 1))
4089
4090             (setq gnus-tmp-prev-subject subject)))
4091
4092         (when (nth 1 thread)
4093           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4094         (incf gnus-tmp-level)
4095         (setq threads (if thread-end nil (cdar thread)))
4096         (unless threads
4097           (setq gnus-tmp-level 0)))))
4098   (gnus-message 7 "Generating summary...done"))
4099
4100 (defun gnus-summary-prepare-unthreaded (headers)
4101   "Generate an unthreaded summary buffer based on HEADERS."
4102   (let (header number mark)
4103
4104     (beginning-of-line)
4105
4106     (while headers
4107       ;; We may have to root out some bad articles...
4108       (when (memq (setq number (mail-header-number
4109                                 (setq header (pop headers))))
4110                   gnus-newsgroup-limit)
4111         ;; Mark article as read when it has a low score.
4112         (when (and gnus-summary-mark-below
4113                    (< (or (cdr (assq number gnus-newsgroup-scored))
4114                           gnus-summary-default-score 0)
4115                       gnus-summary-mark-below)
4116                    (not (gnus-summary-article-ancient-p number)))
4117           (setq gnus-newsgroup-unreads
4118                 (delq number gnus-newsgroup-unreads))
4119           (if gnus-newsgroup-auto-expire
4120               (push number gnus-newsgroup-expirable)
4121             (push (cons number gnus-low-score-mark)
4122                   gnus-newsgroup-reads)))
4123
4124         (setq mark (gnus-article-mark number))
4125         (push (gnus-data-make number mark (1+ (point)) header 0)
4126               gnus-newsgroup-data)
4127         (gnus-summary-insert-line
4128          header 0 number
4129          mark (memq number gnus-newsgroup-replied)
4130          (memq number gnus-newsgroup-expirable)
4131          (mail-header-subject header) nil
4132          (cdr (assq number gnus-newsgroup-scored))
4133          (memq number gnus-newsgroup-processable))))))
4134
4135 (defun gnus-summary-remove-list-identifiers ()
4136   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4137   (let ((regexp (if (stringp gnus-list-identifiers)
4138                     gnus-list-identifiers
4139                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4140     (dolist (header gnus-newsgroup-headers)
4141       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4142                                   " *\\)\\)+\\(Re: +\\)?\\)")
4143                           (mail-header-subject header))
4144         (mail-header-set-subject
4145          header (concat (substring (mail-header-subject header)
4146                                    0 (match-beginning 1))
4147                         (or
4148                          (match-string 3 (mail-header-subject header))
4149                          (match-string 5 (mail-header-subject header)))
4150                         (substring (mail-header-subject header)
4151                                    (match-end 1))))))))
4152
4153 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4154   "Select newsgroup GROUP.
4155 If READ-ALL is non-nil, all articles in the group are selected.
4156 If SELECT-ARTICLES, only select those articles from GROUP."
4157   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4158          ;;!!! Dirty hack; should be removed.
4159          (gnus-summary-ignore-duplicates
4160           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4161               t
4162             gnus-summary-ignore-duplicates))
4163          (info (nth 2 entry))
4164          articles fetched-articles cached)
4165
4166     (unless (gnus-check-server
4167              (setq gnus-current-select-method
4168                    (gnus-find-method-for-group group)))
4169       (error "Couldn't open server"))
4170
4171     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4172         (gnus-activate-group group)     ; Or we can activate it...
4173         (progn                          ; Or we bug out.
4174           (when (equal major-mode 'gnus-summary-mode)
4175             (kill-buffer (current-buffer)))
4176           (error "Couldn't activate group %s: %s"
4177                  group (gnus-status-message group))))
4178
4179     (unless (gnus-request-group group t)
4180       (when (equal major-mode 'gnus-summary-mode)
4181         (kill-buffer (current-buffer)))
4182       (error "Couldn't request group %s: %s"
4183              group (gnus-status-message group)))
4184
4185     (setq gnus-newsgroup-name group)
4186     (setq gnus-newsgroup-unselected nil)
4187     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4188     (gnus-summary-setup-default-charset)
4189
4190     ;; Adjust and set lists of article marks.
4191     (when info
4192       (gnus-adjust-marked-articles info))
4193
4194     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4195     (when (gnus-virtual-group-p group)
4196       (setq cached gnus-newsgroup-cached))
4197
4198     (setq gnus-newsgroup-unreads
4199           (gnus-set-difference
4200            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4201            gnus-newsgroup-dormant))
4202
4203     (setq gnus-newsgroup-processable nil)
4204
4205     (gnus-update-read-articles group gnus-newsgroup-unreads)
4206
4207     (if (setq articles select-articles)
4208         (setq gnus-newsgroup-unselected
4209               (gnus-sorted-intersection
4210                gnus-newsgroup-unreads
4211                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4212       (setq articles (gnus-articles-to-read group read-all)))
4213
4214     (cond
4215      ((null articles)
4216       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4217       'quit)
4218      ((eq articles 0) nil)
4219      (t
4220       ;; Init the dependencies hash table.
4221       (setq gnus-newsgroup-dependencies
4222             (gnus-make-hashtable (length articles)))
4223       (gnus-set-global-variables)
4224       ;; Retrieve the headers and read them in.
4225       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4226       (setq gnus-newsgroup-headers
4227             (if (eq 'nov
4228                     (setq gnus-headers-retrieved-by
4229                           (gnus-retrieve-headers
4230                            articles gnus-newsgroup-name
4231                            ;; We might want to fetch old headers, but
4232                            ;; not if there is only 1 article.
4233                            (and (or (and
4234                                      (not (eq gnus-fetch-old-headers 'some))
4235                                      (not (numberp gnus-fetch-old-headers)))
4236                                     (> (length articles) 1))
4237                                 gnus-fetch-old-headers))))
4238                 (gnus-get-newsgroup-headers-xover
4239                  articles nil nil gnus-newsgroup-name t)
4240               (gnus-get-newsgroup-headers)))
4241       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4242
4243       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4244       (when cached
4245         (setq gnus-newsgroup-cached cached))
4246
4247       ;; Suppress duplicates?
4248       (when gnus-suppress-duplicates
4249         (gnus-dup-suppress-articles))
4250
4251       ;; Set the initial limit.
4252       (setq gnus-newsgroup-limit (copy-sequence articles))
4253       ;; Remove canceled articles from the list of unread articles.
4254       (setq gnus-newsgroup-unreads
4255             (gnus-set-sorted-intersection
4256              gnus-newsgroup-unreads
4257              (setq fetched-articles
4258                    (mapcar (lambda (headers) (mail-header-number headers))
4259                            gnus-newsgroup-headers))))
4260       ;; Removed marked articles that do not exist.
4261       (gnus-update-missing-marks
4262        (gnus-sorted-complement fetched-articles articles))
4263       ;; We might want to build some more threads first.
4264       (when (and gnus-fetch-old-headers
4265                  (eq gnus-headers-retrieved-by 'nov))
4266         (if (eq gnus-fetch-old-headers 'invisible)
4267             (gnus-build-all-threads)
4268           (gnus-build-old-threads)))
4269       ;; Let the Gnus agent mark articles as read.
4270       (when gnus-agent
4271         (gnus-agent-get-undownloaded-list))
4272       ;; Remove list identifiers from subject
4273       (when gnus-list-identifiers
4274         (gnus-summary-remove-list-identifiers))
4275       ;; Check whether auto-expire is to be done in this group.
4276       (setq gnus-newsgroup-auto-expire
4277             (gnus-group-auto-expirable-p group))
4278       ;; Set up the article buffer now, if necessary.
4279       (unless gnus-single-article-buffer
4280         (gnus-article-setup-buffer))
4281       ;; First and last article in this newsgroup.
4282       (when gnus-newsgroup-headers
4283         (setq gnus-newsgroup-begin
4284               (mail-header-number (car gnus-newsgroup-headers))
4285               gnus-newsgroup-end
4286               (mail-header-number
4287                (gnus-last-element gnus-newsgroup-headers))))
4288       ;; GROUP is successfully selected.
4289       (or gnus-newsgroup-headers t)))))
4290
4291 (defun gnus-articles-to-read (group &optional read-all)
4292   "Find out what articles the user wants to read."
4293   (let* ((articles
4294           ;; Select all articles if `read-all' is non-nil, or if there
4295           ;; are no unread articles.
4296           (if (or read-all
4297                   (and (zerop (length gnus-newsgroup-marked))
4298                        (zerop (length gnus-newsgroup-unreads)))
4299                   (eq (gnus-group-find-parameter group 'display)
4300                       'all))
4301               (or
4302                (gnus-uncompress-range (gnus-active group))
4303                (gnus-cache-articles-in-group group))
4304             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4305                           (copy-sequence gnus-newsgroup-unreads))
4306                   '<)))
4307          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4308          (scored (length scored-list))
4309          (number (length articles))
4310          (marked (+ (length gnus-newsgroup-marked)
4311                     (length gnus-newsgroup-dormant)))
4312          (select
4313           (cond
4314            ((numberp read-all)
4315             read-all)
4316            (t
4317             (condition-case ()
4318                 (cond
4319                  ((and (or (<= scored marked) (= scored number))
4320                        (numberp gnus-large-newsgroup)
4321                        (> number gnus-large-newsgroup))
4322                   (let ((input
4323                          (read-string
4324                           (format
4325                            "How many articles from %s (default %d): "
4326                            (gnus-limit-string gnus-newsgroup-name 35)
4327                            number))))
4328                     (if (string-match "^[ \t]*$" input) number input)))
4329                  ((and (> scored marked) (< scored number)
4330                        (> (- scored number) 20))
4331                   (let ((input
4332                          (read-string
4333                           (format "%s %s (%d scored, %d total): "
4334                                   "How many articles from"
4335                                   group scored number))))
4336                     (if (string-match "^[ \t]*$" input)
4337                         number input)))
4338                  (t number))
4339               (quit
4340                (message "Quit getting the articles to read")
4341                nil))))))
4342     (setq select (if (stringp select) (string-to-number select) select))
4343     (if (or (null select) (zerop select))
4344         select
4345       (if (and (not (zerop scored)) (<= (abs select) scored))
4346           (progn
4347             (setq articles (sort scored-list '<))
4348             (setq number (length articles)))
4349         (setq articles (copy-sequence articles)))
4350
4351       (when (< (abs select) number)
4352         (if (< select 0)
4353             ;; Select the N oldest articles.
4354             (setcdr (nthcdr (1- (abs select)) articles) nil)
4355           ;; Select the N most recent articles.
4356           (setq articles (nthcdr (- number select) articles))))
4357       (setq gnus-newsgroup-unselected
4358             (gnus-sorted-intersection
4359              gnus-newsgroup-unreads
4360              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4361       (when gnus-alter-articles-to-read-function
4362         (setq gnus-newsgroup-unreads
4363               (sort 
4364                (funcall gnus-alter-articles-to-read-function
4365                         gnus-newsgroup-name gnus-newsgroup-unreads)
4366                '<)))
4367       articles)))
4368
4369 (defun gnus-killed-articles (killed articles)
4370   (let (out)
4371     (while articles
4372       (when (inline (gnus-member-of-range (car articles) killed))
4373         (push (car articles) out))
4374       (setq articles (cdr articles)))
4375     out))
4376
4377 (defun gnus-uncompress-marks (marks)
4378   "Uncompress the mark ranges in MARKS."
4379   (let ((uncompressed '(score bookmark))
4380         out)
4381     (while marks
4382       (if (memq (caar marks) uncompressed)
4383           (push (car marks) out)
4384         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4385       (setq marks (cdr marks)))
4386     out))
4387
4388 (defun gnus-adjust-marked-articles (info)
4389   "Set all article lists and remove all marks that are no longer valid."
4390   (let* ((marked-lists (gnus-info-marks info))
4391          (active (gnus-active (gnus-info-group info)))
4392          (min (car active))
4393          (max (cdr active))
4394          (types gnus-article-mark-lists)
4395          (uncompressed '(score bookmark killed))
4396          marks var articles article mark)
4397
4398     (while marked-lists
4399       (setq marks (pop marked-lists))
4400       (set (setq var (intern (format "gnus-newsgroup-%s"
4401                                      (car (rassq (setq mark (car marks))
4402                                                  types)))))
4403            (if (memq (car marks) uncompressed) (cdr marks)
4404              (gnus-uncompress-range (cdr marks))))
4405
4406       (setq articles (symbol-value var))
4407
4408       ;; All articles have to be subsets of the active articles.
4409       (cond
4410        ;; Adjust "simple" lists.
4411        ((memq mark '(tick dormant expire reply save))
4412         (while articles
4413           (when (or (< (setq article (pop articles)) min) (> article max))
4414             (set var (delq article (symbol-value var))))))
4415        ;; Adjust assocs.
4416        ((memq mark uncompressed)
4417         (when (not (listp (cdr (symbol-value var))))
4418           (set var (list (symbol-value var))))
4419         (when (not (listp (cdr articles)))
4420           (setq articles (list articles)))
4421         (while articles
4422           (when (or (not (consp (setq article (pop articles))))
4423                     (< (car article) min)
4424                     (> (car article) max))
4425             (set var (delq article (symbol-value var))))))))))
4426
4427 (defun gnus-update-missing-marks (missing)
4428   "Go through the list of MISSING articles and remove them from the mark lists."
4429   (when missing
4430     (let ((types gnus-article-mark-lists)
4431           var m)
4432       ;; Go through all types.
4433       (while types
4434         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4435         (when (symbol-value var)
4436           ;; This list has articles.  So we delete all missing articles
4437           ;; from it.
4438           (setq m missing)
4439           (while m
4440             (set var (delq (pop m) (symbol-value var)))))))))
4441
4442 (defun gnus-update-marks ()
4443   "Enter the various lists of marked articles into the newsgroup info list."
4444   (let ((types gnus-article-mark-lists)
4445         (info (gnus-get-info gnus-newsgroup-name))
4446         (uncompressed '(score bookmark killed))
4447         type list newmarked symbol delta-marks)
4448     (when info
4449       ;; Add all marks lists to the list of marks lists.
4450       (while (setq type (pop types))
4451         (setq list (symbol-value
4452                     (setq symbol
4453                           (intern (format "gnus-newsgroup-%s"
4454                                           (car type))))))
4455
4456         (when list
4457           ;; Get rid of the entries of the articles that have the
4458           ;; default score.
4459           (when (and (eq (cdr type) 'score)
4460                      gnus-save-score
4461                      list)
4462             (let* ((arts list)
4463                    (prev (cons nil list))
4464                    (all prev))
4465               (while arts
4466                 (if (or (not (consp (car arts)))
4467                         (= (cdar arts) gnus-summary-default-score))
4468                     (setcdr prev (cdr arts))
4469                   (setq prev arts))
4470                 (setq arts (cdr arts)))
4471               (setq list (cdr all)))))
4472
4473         (unless (memq (cdr type) uncompressed)
4474           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4475        
4476         (when (gnus-check-backend-function
4477                'request-set-mark gnus-newsgroup-name)
4478           ;; propagate flags to server, with the following exceptions:
4479           ;; uncompressed:s are not proper flags (they are cons cells)
4480           ;; cache is a internal gnus flag
4481           ;; download are local to one gnus installation (well)
4482           ;; unsend are for nndraft groups only
4483           ;; xxx: generality of this?  this suits nnimap anyway
4484           (unless (memq (cdr type) (append '(cache download unsend)
4485                                            uncompressed))
4486             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4487                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4488                    (add (gnus-remove-from-range
4489                          (gnus-copy-sequence list) old)))
4490               (when add
4491                 (push (list add 'add (list (cdr type))) delta-marks))
4492               (when del
4493                 (push (list del 'del (list (cdr type))) delta-marks)))))
4494           
4495         (when list
4496           (push (cons (cdr type) list) newmarked)))
4497
4498       (when delta-marks
4499         (unless (gnus-check-group gnus-newsgroup-name)
4500           (error "Can't open server for %s" gnus-newsgroup-name))
4501         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4502           
4503       ;; Enter these new marks into the info of the group.
4504       (if (nthcdr 3 info)
4505           (setcar (nthcdr 3 info) newmarked)
4506         ;; Add the marks lists to the end of the info.
4507         (when newmarked
4508           (setcdr (nthcdr 2 info) (list newmarked))))
4509
4510       ;; Cut off the end of the info if there's nothing else there.
4511       (let ((i 5))
4512         (while (and (> i 2)
4513                     (not (nth i info)))
4514           (when (nthcdr (decf i) info)
4515             (setcdr (nthcdr i info) nil)))))))
4516
4517 (defun gnus-set-mode-line (where)
4518   "Set the mode line of the article or summary buffers.
4519 If WHERE is `summary', the summary mode line format will be used."
4520   ;; Is this mode line one we keep updated?
4521   (when (and (memq where gnus-updated-mode-lines)
4522              (symbol-value
4523               (intern (format "gnus-%s-mode-line-format-spec" where))))
4524     (let (mode-string)
4525       (save-excursion
4526         ;; We evaluate this in the summary buffer since these
4527         ;; variables are buffer-local to that buffer.
4528         (set-buffer gnus-summary-buffer)
4529         ;; We bind all these variables that are used in the `eval' form
4530         ;; below.
4531         (let* ((mformat (symbol-value
4532                          (intern
4533                           (format "gnus-%s-mode-line-format-spec" where))))
4534                (gnus-tmp-group-name (gnus-group-name-decode 
4535                                      gnus-newsgroup-name
4536                                      (gnus-group-name-charset 
4537                                       nil
4538                                       gnus-newsgroup-name)))
4539                (gnus-tmp-article-number (or gnus-current-article 0))
4540                (gnus-tmp-unread gnus-newsgroup-unreads)
4541                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4542                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4543                (gnus-tmp-unread-and-unselected
4544                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4545                             (zerop gnus-tmp-unselected))
4546                        "")
4547                       ((zerop gnus-tmp-unselected)
4548                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4549                       (t (format "{%d(+%d) more}"
4550                                  gnus-tmp-unread-and-unticked
4551                                  gnus-tmp-unselected))))
4552                (gnus-tmp-subject
4553                 (if (and gnus-current-headers
4554                          (vectorp gnus-current-headers))
4555                     (gnus-mode-string-quote
4556                      (mail-header-subject gnus-current-headers))
4557                   ""))
4558                bufname-length max-len
4559                gnus-tmp-header);; passed as argument to any user-format-funcs
4560           (setq mode-string (eval mformat))
4561           (setq bufname-length (if (string-match "%b" mode-string)
4562                                    (- (length
4563                                        (buffer-name
4564                                         (if (eq where 'summary)
4565                                             nil
4566                                           (get-buffer gnus-article-buffer))))
4567                                       2)
4568                                  0))
4569           (setq max-len (max 4 (if gnus-mode-non-string-length
4570                                    (- (window-width)
4571                                       gnus-mode-non-string-length
4572                                       bufname-length)
4573                                  (length mode-string))))
4574           ;; We might have to chop a bit of the string off...
4575           (when (> (length mode-string) max-len)
4576             (setq mode-string
4577                   (concat (truncate-string-to-width mode-string (- max-len 3))
4578                           "...")))
4579           ;; Pad the mode string a bit.
4580           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4581       ;; Update the mode line.
4582       (setq mode-line-buffer-identification
4583             (gnus-mode-line-buffer-identification (list mode-string)))
4584       (set-buffer-modified-p t))))
4585
4586 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4587   "Go through the HEADERS list and add all Xrefs to a hash table.
4588 The resulting hash table is returned, or nil if no Xrefs were found."
4589   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4590          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4591          (xref-hashtb (gnus-make-hashtable))
4592          start group entry number xrefs header)
4593     (while headers
4594       (setq header (pop headers))
4595       (when (and (setq xrefs (mail-header-xref header))
4596                  (not (memq (setq number (mail-header-number header))
4597                             unreads)))
4598         (setq start 0)
4599         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4600           (setq start (match-end 0))
4601           (setq group (if prefix
4602                           (concat prefix (substring xrefs (match-beginning 1)
4603                                                     (match-end 1)))
4604                         (substring xrefs (match-beginning 1) (match-end 1))))
4605           (setq number
4606                 (string-to-int (substring xrefs (match-beginning 2)
4607                                           (match-end 2))))
4608           (if (setq entry (gnus-gethash group xref-hashtb))
4609               (setcdr entry (cons number (cdr entry)))
4610             (gnus-sethash group (cons number nil) xref-hashtb)))))
4611     (and start xref-hashtb)))
4612
4613 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4614   "Look through all the headers and mark the Xrefs as read."
4615   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4616         name entry info xref-hashtb idlist method nth4)
4617     (save-excursion
4618       (set-buffer gnus-group-buffer)
4619       (when (setq xref-hashtb
4620                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4621         (mapatoms
4622          (lambda (group)
4623            (unless (string= from-newsgroup (setq name (symbol-name group)))
4624              (setq idlist (symbol-value group))
4625              ;; Dead groups are not updated.
4626              (and (prog1
4627                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4628                             info (nth 2 entry))
4629                     (when (stringp (setq nth4 (gnus-info-method info)))
4630                       (setq nth4 (gnus-server-to-method nth4))))
4631                   ;; Only do the xrefs if the group has the same
4632                   ;; select method as the group we have just read.
4633                   (or (gnus-methods-equal-p
4634                        nth4 (gnus-find-method-for-group from-newsgroup))
4635                       virtual
4636                       (equal nth4 (setq method (gnus-find-method-for-group
4637                                                 from-newsgroup)))
4638                       (and (equal (car nth4) (car method))
4639                            (equal (nth 1 nth4) (nth 1 method))))
4640                   gnus-use-cross-reference
4641                   (or (not (eq gnus-use-cross-reference t))
4642                       virtual
4643                       ;; Only do cross-references on subscribed
4644                       ;; groups, if that is what is wanted.
4645                       (<= (gnus-info-level info) gnus-level-subscribed))
4646                   (gnus-group-make-articles-read name idlist))))
4647          xref-hashtb)))))
4648
4649 (defun gnus-compute-read-articles (group articles)
4650   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4651          (info (nth 2 entry))
4652          (active (gnus-active group))
4653          ninfo)
4654     (when entry
4655       ;; First peel off all invalid article numbers.
4656       (when active
4657         (let ((ids articles)
4658               id first)
4659           (while (setq id (pop ids))
4660             (when (and first (> id (cdr active)))
4661               ;; We'll end up in this situation in one particular
4662               ;; obscure situation.  If you re-scan a group and get
4663               ;; a new article that is cross-posted to a different
4664               ;; group that has not been re-scanned, you might get
4665               ;; crossposted article that has a higher number than
4666               ;; Gnus believes possible.  So we re-activate this
4667               ;; group as well.  This might mean doing the
4668               ;; crossposting thingy will *increase* the number
4669               ;; of articles in some groups.  Tsk, tsk.
4670               (setq active (or (gnus-activate-group group) active)))
4671             (when (or (> id (cdr active))
4672                       (< id (car active)))
4673               (setq articles (delq id articles))))))
4674       ;; If the read list is nil, we init it.
4675       (if (and active
4676                (null (gnus-info-read info))
4677                (> (car active) 1))
4678           (setq ninfo (cons 1 (1- (car active))))
4679         (setq ninfo (gnus-info-read info)))
4680       ;; Then we add the read articles to the range.
4681       (gnus-add-to-range
4682        ninfo (setq articles (sort articles '<))))))
4683
4684 (defun gnus-group-make-articles-read (group articles)
4685   "Update the info of GROUP to say that ARTICLES are read."
4686   (let* ((num 0)
4687          (entry (gnus-gethash group gnus-newsrc-hashtb))
4688          (info (nth 2 entry))
4689          (active (gnus-active group))
4690          range)
4691     (when entry
4692       (setq range (gnus-compute-read-articles group articles))
4693       (save-excursion
4694         (set-buffer gnus-group-buffer)
4695         (gnus-undo-register
4696           `(progn
4697              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4698              (gnus-info-set-read ',info ',(gnus-info-read info))
4699              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4700              (gnus-group-update-group ,group t))))
4701       ;; Add the read articles to the range.
4702       (gnus-info-set-read info range)
4703       ;; Then we have to re-compute how many unread
4704       ;; articles there are in this group.
4705       (when active
4706         (cond
4707          ((not range)
4708           (setq num (- (1+ (cdr active)) (car active))))
4709          ((not (listp (cdr range)))
4710           (setq num (- (cdr active) (- (1+ (cdr range))
4711                                        (car range)))))
4712          (t
4713           (while range
4714             (if (numberp (car range))
4715                 (setq num (1+ num))
4716               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4717             (setq range (cdr range)))
4718           (setq num (- (cdr active) num))))
4719         ;; Update the number of unread articles.
4720         (setcar entry num)
4721         ;; Update the group buffer.
4722         (gnus-group-update-group group t)))))
4723
4724 (defvar gnus-newsgroup-none-id 0)
4725
4726 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4727   (let ((cur nntp-server-buffer)
4728         (dependencies
4729          (or dependencies
4730              (save-excursion (set-buffer gnus-summary-buffer)
4731                              gnus-newsgroup-dependencies)))
4732         headers id end ref
4733         (mail-parse-charset gnus-newsgroup-charset)
4734         (mail-parse-ignored-charsets
4735          (save-excursion (condition-case nil
4736                              (set-buffer gnus-summary-buffer)
4737                            (error))
4738                          gnus-newsgroup-ignored-charsets)))
4739     (save-excursion
4740       (set-buffer nntp-server-buffer)
4741       ;; Translate all TAB characters into SPACE characters.
4742       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4743       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4744       (gnus-run-hooks 'gnus-parse-headers-hook)
4745       (let ((case-fold-search t)
4746             in-reply-to header p lines chars)
4747         (goto-char (point-min))
4748         ;; Search to the beginning of the next header.  Error messages
4749         ;; do not begin with 2 or 3.
4750         (while (re-search-forward "^[23][0-9]+ " nil t)
4751           (setq id nil
4752                 ref nil)
4753           ;; This implementation of this function, with nine
4754           ;; search-forwards instead of the one re-search-forward and
4755           ;; a case (which basically was the old function) is actually
4756           ;; about twice as fast, even though it looks messier.  You
4757           ;; can't have everything, I guess.  Speed and elegance
4758           ;; doesn't always go hand in hand.
4759           (setq
4760            header
4761            (vector
4762             ;; Number.
4763             (prog1
4764                 (read cur)
4765               (end-of-line)
4766               (setq p (point))
4767               (narrow-to-region (point)
4768                                 (or (and (search-forward "\n.\n" nil t)
4769                                          (- (point) 2))
4770                                     (point))))
4771             ;; Subject.
4772             (progn
4773               (goto-char p)
4774               (if (search-forward "\nsubject: " nil t)
4775                   (funcall gnus-decode-encoded-word-function
4776                            (nnheader-header-value))
4777                 "(none)"))
4778             ;; From.
4779             (progn
4780               (goto-char p)
4781               (if (or (search-forward "\nfrom: " nil t)
4782                       (search-forward "\nfrom:" nil t))
4783                   (funcall gnus-decode-encoded-word-function
4784                            (nnheader-header-value))
4785                 "(nobody)"))
4786             ;; Date.
4787             (progn
4788               (goto-char p)
4789               (if (search-forward "\ndate: " nil t)
4790                   (nnheader-header-value) ""))
4791             ;; Message-ID.
4792             (progn
4793               (goto-char p)
4794               (setq id (if (re-search-forward
4795                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4796                            ;; We do it this way to make sure the Message-ID
4797                            ;; is (somewhat) syntactically valid.
4798                            (buffer-substring (match-beginning 1)
4799                                              (match-end 1))
4800                          ;; If there was no message-id, we just fake one
4801                          ;; to make subsequent routines simpler.
4802                          (nnheader-generate-fake-message-id))))
4803             ;; References.
4804             (progn
4805               (goto-char p)
4806               (if (search-forward "\nreferences: " nil t)
4807                   (progn
4808                     (setq end (point))
4809                     (prog1
4810                         (nnheader-header-value)
4811                       (setq ref
4812                             (buffer-substring
4813                              (progn
4814                                (end-of-line)
4815                                (search-backward ">" end t)
4816                                (1+ (point)))
4817                              (progn
4818                                (search-backward "<" end t)
4819                                (point))))))
4820                 ;; Get the references from the in-reply-to header if there
4821                 ;; were no references and the in-reply-to header looks
4822                 ;; promising.
4823                 (if (and (search-forward "\nin-reply-to: " nil t)
4824                          (setq in-reply-to (nnheader-header-value))
4825                          (string-match "<[^>]+>" in-reply-to))
4826                     (let (ref2)
4827                       (setq ref (substring in-reply-to (match-beginning 0)
4828                                            (match-end 0)))
4829                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4830                         (setq ref2 (substring in-reply-to (match-beginning 0)
4831                                               (match-end 0)))
4832                         (when (> (length ref2) (length ref))
4833                           (setq ref ref2)))
4834                       ref)
4835                   (setq ref nil))))
4836             ;; Chars.
4837             (progn
4838               (goto-char p)
4839               (if (search-forward "\nchars: " nil t)
4840                   (if (numberp (setq chars (ignore-errors (read cur))))
4841                       chars 0)
4842                 0))
4843             ;; Lines.
4844             (progn
4845               (goto-char p)
4846               (if (search-forward "\nlines: " nil t)
4847                   (if (numberp (setq lines (ignore-errors (read cur))))
4848                       lines 0)
4849                 0))
4850             ;; Xref.
4851             (progn
4852               (goto-char p)
4853               (and (search-forward "\nxref: " nil t)
4854                    (nnheader-header-value)))
4855             ;; Extra.
4856             (when gnus-extra-headers
4857               (let ((extra gnus-extra-headers)
4858                     out)
4859                 (while extra
4860                   (goto-char p)
4861                   (when (search-forward
4862                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4863                     (push (cons (car extra) (nnheader-header-value))
4864                           out))
4865                   (pop extra))
4866                 out))))
4867           (when (equal id ref)
4868             (setq ref nil))
4869
4870           (when gnus-alter-header-function
4871             (funcall gnus-alter-header-function header)
4872             (setq id (mail-header-id header)
4873                   ref (gnus-parent-id (mail-header-references header))))
4874
4875           (when (setq header
4876                       (gnus-dependencies-add-header
4877                        header dependencies force-new))
4878             (push header headers))
4879           (goto-char (point-max))
4880           (widen))
4881         (nreverse headers)))))
4882
4883 ;; Goes through the xover lines and returns a list of vectors
4884 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4885                                                   force-new dependencies
4886                                                   group also-fetch-heads)
4887   "Parse the news overview data in the server buffer.
4888 Return a list of headers that match SEQUENCE (see
4889 `nntp-retrieve-headers')."
4890   ;; Get the Xref when the users reads the articles since most/some
4891   ;; NNTP servers do not include Xrefs when using XOVER.
4892   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4893   (let ((mail-parse-charset gnus-newsgroup-charset)
4894         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4895         (cur nntp-server-buffer)
4896         (dependencies (or dependencies gnus-newsgroup-dependencies))
4897         number headers header)
4898     (save-excursion
4899       (set-buffer nntp-server-buffer)
4900       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4901       ;; Allow the user to mangle the headers before parsing them.
4902       (gnus-run-hooks 'gnus-parse-headers-hook)
4903       (goto-char (point-min))
4904       (while (not (eobp))
4905         (condition-case ()
4906             (while (and sequence (not (eobp)))
4907               (setq number (read cur))
4908               (while (and sequence
4909                           (< (car sequence) number))
4910                 (setq sequence (cdr sequence)))
4911               (and sequence
4912                    (eq number (car sequence))
4913                    (progn
4914                      (setq sequence (cdr sequence))
4915                      (setq header (inline
4916                                     (gnus-nov-parse-line
4917                                      number dependencies force-new))))
4918                    (push header headers))
4919               (forward-line 1))
4920           (error
4921            (gnus-error 4 "Strange nov line (%d)"
4922                        (count-lines (point-min) (point)))))
4923         (forward-line 1))
4924       ;; A common bug in inn is that if you have posted an article and
4925       ;; then retrieves the active file, it will answer correctly --
4926       ;; the new article is included.  However, a NOV entry for the
4927       ;; article may not have been generated yet, so this may fail.
4928       ;; We work around this problem by retrieving the last few
4929       ;; headers using HEAD.
4930       (if (or (not also-fetch-heads)
4931               (not sequence))
4932           ;; We (probably) got all the headers.
4933           (nreverse headers)
4934         (let ((gnus-nov-is-evil t))
4935           (nconc
4936            (nreverse headers)
4937            (when (gnus-retrieve-headers sequence group)
4938              (gnus-get-newsgroup-headers))))))))
4939
4940 (defun gnus-article-get-xrefs ()
4941   "Fill in the Xref value in `gnus-current-headers', if necessary.
4942 This is meant to be called in `gnus-article-internal-prepare-hook'."
4943   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4944                                  gnus-current-headers)))
4945     (or (not gnus-use-cross-reference)
4946         (not headers)
4947         (and (mail-header-xref headers)
4948              (not (string= (mail-header-xref headers) "")))
4949         (let ((case-fold-search t)
4950               xref)
4951           (save-restriction
4952             (nnheader-narrow-to-headers)
4953             (goto-char (point-min))
4954             (when (or (and (not (eobp))
4955                            (eq (downcase (char-after)) ?x)
4956                            (looking-at "Xref:"))
4957                       (search-forward "\nXref:" nil t))
4958               (goto-char (1+ (match-end 0)))
4959               (setq xref (buffer-substring (point)
4960                                            (progn (end-of-line) (point))))
4961               (mail-header-set-xref headers xref)))))))
4962
4963 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4964   "Find article ID and insert the summary line for that article.
4965 OLD-HEADER can either be a header or a line number to insert
4966 the subject line on."
4967   (let* ((line (and (numberp old-header) old-header))
4968          (old-header (and (vectorp old-header) old-header))
4969          (header (cond ((and old-header use-old-header)
4970                         old-header)
4971                        ((and (numberp id)
4972                              (gnus-number-to-header id))
4973                         (gnus-number-to-header id))
4974                        (t
4975                         (gnus-read-header id))))
4976          (number (and (numberp id) id))
4977          d)
4978     (when header
4979       ;; Rebuild the thread that this article is part of and go to the
4980       ;; article we have fetched.
4981       (when (and (not gnus-show-threads)
4982                  old-header)
4983         (when (and number
4984                    (setq d (gnus-data-find (mail-header-number old-header))))
4985           (goto-char (gnus-data-pos d))
4986           (gnus-data-remove
4987            number
4988            (- (gnus-point-at-bol)
4989               (prog1
4990                   (1+ (gnus-point-at-eol))
4991                 (gnus-delete-line))))))
4992       (when old-header
4993         (mail-header-set-number header (mail-header-number old-header)))
4994       (setq gnus-newsgroup-sparse
4995             (delq (setq number (mail-header-number header))
4996                   gnus-newsgroup-sparse))
4997       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4998       (push number gnus-newsgroup-limit)
4999       (gnus-rebuild-thread (mail-header-id header) line)
5000       (gnus-summary-goto-subject number nil t))
5001     (when (and (numberp number)
5002                (> number 0))
5003       ;; We have to update the boundaries even if we can't fetch the
5004       ;; article if ID is a number -- so that the next `P' or `N'
5005       ;; command will fetch the previous (or next) article even
5006       ;; if the one we tried to fetch this time has been canceled.
5007       (when (> number gnus-newsgroup-end)
5008         (setq gnus-newsgroup-end number))
5009       (when (< number gnus-newsgroup-begin)
5010         (setq gnus-newsgroup-begin number))
5011       (setq gnus-newsgroup-unselected
5012             (delq number gnus-newsgroup-unselected)))
5013     ;; Report back a success?
5014     (and header (mail-header-number header))))
5015
5016 ;;; Process/prefix in the summary buffer
5017
5018 (defun gnus-summary-work-articles (n)
5019   "Return a list of articles to be worked upon.
5020 The prefix argument, the list of process marked articles, and the
5021 current article will be taken into consideration."
5022   (save-excursion
5023     (set-buffer gnus-summary-buffer)
5024     (cond
5025      (n
5026       ;; A numerical prefix has been given.
5027       (setq n (prefix-numeric-value n))
5028       (let ((backward (< n 0))
5029             (n (abs (prefix-numeric-value n)))
5030             articles article)
5031         (save-excursion
5032           (while
5033               (and (> n 0)
5034                    (push (setq article (gnus-summary-article-number))
5035                          articles)
5036                    (if backward
5037                        (gnus-summary-find-prev nil article)
5038                      (gnus-summary-find-next nil article)))
5039             (decf n)))
5040         (nreverse articles)))
5041      ((and (gnus-region-active-p) (mark))
5042       (message "region active")
5043       ;; Work on the region between point and mark.
5044       (let ((max (max (point) (mark)))
5045             articles article)
5046         (save-excursion
5047           (goto-char (min (min (point) (mark))))
5048           (while
5049               (and
5050                (push (setq article (gnus-summary-article-number)) articles)
5051                (gnus-summary-find-next nil article)
5052                (< (point) max)))
5053           (nreverse articles))))
5054      (gnus-newsgroup-processable
5055       ;; There are process-marked articles present.
5056       ;; Save current state.
5057       (gnus-summary-save-process-mark)
5058       ;; Return the list.
5059       (reverse gnus-newsgroup-processable))
5060      (t
5061       ;; Just return the current article.
5062       (list (gnus-summary-article-number))))))
5063
5064 (defmacro gnus-summary-iterate (arg &rest forms)
5065   "Iterate over the process/prefixed articles and do FORMS.
5066 ARG is the interactive prefix given to the command.  FORMS will be
5067 executed with point over the summary line of the articles."
5068   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5069     `(let ((,articles (gnus-summary-work-articles ,arg)))
5070        (while ,articles
5071          (gnus-summary-goto-subject (car ,articles))
5072          ,@forms
5073          (pop ,articles)))))
5074
5075 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5076 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5077
5078 (defun gnus-summary-save-process-mark ()
5079   "Push the current set of process marked articles on the stack."
5080   (interactive)
5081   (push (copy-sequence gnus-newsgroup-processable)
5082         gnus-newsgroup-process-stack))
5083
5084 (defun gnus-summary-kill-process-mark ()
5085   "Push the current set of process marked articles on the stack and unmark."
5086   (interactive)
5087   (gnus-summary-save-process-mark)
5088   (gnus-summary-unmark-all-processable))
5089
5090 (defun gnus-summary-yank-process-mark ()
5091   "Pop the last process mark state off the stack and restore it."
5092   (interactive)
5093   (unless gnus-newsgroup-process-stack
5094     (error "Empty mark stack"))
5095   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5096
5097 (defun gnus-summary-process-mark-set (set)
5098   "Make SET into the current process marked articles."
5099   (gnus-summary-unmark-all-processable)
5100   (while set
5101     (gnus-summary-set-process-mark (pop set))))
5102
5103 ;;; Searching and stuff
5104
5105 (defun gnus-summary-search-group (&optional backward use-level)
5106   "Search for next unread newsgroup.
5107 If optional argument BACKWARD is non-nil, search backward instead."
5108   (save-excursion
5109     (set-buffer gnus-group-buffer)
5110     (when (gnus-group-search-forward
5111            backward nil (if use-level (gnus-group-group-level) nil))
5112       (gnus-group-group-name))))
5113
5114 (defun gnus-summary-best-group (&optional exclude-group)
5115   "Find the name of the best unread group.
5116 If EXCLUDE-GROUP, do not go to this group."
5117   (save-excursion
5118     (set-buffer gnus-group-buffer)
5119     (save-excursion
5120       (gnus-group-best-unread-group exclude-group))))
5121
5122 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5123   (if backward (gnus-summary-find-prev)
5124     (let* ((dummy (gnus-summary-article-intangible-p))
5125            (article (or article (gnus-summary-article-number)))
5126            (arts (gnus-data-find-list article))
5127            result)
5128       (when (and (not dummy)
5129                  (or (not gnus-summary-check-current)
5130                      (not unread)
5131                      (not (gnus-data-unread-p (car arts)))))
5132         (setq arts (cdr arts)))
5133       (when (setq result
5134                   (if unread
5135                       (progn
5136                         (while arts
5137                           (when (or (and undownloaded
5138                                          (eq gnus-undownloaded-mark
5139                                              (gnus-data-mark (car arts))))
5140                                     (gnus-data-unread-p (car arts)))
5141                             (setq result (car arts)
5142                                   arts nil))
5143                           (setq arts (cdr arts)))
5144                         result)
5145                     (car arts)))
5146         (goto-char (gnus-data-pos result))
5147         (gnus-data-number result)))))
5148
5149 (defun gnus-summary-find-prev (&optional unread article)
5150   (let* ((eobp (eobp))
5151          (article (or article (gnus-summary-article-number)))
5152          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5153          result)
5154     (when (and (not eobp)
5155                (or (not gnus-summary-check-current)
5156                    (not unread)
5157                    (not (gnus-data-unread-p (car arts)))))
5158       (setq arts (cdr arts)))
5159     (when (setq result
5160                 (if unread
5161                     (progn
5162                       (while arts
5163                         (when (gnus-data-unread-p (car arts))
5164                           (setq result (car arts)
5165                                 arts nil))
5166                         (setq arts (cdr arts)))
5167                       result)
5168                   (car arts)))
5169       (goto-char (gnus-data-pos result))
5170       (gnus-data-number result))))
5171
5172 (defun gnus-summary-find-subject (subject &optional unread backward article)
5173   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5174          (article (or article (gnus-summary-article-number)))
5175          (articles (gnus-data-list backward))
5176          (arts (gnus-data-find-list article articles))
5177          result)
5178     (when (or (not gnus-summary-check-current)
5179               (not unread)
5180               (not (gnus-data-unread-p (car arts))))
5181       (setq arts (cdr arts)))
5182     (while arts
5183       (and (or (not unread)
5184                (gnus-data-unread-p (car arts)))
5185            (vectorp (gnus-data-header (car arts)))
5186            (gnus-subject-equal
5187             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5188            (setq result (car arts)
5189                  arts nil))
5190       (setq arts (cdr arts)))
5191     (and result
5192          (goto-char (gnus-data-pos result))
5193          (gnus-data-number result))))
5194
5195 (defun gnus-summary-search-forward (&optional unread subject backward)
5196   "Search forward for an article.
5197 If UNREAD, look for unread articles.  If SUBJECT, look for
5198 articles with that subject.  If BACKWARD, search backward instead."
5199   (cond (subject (gnus-summary-find-subject subject unread backward))
5200         (backward (gnus-summary-find-prev unread))
5201         (t (gnus-summary-find-next unread))))
5202
5203 (defun gnus-recenter (&optional n)
5204   "Center point in window and redisplay frame.
5205 Also do horizontal recentering."
5206   (interactive "P")
5207   (when (and gnus-auto-center-summary
5208              (not (eq gnus-auto-center-summary 'vertical)))
5209     (gnus-horizontal-recenter))
5210   (recenter n))
5211
5212 (defun gnus-summary-recenter ()
5213   "Center point in the summary window.
5214 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5215 displayed, no centering will be performed."
5216   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5217   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5218   (interactive)
5219   (let* ((top (cond ((< (window-height) 4) 0)
5220                     ((< (window-height) 7) 1)
5221                     (t (if (numberp gnus-auto-center-summary)
5222                            gnus-auto-center-summary
5223                          2))))
5224          (height (1- (window-height)))
5225          (bottom (save-excursion (goto-char (point-max))
5226                                  (forward-line (- height))
5227                                  (point)))
5228          (window (get-buffer-window (current-buffer))))
5229     ;; The user has to want it.
5230     (when gnus-auto-center-summary
5231       (when (get-buffer-window gnus-article-buffer)
5232         ;; Only do recentering when the article buffer is displayed,
5233         ;; Set the window start to either `bottom', which is the biggest
5234         ;; possible valid number, or the second line from the top,
5235         ;; whichever is the least.
5236         (set-window-start
5237          window (min bottom (save-excursion
5238                               (forward-line (- top)) (point)))
5239          t))
5240       ;; Do horizontal recentering while we're at it.
5241       (when (and (get-buffer-window (current-buffer) t)
5242                  (not (eq gnus-auto-center-summary 'vertical)))
5243         (let ((selected (selected-window)))
5244           (select-window (get-buffer-window (current-buffer) t))
5245           (gnus-summary-position-point)
5246           (gnus-horizontal-recenter)
5247           (select-window selected))))))
5248
5249 (defun gnus-summary-jump-to-group (newsgroup)
5250   "Move point to NEWSGROUP in group mode buffer."
5251   ;; Keep update point of group mode buffer if visible.
5252   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5253       (save-window-excursion
5254         ;; Take care of tree window mode.
5255         (when (get-buffer-window gnus-group-buffer)
5256           (pop-to-buffer gnus-group-buffer))
5257         (gnus-group-jump-to-group newsgroup))
5258     (save-excursion
5259       ;; Take care of tree window mode.
5260       (if (get-buffer-window gnus-group-buffer)
5261           (pop-to-buffer gnus-group-buffer)
5262         (set-buffer gnus-group-buffer))
5263       (gnus-group-jump-to-group newsgroup))))
5264
5265 ;; This function returns a list of article numbers based on the
5266 ;; difference between the ranges of read articles in this group and
5267 ;; the range of active articles.
5268 (defun gnus-list-of-unread-articles (group)
5269   (let* ((read (gnus-info-read (gnus-get-info group)))
5270          (active (or (gnus-active group) (gnus-activate-group group)))
5271          (last (cdr active))
5272          first nlast unread)
5273     ;; If none are read, then all are unread.
5274     (if (not read)
5275         (setq first (car active))
5276       ;; If the range of read articles is a single range, then the
5277       ;; first unread article is the article after the last read
5278       ;; article.  Sounds logical, doesn't it?
5279       (if (and (not (listp (cdr read)))
5280                (or (< (car read) (car active))
5281                    (progn (setq read (list read))
5282                           nil)))
5283           (setq first (max (car active) (1+ (cdr read))))
5284         ;; `read' is a list of ranges.
5285         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5286                                   (caar read)))
5287                   1)
5288           (setq first (car active)))
5289         (while read
5290           (when first
5291             (while (< first nlast)
5292               (push first unread)
5293               (setq first (1+ first))))
5294           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5295           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5296           (setq read (cdr read)))))
5297     ;; And add the last unread articles.
5298     (while (<= first last)
5299       (push first unread)
5300       (setq first (1+ first)))
5301     ;; Return the list of unread articles.
5302     (delq 0 (nreverse unread))))
5303
5304 (defun gnus-list-of-read-articles (group)
5305   "Return a list of unread, unticked and non-dormant articles."
5306   (let* ((info (gnus-get-info group))
5307          (marked (gnus-info-marks info))
5308          (active (gnus-active group)))
5309     (and info active
5310          (gnus-set-difference
5311           (gnus-sorted-complement
5312            (gnus-uncompress-range active)
5313            (gnus-list-of-unread-articles group))
5314           (append
5315            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5316            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5317
5318 ;; Various summary commands
5319
5320 (defun gnus-summary-select-article-buffer ()
5321   "Reconfigure windows to show article buffer."
5322   (interactive)
5323   (if (not (gnus-buffer-live-p gnus-article-buffer))
5324       (error "There is no article buffer for this summary buffer")
5325     (gnus-configure-windows 'article)
5326     (select-window (get-buffer-window gnus-article-buffer))))
5327
5328 (defun gnus-summary-universal-argument (arg)
5329   "Perform any operation on all articles that are process/prefixed."
5330   (interactive "P")
5331   (let ((articles (gnus-summary-work-articles arg))
5332         func article)
5333     (if (eq
5334          (setq
5335           func
5336           (key-binding
5337            (read-key-sequence
5338             (substitute-command-keys
5339              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5340          'undefined)
5341         (gnus-error 1 "Undefined key")
5342       (save-excursion
5343         (while articles
5344           (gnus-summary-goto-subject (setq article (pop articles)))
5345           (let (gnus-newsgroup-processable)
5346             (command-execute func))
5347           (gnus-summary-remove-process-mark article)))))
5348   (gnus-summary-position-point))
5349
5350 (defun gnus-summary-toggle-truncation (&optional arg)
5351   "Toggle truncation of summary lines.
5352 With arg, turn line truncation on iff arg is positive."
5353   (interactive "P")
5354   (setq truncate-lines
5355         (if (null arg) (not truncate-lines)
5356           (> (prefix-numeric-value arg) 0)))
5357   (redraw-display))
5358
5359 (defun gnus-summary-reselect-current-group (&optional all rescan)
5360   "Exit and then reselect the current newsgroup.
5361 The prefix argument ALL means to select all articles."
5362   (interactive "P")
5363   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5364     (error "Ephemeral groups can't be reselected"))
5365   (let ((current-subject (gnus-summary-article-number))
5366         (group gnus-newsgroup-name))
5367     (setq gnus-newsgroup-begin nil)
5368     (gnus-summary-exit)
5369     ;; We have to adjust the point of group mode buffer because
5370     ;; point was moved to the next unread newsgroup by exiting.
5371     (gnus-summary-jump-to-group group)
5372     (when rescan
5373       (save-excursion
5374         (gnus-group-get-new-news-this-group 1)))
5375     (gnus-group-read-group all t)
5376     (gnus-summary-goto-subject current-subject nil t)))
5377
5378 (defun gnus-summary-rescan-group (&optional all)
5379   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5380   (interactive "P")
5381   (gnus-summary-reselect-current-group all t))
5382
5383 (defun gnus-summary-update-info (&optional non-destructive)
5384   (save-excursion
5385     (let ((group gnus-newsgroup-name))
5386       (when group
5387         (when gnus-newsgroup-kill-headers
5388           (setq gnus-newsgroup-killed
5389                 (gnus-compress-sequence
5390                  (nconc
5391                   (gnus-set-sorted-intersection
5392                    (gnus-uncompress-range gnus-newsgroup-killed)
5393                    (setq gnus-newsgroup-unselected
5394                          (sort gnus-newsgroup-unselected '<)))
5395                   (setq gnus-newsgroup-unreads
5396                         (sort gnus-newsgroup-unreads '<)))
5397                  t)))
5398         (unless (listp (cdr gnus-newsgroup-killed))
5399           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5400         (let ((headers gnus-newsgroup-headers))
5401           ;; Set the new ranges of read articles.
5402           (save-excursion
5403             (set-buffer gnus-group-buffer)
5404             (gnus-undo-force-boundary))
5405           (gnus-update-read-articles
5406            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5407           ;; Set the current article marks.
5408           (let ((gnus-newsgroup-scored
5409                  (if (and (not gnus-save-score)
5410                           (not non-destructive))
5411                      nil
5412                    gnus-newsgroup-scored)))
5413             (save-excursion
5414               (gnus-update-marks)))
5415           ;; Do the cross-ref thing.
5416           (when gnus-use-cross-reference
5417             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5418           ;; Do not switch windows but change the buffer to work.
5419           (set-buffer gnus-group-buffer)
5420           (unless (gnus-ephemeral-group-p group)
5421             (gnus-group-update-group group)))))))
5422
5423 (defun gnus-summary-save-newsrc (&optional force)
5424   "Save the current number of read/marked articles in the dribble buffer.
5425 The dribble buffer will then be saved.
5426 If FORCE (the prefix), also save the .newsrc file(s)."
5427   (interactive "P")
5428   (gnus-summary-update-info t)
5429   (if force
5430       (gnus-save-newsrc-file)
5431     (gnus-dribble-save)))
5432
5433 (defun gnus-summary-exit (&optional temporary)
5434   "Exit reading current newsgroup, and then return to group selection mode.
5435 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5436   (interactive)
5437   (gnus-set-global-variables)
5438   (when (gnus-buffer-live-p gnus-article-buffer)
5439     (save-excursion
5440       (set-buffer gnus-article-buffer)
5441       (mm-destroy-parts gnus-article-mime-handles)
5442       ;; Set it to nil for safety reason.
5443       (setq gnus-article-mime-handle-alist nil)
5444       (setq gnus-article-mime-handles nil)))
5445   (gnus-kill-save-kill-buffer)
5446   (gnus-async-halt-prefetch)
5447   (let* ((group gnus-newsgroup-name)
5448          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5449          (mode major-mode)
5450          (group-point nil)
5451          (buf (current-buffer)))
5452     (unless quit-config
5453       ;; Do adaptive scoring, and possibly save score files.
5454       (when gnus-newsgroup-adaptive
5455         (gnus-score-adaptive))
5456       (when gnus-use-scoring
5457         (gnus-score-save)))
5458     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5459     ;; If we have several article buffers, we kill them at exit.
5460     (unless gnus-single-article-buffer
5461       (gnus-kill-buffer gnus-original-article-buffer)
5462       (setq gnus-article-current nil))
5463     (when gnus-use-cache
5464       (gnus-cache-possibly-remove-articles)
5465       (gnus-cache-save-buffers))
5466     (gnus-async-prefetch-remove-group group)
5467     (when gnus-suppress-duplicates
5468       (gnus-dup-enter-articles))
5469     (when gnus-use-trees
5470       (gnus-tree-close group))
5471     (when gnus-use-cache
5472       (gnus-cache-write-active))
5473     ;; Remove entries for this group.
5474     (nnmail-purge-split-history (gnus-group-real-name group))
5475     ;; Make all changes in this group permanent.
5476     (unless quit-config
5477       (gnus-run-hooks 'gnus-exit-group-hook)
5478       (gnus-summary-update-info))
5479     (gnus-close-group group)
5480     ;; Make sure where we were, and go to next newsgroup.
5481     (set-buffer gnus-group-buffer)
5482     (unless quit-config
5483       (gnus-group-jump-to-group group))
5484     (gnus-run-hooks 'gnus-summary-exit-hook)
5485     (unless (or quit-config
5486                 ;; If this group has disappeared from the summary
5487                 ;; buffer, don't skip forwards.
5488                 (not (string= group (gnus-group-group-name))))
5489       (gnus-group-next-unread-group 1))
5490     (setq group-point (point))
5491     (if temporary
5492         nil                             ;Nothing to do.
5493       ;; If we have several article buffers, we kill them at exit.
5494       (unless gnus-single-article-buffer
5495         (gnus-kill-buffer gnus-article-buffer)
5496         (gnus-kill-buffer gnus-original-article-buffer)
5497         (setq gnus-article-current nil))
5498       (set-buffer buf)
5499       (if (not gnus-kill-summary-on-exit)
5500           (gnus-deaden-summary)
5501         ;; We set all buffer-local variables to nil.  It is unclear why
5502         ;; this is needed, but if we don't, buffer-local variables are
5503         ;; not garbage-collected, it seems.  This would the lead to en
5504         ;; ever-growing Emacs.
5505         (gnus-summary-clear-local-variables)
5506         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5507           (gnus-summary-clear-local-variables))
5508         (when (get-buffer gnus-article-buffer)
5509           (bury-buffer gnus-article-buffer))
5510         ;; We clear the global counterparts of the buffer-local
5511         ;; variables as well, just to be on the safe side.
5512         (set-buffer gnus-group-buffer)
5513         (gnus-summary-clear-local-variables)
5514         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5515           (gnus-summary-clear-local-variables))
5516         ;; Return to group mode buffer.
5517         (when (eq mode 'gnus-summary-mode)
5518           (gnus-kill-buffer buf)))
5519       (setq gnus-current-select-method gnus-select-method)
5520       (pop-to-buffer gnus-group-buffer)
5521       (if (not quit-config)
5522           (progn
5523             (goto-char group-point)
5524             (gnus-configure-windows 'group 'force))
5525         (gnus-handle-ephemeral-exit quit-config))
5526       ;; Clear the current group name.
5527       (unless quit-config
5528         (setq gnus-newsgroup-name nil)))))
5529
5530 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5531 (defun gnus-summary-exit-no-update (&optional no-questions)
5532   "Quit reading current newsgroup without updating read article info."
5533   (interactive)
5534   (let* ((group gnus-newsgroup-name)
5535          (quit-config (gnus-group-quit-config group)))
5536     (when (or no-questions
5537               gnus-expert-user
5538               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5539       (gnus-async-halt-prefetch)
5540       (mapcar 'funcall
5541               (delq 'gnus-summary-expire-articles
5542                     (copy-sequence gnus-summary-prepare-exit-hook)))
5543       (when (gnus-buffer-live-p gnus-article-buffer)
5544         (save-excursion
5545           (set-buffer gnus-article-buffer)
5546           (mm-destroy-parts gnus-article-mime-handles)
5547           ;; Set it to nil for safety reason.
5548           (setq gnus-article-mime-handle-alist nil)
5549           (setq gnus-article-mime-handles nil)))
5550       ;; If we have several article buffers, we kill them at exit.
5551       (unless gnus-single-article-buffer
5552         (gnus-kill-buffer gnus-article-buffer)
5553         (gnus-kill-buffer gnus-original-article-buffer)
5554         (setq gnus-article-current nil))
5555       (if (not gnus-kill-summary-on-exit)
5556           (gnus-deaden-summary)
5557         (gnus-close-group group)
5558         (gnus-summary-clear-local-variables)
5559         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5560           (gnus-summary-clear-local-variables))
5561         (set-buffer gnus-group-buffer)
5562         (gnus-summary-clear-local-variables)
5563         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5564           (gnus-summary-clear-local-variables))
5565         (when (get-buffer gnus-summary-buffer)
5566           (kill-buffer gnus-summary-buffer)))
5567       (unless gnus-single-article-buffer
5568         (setq gnus-article-current nil))
5569       (when gnus-use-trees
5570         (gnus-tree-close group))
5571       (gnus-async-prefetch-remove-group group)
5572       (when (get-buffer gnus-article-buffer)
5573         (bury-buffer gnus-article-buffer))
5574       ;; Return to the group buffer.
5575       (gnus-configure-windows 'group 'force)
5576       ;; Clear the current group name.
5577       (setq gnus-newsgroup-name nil)
5578       (when (equal (gnus-group-group-name) group)
5579         (gnus-group-next-unread-group 1))
5580       (when quit-config
5581         (gnus-handle-ephemeral-exit quit-config)))))
5582
5583 (defun gnus-handle-ephemeral-exit (quit-config)
5584   "Handle movement when leaving an ephemeral group.
5585 The state which existed when entering the ephemeral is reset."
5586   (if (not (buffer-name (car quit-config)))
5587       (gnus-configure-windows 'group 'force)
5588     (set-buffer (car quit-config))
5589     (cond ((eq major-mode 'gnus-summary-mode)
5590            (gnus-set-global-variables))
5591           ((eq major-mode 'gnus-article-mode)
5592            (save-excursion
5593              ;; The `gnus-summary-buffer' variable may point
5594              ;; to the old summary buffer when using a single
5595              ;; article buffer.
5596              (unless (gnus-buffer-live-p gnus-summary-buffer)
5597                (set-buffer gnus-group-buffer))
5598              (set-buffer gnus-summary-buffer)
5599              (gnus-set-global-variables))))
5600     (if (or (eq (cdr quit-config) 'article)
5601             (eq (cdr quit-config) 'pick))
5602         (progn
5603           ;; The current article may be from the ephemeral group
5604           ;; thus it is best that we reload this article
5605           (gnus-summary-show-article)
5606           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5607               (gnus-configure-windows 'pick 'force)
5608             (gnus-configure-windows (cdr quit-config) 'force)))
5609       (gnus-configure-windows (cdr quit-config) 'force))
5610     (when (eq major-mode 'gnus-summary-mode)
5611       (gnus-summary-next-subject 1 nil t)
5612       (gnus-summary-recenter)
5613       (gnus-summary-position-point))))
5614
5615 ;;; Dead summaries.
5616
5617 (defvar gnus-dead-summary-mode-map nil)
5618
5619 (unless gnus-dead-summary-mode-map
5620   (setq gnus-dead-summary-mode-map (make-keymap))
5621   (suppress-keymap gnus-dead-summary-mode-map)
5622   (substitute-key-definition
5623    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5624   (let ((keys '("\C-d" "\r" "\177" [delete])))
5625     (while keys
5626       (define-key gnus-dead-summary-mode-map
5627         (pop keys) 'gnus-summary-wake-up-the-dead))))
5628
5629 (defvar gnus-dead-summary-mode nil
5630   "Minor mode for Gnus summary buffers.")
5631
5632 (defun gnus-dead-summary-mode (&optional arg)
5633   "Minor mode for Gnus summary buffers."
5634   (interactive "P")
5635   (when (eq major-mode 'gnus-summary-mode)
5636     (make-local-variable 'gnus-dead-summary-mode)
5637     (setq gnus-dead-summary-mode
5638           (if (null arg) (not gnus-dead-summary-mode)
5639             (> (prefix-numeric-value arg) 0)))
5640     (when gnus-dead-summary-mode
5641       (gnus-add-minor-mode
5642        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5643
5644 (defun gnus-deaden-summary ()
5645   "Make the current summary buffer into a dead summary buffer."
5646   ;; Kill any previous dead summary buffer.
5647   (when (and gnus-dead-summary
5648              (buffer-name gnus-dead-summary))
5649     (save-excursion
5650       (set-buffer gnus-dead-summary)
5651       (when gnus-dead-summary-mode
5652         (kill-buffer (current-buffer)))))
5653   ;; Make this the current dead summary.
5654   (setq gnus-dead-summary (current-buffer))
5655   (gnus-dead-summary-mode 1)
5656   (let ((name (buffer-name)))
5657     (when (string-match "Summary" name)
5658       (rename-buffer
5659        (concat (substring name 0 (match-beginning 0)) "Dead "
5660                (substring name (match-beginning 0)))
5661        t)
5662       (bury-buffer))))
5663
5664 (defun gnus-kill-or-deaden-summary (buffer)
5665   "Kill or deaden the summary BUFFER."
5666   (save-excursion
5667     (when (and (buffer-name buffer)
5668                (not gnus-single-article-buffer))
5669       (save-excursion
5670         (set-buffer buffer)
5671         (gnus-kill-buffer gnus-article-buffer)
5672         (gnus-kill-buffer gnus-original-article-buffer)))
5673     (cond (gnus-kill-summary-on-exit
5674            (when (and gnus-use-trees
5675                       (gnus-buffer-exists-p buffer))
5676              (save-excursion
5677                (set-buffer buffer)
5678                (gnus-tree-close gnus-newsgroup-name)))
5679            (gnus-kill-buffer buffer))
5680           ((gnus-buffer-exists-p buffer)
5681            (save-excursion
5682              (set-buffer buffer)
5683              (gnus-deaden-summary))))))
5684
5685 (defun gnus-summary-wake-up-the-dead (&rest args)
5686   "Wake up the dead summary buffer."
5687   (interactive)
5688   (gnus-dead-summary-mode -1)
5689   (let ((name (buffer-name)))
5690     (when (string-match "Dead " name)
5691       (rename-buffer
5692        (concat (substring name 0 (match-beginning 0))
5693                (substring name (match-end 0)))
5694        t)))
5695   (gnus-message 3 "This dead summary is now alive again"))
5696
5697 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5698 (defun gnus-summary-fetch-faq (&optional faq-dir)
5699   "Fetch the FAQ for the current group.
5700 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5701 in."
5702   (interactive
5703    (list
5704     (when current-prefix-arg
5705       (completing-read
5706        "Faq dir: " (and (listp gnus-group-faq-directory)
5707                         (mapcar (lambda (file) (list file))
5708                                 gnus-group-faq-directory))))))
5709   (let (gnus-faq-buffer)
5710     (when (setq gnus-faq-buffer
5711                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5712       (gnus-configure-windows 'summary-faq))))
5713
5714 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5715 (defun gnus-summary-describe-group (&optional force)
5716   "Describe the current newsgroup."
5717   (interactive "P")
5718   (gnus-group-describe-group force gnus-newsgroup-name))
5719
5720 (defun gnus-summary-describe-briefly ()
5721   "Describe summary mode commands briefly."
5722   (interactive)
5723   (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")))
5724
5725 ;; Walking around group mode buffer from summary mode.
5726
5727 (defun gnus-summary-next-group (&optional no-article target-group backward)
5728   "Exit current newsgroup and then select next unread newsgroup.
5729 If prefix argument NO-ARTICLE is non-nil, no article is selected
5730 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5731 previous group instead."
5732   (interactive "P")
5733   ;; Stop pre-fetching.
5734   (gnus-async-halt-prefetch)
5735   (let ((current-group gnus-newsgroup-name)
5736         (current-buffer (current-buffer))
5737         entered)
5738     ;; First we semi-exit this group to update Xrefs and all variables.
5739     ;; We can't do a real exit, because the window conf must remain
5740     ;; the same in case the user is prompted for info, and we don't
5741     ;; want the window conf to change before that...
5742     (gnus-summary-exit t)
5743     (while (not entered)
5744       ;; Then we find what group we are supposed to enter.
5745       (set-buffer gnus-group-buffer)
5746       (gnus-group-jump-to-group current-group)
5747       (setq target-group
5748             (or target-group
5749                 (if (eq gnus-keep-same-level 'best)
5750                     (gnus-summary-best-group gnus-newsgroup-name)
5751                   (gnus-summary-search-group backward gnus-keep-same-level))))
5752       (if (not target-group)
5753           ;; There are no further groups, so we return to the group
5754           ;; buffer.
5755           (progn
5756             (gnus-message 5 "Returning to the group buffer")
5757             (setq entered t)
5758             (when (gnus-buffer-live-p current-buffer)
5759               (set-buffer current-buffer)
5760               (gnus-summary-exit))
5761             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5762         ;; We try to enter the target group.
5763         (gnus-group-jump-to-group target-group)
5764         (let ((unreads (gnus-group-group-unread)))
5765           (if (and (or (eq t unreads)
5766                        (and unreads (not (zerop unreads))))
5767                    (gnus-summary-read-group
5768                     target-group nil no-article
5769                     (and (buffer-name current-buffer) current-buffer)
5770                     nil backward))
5771               (setq entered t)
5772             (setq current-group target-group
5773                   target-group nil)))))))
5774
5775 (defun gnus-summary-prev-group (&optional no-article)
5776   "Exit current newsgroup and then select previous unread newsgroup.
5777 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5778   (interactive "P")
5779   (gnus-summary-next-group no-article nil t))
5780
5781 ;; Walking around summary lines.
5782
5783 (defun gnus-summary-first-subject (&optional unread undownloaded)
5784   "Go to the first unread subject.
5785 If UNREAD is non-nil, go to the first unread article.
5786 Returns the article selected or nil if there are no unread articles."
5787   (interactive "P")
5788   (prog1
5789       (cond
5790        ;; Empty summary.
5791        ((null gnus-newsgroup-data)
5792         (gnus-message 3 "No articles in the group")
5793         nil)
5794        ;; Pick the first article.
5795        ((not unread)
5796         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5797         (gnus-data-number (car gnus-newsgroup-data)))
5798        ;; No unread articles.
5799        ((null gnus-newsgroup-unreads)
5800         (gnus-message 3 "No more unread articles")
5801         nil)
5802        ;; Find the first unread article.
5803        (t
5804         (let ((data gnus-newsgroup-data))
5805           (while (and data
5806                       (and (not (and undownloaded
5807                                      (eq gnus-undownloaded-mark
5808                                          (gnus-data-mark (car data)))))
5809                            (not (gnus-data-unread-p (car data)))))
5810             (setq data (cdr data)))
5811           (when data
5812             (goto-char (gnus-data-pos (car data)))
5813             (gnus-data-number (car data))))))
5814     (gnus-summary-position-point)))
5815
5816 (defun gnus-summary-next-subject (n &optional unread dont-display)
5817   "Go to next N'th summary line.
5818 If N is negative, go to the previous N'th subject line.
5819 If UNREAD is non-nil, only unread articles are selected.
5820 The difference between N and the actual number of steps taken is
5821 returned."
5822   (interactive "p")
5823   (let ((backward (< n 0))
5824         (n (abs n)))
5825     (while (and (> n 0)
5826                 (if backward
5827                     (gnus-summary-find-prev unread)
5828                   (gnus-summary-find-next unread)))
5829       (unless (zerop (setq n (1- n)))
5830         (gnus-summary-show-thread)))
5831     (when (/= 0 n)
5832       (gnus-message 7 "No more%s articles"
5833                     (if unread " unread" "")))
5834     (unless dont-display
5835       (gnus-summary-recenter)
5836       (gnus-summary-position-point))
5837     n))
5838
5839 (defun gnus-summary-next-unread-subject (n)
5840   "Go to next N'th unread summary line."
5841   (interactive "p")
5842   (gnus-summary-next-subject n t))
5843
5844 (defun gnus-summary-prev-subject (n &optional unread)
5845   "Go to previous N'th summary line.
5846 If optional argument UNREAD is non-nil, only unread article is selected."
5847   (interactive "p")
5848   (gnus-summary-next-subject (- n) unread))
5849
5850 (defun gnus-summary-prev-unread-subject (n)
5851   "Go to previous N'th unread summary line."
5852   (interactive "p")
5853   (gnus-summary-next-subject (- n) t))
5854
5855 (defun gnus-summary-goto-subject (article &optional force silent)
5856   "Go the subject line of ARTICLE.
5857 If FORCE, also allow jumping to articles not currently shown."
5858   (interactive "nArticle number: ")
5859   (let ((b (point))
5860         (data (gnus-data-find article)))
5861     ;; We read in the article if we have to.
5862     (and (not data)
5863          force
5864          (gnus-summary-insert-subject
5865           article
5866           (if (or (numberp force) (vectorp force)) force)
5867           t)
5868          (setq data (gnus-data-find article)))
5869     (goto-char b)
5870     (if (not data)
5871         (progn
5872           (unless silent
5873             (gnus-message 3 "Can't find article %d" article))
5874           nil)
5875       (goto-char (gnus-data-pos data))
5876       (gnus-summary-position-point)
5877       article)))
5878
5879 ;; Walking around summary lines with displaying articles.
5880
5881 (defun gnus-summary-expand-window (&optional arg)
5882   "Make the summary buffer take up the entire Emacs frame.
5883 Given a prefix, will force an `article' buffer configuration."
5884   (interactive "P")
5885   (if arg
5886       (gnus-configure-windows 'article 'force)
5887     (gnus-configure-windows 'summary 'force)))
5888
5889 (defun gnus-summary-display-article (article &optional all-header)
5890   "Display ARTICLE in article buffer."
5891   (gnus-set-global-variables)
5892   (when (gnus-buffer-live-p gnus-article-buffer)
5893     (with-current-buffer gnus-article-buffer
5894       (setq gnus-article-charset gnus-newsgroup-charset)
5895       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
5896       (mm-enable-multibyte-mule4)))
5897   (if (null article)
5898       nil
5899     (prog1
5900         (if gnus-summary-display-article-function
5901             (funcall gnus-summary-display-article-function article all-header)
5902           (gnus-article-prepare article all-header))
5903       (gnus-run-hooks 'gnus-select-article-hook)
5904       (when (and gnus-current-article
5905                  (not (zerop gnus-current-article)))
5906         (gnus-summary-goto-subject gnus-current-article))
5907       (gnus-summary-recenter)
5908       (when (and gnus-use-trees gnus-show-threads)
5909         (gnus-possibly-generate-tree article)
5910         (gnus-highlight-selected-tree article))
5911       ;; Successfully display article.
5912       (gnus-article-set-window-start
5913        (cdr (assq article gnus-newsgroup-bookmarks))))))
5914
5915 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5916   "Select the current article.
5917 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5918 non-nil, the article will be re-fetched even if it already present in
5919 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5920 be displayed."
5921   ;; Make sure we are in the summary buffer to work around bbdb bug.
5922   (unless (eq major-mode 'gnus-summary-mode)
5923     (set-buffer gnus-summary-buffer))
5924   (let ((article (or article (gnus-summary-article-number)))
5925         (all-headers (not (not all-headers))) ;Must be T or NIL.
5926         gnus-summary-display-article-function)
5927     (and (not pseudo)
5928          (gnus-summary-article-pseudo-p article)
5929          (error "This is a pseudo-article"))
5930     (save-excursion
5931       (set-buffer gnus-summary-buffer)
5932       (if (or (and gnus-single-article-buffer
5933                    (or (null gnus-current-article)
5934                        (null gnus-article-current)
5935                        (null (get-buffer gnus-article-buffer))
5936                        (not (eq article (cdr gnus-article-current)))
5937                        (not (equal (car gnus-article-current)
5938                                    gnus-newsgroup-name))))
5939               (and (not gnus-single-article-buffer)
5940                    (or (null gnus-current-article)
5941                        (not (eq gnus-current-article article))))
5942               force)
5943           ;; The requested article is different from the current article.
5944           (progn
5945             (gnus-summary-display-article article all-headers)
5946             (when (gnus-buffer-live-p gnus-article-buffer)
5947                (with-current-buffer gnus-article-buffer
5948                 (if (not gnus-article-decoded-p) ;; a local variable
5949                     (mm-disable-multibyte-mule4))))
5950             (when (or all-headers gnus-show-all-headers)
5951               (gnus-article-show-all-headers))
5952             (gnus-article-set-window-start
5953              (cdr (assq article gnus-newsgroup-bookmarks)))
5954             article)
5955         (when (or all-headers gnus-show-all-headers)
5956           (gnus-article-show-all-headers))
5957         'old))))
5958
5959 (defun gnus-summary-force-verify-and-decrypt ()
5960   (interactive)
5961   (let ((mm-verify-option 'known)
5962         (mm-decrypt-option 'known))
5963     (gnus-summary-select-article nil 'force)))
5964
5965 (defun gnus-summary-set-current-mark (&optional current-mark)
5966   "Obsolete function."
5967   nil)
5968
5969 (defun gnus-summary-next-article (&optional unread subject backward push)
5970   "Select the next article.
5971 If UNREAD, only unread articles are selected.
5972 If SUBJECT, only articles with SUBJECT are selected.
5973 If BACKWARD, the previous article is selected instead of the next."
5974   (interactive "P")
5975   (cond
5976    ;; Is there such an article?
5977    ((and (gnus-summary-search-forward unread subject backward)
5978          (or (gnus-summary-display-article (gnus-summary-article-number))
5979              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5980     (gnus-summary-position-point))
5981    ;; If not, we try the first unread, if that is wanted.
5982    ((and subject
5983          gnus-auto-select-same
5984          (gnus-summary-first-unread-article))
5985     (gnus-summary-position-point)
5986     (gnus-message 6 "Wrapped"))
5987    ;; Try to get next/previous article not displayed in this group.
5988    ((and gnus-auto-extend-newsgroup
5989          (not unread) (not subject))
5990     (gnus-summary-goto-article
5991      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5992      nil (count-lines (point-min) (point))))
5993    ;; Go to next/previous group.
5994    (t
5995     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5996       (gnus-summary-jump-to-group gnus-newsgroup-name))
5997     (let ((cmd last-command-char)
5998           (point
5999            (save-excursion
6000              (set-buffer gnus-group-buffer)
6001              (point)))
6002           (group
6003            (if (eq gnus-keep-same-level 'best)
6004                (gnus-summary-best-group gnus-newsgroup-name)
6005              (gnus-summary-search-group backward gnus-keep-same-level))))
6006       ;; For some reason, the group window gets selected.  We change
6007       ;; it back.
6008       (select-window (get-buffer-window (current-buffer)))
6009       ;; Select next unread newsgroup automagically.
6010       (cond
6011        ((or (not gnus-auto-select-next)
6012             (not cmd))
6013         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6014        ((or (eq gnus-auto-select-next 'quietly)
6015             (and (eq gnus-auto-select-next 'slightly-quietly)
6016                  push)
6017             (and (eq gnus-auto-select-next 'almost-quietly)
6018                  (gnus-summary-last-article-p)))
6019         ;; Select quietly.
6020         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6021             (gnus-summary-exit)
6022           (gnus-message 7 "No more%s articles (%s)..."
6023                         (if unread " unread" "")
6024                         (if group (concat "selecting " group)
6025                           "exiting"))
6026           (gnus-summary-next-group nil group backward)))
6027        (t
6028         (when (gnus-key-press-event-p last-input-event)
6029           (gnus-summary-walk-group-buffer
6030            gnus-newsgroup-name cmd unread backward point))))))))
6031
6032 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6033   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6034                       (?\C-p (gnus-group-prev-unread-group 1))))
6035         (cursor-in-echo-area t)
6036         keve key group ended)
6037     (save-excursion
6038       (set-buffer gnus-group-buffer)
6039       (goto-char start)
6040       (setq group
6041             (if (eq gnus-keep-same-level 'best)
6042                 (gnus-summary-best-group gnus-newsgroup-name)
6043               (gnus-summary-search-group backward gnus-keep-same-level))))
6044     (while (not ended)
6045       (gnus-message
6046        5 "No more%s articles%s" (if unread " unread" "")
6047        (if (and group
6048                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6049            (format " (Type %s for %s [%s])"
6050                    (single-key-description cmd) group
6051                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6052          (format " (Type %s to exit %s)"
6053                  (single-key-description cmd)
6054                  gnus-newsgroup-name)))
6055       ;; Confirm auto selection.
6056       (setq key (car (setq keve (gnus-read-event-char))))
6057       (setq ended t)
6058       (cond
6059        ((assq key keystrokes)
6060         (let ((obuf (current-buffer)))
6061           (switch-to-buffer gnus-group-buffer)
6062           (when group
6063             (gnus-group-jump-to-group group))
6064           (eval (cadr (assq key keystrokes)))
6065           (setq group (gnus-group-group-name))
6066           (switch-to-buffer obuf))
6067         (setq ended nil))
6068        ((equal key cmd)
6069         (if (or (not group)
6070                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6071             (gnus-summary-exit)
6072           (gnus-summary-next-group nil group backward)))
6073        (t
6074         (push (cdr keve) unread-command-events))))))
6075
6076 (defun gnus-summary-next-unread-article ()
6077   "Select unread article after current one."
6078   (interactive)
6079   (gnus-summary-next-article
6080    (or (not (eq gnus-summary-goto-unread 'never))
6081        (gnus-summary-last-article-p (gnus-summary-article-number)))
6082    (and gnus-auto-select-same
6083         (gnus-summary-article-subject))))
6084
6085 (defun gnus-summary-prev-article (&optional unread subject)
6086   "Select the article after the current one.
6087 If UNREAD is non-nil, only unread articles are selected."
6088   (interactive "P")
6089   (gnus-summary-next-article unread subject t))
6090
6091 (defun gnus-summary-prev-unread-article ()
6092   "Select unread article before current one."
6093   (interactive)
6094   (gnus-summary-prev-article
6095    (or (not (eq gnus-summary-goto-unread 'never))
6096        (gnus-summary-first-article-p (gnus-summary-article-number)))
6097    (and gnus-auto-select-same
6098         (gnus-summary-article-subject))))
6099
6100 (defun gnus-summary-next-page (&optional lines circular)
6101   "Show next page of the selected article.
6102 If at the end of the current article, select the next article.
6103 LINES says how many lines should be scrolled up.
6104
6105 If CIRCULAR is non-nil, go to the start of the article instead of
6106 selecting the next article when reaching the end of the current
6107 article."
6108   (interactive "P")
6109   (setq gnus-summary-buffer (current-buffer))
6110   (gnus-set-global-variables)
6111   (let ((article (gnus-summary-article-number))
6112         (article-window (get-buffer-window gnus-article-buffer t))
6113         endp)
6114     ;; If the buffer is empty, we have no article.
6115     (unless article
6116       (error "No article to select"))
6117     (gnus-configure-windows 'article)
6118     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6119         (if (and (eq gnus-summary-goto-unread 'never)
6120                  (not (gnus-summary-last-article-p article)))
6121             (gnus-summary-next-article)
6122           (gnus-summary-next-unread-article))
6123       (if (or (null gnus-current-article)
6124               (null gnus-article-current)
6125               (/= article (cdr gnus-article-current))
6126               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6127           ;; Selected subject is different from current article's.
6128           (gnus-summary-display-article article)
6129         (when article-window
6130           (gnus-eval-in-buffer-window gnus-article-buffer
6131             (setq endp (gnus-article-next-page lines)))
6132           (when endp
6133             (cond (circular
6134                    (gnus-summary-beginning-of-article))
6135                   (lines
6136                    (gnus-message 3 "End of message"))
6137                   ((null lines)
6138                    (if (and (eq gnus-summary-goto-unread 'never)
6139                             (not (gnus-summary-last-article-p article)))
6140                        (gnus-summary-next-article)
6141                      (gnus-summary-next-unread-article))))))))
6142     (gnus-summary-recenter)
6143     (gnus-summary-position-point)))
6144
6145 (defun gnus-summary-prev-page (&optional lines move)
6146   "Show previous page of selected article.
6147 Argument LINES specifies lines to be scrolled down.
6148 If MOVE, move to the previous unread article if point is at
6149 the beginning of the buffer."
6150   (interactive "P")
6151   (let ((article (gnus-summary-article-number))
6152         (article-window (get-buffer-window gnus-article-buffer t))
6153         endp)
6154     (gnus-configure-windows 'article)
6155     (if (or (null gnus-current-article)
6156             (null gnus-article-current)
6157             (/= article (cdr gnus-article-current))
6158             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6159         ;; Selected subject is different from current article's.
6160         (gnus-summary-display-article article)
6161       (gnus-summary-recenter)
6162       (when article-window
6163         (gnus-eval-in-buffer-window gnus-article-buffer
6164           (setq endp (gnus-article-prev-page lines)))
6165         (when (and move endp)
6166           (cond (lines
6167                  (gnus-message 3 "Beginning of message"))
6168                 ((null lines)
6169                  (if (and (eq gnus-summary-goto-unread 'never)
6170                           (not (gnus-summary-first-article-p article)))
6171                      (gnus-summary-prev-article)
6172                    (gnus-summary-prev-unread-article))))))))
6173   (gnus-summary-position-point))
6174
6175 (defun gnus-summary-prev-page-or-article (&optional lines)
6176   "Show previous page of selected article.
6177 Argument LINES specifies lines to be scrolled down.
6178 If at the beginning of the article, go to the next article."
6179   (interactive "P")
6180   (gnus-summary-prev-page lines t))
6181
6182 (defun gnus-summary-scroll-up (lines)
6183   "Scroll up (or down) one line current article.
6184 Argument LINES specifies lines to be scrolled up (or down if negative)."
6185   (interactive "p")
6186   (gnus-configure-windows 'article)
6187   (gnus-summary-show-thread)
6188   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6189     (gnus-eval-in-buffer-window gnus-article-buffer
6190       (cond ((> lines 0)
6191              (when (gnus-article-next-page lines)
6192                (gnus-message 3 "End of message")))
6193             ((< lines 0)
6194              (gnus-article-prev-page (- lines))))))
6195   (gnus-summary-recenter)
6196   (gnus-summary-position-point))
6197
6198 (defun gnus-summary-scroll-down (lines)
6199   "Scroll down (or up) one line current article.
6200 Argument LINES specifies lines to be scrolled down (or up if negative)."
6201   (interactive "p")
6202   (gnus-summary-scroll-up (- lines)))
6203
6204 (defun gnus-summary-next-same-subject ()
6205   "Select next article which has the same subject as current one."
6206   (interactive)
6207   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6208
6209 (defun gnus-summary-prev-same-subject ()
6210   "Select previous article which has the same subject as current one."
6211   (interactive)
6212   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6213
6214 (defun gnus-summary-next-unread-same-subject ()
6215   "Select next unread article which has the same subject as current one."
6216   (interactive)
6217   (gnus-summary-next-article t (gnus-summary-article-subject)))
6218
6219 (defun gnus-summary-prev-unread-same-subject ()
6220   "Select previous unread article which has the same subject as current one."
6221   (interactive)
6222   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6223
6224 (defun gnus-summary-first-unread-article ()
6225   "Select the first unread article.
6226 Return nil if there are no unread articles."
6227   (interactive)
6228   (prog1
6229       (when (gnus-summary-first-subject t)
6230         (gnus-summary-show-thread)
6231         (gnus-summary-first-subject t)
6232         (gnus-summary-display-article (gnus-summary-article-number)))
6233     (gnus-summary-position-point)))
6234
6235 (defun gnus-summary-first-unread-subject ()
6236   "Place the point on the subject line of the first unread article.
6237 Return nil if there are no unread articles."
6238   (interactive)
6239   (prog1
6240       (when (gnus-summary-first-subject t)
6241         (gnus-summary-show-thread)
6242         (gnus-summary-first-subject t))
6243     (gnus-summary-position-point)))
6244
6245 (defun gnus-summary-first-article ()
6246   "Select the first article.
6247 Return nil if there are no articles."
6248   (interactive)
6249   (prog1
6250       (when (gnus-summary-first-subject)
6251         (gnus-summary-show-thread)
6252         (gnus-summary-first-subject)
6253         (gnus-summary-display-article (gnus-summary-article-number)))
6254     (gnus-summary-position-point)))
6255
6256 (defun gnus-summary-best-unread-article ()
6257   "Select the unread article with the highest score."
6258   (interactive)
6259   (let ((best -1000000)
6260         (data gnus-newsgroup-data)
6261         article score)
6262     (while data
6263       (and (gnus-data-unread-p (car data))
6264            (> (setq score
6265                     (gnus-summary-article-score (gnus-data-number (car data))))
6266               best)
6267            (setq best score
6268                  article (gnus-data-number (car data))))
6269       (setq data (cdr data)))
6270     (prog1
6271         (if article
6272             (gnus-summary-goto-article article)
6273           (error "No unread articles"))
6274       (gnus-summary-position-point))))
6275
6276 (defun gnus-summary-last-subject ()
6277   "Go to the last displayed subject line in the group."
6278   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6279     (when article
6280       (gnus-summary-goto-subject article))))
6281
6282 (defun gnus-summary-goto-article (article &optional all-headers force)
6283   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6284 If ALL-HEADERS is non-nil, no header lines are hidden.
6285 If FORCE, go to the article even if it isn't displayed.  If FORCE
6286 is a number, it is the line the article is to be displayed on."
6287   (interactive
6288    (list
6289     (completing-read
6290      "Article number or Message-ID: "
6291      (mapcar (lambda (number) (list (int-to-string number)))
6292              gnus-newsgroup-limit))
6293     current-prefix-arg
6294     t))
6295   (prog1
6296       (if (and (stringp article)
6297                (string-match "@" article))
6298           (gnus-summary-refer-article article)
6299         (when (stringp article)
6300           (setq article (string-to-number article)))
6301         (if (gnus-summary-goto-subject article force)
6302             (gnus-summary-display-article article all-headers)
6303           (gnus-message 4 "Couldn't go to article %s" article) nil))
6304     (gnus-summary-position-point)))
6305
6306 (defun gnus-summary-goto-last-article ()
6307   "Go to the previously read article."
6308   (interactive)
6309   (prog1
6310       (when gnus-last-article
6311         (gnus-summary-goto-article gnus-last-article nil t))
6312     (gnus-summary-position-point)))
6313
6314 (defun gnus-summary-pop-article (number)
6315   "Pop one article off the history and go to the previous.
6316 NUMBER articles will be popped off."
6317   (interactive "p")
6318   (let (to)
6319     (setq gnus-newsgroup-history
6320           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6321     (if to
6322         (gnus-summary-goto-article (car to) nil t)
6323       (error "Article history empty")))
6324   (gnus-summary-position-point))
6325
6326 ;; Summary commands and functions for limiting the summary buffer.
6327
6328 (defun gnus-summary-limit-to-articles (n)
6329   "Limit the summary buffer to the next N articles.
6330 If not given a prefix, use the process marked articles instead."
6331   (interactive "P")
6332   (prog1
6333       (let ((articles (gnus-summary-work-articles n)))
6334         (setq gnus-newsgroup-processable nil)
6335         (gnus-summary-limit articles))
6336     (gnus-summary-position-point)))
6337
6338 (defun gnus-summary-pop-limit (&optional total)
6339   "Restore the previous limit.
6340 If given a prefix, remove all limits."
6341   (interactive "P")
6342   (when total
6343     (setq gnus-newsgroup-limits
6344           (list (mapcar (lambda (h) (mail-header-number h))
6345                         gnus-newsgroup-headers))))
6346   (unless gnus-newsgroup-limits
6347     (error "No limit to pop"))
6348   (prog1
6349       (gnus-summary-limit nil 'pop)
6350     (gnus-summary-position-point)))
6351
6352 (defun gnus-summary-limit-to-subject (subject &optional header)
6353   "Limit the summary buffer to articles that have subjects that match a regexp."
6354   (interactive "sLimit to subject (regexp): ")
6355   (unless header
6356     (setq header "subject"))
6357   (when (not (equal "" subject))
6358     (prog1
6359         (let ((articles (gnus-summary-find-matching
6360                          (or header "subject") subject 'all)))
6361           (unless articles
6362             (error "Found no matches for \"%s\"" subject))
6363           (gnus-summary-limit articles))
6364       (gnus-summary-position-point))))
6365
6366 (defun gnus-summary-limit-to-author (from)
6367   "Limit the summary buffer to articles that have authors that match a regexp."
6368   (interactive "sLimit to author (regexp): ")
6369   (gnus-summary-limit-to-subject from "from"))
6370
6371 (defun gnus-summary-limit-to-age (age &optional younger-p)
6372   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6373 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6374 articles that are younger than AGE days."
6375   (interactive
6376    (let ((younger current-prefix-arg)
6377          (days-got nil)
6378          days)
6379      (while (not days-got)
6380        (setq days (if younger
6381                       (read-string "Limit to articles within (in days): ")
6382                     (read-string "Limit to articles older than (in days): ")))
6383        (when (> (length days) 0)
6384          (setq days (read days)))
6385        (if (numberp days)
6386            (setq days-got t)
6387          (message "Please enter a number.")
6388          (sleep-for 1)))
6389      (list days younger)))
6390   (prog1
6391       (let ((data gnus-newsgroup-data)
6392             (cutoff (days-to-time age))
6393             articles d date is-younger)
6394         (while (setq d (pop data))
6395           (when (and (vectorp (gnus-data-header d))
6396                      (setq date (mail-header-date (gnus-data-header d))))
6397             (setq is-younger (time-less-p
6398                               (time-since (condition-case ()
6399                                               (date-to-time date)
6400                                             (error '(0 0))))
6401                               cutoff))
6402             (when (if younger-p
6403                       is-younger
6404                     (not is-younger))
6405               (push (gnus-data-number d) articles))))
6406         (gnus-summary-limit (nreverse articles)))
6407     (gnus-summary-position-point)))
6408
6409 (defun gnus-summary-limit-to-extra (header regexp)
6410   "Limit the summary buffer to articles that match an 'extra' header."
6411   (interactive
6412    (let ((header
6413           (intern
6414            (gnus-completing-read
6415             (symbol-name (car gnus-extra-headers))
6416             "Limit extra header:"
6417             (mapcar (lambda (x)
6418                       (cons (symbol-name x) x))
6419                     gnus-extra-headers)
6420             nil
6421             t))))
6422      (list header
6423            (read-string (format "Limit to header %s (regexp): " header)))))
6424   (when (not (equal "" regexp))
6425     (prog1
6426         (let ((articles (gnus-summary-find-matching
6427                          (cons 'extra header) regexp 'all)))
6428           (unless articles
6429             (error "Found no matches for \"%s\"" regexp))
6430           (gnus-summary-limit articles))
6431       (gnus-summary-position-point))))
6432
6433 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6434 (make-obsolete
6435  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6436
6437 (defun gnus-summary-limit-to-unread (&optional all)
6438   "Limit the summary buffer to articles that are not marked as read.
6439 If ALL is non-nil, limit strictly to unread articles."
6440   (interactive "P")
6441   (if all
6442       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6443     (gnus-summary-limit-to-marks
6444      ;; Concat all the marks that say that an article is read and have
6445      ;; those removed.
6446      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6447            gnus-killed-mark gnus-kill-file-mark
6448            gnus-low-score-mark gnus-expirable-mark
6449            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6450            gnus-duplicate-mark gnus-souped-mark)
6451      'reverse)))
6452
6453 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6454 (make-obsolete 'gnus-summary-delete-marked-with
6455                'gnus-summary-limit-exlude-marks)
6456
6457 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6458   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6459 If REVERSE, limit the summary buffer to articles that are marked
6460 with MARKS.  MARKS can either be a string of marks or a list of marks.
6461 Returns how many articles were removed."
6462   (interactive "sMarks: ")
6463   (gnus-summary-limit-to-marks marks t))
6464
6465 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6466   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6467 If REVERSE (the prefix), limit the summary buffer to articles that are
6468 not marked with MARKS.  MARKS can either be a string of marks or a
6469 list of marks.
6470 Returns how many articles were removed."
6471   (interactive "sMarks: \nP")
6472   (prog1
6473       (let ((data gnus-newsgroup-data)
6474             (marks (if (listp marks) marks
6475                      (append marks nil))) ; Transform to list.
6476             articles)
6477         (while data
6478           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6479                   (memq (gnus-data-mark (car data)) marks))
6480             (push (gnus-data-number (car data)) articles))
6481           (setq data (cdr data)))
6482         (gnus-summary-limit articles))
6483     (gnus-summary-position-point)))
6484
6485 (defun gnus-summary-limit-to-score (&optional score)
6486   "Limit to articles with score at or above SCORE."
6487   (interactive "P")
6488   (setq score (if score
6489                   (prefix-numeric-value score)
6490                 (or gnus-summary-default-score 0)))
6491   (let ((data gnus-newsgroup-data)
6492         articles)
6493     (while data
6494       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6495                 score)
6496         (push (gnus-data-number (car data)) articles))
6497       (setq data (cdr data)))
6498     (prog1
6499         (gnus-summary-limit articles)
6500       (gnus-summary-position-point))))
6501
6502 (defun gnus-summary-limit-include-thread (id)
6503   "Display all the hidden articles that in the current thread."
6504   (interactive (list (mail-header-id (gnus-summary-article-header))))
6505   (let ((articles (gnus-articles-in-thread
6506                    (gnus-id-to-thread (gnus-root-id id)))))
6507     (prog1
6508         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6509       (gnus-summary-position-point))))
6510
6511 (defun gnus-summary-limit-include-dormant ()
6512   "Display all the hidden articles that are marked as dormant.
6513 Note that this command only works on a subset of the articles currently
6514 fetched for this group."
6515   (interactive)
6516   (unless gnus-newsgroup-dormant
6517     (error "There are no dormant articles in this group"))
6518   (prog1
6519       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6520     (gnus-summary-position-point)))
6521
6522 (defun gnus-summary-limit-exclude-dormant ()
6523   "Hide all dormant articles."
6524   (interactive)
6525   (prog1
6526       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6527     (gnus-summary-position-point)))
6528
6529 (defun gnus-summary-limit-exclude-childless-dormant ()
6530   "Hide all dormant articles that have no children."
6531   (interactive)
6532   (let ((data (gnus-data-list t))
6533         articles d children)
6534     ;; Find all articles that are either not dormant or have
6535     ;; children.
6536     (while (setq d (pop data))
6537       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6538                 (and (setq children
6539                            (gnus-article-children (gnus-data-number d)))
6540                      (let (found)
6541                        (while children
6542                          (when (memq (car children) articles)
6543                            (setq children nil
6544                                  found t))
6545                          (pop children))
6546                        found)))
6547         (push (gnus-data-number d) articles)))
6548     ;; Do the limiting.
6549     (prog1
6550         (gnus-summary-limit articles)
6551       (gnus-summary-position-point))))
6552
6553 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6554   "Mark all unread excluded articles as read.
6555 If ALL, mark even excluded ticked and dormants as read."
6556   (interactive "P")
6557   (let ((articles (gnus-sorted-complement
6558                    (sort
6559                     (mapcar (lambda (h) (mail-header-number h))
6560                             gnus-newsgroup-headers)
6561                     '<)
6562                    (sort gnus-newsgroup-limit '<)))
6563         article)
6564     (setq gnus-newsgroup-unreads
6565           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6566     (if all
6567         (setq gnus-newsgroup-dormant nil
6568               gnus-newsgroup-marked nil
6569               gnus-newsgroup-reads
6570               (nconc
6571                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6572                gnus-newsgroup-reads))
6573       (while (setq article (pop articles))
6574         (unless (or (memq article gnus-newsgroup-dormant)
6575                     (memq article gnus-newsgroup-marked))
6576           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6577
6578 (defun gnus-summary-limit (articles &optional pop)
6579   (if pop
6580       ;; We pop the previous limit off the stack and use that.
6581       (setq articles (car gnus-newsgroup-limits)
6582             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6583     ;; We use the new limit, so we push the old limit on the stack.
6584     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6585   ;; Set the limit.
6586   (setq gnus-newsgroup-limit articles)
6587   (let ((total (length gnus-newsgroup-data))
6588         (data (gnus-data-find-list (gnus-summary-article-number)))
6589         (gnus-summary-mark-below nil)   ; Inhibit this.
6590         found)
6591     ;; This will do all the work of generating the new summary buffer
6592     ;; according to the new limit.
6593     (gnus-summary-prepare)
6594     ;; Hide any threads, possibly.
6595     (and gnus-show-threads
6596          gnus-thread-hide-subtree
6597          (gnus-summary-hide-all-threads))
6598     ;; Try to return to the article you were at, or one in the
6599     ;; neighborhood.
6600     (when data
6601       ;; We try to find some article after the current one.
6602       (while data
6603         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6604           (setq data nil
6605                 found t))
6606         (setq data (cdr data))))
6607     (unless found
6608       ;; If there is no data, that means that we were after the last
6609       ;; article.  The same goes when we can't find any articles
6610       ;; after the current one.
6611       (goto-char (point-max))
6612       (gnus-summary-find-prev))
6613     (gnus-set-mode-line 'summary)
6614     ;; We return how many articles were removed from the summary
6615     ;; buffer as a result of the new limit.
6616     (- total (length gnus-newsgroup-data))))
6617
6618 (defsubst gnus-invisible-cut-children (threads)
6619   (let ((num 0))
6620     (while threads
6621       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6622         (incf num))
6623       (pop threads))
6624     (< num 2)))
6625
6626 (defsubst gnus-cut-thread (thread)
6627   "Go forwards in the thread until we find an article that we want to display."
6628   (when (or (eq gnus-fetch-old-headers 'some)
6629             (eq gnus-fetch-old-headers 'invisible)
6630             (numberp gnus-fetch-old-headers)
6631             (eq gnus-build-sparse-threads 'some)
6632             (eq gnus-build-sparse-threads 'more))
6633     ;; Deal with old-fetched headers and sparse threads.
6634     (while (and
6635             thread
6636             (or
6637              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6638              (gnus-summary-article-ancient-p
6639               (mail-header-number (car thread))))
6640             (if (or (<= (length (cdr thread)) 1)
6641                     (eq gnus-fetch-old-headers 'invisible))
6642                 (setq gnus-newsgroup-limit
6643                       (delq (mail-header-number (car thread))
6644                             gnus-newsgroup-limit)
6645                       thread (cadr thread))
6646               (when (gnus-invisible-cut-children (cdr thread))
6647                 (let ((th (cdr thread)))
6648                   (while th
6649                     (if (memq (mail-header-number (caar th))
6650                               gnus-newsgroup-limit)
6651                         (setq thread (car th)
6652                               th nil)
6653                       (setq th (cdr th))))))))))
6654   thread)
6655
6656 (defun gnus-cut-threads (threads)
6657   "Cut off all uninteresting articles from the beginning of threads."
6658   (when (or (eq gnus-fetch-old-headers 'some)
6659             (eq gnus-fetch-old-headers 'invisible)
6660             (numberp gnus-fetch-old-headers)
6661             (eq gnus-build-sparse-threads 'some)
6662             (eq gnus-build-sparse-threads 'more))
6663     (let ((th threads))
6664       (while th
6665         (setcar th (gnus-cut-thread (car th)))
6666         (setq th (cdr th)))))
6667   ;; Remove nixed out threads.
6668   (delq nil threads))
6669
6670 (defun gnus-summary-initial-limit (&optional show-if-empty)
6671   "Figure out what the initial limit is supposed to be on group entry.
6672 This entails weeding out unwanted dormants, low-scored articles,
6673 fetch-old-headers verbiage, and so on."
6674   ;; Most groups have nothing to remove.
6675   (if (or gnus-inhibit-limiting
6676           (and (null gnus-newsgroup-dormant)
6677                (not (eq gnus-fetch-old-headers 'some))
6678                (not (numberp gnus-fetch-old-headers))
6679                (not (eq gnus-fetch-old-headers 'invisible))
6680                (null gnus-summary-expunge-below)
6681                (not (eq gnus-build-sparse-threads 'some))
6682                (not (eq gnus-build-sparse-threads 'more))
6683                (null gnus-thread-expunge-below)
6684                (not gnus-use-nocem)))
6685       ()                                ; Do nothing.
6686     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6687     (setq gnus-newsgroup-limit nil)
6688     (mapatoms
6689      (lambda (node)
6690        (unless (car (symbol-value node))
6691          ;; These threads have no parents -- they are roots.
6692          (let ((nodes (cdr (symbol-value node)))
6693                thread)
6694            (while nodes
6695              (if (and gnus-thread-expunge-below
6696                       (< (gnus-thread-total-score (car nodes))
6697                          gnus-thread-expunge-below))
6698                  (gnus-expunge-thread (pop nodes))
6699                (setq thread (pop nodes))
6700                (gnus-summary-limit-children thread))))))
6701      gnus-newsgroup-dependencies)
6702     ;; If this limitation resulted in an empty group, we might
6703     ;; pop the previous limit and use it instead.
6704     (when (and (not gnus-newsgroup-limit)
6705                show-if-empty)
6706       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6707     gnus-newsgroup-limit))
6708
6709 (defun gnus-summary-limit-children (thread)
6710   "Return 1 if this subthread is visible and 0 if it is not."
6711   ;; First we get the number of visible children to this thread.  This
6712   ;; is done by recursing down the thread using this function, so this
6713   ;; will really go down to a leaf article first, before slowly
6714   ;; working its way up towards the root.
6715   (when thread
6716     (let ((children
6717            (if (cdr thread)
6718                (apply '+ (mapcar 'gnus-summary-limit-children
6719                                  (cdr thread)))
6720              0))
6721           (number (mail-header-number (car thread)))
6722           score)
6723       (if (and
6724            (not (memq number gnus-newsgroup-marked))
6725            (or
6726             ;; If this article is dormant and has absolutely no visible
6727             ;; children, then this article isn't visible.
6728             (and (memq number gnus-newsgroup-dormant)
6729                  (zerop children))
6730             ;; If this is "fetch-old-headered" and there is no
6731             ;; visible children, then we don't want this article.
6732             (and (or (eq gnus-fetch-old-headers 'some)
6733                      (numberp gnus-fetch-old-headers))
6734                  (gnus-summary-article-ancient-p number)
6735                  (zerop children))
6736             ;; If this is "fetch-old-headered" and `invisible', then
6737             ;; we don't want this article.
6738             (and (eq gnus-fetch-old-headers 'invisible)
6739                  (gnus-summary-article-ancient-p number))
6740             ;; If this is a sparsely inserted article with no children,
6741             ;; we don't want it.
6742             (and (eq gnus-build-sparse-threads 'some)
6743                  (gnus-summary-article-sparse-p number)
6744                  (zerop children))
6745             ;; If we use expunging, and this article is really
6746             ;; low-scored, then we don't want this article.
6747             (when (and gnus-summary-expunge-below
6748                        (< (setq score
6749                                 (or (cdr (assq number gnus-newsgroup-scored))
6750                                     gnus-summary-default-score))
6751                           gnus-summary-expunge-below))
6752               ;; We increase the expunge-tally here, but that has
6753               ;; nothing to do with the limits, really.
6754               (incf gnus-newsgroup-expunged-tally)
6755               ;; We also mark as read here, if that's wanted.
6756               (when (and gnus-summary-mark-below
6757                          (< score gnus-summary-mark-below))
6758                 (setq gnus-newsgroup-unreads
6759                       (delq number gnus-newsgroup-unreads))
6760                 (if gnus-newsgroup-auto-expire
6761                     (push number gnus-newsgroup-expirable)
6762                   (push (cons number gnus-low-score-mark)
6763                         gnus-newsgroup-reads)))
6764               t)
6765             ;; Check NoCeM things.
6766             (if (and gnus-use-nocem
6767                      (gnus-nocem-unwanted-article-p
6768                       (mail-header-id (car thread))))
6769                 (progn
6770                   (setq gnus-newsgroup-unreads
6771                         (delq number gnus-newsgroup-unreads))
6772                   t))))
6773           ;; Nope, invisible article.
6774           0
6775         ;; Ok, this article is to be visible, so we add it to the limit
6776         ;; and return 1.
6777         (push number gnus-newsgroup-limit)
6778         1))))
6779
6780 (defun gnus-expunge-thread (thread)
6781   "Mark all articles in THREAD as read."
6782   (let* ((number (mail-header-number (car thread))))
6783     (incf gnus-newsgroup-expunged-tally)
6784     ;; We also mark as read here, if that's wanted.
6785     (setq gnus-newsgroup-unreads
6786           (delq number gnus-newsgroup-unreads))
6787     (if gnus-newsgroup-auto-expire
6788         (push number gnus-newsgroup-expirable)
6789       (push (cons number gnus-low-score-mark)
6790             gnus-newsgroup-reads)))
6791   ;; Go recursively through all subthreads.
6792   (mapcar 'gnus-expunge-thread (cdr thread)))
6793
6794 ;; Summary article oriented commands
6795
6796 (defun gnus-summary-refer-parent-article (n)
6797   "Refer parent article N times.
6798 If N is negative, go to ancestor -N instead.
6799 The difference between N and the number of articles fetched is returned."
6800   (interactive "p")
6801   (let ((skip 1)
6802         error header ref)
6803     (when (not (natnump n))
6804       (setq skip (abs n)
6805             n 1))
6806     (while (and (> n 0)
6807                 (not error))
6808       (setq header (gnus-summary-article-header))
6809       (if (and (eq (mail-header-number header)
6810                    (cdr gnus-article-current))
6811                (equal gnus-newsgroup-name
6812                       (car gnus-article-current)))
6813           ;; If we try to find the parent of the currently
6814           ;; displayed article, then we take a look at the actual
6815           ;; References header, since this is slightly more
6816           ;; reliable than the References field we got from the
6817           ;; server.
6818           (save-excursion
6819             (set-buffer gnus-original-article-buffer)
6820             (nnheader-narrow-to-headers)
6821             (unless (setq ref (message-fetch-field "references"))
6822               (setq ref (message-fetch-field "in-reply-to")))
6823             (widen))
6824         (setq ref
6825               ;; It's not the current article, so we take a bet on
6826               ;; the value we got from the server.
6827               (mail-header-references header)))
6828       (if (and ref
6829                (not (equal ref "")))
6830           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6831             (gnus-message 1 "Couldn't find parent"))
6832         (gnus-message 1 "No references in article %d"
6833                       (gnus-summary-article-number))
6834         (setq error t))
6835       (decf n))
6836     (gnus-summary-position-point)
6837     n))
6838
6839 (defun gnus-summary-refer-references ()
6840   "Fetch all articles mentioned in the References header.
6841 Return the number of articles fetched."
6842   (interactive)
6843   (let ((ref (mail-header-references (gnus-summary-article-header)))
6844         (current (gnus-summary-article-number))
6845         (n 0))
6846     (if (or (not ref)
6847             (equal ref ""))
6848         (error "No References in the current article")
6849       ;; For each Message-ID in the References header...
6850       (while (string-match "<[^>]*>" ref)
6851         (incf n)
6852         ;; ... fetch that article.
6853         (gnus-summary-refer-article
6854          (prog1 (match-string 0 ref)
6855            (setq ref (substring ref (match-end 0))))))
6856       (gnus-summary-goto-subject current)
6857       (gnus-summary-position-point)
6858       n)))
6859
6860 (defun gnus-summary-refer-thread (&optional limit)
6861   "Fetch all articles in the current thread.
6862 If LIMIT (the numerical prefix), fetch that many old headers instead
6863 of what's specified by the `gnus-refer-thread-limit' variable."
6864   (interactive "P")
6865   (let ((id (mail-header-id (gnus-summary-article-header)))
6866         (limit (if limit (prefix-numeric-value limit)
6867                  gnus-refer-thread-limit)))
6868     ;; We want to fetch LIMIT *old* headers, but we also have to
6869     ;; re-fetch all the headers in the current buffer, because many of
6870     ;; them may be undisplayed.  So we adjust LIMIT.
6871     (when (numberp limit)
6872       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6873     (unless (eq gnus-fetch-old-headers 'invisible)
6874       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6875       ;; Retrieve the headers and read them in.
6876       (if (eq (gnus-retrieve-headers
6877                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6878               'nov)
6879           (gnus-build-all-threads)
6880         (error "Can't fetch thread from backends that don't support NOV"))
6881       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6882     (gnus-summary-limit-include-thread id)))
6883
6884 (defun gnus-summary-refer-article (message-id)
6885   "Fetch an article specified by MESSAGE-ID."
6886   (interactive "sMessage-ID: ")
6887   (when (and (stringp message-id)
6888              (not (zerop (length message-id))))
6889     ;; Construct the correct Message-ID if necessary.
6890     ;; Suggested by tale@pawl.rpi.edu.
6891     (unless (string-match "^<" message-id)
6892       (setq message-id (concat "<" message-id)))
6893     (unless (string-match ">$" message-id)
6894       (setq message-id (concat message-id ">")))
6895     (let* ((header (gnus-id-to-header message-id))
6896            (sparse (and header
6897                         (gnus-summary-article-sparse-p
6898                          (mail-header-number header))
6899                         (memq (mail-header-number header)
6900                               gnus-newsgroup-limit)))
6901            number)
6902       (cond
6903        ;; If the article is present in the buffer we just go to it.
6904        ((and header
6905              (or (not (gnus-summary-article-sparse-p
6906                        (mail-header-number header)))
6907                  sparse))
6908         (prog1
6909             (gnus-summary-goto-article
6910              (mail-header-number header) nil t)
6911           (when sparse
6912             (gnus-summary-update-article (mail-header-number header)))))
6913        (t
6914         ;; We fetch the article.
6915         (catch 'found
6916           (dolist (gnus-override-method (gnus-refer-article-methods))
6917             (gnus-check-server gnus-override-method)
6918             ;; Fetch the header, and display the article.
6919             (when (setq number (gnus-summary-insert-subject message-id))
6920               (gnus-summary-select-article nil nil nil number)
6921               (throw 'found t)))
6922           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6923
6924 (defun gnus-refer-article-methods ()
6925   "Return a list of referrable methods."
6926   (cond
6927    ;; No method, so we default to current and native.
6928    ((null gnus-refer-article-method)
6929     (list gnus-current-select-method gnus-select-method))
6930    ;; Current.
6931    ((eq 'current gnus-refer-article-method)
6932     (list gnus-current-select-method))
6933    ;; List of select methods.
6934    ((not (and (symbolp (car gnus-refer-article-method))
6935               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6936     (let (out)
6937       (dolist (method gnus-refer-article-method)
6938         (push (if (eq 'current method)
6939                   gnus-current-select-method
6940                 method)
6941               out))
6942       (nreverse out)))
6943    ;; One single select method.
6944    (t
6945     (list gnus-refer-article-method))))
6946
6947 (defun gnus-summary-edit-parameters ()
6948   "Edit the group parameters of the current group."
6949   (interactive)
6950   (gnus-group-edit-group gnus-newsgroup-name 'params))
6951
6952 (defun gnus-summary-customize-parameters ()
6953   "Customize the group parameters of the current group."
6954   (interactive)
6955   (gnus-group-customize gnus-newsgroup-name))
6956
6957 (defun gnus-summary-enter-digest-group (&optional force)
6958   "Enter an nndoc group based on the current article.
6959 If FORCE, force a digest interpretation.  If not, try
6960 to guess what the document format is."
6961   (interactive "P")
6962   (let ((conf gnus-current-window-configuration))
6963     (save-excursion
6964       (gnus-summary-select-article))
6965     (setq gnus-current-window-configuration conf)
6966     (let* ((name (format "%s-%d"
6967                          (gnus-group-prefixed-name
6968                           gnus-newsgroup-name (list 'nndoc ""))
6969                          (save-excursion
6970                            (set-buffer gnus-summary-buffer)
6971                            gnus-current-article)))
6972            (ogroup gnus-newsgroup-name)
6973            (params (append (gnus-info-params (gnus-get-info ogroup))
6974                            (list (cons 'to-group ogroup))
6975                            (list (cons 'save-article-group ogroup))))
6976            (case-fold-search t)
6977            (buf (current-buffer))
6978            dig to-address)
6979       (save-excursion
6980         (set-buffer gnus-original-article-buffer)
6981         ;; Have the digest group inherit the main mail address of
6982         ;; the parent article.
6983         (when (setq to-address (or (message-fetch-field "reply-to")
6984                                    (message-fetch-field "from")))
6985           (setq params (append 
6986                         (list (cons 'to-address 
6987                                     (funcall gnus-decode-encoded-word-function
6988                                              to-address))))))
6989         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6990         (insert-buffer-substring gnus-original-article-buffer)
6991         ;; Remove lines that may lead nndoc to misinterpret the
6992         ;; document type.
6993         (narrow-to-region
6994          (goto-char (point-min))
6995          (or (search-forward "\n\n" nil t) (point)))
6996         (goto-char (point-min))
6997         (delete-matching-lines "^Path:\\|^From ")
6998         (widen))
6999       (unwind-protect
7000           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7001                     (gnus-newsgroup-ephemeral-ignored-charsets
7002                      gnus-newsgroup-ignored-charsets))
7003                 (gnus-group-read-ephemeral-group
7004                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7005                               (nndoc-article-type
7006                                ,(if force 'mbox 'guess))) t))
7007               ;; Make all postings to this group go to the parent group.
7008               (nconc (gnus-info-params (gnus-get-info name))
7009                      params)
7010             ;; Couldn't select this doc group.
7011             (switch-to-buffer buf)
7012             (gnus-set-global-variables)
7013             (gnus-configure-windows 'summary)
7014             (gnus-message 3 "Article couldn't be entered?"))
7015         (kill-buffer dig)))))
7016
7017 (defun gnus-summary-read-document (n)
7018   "Open a new group based on the current article(s).
7019 This will allow you to read digests and other similar
7020 documents as newsgroups.
7021 Obeys the standard process/prefix convention."
7022   (interactive "P")
7023   (let* ((articles (gnus-summary-work-articles n))
7024          (ogroup gnus-newsgroup-name)
7025          (params (append (gnus-info-params (gnus-get-info ogroup))
7026                          (list (cons 'to-group ogroup))))
7027          article group egroup groups vgroup)
7028     (while (setq article (pop articles))
7029       (setq group (format "%s-%d" gnus-newsgroup-name article))
7030       (gnus-summary-remove-process-mark article)
7031       (when (gnus-summary-display-article article)
7032         (save-excursion
7033           (with-temp-buffer
7034             (insert-buffer-substring gnus-original-article-buffer)
7035             ;; Remove some headers that may lead nndoc to make
7036             ;; the wrong guess.
7037             (message-narrow-to-head)
7038             (goto-char (point-min))
7039             (delete-matching-lines "^\\(Path\\):\\|^From ")
7040             (widen)
7041             (if (setq egroup
7042                       (gnus-group-read-ephemeral-group
7043                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7044                                      (nndoc-article-type guess))
7045                        t nil t))
7046                 (progn
7047                   ;; Make all postings to this group go to the parent group.
7048                   (nconc (gnus-info-params (gnus-get-info egroup))
7049                          params)
7050                   (push egroup groups))
7051               ;; Couldn't select this doc group.
7052               (gnus-error 3 "Article couldn't be entered"))))))
7053     ;; Now we have selected all the documents.
7054     (cond
7055      ((not groups)
7056       (error "None of the articles could be interpreted as documents"))
7057      ((gnus-group-read-ephemeral-group
7058        (setq vgroup (format
7059                      "nnvirtual:%s-%s" gnus-newsgroup-name
7060                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7061        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7062        t
7063        (cons (current-buffer) 'summary)))
7064      (t
7065       (error "Couldn't select virtual nndoc group")))))
7066
7067 (defun gnus-summary-isearch-article (&optional regexp-p)
7068   "Do incremental search forward on the current article.
7069 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7070   (interactive "P")
7071   (gnus-summary-select-article)
7072   (gnus-configure-windows 'article)
7073   (gnus-eval-in-buffer-window gnus-article-buffer
7074     (save-restriction
7075       (widen)
7076       (isearch-forward regexp-p))))
7077
7078 (defun gnus-summary-search-article-forward (regexp &optional backward)
7079   "Search for an article containing REGEXP forward.
7080 If BACKWARD, search backward instead."
7081   (interactive
7082    (list (read-string
7083           (format "Search article %s (regexp%s): "
7084                   (if current-prefix-arg "backward" "forward")
7085                   (if gnus-last-search-regexp
7086                       (concat ", default " gnus-last-search-regexp)
7087                     "")))
7088          current-prefix-arg))
7089   (if (string-equal regexp "")
7090       (setq regexp (or gnus-last-search-regexp ""))
7091     (setq gnus-last-search-regexp regexp)
7092     (setq gnus-article-before-search gnus-current-article))
7093   ;; Intentionally set gnus-last-article.
7094   (setq gnus-last-article gnus-article-before-search)
7095   (let ((gnus-last-article gnus-last-article))
7096     (if (gnus-summary-search-article regexp backward)
7097         (gnus-summary-show-thread)
7098       (error "Search failed: \"%s\"" regexp))))
7099
7100 (defun gnus-summary-search-article-backward (regexp)
7101   "Search for an article containing REGEXP backward."
7102   (interactive
7103    (list (read-string
7104           (format "Search article backward (regexp%s): "
7105                   (if gnus-last-search-regexp
7106                       (concat ", default " gnus-last-search-regexp)
7107                     "")))))
7108   (gnus-summary-search-article-forward regexp 'backward))
7109
7110 (defun gnus-summary-search-article (regexp &optional backward)
7111   "Search for an article containing REGEXP.
7112 Optional argument BACKWARD means do search for backward.
7113 `gnus-select-article-hook' is not called during the search."
7114   ;; We have to require this here to make sure that the following
7115   ;; dynamic binding isn't shadowed by autoloading.
7116   (require 'gnus-async)
7117   (require 'gnus-art)
7118   (let ((gnus-select-article-hook nil)  ;Disable hook.
7119         (gnus-article-prepare-hook nil)
7120         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7121         (gnus-use-article-prefetch nil)
7122         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7123         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7124         (sum (current-buffer))
7125         (gnus-display-mime-function nil)
7126         (found nil)
7127         point)
7128     (gnus-save-hidden-threads
7129       (gnus-summary-select-article)
7130       (set-buffer gnus-article-buffer)
7131       (goto-char (window-point (get-buffer-window (current-buffer))))
7132       (when backward
7133         (forward-line -1))
7134       (while (not found)
7135         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7136         (if (if backward
7137                 (re-search-backward regexp nil t)
7138               (re-search-forward regexp nil t))
7139             ;; We found the regexp.
7140             (progn
7141               (setq found 'found)
7142               (beginning-of-line)
7143               (set-window-start
7144                (get-buffer-window (current-buffer))
7145                (point))
7146               (forward-line 1)
7147               (set-window-point
7148                (get-buffer-window (current-buffer))
7149                (point))
7150               (set-buffer sum)
7151               (setq point (point)))
7152           ;; We didn't find it, so we go to the next article.
7153           (set-buffer sum)
7154           (setq found 'not)
7155           (while (eq found 'not)
7156             (if (not (if backward (gnus-summary-find-prev)
7157                        (gnus-summary-find-next)))
7158                 ;; No more articles.
7159                 (setq found t)
7160               ;; Select the next article and adjust point.
7161               (unless (gnus-summary-article-sparse-p
7162                        (gnus-summary-article-number))
7163                 (setq found nil)
7164                 (gnus-summary-select-article)
7165                 (set-buffer gnus-article-buffer)
7166                 (widen)
7167                 (goto-char (if backward (point-max) (point-min))))))))
7168       (gnus-message 7 ""))
7169     ;; Return whether we found the regexp.
7170     (when (eq found 'found)
7171       (goto-char point)
7172       (gnus-summary-show-thread)
7173       (gnus-summary-goto-subject gnus-current-article)
7174       (gnus-summary-position-point)
7175       t)))
7176
7177 (defun gnus-summary-find-matching (header regexp &optional backward unread
7178                                           not-case-fold)
7179   "Return a list of all articles that match REGEXP on HEADER.
7180 The search stars on the current article and goes forwards unless
7181 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7182 If UNREAD is non-nil, only unread articles will
7183 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7184 in the comparisons."
7185   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7186                 (gnus-data-find-list
7187                  (gnus-summary-article-number) (gnus-data-list backward))))
7188         (case-fold-search (not not-case-fold))
7189         articles d func)
7190     (if (consp header)
7191         (if (eq (car header) 'extra)
7192             (setq func
7193                   `(lambda (h)
7194                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7195                          "")))
7196           (error "%s is an invalid header" header))
7197       (unless (fboundp (intern (concat "mail-header-" header)))
7198         (error "%s is not a valid header" header))
7199       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7200     (while data
7201       (setq d (car data))
7202       (and (or (not unread)             ; We want all articles...
7203                (gnus-data-unread-p d))  ; Or just unreads.
7204            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7205            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7206            (push (gnus-data-number d) articles)) ; Success!
7207       (setq data (cdr data)))
7208     (nreverse articles)))
7209
7210 (defun gnus-summary-execute-command (header regexp command &optional backward)
7211   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7212 If HEADER is an empty string (or nil), the match is done on the entire
7213 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7214   (interactive
7215    (list (let ((completion-ignore-case t))
7216            (completing-read
7217             "Header name: "
7218             (mapcar (lambda (string) (list string))
7219                     '("Number" "Subject" "From" "Lines" "Date"
7220                       "Message-ID" "Xref" "References" "Body"))
7221             nil 'require-match))
7222          (read-string "Regexp: ")
7223          (read-key-sequence "Command: ")
7224          current-prefix-arg))
7225   (when (equal header "Body")
7226     (setq header ""))
7227   ;; Hidden thread subtrees must be searched as well.
7228   (gnus-summary-show-all-threads)
7229   ;; We don't want to change current point nor window configuration.
7230   (save-excursion
7231     (save-window-excursion
7232       (gnus-message 6 "Executing %s..." (key-description command))
7233       ;; We'd like to execute COMMAND interactively so as to give arguments.
7234       (gnus-execute header regexp
7235                     `(call-interactively ',(key-binding command))
7236                     backward)
7237       (gnus-message 6 "Executing %s...done" (key-description command)))))
7238
7239 (defun gnus-summary-beginning-of-article ()
7240   "Scroll the article back to the beginning."
7241   (interactive)
7242   (gnus-summary-select-article)
7243   (gnus-configure-windows 'article)
7244   (gnus-eval-in-buffer-window gnus-article-buffer
7245     (widen)
7246     (goto-char (point-min))
7247     (when gnus-page-broken
7248       (gnus-narrow-to-page))))
7249
7250 (defun gnus-summary-end-of-article ()
7251   "Scroll to the end of the article."
7252   (interactive)
7253   (gnus-summary-select-article)
7254   (gnus-configure-windows 'article)
7255   (gnus-eval-in-buffer-window gnus-article-buffer
7256     (widen)
7257     (goto-char (point-max))
7258     (recenter -3)
7259     (when gnus-page-broken
7260       (gnus-narrow-to-page))))
7261
7262 (defun gnus-summary-print-article (&optional filename n)
7263   "Generate and print a PostScript image of the N next (mail) articles.
7264
7265 If N is negative, print the N previous articles.  If N is nil and articles
7266 have been marked with the process mark, print these instead.
7267
7268 If the optional first argument FILENAME is nil, send the image to the
7269 printer.  If FILENAME is a string, save the PostScript image in a file with
7270 that name.  If FILENAME is a number, prompt the user for the name of the file
7271 to save in."
7272   (interactive (list (ps-print-preprint current-prefix-arg)
7273                      current-prefix-arg))
7274   (dolist (article (gnus-summary-work-articles n))
7275     (gnus-summary-select-article nil nil 'pseudo article)
7276     (gnus-eval-in-buffer-window gnus-article-buffer
7277       (let ((buffer (generate-new-buffer " *print*")))
7278         (unwind-protect
7279             (progn
7280               (copy-to-buffer buffer (point-min) (point-max))
7281               (set-buffer buffer)
7282               (gnus-article-delete-invisible-text)
7283               (let ((ps-left-header
7284                      (list
7285                       (concat "("
7286                               (mail-header-subject gnus-current-headers) ")")
7287                       (concat "("
7288                               (mail-header-from gnus-current-headers) ")")))
7289                     (ps-right-header
7290                      (list
7291                       "/pagenumberstring load"
7292                       (concat "("
7293                               (mail-header-date gnus-current-headers) ")"))))
7294                 (gnus-run-hooks 'gnus-ps-print-hook)
7295                 (save-excursion
7296                   (ps-print-buffer-with-faces filename))))
7297           (kill-buffer buffer))))))
7298
7299 (defun gnus-summary-show-article (&optional arg)
7300   "Force re-fetching of the current article.
7301 If ARG (the prefix) is a number, show the article with the charset
7302 defined in `gnus-summary-show-article-charset-alist', or the charset
7303 inputed.
7304 If ARG (the prefix) is non-nil and not a number, show the raw article
7305 without any article massaging functions being run."
7306   (interactive "P")
7307   (cond
7308    ((numberp arg)
7309     (let ((gnus-newsgroup-charset
7310            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7311                (read-coding-system "Charset: ")))
7312           (gnus-newsgroup-ignored-charsets 'gnus-all))
7313       (gnus-summary-select-article nil 'force)
7314       (let ((deps gnus-newsgroup-dependencies)
7315             head header)
7316         (save-excursion
7317           (set-buffer gnus-original-article-buffer)
7318           (save-restriction
7319             (message-narrow-to-head)
7320             (setq head (buffer-string)))
7321           (with-temp-buffer
7322             (insert (format "211 %d Article retrieved.\n"
7323                             (cdr gnus-article-current)))
7324             (insert head)
7325             (insert ".\n")
7326             (let ((nntp-server-buffer (current-buffer)))
7327               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7328         (gnus-data-set-header
7329          (gnus-data-find (cdr gnus-article-current))
7330          header)
7331         (gnus-summary-update-article-line
7332          (cdr gnus-article-current) header))))
7333    ((not arg)
7334     ;; Select the article the normal way.
7335     (gnus-summary-select-article nil 'force))
7336    (t
7337     ;; We have to require this here to make sure that the following
7338     ;; dynamic binding isn't shadowed by autoloading.
7339     (require 'gnus-async)
7340     (require 'gnus-art)
7341     ;; Bind the article treatment functions to nil.
7342     (let ((gnus-have-all-headers t)
7343           gnus-article-prepare-hook
7344           gnus-article-decode-hook
7345           gnus-display-mime-function
7346           gnus-break-pages)
7347       ;; Destroy any MIME parts.
7348       (when (gnus-buffer-live-p gnus-article-buffer)
7349         (save-excursion
7350           (set-buffer gnus-article-buffer)
7351           (mm-destroy-parts gnus-article-mime-handles)
7352           ;; Set it to nil for safety reason.
7353           (setq gnus-article-mime-handle-alist nil)
7354           (setq gnus-article-mime-handles nil)))
7355       (gnus-summary-select-article nil 'force))))
7356   (gnus-summary-goto-subject gnus-current-article)
7357   (gnus-summary-position-point))
7358
7359 (defun gnus-summary-verbose-headers (&optional arg)
7360   "Toggle permanent full header display.
7361 If ARG is a positive number, turn header display on.
7362 If ARG is a negative number, turn header display off."
7363   (interactive "P")
7364   (setq gnus-show-all-headers
7365         (cond ((or (not (numberp arg))
7366                    (zerop arg))
7367                (not gnus-show-all-headers))
7368               ((natnump arg)
7369                t)))
7370   (gnus-summary-show-article))
7371
7372 (defun gnus-summary-toggle-header (&optional arg)
7373   "Show the headers if they are hidden, or hide them if they are shown.
7374 If ARG is a positive number, show the entire header.
7375 If ARG is a negative number, hide the unwanted header lines."
7376   (interactive "P")
7377   (save-excursion
7378     (set-buffer gnus-article-buffer)
7379     (save-restriction
7380       (let* ((buffer-read-only nil)
7381              (inhibit-point-motion-hooks t)
7382              hidden e)
7383         (setq hidden
7384               (if (numberp arg)
7385                   (>= arg 0)
7386                 (save-restriction
7387                   (article-narrow-to-head)
7388                   (gnus-article-hidden-text-p 'headers))))
7389         (goto-char (point-min))
7390         (when (search-forward "\n\n" nil t)
7391           (delete-region (point-min) (1- (point))))
7392         (goto-char (point-min))
7393         (save-excursion
7394           (set-buffer gnus-original-article-buffer)
7395           (goto-char (point-min))
7396           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7397         (insert-buffer-substring gnus-original-article-buffer 1 e)
7398         (save-restriction
7399           (narrow-to-region (point-min) (point))
7400           (article-decode-encoded-words)
7401           (if  hidden
7402               (let ((gnus-treat-hide-headers nil)
7403                     (gnus-treat-hide-boring-headers nil))
7404                 (setq gnus-article-wash-types
7405                       (delq 'headers gnus-article-wash-types))
7406                 (gnus-treat-article 'head))
7407             (gnus-treat-article 'head)))
7408         (gnus-set-mode-line 'article)))))
7409
7410 (defun gnus-summary-show-all-headers ()
7411   "Make all header lines visible."
7412   (interactive)
7413   (gnus-article-show-all-headers))
7414
7415 (defun gnus-summary-caesar-message (&optional arg)
7416   "Caesar rotate the current article by 13.
7417 The numerical prefix specifies how many places to rotate each letter
7418 forward."
7419   (interactive "P")
7420   (gnus-summary-select-article)
7421   (let ((mail-header-separator ""))
7422     (gnus-eval-in-buffer-window gnus-article-buffer
7423       (save-restriction
7424         (widen)
7425         (let ((start (window-start))
7426               buffer-read-only)
7427           (message-caesar-buffer-body arg)
7428           (set-window-start (get-buffer-window (current-buffer)) start))))))
7429
7430 (defun gnus-summary-stop-page-breaking ()
7431   "Stop page breaking in the current article."
7432   (interactive)
7433   (gnus-summary-select-article)
7434   (gnus-eval-in-buffer-window gnus-article-buffer
7435     (widen)
7436     (when (gnus-visual-p 'page-marker)
7437       (let ((buffer-read-only nil))
7438         (gnus-remove-text-with-property 'gnus-prev)
7439         (gnus-remove-text-with-property 'gnus-next))
7440       (setq gnus-page-broken nil))))
7441
7442 (defun gnus-summary-move-article (&optional n to-newsgroup
7443                                             select-method action)
7444   "Move the current article to a different newsgroup.
7445 If N is a positive number, move the N next articles.
7446 If N is a negative number, move the N previous articles.
7447 If N is nil and any articles have been marked with the process mark,
7448 move those articles instead.
7449 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7450 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7451 re-spool using this method.
7452
7453 For this function to work, both the current newsgroup and the
7454 newsgroup that you want to move to have to support the `request-move'
7455 and `request-accept' functions.
7456
7457 ACTION can be either `move' (the default), `crosspost' or `copy'."
7458   (interactive "P")
7459   (unless action
7460     (setq action 'move))
7461   ;; Disable marking as read.
7462   (let (gnus-mark-article-hook)
7463     (save-window-excursion
7464       (gnus-summary-select-article)))
7465   ;; Check whether the source group supports the required functions.
7466   (cond ((and (eq action 'move)
7467               (not (gnus-check-backend-function
7468                     'request-move-article gnus-newsgroup-name)))
7469          (error "The current group does not support article moving"))
7470         ((and (eq action 'crosspost)
7471               (not (gnus-check-backend-function
7472                     'request-replace-article gnus-newsgroup-name)))
7473          (error "The current group does not support article editing")))
7474   (let ((articles (gnus-summary-work-articles n))
7475         (prefix (if (gnus-check-backend-function
7476                     'request-move-article gnus-newsgroup-name)
7477                     (gnus-group-real-prefix gnus-newsgroup-name)
7478                   ""))
7479         (names '((move "Move" "Moving")
7480                  (copy "Copy" "Copying")
7481                  (crosspost "Crosspost" "Crossposting")))
7482         (copy-buf (save-excursion
7483                     (nnheader-set-temp-buffer " *copy article*")))
7484         art-group to-method new-xref article to-groups)
7485     (unless (assq action names)
7486       (error "Unknown action %s" action))
7487     ;; Read the newsgroup name.
7488     (when (and (not to-newsgroup)
7489                (not select-method))
7490       (setq to-newsgroup
7491             (gnus-read-move-group-name
7492              (cadr (assq action names))
7493              (symbol-value (intern (format "gnus-current-%s-group" action)))
7494              articles prefix))
7495       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7496     (setq to-method (or select-method
7497                         (gnus-server-to-method
7498                          (gnus-group-method to-newsgroup))))
7499     ;; Check the method we are to move this article to...
7500     (unless (gnus-check-backend-function
7501              'request-accept-article (car to-method))
7502       (error "%s does not support article copying" (car to-method)))
7503     (unless (gnus-check-server to-method)
7504       (error "Can't open server %s" (car to-method)))
7505     (gnus-message 6 "%s to %s: %s..."
7506                   (caddr (assq action names))
7507                   (or (car select-method) to-newsgroup) articles)
7508     (while articles
7509       (setq article (pop articles))
7510       (setq
7511        art-group
7512        (cond
7513         ;; Move the article.
7514         ((eq action 'move)
7515          ;; Remove this article from future suppression.
7516          (gnus-dup-unsuppress-article article)
7517          (gnus-request-move-article
7518           article                       ; Article to move
7519           gnus-newsgroup-name           ; From newsgroup
7520           (nth 1 (gnus-find-method-for-group
7521                   gnus-newsgroup-name)) ; Server
7522           (list 'gnus-request-accept-article
7523                 to-newsgroup (list 'quote select-method)
7524                 (not articles) t)       ; Accept form
7525           (not articles)))              ; Only save nov last time
7526         ;; Copy the article.
7527         ((eq action 'copy)
7528          (save-excursion
7529            (set-buffer copy-buf)
7530            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7531              (gnus-request-accept-article
7532               to-newsgroup select-method (not articles) t))))
7533         ;; Crosspost the article.
7534         ((eq action 'crosspost)
7535          (let ((xref (message-tokenize-header
7536                       (mail-header-xref (gnus-summary-article-header article))
7537                       " ")))
7538            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7539                                   ":" article))
7540            (unless xref
7541              (setq xref (list (system-name))))
7542            (setq new-xref
7543                  (concat
7544                   (mapconcat 'identity
7545                              (delete "Xref:" (delete new-xref xref))
7546                              " ")
7547                   " " new-xref))
7548            (save-excursion
7549              (set-buffer copy-buf)
7550              ;; First put the article in the destination group.
7551              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7552              (when (consp (setq art-group
7553                                 (gnus-request-accept-article
7554                                  to-newsgroup select-method (not articles))))
7555                (setq new-xref (concat new-xref " " (car art-group)
7556                                       ":" (cdr art-group)))
7557                ;; Now we have the new Xrefs header, so we insert
7558                ;; it and replace the new article.
7559                (nnheader-replace-header "Xref" new-xref)
7560                (gnus-request-replace-article
7561                 (cdr art-group) to-newsgroup (current-buffer))
7562                art-group))))))
7563       (cond
7564        ((not art-group)
7565         (gnus-message 1 "Couldn't %s article %s: %s"
7566                       (cadr (assq action names)) article
7567                       (nnheader-get-report (car to-method))))
7568        ((eq art-group 'junk)
7569         (when (eq action 'move)
7570           (gnus-summary-mark-article article gnus-canceled-mark)
7571           (gnus-message 4 "Deleted article %s" article)))
7572        (t
7573         (let* ((pto-group (gnus-group-prefixed-name
7574                            (car art-group) to-method))
7575                (entry
7576                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7577                (info (nth 2 entry))
7578                (to-group (gnus-info-group info))
7579                to-marks)
7580           ;; Update the group that has been moved to.
7581           (when (and info
7582                      (memq action '(move copy)))
7583             (unless (member to-group to-groups)
7584               (push to-group to-groups))
7585
7586             (unless (memq article gnus-newsgroup-unreads)
7587               (push 'read to-marks)
7588               (gnus-info-set-read
7589                info (gnus-add-to-range (gnus-info-read info)
7590                                        (list (cdr art-group)))))
7591
7592             ;; See whether the article is to be put in the cache.
7593             (let ((marks gnus-article-mark-lists)
7594                   (to-article (cdr art-group)))
7595
7596               ;; Enter the article into the cache in the new group,
7597               ;; if that is required.
7598               (when gnus-use-cache
7599                 (gnus-cache-possibly-enter-article
7600                  to-group to-article
7601                  (memq article gnus-newsgroup-marked)
7602                  (memq article gnus-newsgroup-dormant)
7603                  (memq article gnus-newsgroup-unreads)))
7604
7605               (when gnus-preserve-marks
7606                 ;; Copy any marks over to the new group.
7607                 (when (and (equal to-group gnus-newsgroup-name)
7608                            (not (memq article gnus-newsgroup-unreads)))
7609                   ;; Mark this article as read in this group.
7610                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7611                   (setcdr (gnus-active to-group) to-article)
7612                   (setcdr gnus-newsgroup-active to-article))
7613
7614                 (while marks
7615                   (when (memq article (symbol-value
7616                                        (intern (format "gnus-newsgroup-%s"
7617                                                        (caar marks)))))
7618                     (push (cdar marks) to-marks)
7619                     ;; If the other group is the same as this group,
7620                     ;; then we have to add the mark to the list.
7621                     (when (equal to-group gnus-newsgroup-name)
7622                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7623                            (cons to-article
7624                                  (symbol-value
7625                                   (intern (format "gnus-newsgroup-%s"
7626                                                   (caar marks)))))))
7627                     ;; Copy the marks to other group.
7628                     (gnus-add-marked-articles
7629                      to-group (cdar marks) (list to-article) info))
7630                   (setq marks (cdr marks)))
7631
7632                 (gnus-request-set-mark to-group (list (list (list to-article)
7633                                                             'set
7634                                                             to-marks))))
7635
7636               (gnus-dribble-enter
7637                (concat "(gnus-group-set-info '"
7638                        (gnus-prin1-to-string (gnus-get-info to-group))
7639                        ")"))))
7640
7641           ;; Update the Xref header in this article to point to
7642           ;; the new crossposted article we have just created.
7643           (when (eq action 'crosspost)
7644             (save-excursion
7645               (set-buffer copy-buf)
7646               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7647               (nnheader-replace-header "Xref" new-xref)
7648               (gnus-request-replace-article
7649                article gnus-newsgroup-name (current-buffer)))))
7650
7651         ;;;!!!Why is this necessary?
7652         (set-buffer gnus-summary-buffer)
7653
7654         (gnus-summary-goto-subject article)
7655         (when (eq action 'move)
7656           (gnus-summary-mark-article article gnus-canceled-mark))))
7657       (gnus-summary-remove-process-mark article))
7658     ;; Re-activate all groups that have been moved to.
7659     (while to-groups
7660       (save-excursion
7661         (set-buffer gnus-group-buffer)
7662         (when (gnus-group-goto-group (car to-groups) t)
7663           (gnus-group-get-new-news-this-group 1 t))
7664         (pop to-groups)))
7665
7666     (gnus-kill-buffer copy-buf)
7667     (gnus-summary-position-point)
7668     (gnus-set-mode-line 'summary)))
7669
7670 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7671   "Move the current article to a different newsgroup.
7672 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7673 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7674 re-spool using this method."
7675   (interactive "P")
7676   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7677
7678 (defun gnus-summary-crosspost-article (&optional n)
7679   "Crosspost the current article to some other group."
7680   (interactive "P")
7681   (gnus-summary-move-article n nil nil 'crosspost))
7682
7683 (defcustom gnus-summary-respool-default-method nil
7684   "Default method for respooling an article.
7685 If nil, use to the current newsgroup method."
7686   :type '(choice (gnus-select-method :value (nnml ""))
7687                  (const nil))
7688   :group 'gnus-summary-mail)
7689
7690 (defun gnus-summary-respool-article (&optional n method)
7691   "Respool the current article.
7692 The article will be squeezed through the mail spooling process again,
7693 which means that it will be put in some mail newsgroup or other
7694 depending on `nnmail-split-methods'.
7695 If N is a positive number, respool the N next articles.
7696 If N is a negative number, respool the N previous articles.
7697 If N is nil and any articles have been marked with the process mark,
7698 respool those articles instead.
7699
7700 Respooling can be done both from mail groups and \"real\" newsgroups.
7701 In the former case, the articles in question will be moved from the
7702 current group into whatever groups they are destined to.  In the
7703 latter case, they will be copied into the relevant groups."
7704   (interactive
7705    (list current-prefix-arg
7706          (let* ((methods (gnus-methods-using 'respool))
7707                 (methname
7708                  (symbol-name (or gnus-summary-respool-default-method
7709                                   (car (gnus-find-method-for-group
7710                                         gnus-newsgroup-name)))))
7711                 (method
7712                  (gnus-completing-read
7713                   methname "What backend do you want to use when respooling?"
7714                   methods nil t nil 'gnus-mail-method-history))
7715                 ms)
7716            (cond
7717             ((zerop (length (setq ms (gnus-servers-using-backend
7718                                       (intern method)))))
7719              (list (intern method) ""))
7720             ((= 1 (length ms))
7721              (car ms))
7722             (t
7723              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7724                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7725                            ms-alist))))))))
7726   (unless method
7727     (error "No method given for respooling"))
7728   (if (assoc (symbol-name
7729               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7730              (gnus-methods-using 'respool))
7731       (gnus-summary-move-article n nil method)
7732     (gnus-summary-copy-article n nil method)))
7733
7734 (defun gnus-summary-import-article (file)
7735   "Import an arbitrary file into a mail newsgroup."
7736   (interactive "fImport file: ")
7737   (let ((group gnus-newsgroup-name)
7738         (now (current-time))
7739         atts lines)
7740     (unless (gnus-check-backend-function 'request-accept-article group)
7741       (error "%s does not support article importing" group))
7742     (or (file-readable-p file)
7743         (not (file-regular-p file))
7744         (error "Can't read %s" file))
7745     (save-excursion
7746       (set-buffer (gnus-get-buffer-create " *import file*"))
7747       (erase-buffer)
7748       (nnheader-insert-file-contents file)
7749       (goto-char (point-min))
7750       (unless (nnheader-article-p)
7751         ;; This doesn't look like an article, so we fudge some headers.
7752         (setq atts (file-attributes file)
7753               lines (count-lines (point-min) (point-max)))
7754         (insert "From: " (read-string "From: ") "\n"
7755                 "Subject: " (read-string "Subject: ") "\n"
7756                 "Date: " (message-make-date (nth 5 atts))
7757                 "\n"
7758                 "Message-ID: " (message-make-message-id) "\n"
7759                 "Lines: " (int-to-string lines) "\n"
7760                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7761       (gnus-request-accept-article group nil t)
7762       (kill-buffer (current-buffer)))))
7763
7764 (defun gnus-summary-article-posted-p ()
7765   "Say whether the current (mail) article is available from news as well.
7766 This will be the case if the article has both been mailed and posted."
7767   (interactive)
7768   (let ((id (mail-header-references (gnus-summary-article-header)))
7769         (gnus-override-method (car (gnus-refer-article-methods))))
7770     (if (gnus-request-head id "")
7771         (gnus-message 2 "The current message was found on %s"
7772                       gnus-override-method)
7773       (gnus-message 2 "The current message couldn't be found on %s"
7774                     gnus-override-method)
7775       nil)))
7776
7777 (defun gnus-summary-expire-articles (&optional now)
7778   "Expire all articles that are marked as expirable in the current group."
7779   (interactive)
7780   (when (gnus-check-backend-function
7781          'request-expire-articles gnus-newsgroup-name)
7782     ;; This backend supports expiry.
7783     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7784            (expirable (if total
7785                           (progn
7786                             ;; We need to update the info for
7787                             ;; this group for `gnus-list-of-read-articles'
7788                             ;; to give us the right answer.
7789                             (gnus-run-hooks 'gnus-exit-group-hook)
7790                             (gnus-summary-update-info)
7791                             (gnus-list-of-read-articles gnus-newsgroup-name))
7792                         (setq gnus-newsgroup-expirable
7793                               (sort gnus-newsgroup-expirable '<))))
7794            (expiry-wait (if now 'immediate
7795                           (gnus-group-find-parameter
7796                            gnus-newsgroup-name 'expiry-wait)))
7797            (nnmail-expiry-target
7798             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7799                 nnmail-expiry-target))
7800            es)
7801       (when expirable
7802         ;; There are expirable articles in this group, so we run them
7803         ;; through the expiry process.
7804         (gnus-message 6 "Expiring articles...")
7805         (unless (gnus-check-group gnus-newsgroup-name)
7806           (error "Can't open server for %s" gnus-newsgroup-name))
7807         ;; The list of articles that weren't expired is returned.
7808         (save-excursion
7809           (if expiry-wait
7810               (let ((nnmail-expiry-wait-function nil)
7811                     (nnmail-expiry-wait expiry-wait))
7812                 (setq es (gnus-request-expire-articles
7813                           expirable gnus-newsgroup-name)))
7814             (setq es (gnus-request-expire-articles
7815                       expirable gnus-newsgroup-name)))
7816           (unless total
7817             (setq gnus-newsgroup-expirable es))
7818           ;; We go through the old list of expirable, and mark all
7819           ;; really expired articles as nonexistent.
7820           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7821             (let ((gnus-use-cache nil))
7822               (while expirable
7823                 (unless (memq (car expirable) es)
7824                   (when (gnus-data-find (car expirable))
7825                     (gnus-summary-mark-article
7826                      (car expirable) gnus-canceled-mark)))
7827                 (setq expirable (cdr expirable))))))
7828         (gnus-message 6 "Expiring articles...done")))))
7829
7830 (defun gnus-summary-expire-articles-now ()
7831   "Expunge all expirable articles in the current group.
7832 This means that *all* articles that are marked as expirable will be
7833 deleted forever, right now."
7834   (interactive)
7835   (or gnus-expert-user
7836       (gnus-yes-or-no-p
7837        "Are you really, really, really sure you want to delete all these messages? ")
7838       (error "Phew!"))
7839   (gnus-summary-expire-articles t))
7840
7841 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7842 (defun gnus-summary-delete-article (&optional n)
7843   "Delete the N next (mail) articles.
7844 This command actually deletes articles.  This is not a marking
7845 command.  The article will disappear forever from your life, never to
7846 return.
7847 If N is negative, delete backwards.
7848 If N is nil and articles have been marked with the process mark,
7849 delete these instead."
7850   (interactive "P")
7851   (unless (gnus-check-backend-function 'request-expire-articles
7852                                        gnus-newsgroup-name)
7853     (error "The current newsgroup does not support article deletion"))
7854   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7855     (error "Couldn't open server"))
7856   ;; Compute the list of articles to delete.
7857   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7858         not-deleted)
7859     (if (and gnus-novice-user
7860              (not (gnus-yes-or-no-p
7861                    (format "Do you really want to delete %s forever? "
7862                            (if (> (length articles) 1)
7863                                (format "these %s articles" (length articles))
7864                              "this article")))))
7865         ()
7866       ;; Delete the articles.
7867       (setq not-deleted (gnus-request-expire-articles
7868                          articles gnus-newsgroup-name 'force))
7869       (while articles
7870         (gnus-summary-remove-process-mark (car articles))
7871         ;; The backend might not have been able to delete the article
7872         ;; after all.
7873         (unless (memq (car articles) not-deleted)
7874           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7875         (setq articles (cdr articles)))
7876       (when not-deleted
7877         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7878     (gnus-summary-position-point)
7879     (gnus-set-mode-line 'summary)
7880     not-deleted))
7881
7882 (defun gnus-summary-edit-article (&optional arg)
7883   "Edit the current article.
7884 This will have permanent effect only in mail groups.
7885 If ARG is nil, edit the decoded articles.
7886 If ARG is 1, edit the raw articles. 
7887 If ARG is 2, edit the raw articles even in read-only groups.
7888 If ARG is 3, edit the articles with the current handles.
7889 Otherwise, allow editing of articles even in read-only
7890 groups."
7891   (interactive "P")
7892   (let (force raw current-handles)
7893     (cond 
7894      ((null arg))
7895      ((eq arg 1) (setq raw t))
7896      ((eq arg 2) (setq raw t
7897                        force t))
7898      ((eq arg 3) (setq current-handles 
7899                        (and (gnus-buffer-live-p gnus-article-buffer)
7900                             (with-current-buffer gnus-article-buffer
7901                               (prog1
7902                                   gnus-article-mime-handles
7903                                   (setq gnus-article-mime-handles nil))))))
7904      (t (setq force t)))
7905     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7906         (error "Can't edit the raw article in group nndraft:drafts."))
7907     (save-excursion
7908       (set-buffer gnus-summary-buffer)
7909       (let ((mail-parse-charset gnus-newsgroup-charset)
7910             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7911         (gnus-set-global-variables)
7912         (when (and (not force)
7913                    (gnus-group-read-only-p))
7914           (error "The current newsgroup does not support article editing"))
7915         (gnus-summary-show-article t)
7916         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7917           (with-current-buffer gnus-article-buffer
7918             (mm-enable-multibyte-mule4)))
7919         (if (equal gnus-newsgroup-name "nndraft:drafts")
7920             (setq raw t))
7921         (gnus-article-edit-article
7922          (if raw 'ignore 
7923            `(lambda () 
7924               (let ((mbl mml-buffer-list))
7925                 (setq mml-buffer-list nil)
7926                 (mime-to-mml ,'current-handles)
7927                 (make-local-hook 'kill-buffer-hook)
7928                 (let ((mbl1 mml-buffer-list))
7929                   (setq mml-buffer-list mbl)
7930                   (set (make-local-variable 'mml-buffer-list) mbl1))
7931                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7932          `(lambda (no-highlight)
7933             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7934                   (message-options message-options)
7935                   (message-options-set-recipient)
7936                   (mail-parse-ignored-charsets 
7937                    ',gnus-newsgroup-ignored-charsets))
7938               ,(if (not raw) '(progn 
7939                                 (mml-to-mime)
7940                                 (mml-destroy-buffers)
7941                                 (remove-hook 'kill-buffer-hook 
7942                                              'mml-destroy-buffers t)
7943                                 (kill-local-variable 'mml-buffer-list)))
7944               (gnus-summary-edit-article-done
7945                ,(or (mail-header-references gnus-current-headers) "")
7946                ,(gnus-group-read-only-p) 
7947                ,gnus-summary-buffer no-highlight))))))))
7948
7949 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7950
7951 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7952                                                  no-highlight)
7953   "Make edits to the current article permanent."
7954   (interactive)
7955   (save-excursion
7956     ;; The buffer restriction contains the entire article if it exists.
7957     (when (article-goto-body)
7958       (let ((lines (count-lines (point) (point-max)))
7959             (length (- (point-max) (point)))
7960             (case-fold-search t)
7961             (body (copy-marker (point))))
7962         (goto-char (point-min))
7963         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
7964           (delete-region (match-beginning 1) (match-end 1))
7965           (insert (number-to-string length)))
7966         (goto-char (point-min))
7967         (when (re-search-forward
7968                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
7969           (delete-region (match-beginning 1) (match-end 1))
7970           (insert (number-to-string length)))
7971         (goto-char (point-min))
7972         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
7973           (delete-region (match-beginning 1) (match-end 1))
7974           (insert (number-to-string lines))))))
7975   ;; Replace the article.
7976   (let ((buf (current-buffer)))
7977     (with-temp-buffer
7978       (insert-buffer-substring buf)
7979       
7980       (if (and (not read-only)
7981                (not (gnus-request-replace-article
7982                      (cdr gnus-article-current) (car gnus-article-current)
7983                      (current-buffer) t)))
7984           (error "Couldn't replace article")
7985         ;; Update the summary buffer.
7986         (if (and references
7987                  (equal (message-tokenize-header references " ")
7988                         (message-tokenize-header
7989                          (or (message-fetch-field "references") "") " ")))
7990             ;; We only have to update this line.
7991             (save-excursion
7992               (save-restriction
7993                 (message-narrow-to-head)
7994                 (let ((head (buffer-string))
7995                       header)
7996                   (with-temp-buffer
7997                     (insert (format "211 %d Article retrieved.\n"
7998                                     (cdr gnus-article-current)))
7999                     (insert head)
8000                     (insert ".\n")
8001                     (let ((nntp-server-buffer (current-buffer)))
8002                       (setq header (car (gnus-get-newsgroup-headers
8003                                          (save-excursion
8004                                            (set-buffer gnus-summary-buffer)
8005                                            gnus-newsgroup-dependencies)
8006                                          t))))
8007                     (save-excursion
8008                       (set-buffer gnus-summary-buffer)
8009                       (gnus-data-set-header
8010                        (gnus-data-find (cdr gnus-article-current))
8011                        header)
8012                       (gnus-summary-update-article-line
8013                        (cdr gnus-article-current) header))))))
8014           ;; Update threads.
8015           (set-buffer (or buffer gnus-summary-buffer))
8016           (gnus-summary-update-article (cdr gnus-article-current)))
8017         ;; Prettify the article buffer again.
8018         (unless no-highlight
8019           (save-excursion
8020             (set-buffer gnus-article-buffer)
8021             ;;;!!! Fix this -- article should be rehighlighted.
8022             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8023             (set-buffer gnus-original-article-buffer)
8024             (gnus-request-article
8025              (cdr gnus-article-current)
8026              (car gnus-article-current) (current-buffer))))
8027         ;; Prettify the summary buffer line.
8028         (when (gnus-visual-p 'summary-highlight 'highlight)
8029           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8030
8031 (defun gnus-summary-edit-wash (key)
8032   "Perform editing command KEY in the article buffer."
8033   (interactive
8034    (list
8035     (progn
8036       (message "%s" (concat (this-command-keys) "- "))
8037       (read-char))))
8038   (message "")
8039   (gnus-summary-edit-article)
8040   (execute-kbd-macro (concat (this-command-keys) key))
8041   (gnus-article-edit-done))
8042
8043 ;;; Respooling
8044
8045 (defun gnus-summary-respool-query (&optional silent trace)
8046   "Query where the respool algorithm would put this article."
8047   (interactive)
8048   (let (gnus-mark-article-hook)
8049     (gnus-summary-select-article)
8050     (save-excursion
8051       (set-buffer gnus-original-article-buffer)
8052       (save-restriction
8053         (message-narrow-to-head)
8054         (let ((groups (nnmail-article-group 'identity trace)))
8055           (unless silent
8056             (if groups
8057                 (message "This message would go to %s"
8058                          (mapconcat 'car groups ", "))
8059               (message "This message would go to no groups"))
8060             groups))))))
8061
8062 (defun gnus-summary-respool-trace ()
8063   "Trace where the respool algorithm would put this article.
8064 Display a buffer showing all fancy splitting patterns which matched."
8065   (interactive)
8066   (gnus-summary-respool-query nil t))
8067
8068 ;; Summary marking commands.
8069
8070 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8071   "Mark articles which has the same subject as read, and then select the next.
8072 If UNMARK is positive, remove any kind of mark.
8073 If UNMARK is negative, tick articles."
8074   (interactive "P")
8075   (when unmark
8076     (setq unmark (prefix-numeric-value unmark)))
8077   (let ((count
8078          (gnus-summary-mark-same-subject
8079           (gnus-summary-article-subject) unmark)))
8080     ;; Select next unread article.  If auto-select-same mode, should
8081     ;; select the first unread article.
8082     (gnus-summary-next-article t (and gnus-auto-select-same
8083                                       (gnus-summary-article-subject)))
8084     (gnus-message 7 "%d article%s marked as %s"
8085                   count (if (= count 1) " is" "s are")
8086                   (if unmark "unread" "read"))))
8087
8088 (defun gnus-summary-kill-same-subject (&optional unmark)
8089   "Mark articles which has the same subject as read.
8090 If UNMARK is positive, remove any kind of mark.
8091 If UNMARK is negative, tick articles."
8092   (interactive "P")
8093   (when unmark
8094     (setq unmark (prefix-numeric-value unmark)))
8095   (let ((count
8096          (gnus-summary-mark-same-subject
8097           (gnus-summary-article-subject) unmark)))
8098     ;; If marked as read, go to next unread subject.
8099     (when (null unmark)
8100       ;; Go to next unread subject.
8101       (gnus-summary-next-subject 1 t))
8102     (gnus-message 7 "%d articles are marked as %s"
8103                   count (if unmark "unread" "read"))))
8104
8105 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8106   "Mark articles with same SUBJECT as read, and return marked number.
8107 If optional argument UNMARK is positive, remove any kinds of marks.
8108 If optional argument UNMARK is negative, mark articles as unread instead."
8109   (let ((count 1))
8110     (save-excursion
8111       (cond
8112        ((null unmark)                   ; Mark as read.
8113         (while (and
8114                 (progn
8115                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8116                   (gnus-summary-show-thread) t)
8117                 (gnus-summary-find-subject subject))
8118           (setq count (1+ count))))
8119        ((> unmark 0)                    ; Tick.
8120         (while (and
8121                 (progn
8122                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8123                   (gnus-summary-show-thread) t)
8124                 (gnus-summary-find-subject subject))
8125           (setq count (1+ count))))
8126        (t                               ; Mark as unread.
8127         (while (and
8128                 (progn
8129                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8130                   (gnus-summary-show-thread) t)
8131                 (gnus-summary-find-subject subject))
8132           (setq count (1+ count)))))
8133       (gnus-set-mode-line 'summary)
8134       ;; Return the number of marked articles.
8135       count)))
8136
8137 (defun gnus-summary-mark-as-processable (n &optional unmark)
8138   "Set the process mark on the next N articles.
8139 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8140 the process mark instead.  The difference between N and the actual
8141 number of articles marked is returned."
8142   (interactive "p")
8143   (let ((backward (< n 0))
8144         (n (abs n)))
8145     (while (and
8146             (> n 0)
8147             (if unmark
8148                 (gnus-summary-remove-process-mark
8149                  (gnus-summary-article-number))
8150               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8151             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8152       (setq n (1- n)))
8153     (when (/= 0 n)
8154       (gnus-message 7 "No more articles"))
8155     (gnus-summary-recenter)
8156     (gnus-summary-position-point)
8157     n))
8158
8159 (defun gnus-summary-unmark-as-processable (n)
8160   "Remove the process mark from the next N articles.
8161 If N is negative, unmark backward instead.  The difference between N and
8162 the actual number of articles unmarked is returned."
8163   (interactive "p")
8164   (gnus-summary-mark-as-processable n t))
8165
8166 (defun gnus-summary-unmark-all-processable ()
8167   "Remove the process mark from all articles."
8168   (interactive)
8169   (save-excursion
8170     (while gnus-newsgroup-processable
8171       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8172   (gnus-summary-position-point))
8173
8174 (defun gnus-summary-mark-as-expirable (n)
8175   "Mark N articles forward as expirable.
8176 If N is negative, mark backward instead.  The difference between N and
8177 the actual number of articles marked is returned."
8178   (interactive "p")
8179   (gnus-summary-mark-forward n gnus-expirable-mark))
8180
8181 (defun gnus-summary-mark-article-as-replied (article)
8182   "Mark ARTICLE replied and update the summary line."
8183   (push article gnus-newsgroup-replied)
8184   (let ((buffer-read-only nil))
8185     (when (gnus-summary-goto-subject article nil t)
8186       (gnus-summary-update-secondary-mark article))))
8187
8188 (defun gnus-summary-set-bookmark (article)
8189   "Set a bookmark in current article."
8190   (interactive (list (gnus-summary-article-number)))
8191   (when (or (not (get-buffer gnus-article-buffer))
8192             (not gnus-current-article)
8193             (not gnus-article-current)
8194             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8195     (error "No current article selected"))
8196   ;; Remove old bookmark, if one exists.
8197   (let ((old (assq article gnus-newsgroup-bookmarks)))
8198     (when old
8199       (setq gnus-newsgroup-bookmarks
8200             (delq old gnus-newsgroup-bookmarks))))
8201   ;; Set the new bookmark, which is on the form
8202   ;; (article-number . line-number-in-body).
8203   (push
8204    (cons article
8205          (save-excursion
8206            (set-buffer gnus-article-buffer)
8207            (count-lines
8208             (min (point)
8209                  (save-excursion
8210                    (goto-char (point-min))
8211                    (search-forward "\n\n" nil t)
8212                    (point)))
8213             (point))))
8214    gnus-newsgroup-bookmarks)
8215   (gnus-message 6 "A bookmark has been added to the current article."))
8216
8217 (defun gnus-summary-remove-bookmark (article)
8218   "Remove the bookmark from the current article."
8219   (interactive (list (gnus-summary-article-number)))
8220   ;; Remove old bookmark, if one exists.
8221   (let ((old (assq article gnus-newsgroup-bookmarks)))
8222     (if old
8223         (progn
8224           (setq gnus-newsgroup-bookmarks
8225                 (delq old gnus-newsgroup-bookmarks))
8226           (gnus-message 6 "Removed bookmark."))
8227       (gnus-message 6 "No bookmark in current article."))))
8228
8229 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8230 (defun gnus-summary-mark-as-dormant (n)
8231   "Mark N articles forward as dormant.
8232 If N is negative, mark backward instead.  The difference between N and
8233 the actual number of articles marked is returned."
8234   (interactive "p")
8235   (gnus-summary-mark-forward n gnus-dormant-mark))
8236
8237 (defun gnus-summary-set-process-mark (article)
8238   "Set the process mark on ARTICLE and update the summary line."
8239   (setq gnus-newsgroup-processable
8240         (cons article
8241               (delq article gnus-newsgroup-processable)))
8242   (when (gnus-summary-goto-subject article)
8243     (gnus-summary-show-thread)
8244     (gnus-summary-goto-subject article)
8245     (gnus-summary-update-secondary-mark article)))
8246
8247 (defun gnus-summary-remove-process-mark (article)
8248   "Remove the process mark from ARTICLE and update the summary line."
8249   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8250   (when (gnus-summary-goto-subject article)
8251     (gnus-summary-show-thread)
8252     (gnus-summary-goto-subject article)
8253     (gnus-summary-update-secondary-mark article)))
8254
8255 (defun gnus-summary-set-saved-mark (article)
8256   "Set the process mark on ARTICLE and update the summary line."
8257   (push article gnus-newsgroup-saved)
8258   (when (gnus-summary-goto-subject article)
8259     (gnus-summary-update-secondary-mark article)))
8260
8261 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8262   "Mark N articles as read forwards.
8263 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8264 The difference between N and the actual number of articles marked is
8265 returned.
8266 Iff NO-EXPIRE, auto-expiry will be inhibited."
8267   (interactive "p")
8268   (gnus-summary-show-thread)
8269   (let ((backward (< n 0))
8270         (gnus-summary-goto-unread
8271          (and gnus-summary-goto-unread
8272               (not (eq gnus-summary-goto-unread 'never))
8273               (not (memq mark (list gnus-unread-mark
8274                                     gnus-ticked-mark gnus-dormant-mark)))))
8275         (n (abs n))
8276         (mark (or mark gnus-del-mark)))
8277     (while (and (> n 0)
8278                 (gnus-summary-mark-article nil mark no-expire)
8279                 (zerop (gnus-summary-next-subject
8280                         (if backward -1 1)
8281                         (and gnus-summary-goto-unread
8282                              (not (eq gnus-summary-goto-unread 'never)))
8283                         t)))
8284       (setq n (1- n)))
8285     (when (/= 0 n)
8286       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8287     (gnus-summary-recenter)
8288     (gnus-summary-position-point)
8289     (gnus-set-mode-line 'summary)
8290     n))
8291
8292 (defun gnus-summary-mark-article-as-read (mark)
8293   "Mark the current article quickly as read with MARK."
8294   (let ((article (gnus-summary-article-number)))
8295     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8296     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8297     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8298     (push (cons article mark) gnus-newsgroup-reads)
8299     ;; Possibly remove from cache, if that is used.
8300     (when gnus-use-cache
8301       (gnus-cache-enter-remove-article article))
8302     ;; Allow the backend to change the mark.
8303     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8304     ;; Check for auto-expiry.
8305     (when (and gnus-newsgroup-auto-expire
8306                (memq mark gnus-auto-expirable-marks))
8307       (setq mark gnus-expirable-mark)
8308       ;; Let the backend know about the mark change.
8309       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8310       (push article gnus-newsgroup-expirable))
8311     ;; Set the mark in the buffer.
8312     (gnus-summary-update-mark mark 'unread)
8313     t))
8314
8315 (defun gnus-summary-mark-article-as-unread (mark)
8316   "Mark the current article quickly as unread with MARK."
8317   (let* ((article (gnus-summary-article-number))
8318          (old-mark (gnus-summary-article-mark article)))
8319     ;; Allow the backend to change the mark.
8320     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8321     (if (eq mark old-mark)
8322         t
8323       (if (<= article 0)
8324           (progn
8325             (gnus-error 1 "Can't mark negative article numbers")
8326             nil)
8327         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8328         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8329         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8330         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8331         (cond ((= mark gnus-ticked-mark)
8332                (push article gnus-newsgroup-marked))
8333               ((= mark gnus-dormant-mark)
8334                (push article gnus-newsgroup-dormant))
8335               (t
8336                (push article gnus-newsgroup-unreads)))
8337         (gnus-pull article gnus-newsgroup-reads)
8338
8339         ;; See whether the article is to be put in the cache.
8340         (and gnus-use-cache
8341              (vectorp (gnus-summary-article-header article))
8342              (save-excursion
8343                (gnus-cache-possibly-enter-article
8344                 gnus-newsgroup-name article
8345                 (= mark gnus-ticked-mark)
8346                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8347
8348         ;; Fix the mark.
8349         (gnus-summary-update-mark mark 'unread)
8350         t))))
8351
8352 (defun gnus-summary-mark-article (&optional article mark no-expire)
8353   "Mark ARTICLE with MARK.  MARK can be any character.
8354 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8355 `??' (dormant) and `?E' (expirable).
8356 If MARK is nil, then the default character `?r' is used.
8357 If ARTICLE is nil, then the article on the current line will be
8358 marked.
8359 Iff NO-EXPIRE, auto-expiry will be inhibited."
8360   ;; The mark might be a string.
8361   (when (stringp mark)
8362     (setq mark (aref mark 0)))
8363   ;; If no mark is given, then we check auto-expiring.
8364   (when (null mark)
8365     (setq mark gnus-del-mark))
8366   (when (and (not no-expire)
8367              gnus-newsgroup-auto-expire
8368              (memq mark gnus-auto-expirable-marks))
8369     (setq mark gnus-expirable-mark))
8370   (let ((article (or article (gnus-summary-article-number)))
8371         (old-mark (gnus-summary-article-mark article)))
8372     ;; Allow the backend to change the mark.
8373     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8374     (if (eq mark old-mark)
8375         t
8376       (unless article
8377         (error "No article on current line"))
8378       (if (not (if (or (= mark gnus-unread-mark)
8379                        (= mark gnus-ticked-mark)
8380                        (= mark gnus-dormant-mark))
8381                    (gnus-mark-article-as-unread article mark)
8382                  (gnus-mark-article-as-read article mark)))
8383           t
8384         ;; See whether the article is to be put in the cache.
8385         (and gnus-use-cache
8386              (not (= mark gnus-canceled-mark))
8387              (vectorp (gnus-summary-article-header article))
8388              (save-excursion
8389                (gnus-cache-possibly-enter-article
8390                 gnus-newsgroup-name article
8391                 (= mark gnus-ticked-mark)
8392                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8393
8394         (when (gnus-summary-goto-subject article nil t)
8395           (let ((buffer-read-only nil))
8396             (gnus-summary-show-thread)
8397             ;; Fix the mark.
8398             (gnus-summary-update-mark mark 'unread)
8399             t))))))
8400
8401 (defun gnus-summary-update-secondary-mark (article)
8402   "Update the secondary (read, process, cache) mark."
8403   (gnus-summary-update-mark
8404    (cond ((memq article gnus-newsgroup-processable)
8405           gnus-process-mark)
8406          ((memq article gnus-newsgroup-cached)
8407           gnus-cached-mark)
8408          ((memq article gnus-newsgroup-replied)
8409           gnus-replied-mark)
8410          ((memq article gnus-newsgroup-saved)
8411           gnus-saved-mark)
8412          (t gnus-unread-mark))
8413    'replied)
8414   (when (gnus-visual-p 'summary-highlight 'highlight)
8415     (gnus-run-hooks 'gnus-summary-update-hook))
8416   t)
8417
8418 (defun gnus-summary-update-mark (mark type)
8419   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8420         (buffer-read-only nil))
8421     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8422     (when forward
8423       (when (looking-at "\r")
8424         (incf forward))
8425       (when (<= (+ forward (point)) (point-max))
8426         ;; Go to the right position on the line.
8427         (goto-char (+ forward (point)))
8428         ;; Replace the old mark with the new mark.
8429         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8430         ;; Optionally update the marks by some user rule.
8431         (when (eq type 'unread)
8432           (gnus-data-set-mark
8433            (gnus-data-find (gnus-summary-article-number)) mark)
8434           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8435
8436 (defun gnus-mark-article-as-read (article &optional mark)
8437   "Enter ARTICLE in the pertinent lists and remove it from others."
8438   ;; Make the article expirable.
8439   (let ((mark (or mark gnus-del-mark)))
8440     (if (= mark gnus-expirable-mark)
8441         (push article gnus-newsgroup-expirable)
8442       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8443     ;; Remove from unread and marked lists.
8444     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8445     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8446     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8447     (push (cons article mark) gnus-newsgroup-reads)
8448     ;; Possibly remove from cache, if that is used.
8449     (when gnus-use-cache
8450       (gnus-cache-enter-remove-article article))
8451     t))
8452
8453 (defun gnus-mark-article-as-unread (article &optional mark)
8454   "Enter ARTICLE in the pertinent lists and remove it from others."
8455   (let ((mark (or mark gnus-ticked-mark)))
8456     (if (<= article 0)
8457         (progn
8458           (gnus-error 1 "Can't mark negative article numbers")
8459           nil)
8460       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8461             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8462             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8463             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8464
8465       ;; Unsuppress duplicates?
8466       (when gnus-suppress-duplicates
8467         (gnus-dup-unsuppress-article article))
8468
8469       (cond ((= mark gnus-ticked-mark)
8470              (push article gnus-newsgroup-marked))
8471             ((= mark gnus-dormant-mark)
8472              (push article gnus-newsgroup-dormant))
8473             (t
8474              (push article gnus-newsgroup-unreads)))
8475       (gnus-pull article gnus-newsgroup-reads)
8476       t)))
8477
8478 (defalias 'gnus-summary-mark-as-unread-forward
8479   'gnus-summary-tick-article-forward)
8480 (make-obsolete 'gnus-summary-mark-as-unread-forward
8481                'gnus-summary-tick-article-forward)
8482 (defun gnus-summary-tick-article-forward (n)
8483   "Tick N articles forwards.
8484 If N is negative, tick backwards instead.
8485 The difference between N and the number of articles ticked is returned."
8486   (interactive "p")
8487   (gnus-summary-mark-forward n gnus-ticked-mark))
8488
8489 (defalias 'gnus-summary-mark-as-unread-backward
8490   'gnus-summary-tick-article-backward)
8491 (make-obsolete 'gnus-summary-mark-as-unread-backward
8492                'gnus-summary-tick-article-backward)
8493 (defun gnus-summary-tick-article-backward (n)
8494   "Tick N articles backwards.
8495 The difference between N and the number of articles ticked is returned."
8496   (interactive "p")
8497   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8498
8499 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8500 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8501 (defun gnus-summary-tick-article (&optional article clear-mark)
8502   "Mark current article as unread.
8503 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8504 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8505   (interactive)
8506   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8507                                        gnus-ticked-mark)))
8508
8509 (defun gnus-summary-mark-as-read-forward (n)
8510   "Mark N articles as read forwards.
8511 If N is negative, mark backwards instead.
8512 The difference between N and the actual number of articles marked is
8513 returned."
8514   (interactive "p")
8515   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8516
8517 (defun gnus-summary-mark-as-read-backward (n)
8518   "Mark the N articles as read backwards.
8519 The difference between N and the actual number of articles marked is
8520 returned."
8521   (interactive "p")
8522   (gnus-summary-mark-forward
8523    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8524
8525 (defun gnus-summary-mark-as-read (&optional article mark)
8526   "Mark current article as read.
8527 ARTICLE specifies the article to be marked as read.
8528 MARK specifies a string to be inserted at the beginning of the line."
8529   (gnus-summary-mark-article article mark))
8530
8531 (defun gnus-summary-clear-mark-forward (n)
8532   "Clear marks from N articles forward.
8533 If N is negative, clear backward instead.
8534 The difference between N and the number of marks cleared is returned."
8535   (interactive "p")
8536   (gnus-summary-mark-forward n gnus-unread-mark))
8537
8538 (defun gnus-summary-clear-mark-backward (n)
8539   "Clear marks from N articles backward.
8540 The difference between N and the number of marks cleared is returned."
8541   (interactive "p")
8542   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8543
8544 (defun gnus-summary-mark-unread-as-read ()
8545   "Intended to be used by `gnus-summary-mark-article-hook'."
8546   (when (memq gnus-current-article gnus-newsgroup-unreads)
8547     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8548
8549 (defun gnus-summary-mark-read-and-unread-as-read ()
8550   "Intended to be used by `gnus-summary-mark-article-hook'."
8551   (let ((mark (gnus-summary-article-mark)))
8552     (when (or (gnus-unread-mark-p mark)
8553               (gnus-read-mark-p mark))
8554       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8555
8556 (defun gnus-summary-mark-region-as-read (point mark all)
8557   "Mark all unread articles between point and mark as read.
8558 If given a prefix, mark all articles between point and mark as read,
8559 even ticked and dormant ones."
8560   (interactive "r\nP")
8561   (save-excursion
8562     (let (article)
8563       (goto-char point)
8564       (beginning-of-line)
8565       (while (and
8566               (< (point) mark)
8567               (progn
8568                 (when (or all
8569                           (memq (setq article (gnus-summary-article-number))
8570                                 gnus-newsgroup-unreads))
8571                   (gnus-summary-mark-article article gnus-del-mark))
8572                 t)
8573               (gnus-summary-find-next))))))
8574
8575 (defun gnus-summary-mark-below (score mark)
8576   "Mark articles with score less than SCORE with MARK."
8577   (interactive "P\ncMark: ")
8578   (setq score (if score
8579                   (prefix-numeric-value score)
8580                 (or gnus-summary-default-score 0)))
8581   (save-excursion
8582     (set-buffer gnus-summary-buffer)
8583     (goto-char (point-min))
8584     (while
8585         (progn
8586           (and (< (gnus-summary-article-score) score)
8587                (gnus-summary-mark-article nil mark))
8588           (gnus-summary-find-next)))))
8589
8590 (defun gnus-summary-kill-below (&optional score)
8591   "Mark articles with score below SCORE as read."
8592   (interactive "P")
8593   (gnus-summary-mark-below score gnus-killed-mark))
8594
8595 (defun gnus-summary-clear-above (&optional score)
8596   "Clear all marks from articles with score above SCORE."
8597   (interactive "P")
8598   (gnus-summary-mark-above score gnus-unread-mark))
8599
8600 (defun gnus-summary-tick-above (&optional score)
8601   "Tick all articles with score above SCORE."
8602   (interactive "P")
8603   (gnus-summary-mark-above score gnus-ticked-mark))
8604
8605 (defun gnus-summary-mark-above (score mark)
8606   "Mark articles with score over SCORE with MARK."
8607   (interactive "P\ncMark: ")
8608   (setq score (if score
8609                   (prefix-numeric-value score)
8610                 (or gnus-summary-default-score 0)))
8611   (save-excursion
8612     (set-buffer gnus-summary-buffer)
8613     (goto-char (point-min))
8614     (while (and (progn
8615                   (when (> (gnus-summary-article-score) score)
8616                     (gnus-summary-mark-article nil mark))
8617                   t)
8618                 (gnus-summary-find-next)))))
8619
8620 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8621 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8622 (defun gnus-summary-limit-include-expunged (&optional no-error)
8623   "Display all the hidden articles that were expunged for low scores."
8624   (interactive)
8625   (let ((buffer-read-only nil))
8626     (let ((scored gnus-newsgroup-scored)
8627           headers h)
8628       (while scored
8629         (unless (gnus-summary-goto-subject (caar scored))
8630           (and (setq h (gnus-summary-article-header (caar scored)))
8631                (< (cdar scored) gnus-summary-expunge-below)
8632                (push h headers)))
8633         (setq scored (cdr scored)))
8634       (if (not headers)
8635           (when (not no-error)
8636             (error "No expunged articles hidden"))
8637         (goto-char (point-min))
8638         (gnus-summary-prepare-unthreaded (nreverse headers))
8639         (goto-char (point-min))
8640         (gnus-summary-position-point)
8641         t))))
8642
8643 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8644   "Mark all unread articles in this newsgroup as read.
8645 If prefix argument ALL is non-nil, ticked and dormant articles will
8646 also be marked as read.
8647 If QUIETLY is non-nil, no questions will be asked.
8648 If TO-HERE is non-nil, it should be a point in the buffer.  All
8649 articles before this point will be marked as read.
8650 Note that this function will only catch up the unread article
8651 in the current summary buffer limitation.
8652 The number of articles marked as read is returned."
8653   (interactive "P")
8654   (prog1
8655       (save-excursion
8656         (when (or quietly
8657                   (not gnus-interactive-catchup) ;Without confirmation?
8658                   gnus-expert-user
8659                   (gnus-y-or-n-p
8660                    (if all
8661                        "Mark absolutely all articles as read? "
8662                      "Mark all unread articles as read? ")))
8663           (if (and not-mark
8664                    (not gnus-newsgroup-adaptive)
8665                    (not gnus-newsgroup-auto-expire)
8666                    (not gnus-suppress-duplicates)
8667                    (or (not gnus-use-cache)
8668                        (eq gnus-use-cache 'passive)))
8669               (progn
8670                 (when all
8671                   (setq gnus-newsgroup-marked nil
8672                         gnus-newsgroup-dormant nil))
8673                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8674             ;; We actually mark all articles as canceled, which we
8675             ;; have to do when using auto-expiry or adaptive scoring.
8676             (gnus-summary-show-all-threads)
8677             (when (gnus-summary-first-subject (not all) t)
8678               (while (and
8679                       (if to-here (< (point) to-here) t)
8680                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8681                       (gnus-summary-find-next (not all) nil nil t))))
8682             (gnus-set-mode-line 'summary))
8683           t))
8684     (gnus-summary-position-point)))
8685
8686 (defun gnus-summary-catchup-to-here (&optional all)
8687   "Mark all unticked articles before the current one as read.
8688 If ALL is non-nil, also mark ticked and dormant articles as read."
8689   (interactive "P")
8690   (save-excursion
8691     (gnus-save-hidden-threads
8692       (let ((beg (point)))
8693         ;; We check that there are unread articles.
8694         (when (or all (gnus-summary-find-prev))
8695           (gnus-summary-catchup all t beg)))))
8696   (gnus-summary-position-point))
8697
8698 (defun gnus-summary-catchup-all (&optional quietly)
8699   "Mark all articles in this newsgroup as read."
8700   (interactive "P")
8701   (gnus-summary-catchup t quietly))
8702
8703 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8704   "Mark all unread articles in this group as read, then exit.
8705 If prefix argument ALL is non-nil, all articles are marked as read."
8706   (interactive "P")
8707   (when (gnus-summary-catchup all quietly nil 'fast)
8708     ;; Select next newsgroup or exit.
8709     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8710              (eq gnus-auto-select-next 'quietly))
8711         (gnus-summary-next-group nil)
8712       (gnus-summary-exit))))
8713
8714 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8715   "Mark all articles in this newsgroup as read, and then exit."
8716   (interactive "P")
8717   (gnus-summary-catchup-and-exit t quietly))
8718
8719 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8720   "Mark all articles in this group as read and select the next group.
8721 If given a prefix, mark all articles, unread as well as ticked, as
8722 read."
8723   (interactive "P")
8724   (save-excursion
8725     (gnus-summary-catchup all))
8726   (gnus-summary-next-group))
8727
8728 ;;;
8729 ;;; with article
8730 ;;;
8731
8732 (defmacro gnus-with-article (article &rest forms)
8733   "Select ARTICLE and perform FORMS in the original article buffer.
8734 Then replace the article with the result."
8735   `(progn
8736      ;; We don't want the article to be marked as read.
8737      (let (gnus-mark-article-hook)
8738        (gnus-summary-select-article t t nil ,article))
8739      (set-buffer gnus-original-article-buffer)
8740      ,@forms
8741      (if (not (gnus-check-backend-function
8742                'request-replace-article (car gnus-article-current)))
8743          (gnus-message 5 "Read-only group; not replacing")
8744        (unless (gnus-request-replace-article
8745                 ,article (car gnus-article-current)
8746                 (current-buffer) t)
8747          (error "Couldn't replace article")))
8748      ;; The cache and backlog have to be flushed somewhat.
8749      (when gnus-keep-backlog
8750        (gnus-backlog-remove-article
8751         (car gnus-article-current) (cdr gnus-article-current)))
8752      (when gnus-use-cache
8753        (gnus-cache-update-article
8754         (car gnus-article-current) (cdr gnus-article-current)))))
8755
8756 (put 'gnus-with-article 'lisp-indent-function 1)
8757 (put 'gnus-with-article 'edebug-form-spec '(form body))
8758
8759 ;; Thread-based commands.
8760
8761 (defun gnus-summary-articles-in-thread (&optional article)
8762   "Return a list of all articles in the current thread.
8763 If ARTICLE is non-nil, return all articles in the thread that starts
8764 with that article."
8765   (let* ((article (or article (gnus-summary-article-number)))
8766          (data (gnus-data-find-list article))
8767          (top-level (gnus-data-level (car data)))
8768          (top-subject
8769           (cond ((null gnus-thread-operation-ignore-subject)
8770                  (gnus-simplify-subject-re
8771                   (mail-header-subject (gnus-data-header (car data)))))
8772                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8773                  (gnus-simplify-subject-fuzzy
8774                   (mail-header-subject (gnus-data-header (car data)))))
8775                 (t nil)))
8776          (end-point (save-excursion
8777                       (if (gnus-summary-go-to-next-thread)
8778                           (point) (point-max))))
8779          articles)
8780     (while (and data
8781                 (< (gnus-data-pos (car data)) end-point))
8782       (when (or (not top-subject)
8783                 (string= top-subject
8784                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8785                              (gnus-simplify-subject-fuzzy
8786                               (mail-header-subject
8787                                (gnus-data-header (car data))))
8788                            (gnus-simplify-subject-re
8789                             (mail-header-subject
8790                              (gnus-data-header (car data)))))))
8791         (push (gnus-data-number (car data)) articles))
8792       (unless (and (setq data (cdr data))
8793                    (> (gnus-data-level (car data)) top-level))
8794         (setq data nil)))
8795     ;; Return the list of articles.
8796     (nreverse articles)))
8797
8798 (defun gnus-summary-rethread-current ()
8799   "Rethread the thread the current article is part of."
8800   (interactive)
8801   (let* ((gnus-show-threads t)
8802          (article (gnus-summary-article-number))
8803          (id (mail-header-id (gnus-summary-article-header)))
8804          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8805     (unless id
8806       (error "No article on the current line"))
8807     (gnus-rebuild-thread id)
8808     (gnus-summary-goto-subject article)))
8809
8810 (defun gnus-summary-reparent-thread ()
8811   "Make the current article child of the marked (or previous) article.
8812
8813 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8814 is non-nil or the Subject: of both articles are the same."
8815   (interactive)
8816   (unless (not (gnus-group-read-only-p))
8817     (error "The current newsgroup does not support article editing"))
8818   (unless (<= (length gnus-newsgroup-processable) 1)
8819     (error "No more than one article may be marked"))
8820   (save-window-excursion
8821     (let ((gnus-article-buffer " *reparent*")
8822           (current-article (gnus-summary-article-number))
8823           ;; First grab the marked article, otherwise one line up.
8824           (parent-article (if (not (null gnus-newsgroup-processable))
8825                               (car gnus-newsgroup-processable)
8826                             (save-excursion
8827                               (if (eq (forward-line -1) 0)
8828                                   (gnus-summary-article-number)
8829                                 (error "Beginning of summary buffer"))))))
8830       (unless (not (eq current-article parent-article))
8831         (error "An article may not be self-referential"))
8832       (let ((message-id (mail-header-id
8833                          (gnus-summary-article-header parent-article))))
8834         (unless (and message-id (not (equal message-id "")))
8835           (error "No message-id in desired parent"))
8836         (gnus-with-article current-article
8837           (save-restriction
8838             (goto-char (point-min))
8839             (message-narrow-to-head)
8840             (if (re-search-forward "^References: " nil t)
8841                 (progn
8842                   (re-search-forward "^[^ \t]" nil t)
8843                   (forward-line -1)
8844                   (end-of-line)
8845                   (insert " " message-id))
8846               (insert "References: " message-id "\n"))))
8847         (set-buffer gnus-summary-buffer)
8848         (gnus-summary-unmark-all-processable)
8849         (gnus-summary-update-article current-article)
8850         (gnus-summary-rethread-current)
8851         (gnus-message 3 "Article %d is now the child of article %d"
8852                       current-article parent-article)))))
8853
8854 (defun gnus-summary-toggle-threads (&optional arg)
8855   "Toggle showing conversation threads.
8856 If ARG is positive number, turn showing conversation threads on."
8857   (interactive "P")
8858   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8859     (setq gnus-show-threads
8860           (if (null arg) (not gnus-show-threads)
8861             (> (prefix-numeric-value arg) 0)))
8862     (gnus-summary-prepare)
8863     (gnus-summary-goto-subject current)
8864     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8865     (gnus-summary-position-point)))
8866
8867 (defun gnus-summary-show-all-threads ()
8868   "Show all threads."
8869   (interactive)
8870   (save-excursion
8871     (let ((buffer-read-only nil))
8872       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8873   (gnus-summary-position-point))
8874
8875 (defun gnus-summary-show-thread ()
8876   "Show thread subtrees.
8877 Returns nil if no thread was there to be shown."
8878   (interactive)
8879   (let ((buffer-read-only nil)
8880         (orig (point))
8881         ;; first goto end then to beg, to have point at beg after let
8882         (end (progn (end-of-line) (point)))
8883         (beg (progn (beginning-of-line) (point))))
8884     (prog1
8885         ;; Any hidden lines here?
8886         (search-forward "\r" end t)
8887       (subst-char-in-region beg end ?\^M ?\n t)
8888       (goto-char orig)
8889       (gnus-summary-position-point))))
8890
8891 (defun gnus-summary-hide-all-threads ()
8892   "Hide all thread subtrees."
8893   (interactive)
8894   (save-excursion
8895     (goto-char (point-min))
8896     (gnus-summary-hide-thread)
8897     (while (zerop (gnus-summary-next-thread 1 t))
8898       (gnus-summary-hide-thread)))
8899   (gnus-summary-position-point))
8900
8901 (defun gnus-summary-hide-thread ()
8902   "Hide thread subtrees.
8903 Returns nil if no threads were there to be hidden."
8904   (interactive)
8905   (let ((buffer-read-only nil)
8906         (start (point))
8907         (article (gnus-summary-article-number)))
8908     (goto-char start)
8909     ;; Go forward until either the buffer ends or the subthread
8910     ;; ends.
8911     (when (and (not (eobp))
8912                (or (zerop (gnus-summary-next-thread 1 t))
8913                    (goto-char (point-max))))
8914       (prog1
8915           (if (and (> (point) start)
8916                    (search-backward "\n" start t))
8917               (progn
8918                 (subst-char-in-region start (point) ?\n ?\^M)
8919                 (gnus-summary-goto-subject article))
8920             (goto-char start)
8921             nil)))))
8922
8923 (defun gnus-summary-go-to-next-thread (&optional previous)
8924   "Go to the same level (or less) next thread.
8925 If PREVIOUS is non-nil, go to previous thread instead.
8926 Return the article number moved to, or nil if moving was impossible."
8927   (let ((level (gnus-summary-thread-level))
8928         (way (if previous -1 1))
8929         (beg (point)))
8930     (forward-line way)
8931     (while (and (not (eobp))
8932                 (< level (gnus-summary-thread-level)))
8933       (forward-line way))
8934     (if (eobp)
8935         (progn
8936           (goto-char beg)
8937           nil)
8938       (setq beg (point))
8939       (prog1
8940           (gnus-summary-article-number)
8941         (goto-char beg)))))
8942
8943 (defun gnus-summary-next-thread (n &optional silent)
8944   "Go to the same level next N'th thread.
8945 If N is negative, search backward instead.
8946 Returns the difference between N and the number of skips actually
8947 done.
8948
8949 If SILENT, don't output messages."
8950   (interactive "p")
8951   (let ((backward (< n 0))
8952         (n (abs n)))
8953     (while (and (> n 0)
8954                 (gnus-summary-go-to-next-thread backward))
8955       (decf n))
8956     (unless silent
8957       (gnus-summary-position-point))
8958     (when (and (not silent) (/= 0 n))
8959       (gnus-message 7 "No more threads"))
8960     n))
8961
8962 (defun gnus-summary-prev-thread (n)
8963   "Go to the same level previous N'th thread.
8964 Returns the difference between N and the number of skips actually
8965 done."
8966   (interactive "p")
8967   (gnus-summary-next-thread (- n)))
8968
8969 (defun gnus-summary-go-down-thread ()
8970   "Go down one level in the current thread."
8971   (let ((children (gnus-summary-article-children)))
8972     (when children
8973       (gnus-summary-goto-subject (car children)))))
8974
8975 (defun gnus-summary-go-up-thread ()
8976   "Go up one level in the current thread."
8977   (let ((parent (gnus-summary-article-parent)))
8978     (when parent
8979       (gnus-summary-goto-subject parent))))
8980
8981 (defun gnus-summary-down-thread (n)
8982   "Go down thread N steps.
8983 If N is negative, go up instead.
8984 Returns the difference between N and how many steps down that were
8985 taken."
8986   (interactive "p")
8987   (let ((up (< n 0))
8988         (n (abs n)))
8989     (while (and (> n 0)
8990                 (if up (gnus-summary-go-up-thread)
8991                   (gnus-summary-go-down-thread)))
8992       (setq n (1- n)))
8993     (gnus-summary-position-point)
8994     (when (/= 0 n)
8995       (gnus-message 7 "Can't go further"))
8996     n))
8997
8998 (defun gnus-summary-up-thread (n)
8999   "Go up thread N steps.
9000 If N is negative, go up instead.
9001 Returns the difference between N and how many steps down that were
9002 taken."
9003   (interactive "p")
9004   (gnus-summary-down-thread (- n)))
9005
9006 (defun gnus-summary-top-thread ()
9007   "Go to the top of the thread."
9008   (interactive)
9009   (while (gnus-summary-go-up-thread))
9010   (gnus-summary-article-number))
9011
9012 (defun gnus-summary-kill-thread (&optional unmark)
9013   "Mark articles under current thread as read.
9014 If the prefix argument is positive, remove any kinds of marks.
9015 If the prefix argument is negative, tick articles instead."
9016   (interactive "P")
9017   (when unmark
9018     (setq unmark (prefix-numeric-value unmark)))
9019   (let ((articles (gnus-summary-articles-in-thread)))
9020     (save-excursion
9021       ;; Expand the thread.
9022       (gnus-summary-show-thread)
9023       ;; Mark all the articles.
9024       (while articles
9025         (gnus-summary-goto-subject (car articles))
9026         (cond ((null unmark)
9027                (gnus-summary-mark-article-as-read gnus-killed-mark))
9028               ((> unmark 0)
9029                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9030               (t
9031                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9032         (setq articles (cdr articles))))
9033     ;; Hide killed subtrees.
9034     (and (null unmark)
9035          gnus-thread-hide-killed
9036          (gnus-summary-hide-thread))
9037     ;; If marked as read, go to next unread subject.
9038     (when (null unmark)
9039       ;; Go to next unread subject.
9040       (gnus-summary-next-subject 1 t)))
9041   (gnus-set-mode-line 'summary))
9042
9043 ;; Summary sorting commands
9044
9045 (defun gnus-summary-sort-by-number (&optional reverse)
9046   "Sort the summary buffer by article number.
9047 Argument REVERSE means reverse order."
9048   (interactive "P")
9049   (gnus-summary-sort 'number reverse))
9050
9051 (defun gnus-summary-sort-by-author (&optional reverse)
9052   "Sort the summary buffer by author name alphabetically.
9053 If `case-fold-search' is non-nil, case of letters is ignored.
9054 Argument REVERSE means reverse order."
9055   (interactive "P")
9056   (gnus-summary-sort 'author reverse))
9057
9058 (defun gnus-summary-sort-by-subject (&optional reverse)
9059   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9060 If `case-fold-search' is non-nil, case of letters is ignored.
9061 Argument REVERSE means reverse order."
9062   (interactive "P")
9063   (gnus-summary-sort 'subject reverse))
9064
9065 (defun gnus-summary-sort-by-date (&optional reverse)
9066   "Sort the summary buffer by date.
9067 Argument REVERSE means reverse order."
9068   (interactive "P")
9069   (gnus-summary-sort 'date reverse))
9070
9071 (defun gnus-summary-sort-by-score (&optional reverse)
9072   "Sort the summary buffer by score.
9073 Argument REVERSE means reverse order."
9074   (interactive "P")
9075   (gnus-summary-sort 'score reverse))
9076
9077 (defun gnus-summary-sort-by-lines (&optional reverse)
9078   "Sort the summary buffer by the number of lines.
9079 Argument REVERSE means reverse order."
9080   (interactive "P")
9081   (gnus-summary-sort 'lines reverse))
9082
9083 (defun gnus-summary-sort-by-chars (&optional reverse)
9084   "Sort the summary buffer by article length.
9085 Argument REVERSE means reverse order."
9086   (interactive "P")
9087   (gnus-summary-sort 'chars reverse))
9088
9089 (defun gnus-summary-sort (predicate reverse)
9090   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9091   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9092          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9093          (gnus-thread-sort-functions
9094           (if (not reverse)
9095               thread
9096             `(lambda (t1 t2)
9097                (,thread t2 t1))))
9098          (gnus-sort-gathered-threads-function
9099           gnus-thread-sort-functions)
9100          (gnus-article-sort-functions
9101           (if (not reverse)
9102               article
9103             `(lambda (t1 t2)
9104                (,article t2 t1))))
9105          (buffer-read-only)
9106          (gnus-summary-prepare-hook nil))
9107     ;; We do the sorting by regenerating the threads.
9108     (gnus-summary-prepare)
9109     ;; Hide subthreads if needed.
9110     (when (and gnus-show-threads gnus-thread-hide-subtree)
9111       (gnus-summary-hide-all-threads))))
9112
9113 ;; Summary saving commands.
9114
9115 (defun gnus-summary-save-article (&optional n not-saved)
9116   "Save the current article using the default saver function.
9117 If N is a positive number, save the N next articles.
9118 If N is a negative number, save the N previous articles.
9119 If N is nil and any articles have been marked with the process mark,
9120 save those articles instead.
9121 The variable `gnus-default-article-saver' specifies the saver function."
9122   (interactive "P")
9123   (let* ((articles (gnus-summary-work-articles n))
9124          (save-buffer (save-excursion
9125                         (nnheader-set-temp-buffer " *Gnus Save*")))
9126          (num (length articles))
9127          header file)
9128     (dolist (article articles)
9129       (setq header (gnus-summary-article-header article))
9130       (if (not (vectorp header))
9131           ;; This is a pseudo-article.
9132           (if (assq 'name header)
9133               (gnus-copy-file (cdr (assq 'name header)))
9134             (gnus-message 1 "Article %d is unsaveable" article))
9135         ;; This is a real article.
9136         (save-window-excursion
9137           (gnus-summary-select-article t nil nil article))
9138         (save-excursion
9139           (set-buffer save-buffer)
9140           (erase-buffer)
9141           (insert-buffer-substring gnus-original-article-buffer))
9142         (setq file (gnus-article-save save-buffer file num))
9143         (gnus-summary-remove-process-mark article)
9144         (unless not-saved
9145           (gnus-summary-set-saved-mark article))))
9146     (gnus-kill-buffer save-buffer)
9147     (gnus-summary-position-point)
9148     (gnus-set-mode-line 'summary)
9149     n))
9150
9151 (defun gnus-summary-pipe-output (&optional arg)
9152   "Pipe the current article to a subprocess.
9153 If N is a positive number, pipe the N next articles.
9154 If N is a negative number, pipe the N previous articles.
9155 If N is nil and any articles have been marked with the process mark,
9156 pipe those articles instead."
9157   (interactive "P")
9158   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9159     (gnus-summary-save-article arg t))
9160   (gnus-configure-windows 'pipe))
9161
9162 (defun gnus-summary-save-article-mail (&optional arg)
9163   "Append the current article to an mail file.
9164 If N is a positive number, save the N next articles.
9165 If N is a negative number, save the N previous articles.
9166 If N is nil and any articles have been marked with the process mark,
9167 save those articles instead."
9168   (interactive "P")
9169   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9170     (gnus-summary-save-article arg)))
9171
9172 (defun gnus-summary-save-article-rmail (&optional arg)
9173   "Append the current article to an rmail file.
9174 If N is a positive number, save the N next articles.
9175 If N is a negative number, save the N previous articles.
9176 If N is nil and any articles have been marked with the process mark,
9177 save those articles instead."
9178   (interactive "P")
9179   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9180     (gnus-summary-save-article arg)))
9181
9182 (defun gnus-summary-save-article-file (&optional arg)
9183   "Append the current article to a file.
9184 If N is a positive number, save the N next articles.
9185 If N is a negative number, save the N previous articles.
9186 If N is nil and any articles have been marked with the process mark,
9187 save those articles instead."
9188   (interactive "P")
9189   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9190     (gnus-summary-save-article arg)))
9191
9192 (defun gnus-summary-write-article-file (&optional arg)
9193   "Write the current article to a file, deleting the previous file.
9194 If N is a positive number, save the N next articles.
9195 If N is a negative number, save the N previous articles.
9196 If N is nil and any articles have been marked with the process mark,
9197 save those articles instead."
9198   (interactive "P")
9199   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9200     (gnus-summary-save-article arg)))
9201
9202 (defun gnus-summary-save-article-body-file (&optional arg)
9203   "Append the current article body to a file.
9204 If N is a positive number, save the N next articles.
9205 If N is a negative number, save the N previous articles.
9206 If N is nil and any articles have been marked with the process mark,
9207 save those articles instead."
9208   (interactive "P")
9209   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9210     (gnus-summary-save-article arg)))
9211
9212 (defun gnus-summary-pipe-message (program)
9213   "Pipe the current article through PROGRAM."
9214   (interactive "sProgram: ")
9215   (gnus-summary-select-article)
9216   (let ((mail-header-separator ""))
9217     (gnus-eval-in-buffer-window gnus-article-buffer
9218       (save-restriction
9219         (widen)
9220         (let ((start (window-start))
9221               buffer-read-only)
9222           (message-pipe-buffer-body program)
9223           (set-window-start (get-buffer-window (current-buffer)) start))))))
9224
9225 (defun gnus-get-split-value (methods)
9226   "Return a value based on the split METHODS."
9227   (let (split-name method result match)
9228     (when methods
9229       (save-excursion
9230         (set-buffer gnus-original-article-buffer)
9231         (save-restriction
9232           (nnheader-narrow-to-headers)
9233           (while (and methods (not split-name))
9234             (goto-char (point-min))
9235             (setq method (pop methods))
9236             (setq match (car method))
9237             (when (cond
9238                    ((stringp match)
9239                     ;; Regular expression.
9240                     (ignore-errors
9241                       (re-search-forward match nil t)))
9242                    ((gnus-functionp match)
9243                     ;; Function.
9244                     (save-restriction
9245                       (widen)
9246                       (setq result (funcall match gnus-newsgroup-name))))
9247                    ((consp match)
9248                     ;; Form.
9249                     (save-restriction
9250                       (widen)
9251                       (setq result (eval match)))))
9252               (setq split-name (cdr method))
9253               (cond ((stringp result)
9254                      (push (expand-file-name
9255                             result gnus-article-save-directory)
9256                            split-name))
9257                     ((consp result)
9258                      (setq split-name (append result split-name)))))))))
9259     (nreverse split-name)))
9260
9261 (defun gnus-valid-move-group-p (group)
9262   (and (boundp group)
9263        (symbol-name group)
9264        (symbol-value group)
9265        (gnus-get-function (gnus-find-method-for-group
9266                            (symbol-name group)) 'request-accept-article t)))
9267
9268 (defun gnus-read-move-group-name (prompt default articles prefix)
9269   "Read a group name."
9270   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9271          (minibuffer-confirm-incomplete nil) ; XEmacs
9272          (prom
9273           (format "%s %s to:"
9274                   prompt
9275                   (if (> (length articles) 1)
9276                       (format "these %d articles" (length articles))
9277                     "this article")))
9278          (to-newsgroup
9279           (cond
9280            ((null split-name)
9281             (gnus-completing-read default prom
9282                                   gnus-active-hashtb
9283                                   'gnus-valid-move-group-p
9284                                   nil prefix
9285                                   'gnus-group-history))
9286            ((= 1 (length split-name))
9287             (gnus-completing-read (car split-name) prom
9288                                   gnus-active-hashtb
9289                                   'gnus-valid-move-group-p
9290                                   nil nil
9291                                   'gnus-group-history))
9292            (t
9293             (gnus-completing-read nil prom
9294                                   (mapcar (lambda (el) (list el))
9295                                           (nreverse split-name))
9296                                   nil nil nil
9297                                   'gnus-group-history))))
9298          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9299     (when to-newsgroup
9300       (if (or (string= to-newsgroup "")
9301               (string= to-newsgroup prefix))
9302           (setq to-newsgroup default))
9303       (unless to-newsgroup
9304         (error "No group name entered"))
9305       (or (gnus-active to-newsgroup)
9306           (gnus-activate-group to-newsgroup nil nil to-method)
9307           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9308                                      to-newsgroup))
9309               (or (and (gnus-request-create-group to-newsgroup to-method)
9310                        (gnus-activate-group
9311                         to-newsgroup nil nil to-method)
9312                        (gnus-subscribe-group to-newsgroup))
9313                   (error "Couldn't create group %s" to-newsgroup)))
9314           (error "No such group: %s" to-newsgroup)))
9315     to-newsgroup))
9316
9317 (defun gnus-summary-save-parts (type dir n &optional reverse)
9318   "Save parts matching TYPE to DIR.
9319 If REVERSE, save parts that do not match TYPE."
9320   (interactive
9321    (list (read-string "Save parts of type: " 
9322                       (or (car gnus-summary-save-parts-type-history)
9323                           gnus-summary-save-parts-default-mime)
9324                       'gnus-summary-save-parts-type-history)
9325          (setq gnus-summary-save-parts-last-directory
9326                (read-file-name "Save to directory: " 
9327                                gnus-summary-save-parts-last-directory
9328                                nil t))
9329          current-prefix-arg))
9330   (gnus-summary-iterate n
9331     (let ((gnus-display-mime-function nil)
9332           (gnus-inhibit-treatment t))
9333       (gnus-summary-select-article))
9334     (save-excursion
9335       (set-buffer gnus-article-buffer)
9336       (let ((handles (or gnus-article-mime-handles
9337                          (mm-dissect-buffer) (mm-uu-dissect))))
9338         (when handles
9339           (gnus-summary-save-parts-1 type dir handles reverse)
9340           (unless gnus-article-mime-handles ;; Don't destroy this case.
9341             (mm-destroy-parts handles)))))))
9342
9343 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9344   (if (stringp (car handle))
9345       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9346               (cdr handle))
9347     (when (if reverse
9348               (not (string-match type (mm-handle-media-type handle)))
9349             (string-match type (mm-handle-media-type handle)))
9350       (let ((file (expand-file-name
9351                    (file-name-nondirectory
9352                     (or
9353                      (mail-content-type-get
9354                       (mm-handle-disposition handle) 'filename)
9355                      (concat gnus-newsgroup-name
9356                              "." (number-to-string
9357                                   (cdr gnus-article-current)))))
9358                    dir)))
9359         (unless (file-exists-p file)
9360           (mm-save-part-to-file handle file))))))
9361
9362 ;; Summary extract commands
9363
9364 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9365   (let ((buffer-read-only nil)
9366         (article (gnus-summary-article-number))
9367         after-article b e)
9368     (unless (gnus-summary-goto-subject article)
9369       (error "No such article: %d" article))
9370     (gnus-summary-position-point)
9371     ;; If all commands are to be bunched up on one line, we collect
9372     ;; them here.
9373     (unless gnus-view-pseudos-separately
9374       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9375             files action)
9376         (while ps
9377           (setq action (cdr (assq 'action (car ps))))
9378           (setq files (list (cdr (assq 'name (car ps)))))
9379           (while (and ps (cdr ps)
9380                       (string= (or action "1")
9381                                (or (cdr (assq 'action (cadr ps))) "2")))
9382             (push (cdr (assq 'name (cadr ps))) files)
9383             (setcdr ps (cddr ps)))
9384           (when files
9385             (when (not (string-match "%s" action))
9386               (push " " files))
9387             (push " " files)
9388             (when (assq 'execute (car ps))
9389               (setcdr (assq 'execute (car ps))
9390                       (funcall (if (string-match "%s" action)
9391                                    'format 'concat)
9392                                action
9393                                (mapconcat
9394                                 (lambda (f)
9395                                   (if (equal f " ")
9396                                       f
9397                                     (mm-quote-arg f)))
9398                                 files " ")))))
9399           (setq ps (cdr ps)))))
9400     (if (and gnus-view-pseudos (not not-view))
9401         (while pslist
9402           (when (assq 'execute (car pslist))
9403             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9404                                   (eq gnus-view-pseudos 'not-confirm)))
9405           (setq pslist (cdr pslist)))
9406       (save-excursion
9407         (while pslist
9408           (setq after-article (or (cdr (assq 'article (car pslist)))
9409                                   (gnus-summary-article-number)))
9410           (gnus-summary-goto-subject after-article)
9411           (forward-line 1)
9412           (setq b (point))
9413           (insert "    " (file-name-nondirectory
9414                           (cdr (assq 'name (car pslist))))
9415                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9416           (setq e (point))
9417           (forward-line -1)             ; back to `b'
9418           (gnus-add-text-properties
9419            b (1- e) (list 'gnus-number gnus-reffed-article-number
9420                           gnus-mouse-face-prop gnus-mouse-face))
9421           (gnus-data-enter
9422            after-article gnus-reffed-article-number
9423            gnus-unread-mark b (car pslist) 0 (- e b))
9424           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9425           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9426           (setq pslist (cdr pslist)))))))
9427
9428 (defun gnus-pseudos< (p1 p2)
9429   (let ((c1 (cdr (assq 'action p1)))
9430         (c2 (cdr (assq 'action p2))))
9431     (and c1 c2 (string< c1 c2))))
9432
9433 (defun gnus-request-pseudo-article (props)
9434   (cond ((assq 'execute props)
9435          (gnus-execute-command (cdr (assq 'execute props)))))
9436   (let ((gnus-current-article (gnus-summary-article-number)))
9437     (gnus-run-hooks 'gnus-mark-article-hook)))
9438
9439 (defun gnus-execute-command (command &optional automatic)
9440   (save-excursion
9441     (gnus-article-setup-buffer)
9442     (set-buffer gnus-article-buffer)
9443     (setq buffer-read-only nil)
9444     (let ((command (if automatic command
9445                      (read-string "Command: " (cons command 0)))))
9446       (erase-buffer)
9447       (insert "$ " command "\n\n")
9448       (if gnus-view-pseudo-asynchronously
9449           (start-process "gnus-execute" (current-buffer) shell-file-name
9450                          shell-command-switch command)
9451         (call-process shell-file-name nil t nil
9452                       shell-command-switch command)))))
9453
9454 ;; Summary kill commands.
9455
9456 (defun gnus-summary-edit-global-kill (article)
9457   "Edit the \"global\" kill file."
9458   (interactive (list (gnus-summary-article-number)))
9459   (gnus-group-edit-global-kill article))
9460
9461 (defun gnus-summary-edit-local-kill ()
9462   "Edit a local kill file applied to the current newsgroup."
9463   (interactive)
9464   (setq gnus-current-headers (gnus-summary-article-header))
9465   (gnus-group-edit-local-kill
9466    (gnus-summary-article-number) gnus-newsgroup-name))
9467
9468 ;;; Header reading.
9469
9470 (defun gnus-read-header (id &optional header)
9471   "Read the headers of article ID and enter them into the Gnus system."
9472   (let ((group gnus-newsgroup-name)
9473         (gnus-override-method
9474          (or
9475           gnus-override-method
9476           (and (gnus-news-group-p gnus-newsgroup-name)
9477                (car (gnus-refer-article-methods)))))
9478         where)
9479     ;; First we check to see whether the header in question is already
9480     ;; fetched.
9481     (if (stringp id)
9482         ;; This is a Message-ID.
9483         (setq header (or header (gnus-id-to-header id)))
9484       ;; This is an article number.
9485       (setq header (or header (gnus-summary-article-header id))))
9486     (if (and header
9487              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9488         ;; We have found the header.
9489         header
9490       ;; If this is a sparse article, we have to nix out its
9491       ;; previous entry in the thread hashtb.
9492       (when (and header
9493                  (gnus-summary-article-sparse-p (mail-header-number header)))
9494         (let* ((parent (gnus-parent-id (mail-header-references header)))
9495                (thread (and parent (gnus-id-to-thread parent))))
9496           (when thread
9497             (delq (assq header thread) thread))))
9498       ;; We have to really fetch the header to this article.
9499       (save-excursion
9500         (set-buffer nntp-server-buffer)
9501         (when (setq where (gnus-request-head id group))
9502           (nnheader-fold-continuation-lines)
9503           (goto-char (point-max))
9504           (insert ".\n")
9505           (goto-char (point-min))
9506           (insert "211 ")
9507           (princ (cond
9508                   ((numberp id) id)
9509                   ((cdr where) (cdr where))
9510                   (header (mail-header-number header))
9511                   (t gnus-reffed-article-number))
9512                  (current-buffer))
9513           (insert " Article retrieved.\n"))
9514         (if (or (not where)
9515                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9516             ()                          ; Malformed head.
9517           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9518             (when (and (stringp id)
9519                        (not (string= (gnus-group-real-name group)
9520                                      (car where))))
9521               ;; If we fetched by Message-ID and the article came
9522               ;; from a different group, we fudge some bogus article
9523               ;; numbers for this article.
9524               (mail-header-set-number header gnus-reffed-article-number))
9525             (save-excursion
9526               (set-buffer gnus-summary-buffer)
9527               (decf gnus-reffed-article-number)
9528               (gnus-remove-header (mail-header-number header))
9529               (push header gnus-newsgroup-headers)
9530               (setq gnus-current-headers header)
9531               (push (mail-header-number header) gnus-newsgroup-limit)))
9532           header)))))
9533
9534 (defun gnus-remove-header (number)
9535   "Remove header NUMBER from `gnus-newsgroup-headers'."
9536   (if (and gnus-newsgroup-headers
9537            (= number (mail-header-number (car gnus-newsgroup-headers))))
9538       (pop gnus-newsgroup-headers)
9539     (let ((headers gnus-newsgroup-headers))
9540       (while (and (cdr headers)
9541                   (not (= number (mail-header-number (cadr headers)))))
9542         (pop headers))
9543       (when (cdr headers)
9544         (setcdr headers (cddr headers))))))
9545
9546 ;;;
9547 ;;; summary highlights
9548 ;;;
9549
9550 (defun gnus-highlight-selected-summary ()
9551   "Highlight selected article in summary buffer."
9552   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9553   (when gnus-summary-selected-face
9554     (save-excursion
9555       (let* ((beg (progn (beginning-of-line) (point)))
9556              (end (progn (end-of-line) (point)))
9557              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9558              (from (if (get-text-property beg gnus-mouse-face-prop)
9559                        beg
9560                      (or (next-single-property-change
9561                           beg gnus-mouse-face-prop nil end)
9562                          beg)))
9563              (to
9564               (if (= from end)
9565                   (- from 2)
9566                 (or (next-single-property-change
9567                      from gnus-mouse-face-prop nil end)
9568                     end))))
9569         ;; If no mouse-face prop on line we will have to = from = end,
9570         ;; so we highlight the entire line instead.
9571         (when (= (+ to 2) from)
9572           (setq from beg)
9573           (setq to end))
9574         (if gnus-newsgroup-selected-overlay
9575             ;; Move old overlay.
9576             (gnus-move-overlay
9577              gnus-newsgroup-selected-overlay from to (current-buffer))
9578           ;; Create new overlay.
9579           (gnus-overlay-put
9580            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9581            'face gnus-summary-selected-face))))))
9582
9583 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9584 (defun gnus-summary-highlight-line ()
9585   "Highlight current line according to `gnus-summary-highlight'."
9586   (let* ((list gnus-summary-highlight)
9587          (p (point))
9588          (end (progn (end-of-line) (point)))
9589          ;; now find out where the line starts and leave point there.
9590          (beg (progn (beginning-of-line) (point)))
9591          (article (gnus-summary-article-number))
9592          (score (or (cdr (assq (or article gnus-current-article)
9593                                gnus-newsgroup-scored))
9594                     gnus-summary-default-score 0))
9595          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9596          (inhibit-read-only t))
9597     ;; Eval the cars of the lists until we find a match.
9598     (let ((default gnus-summary-default-score))
9599       (while (and list
9600                   (not (eval (caar list))))
9601         (setq list (cdr list))))
9602     (let ((face (cdar list)))
9603       (unless (eq face (get-text-property beg 'face))
9604         (gnus-put-text-property-excluding-characters-with-faces
9605          beg end 'face
9606          (setq face (if (boundp face) (symbol-value face) face)))
9607         (when gnus-summary-highlight-line-function
9608           (funcall gnus-summary-highlight-line-function article face))))
9609     (goto-char p)))
9610
9611 (defun gnus-update-read-articles (group unread &optional compute)
9612   "Update the list of read articles in GROUP."
9613   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9614          (entry (gnus-gethash group gnus-newsrc-hashtb))
9615          (info (nth 2 entry))
9616          (prev 1)
9617          (unread (sort (copy-sequence unread) '<))
9618          read)
9619     (if (or (not info) (not active))
9620         ;; There is no info on this group if it was, in fact,
9621         ;; killed.  Gnus stores no information on killed groups, so
9622         ;; there's nothing to be done.
9623         ;; One could store the information somewhere temporarily,
9624         ;; perhaps...  Hmmm...
9625         ()
9626       ;; Remove any negative articles numbers.
9627       (while (and unread (< (car unread) 0))
9628         (setq unread (cdr unread)))
9629       ;; Remove any expired article numbers
9630       (while (and unread (< (car unread) (car active)))
9631         (setq unread (cdr unread)))
9632       ;; Compute the ranges of read articles by looking at the list of
9633       ;; unread articles.
9634       (while unread
9635         (when (/= (car unread) prev)
9636           (push (if (= prev (1- (car unread))) prev
9637                   (cons prev (1- (car unread))))
9638                 read))
9639         (setq prev (1+ (car unread)))
9640         (setq unread (cdr unread)))
9641       (when (<= prev (cdr active))
9642         (push (cons prev (cdr active)) read))
9643       (setq read (if (> (length read) 1) (nreverse read) read))
9644       (if compute
9645           read
9646         (save-excursion
9647           (let (setmarkundo)
9648             ;; Propagate the read marks to the backend.
9649             (when (gnus-check-backend-function 'request-set-mark group)
9650               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9651                     (add (gnus-remove-from-range read (gnus-info-read info))))
9652                 (when (or add del)
9653                   (unless (gnus-check-group group)
9654                     (error "Can't open server for %s" group))
9655                   (gnus-request-set-mark
9656                    group (delq nil (list (if add (list add 'add '(read)))
9657                                          (if del (list del 'del '(read))))))
9658                   (setq setmarkundo
9659                         `(gnus-request-set-mark
9660                           ,group
9661                           ',(delq nil (list
9662                                        (if del (list del 'add '(read)))
9663                                        (if add (list add 'del '(read))))))))))
9664             (set-buffer gnus-group-buffer)
9665             (gnus-undo-register
9666               `(progn
9667                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9668                  (gnus-info-set-read ',info ',(gnus-info-read info))
9669                  (gnus-get-unread-articles-in-group ',info 
9670                                                     (gnus-active ,group))
9671                  (gnus-group-update-group ,group t)
9672                  ,setmarkundo))))
9673         ;; Enter this list into the group info.
9674         (gnus-info-set-read info read)
9675         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9676         (gnus-get-unread-articles-in-group info (gnus-active group))
9677         t))))
9678
9679 (defun gnus-offer-save-summaries ()
9680   "Offer to save all active summary buffers."
9681   (save-excursion
9682     (let ((buflist (buffer-list))
9683           buffers bufname)
9684       ;; Go through all buffers and find all summaries.
9685       (while buflist
9686         (and (setq bufname (buffer-name (car buflist)))
9687              (string-match "Summary" bufname)
9688              (save-excursion
9689                (set-buffer bufname)
9690                ;; We check that this is, indeed, a summary buffer.
9691                (and (eq major-mode 'gnus-summary-mode)
9692                     ;; Also make sure this isn't bogus.
9693                     gnus-newsgroup-prepared
9694                     ;; Also make sure that this isn't a dead summary buffer.
9695                     (not gnus-dead-summary-mode)))
9696              (push bufname buffers))
9697         (setq buflist (cdr buflist)))
9698       ;; Go through all these summary buffers and offer to save them.
9699       (when buffers
9700         (map-y-or-n-p
9701          "Update summary buffer %s? "
9702          (lambda (buf)
9703            (switch-to-buffer buf)
9704            (gnus-summary-exit))
9705          buffers)))))
9706
9707 (defun gnus-summary-setup-default-charset ()
9708   "Setup newsgroup default charset."
9709   (if (equal gnus-newsgroup-name "nndraft:drafts")
9710       (setq gnus-newsgroup-charset nil)
9711     (let* ((name (and gnus-newsgroup-name
9712                       (gnus-group-real-name gnus-newsgroup-name)))
9713            (ignored-charsets
9714             (or gnus-newsgroup-ephemeral-ignored-charsets
9715                 (append
9716                  (and gnus-newsgroup-name
9717                       (or (gnus-group-find-parameter gnus-newsgroup-name
9718                                                      'ignored-charsets t)
9719                           (let ((alist gnus-group-ignored-charsets-alist)
9720                                 elem (charsets nil))
9721                             (while (setq elem (pop alist))
9722                               (when (and name
9723                                          (string-match (car elem) name))
9724                                 (setq alist nil
9725                                       charsets (cdr elem))))
9726                             charsets)))
9727                  gnus-newsgroup-ignored-charsets))))
9728       (setq gnus-newsgroup-charset
9729             (or gnus-newsgroup-ephemeral-charset
9730                 (and gnus-newsgroup-name
9731                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9732                          (let ((alist gnus-group-charset-alist)
9733                                elem charset)
9734                            (while (setq elem (pop alist))
9735                              (when (and name
9736                                         (string-match (car elem) name))
9737                                (setq alist nil
9738                                      charset (cadr elem))))
9739                            charset)))
9740                 gnus-default-charset))
9741       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9742            ignored-charsets))))
9743
9744 ;;;
9745 ;;; Mime Commands
9746 ;;;
9747
9748 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9749   "Display the current article buffer fully MIME-buttonized.
9750 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9751 treated as multipart/mixed."
9752   (interactive "P")
9753   (require 'gnus-art)
9754   (let ((gnus-unbuttonized-mime-types nil)
9755         (gnus-mime-display-multipart-as-mixed show-all-parts))
9756     (gnus-summary-show-article)))
9757
9758 (defun gnus-summary-repair-multipart (article)
9759   "Add a Content-Type header to a multipart article without one."
9760   (interactive (list (gnus-summary-article-number)))
9761   (gnus-with-article article
9762     (message-narrow-to-head)
9763     (message-remove-header "Mime-Version")
9764     (goto-char (point-max))
9765     (insert "Mime-Version: 1.0\n")
9766     (widen)
9767     (when (search-forward "\n--" nil t)
9768       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9769         (message-narrow-to-head)
9770         (message-remove-header "Content-Type")
9771         (goto-char (point-max))
9772         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9773                         separator))
9774         (widen))))
9775   (let (gnus-mark-article-hook)
9776     (gnus-summary-select-article t t nil article)))
9777
9778 (defun gnus-summary-toggle-display-buttonized ()
9779   "Toggle the buttonizing of the article buffer."
9780   (interactive)
9781   (require 'gnus-art)
9782   (if (setq gnus-inhibit-mime-unbuttonizing
9783             (not gnus-inhibit-mime-unbuttonizing))
9784       (let ((gnus-unbuttonized-mime-types nil))
9785         (gnus-summary-show-article))
9786     (gnus-summary-show-article)))
9787
9788 ;;;
9789 ;;; Generic summary marking commands
9790 ;;;
9791
9792 (defvar gnus-summary-marking-alist
9793   '((read gnus-del-mark "d")
9794     (unread gnus-unread-mark "u")
9795     (ticked gnus-ticked-mark "!")
9796     (dormant gnus-dormant-mark "?")
9797     (expirable gnus-expirable-mark "e"))
9798   "An alist of names/marks/keystrokes.")
9799
9800 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9801 (defvar gnus-summary-mark-map)
9802
9803 (defun gnus-summary-make-all-marking-commands ()
9804   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9805   (dolist (elem gnus-summary-marking-alist)
9806     (apply 'gnus-summary-make-marking-command elem)))
9807
9808 (defun gnus-summary-make-marking-command (name mark keystroke)
9809   (let ((map (make-sparse-keymap)))
9810     (define-key gnus-summary-generic-mark-map keystroke map)
9811     (dolist (lway `((next "next" next nil "n")
9812                     (next-unread "next unread" next t "N")
9813                     (prev "previous" prev nil "p")
9814                     (prev-unread "previous unread" prev t "P")
9815                     (nomove "" nil nil ,keystroke)))
9816       (let ((func (gnus-summary-make-marking-command-1
9817                    mark (car lway) lway name)))
9818         (setq func (eval func))
9819         (define-key map (nth 4 lway) func)))))
9820       
9821 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9822   `(defun ,(intern
9823             (format "gnus-summary-put-mark-as-%s%s"
9824                     name (if (eq way 'nomove)
9825                              ""
9826                            (concat "-" (symbol-name way)))))
9827      (n)
9828      ,(format
9829        "Mark the current article as %s%s.
9830 If N, the prefix, then repeat N times.
9831 If N is negative, move in reverse order.
9832 The difference between N and the actual number of articles marked is
9833 returned."
9834        name (cadr lway))
9835      (interactive "p")
9836      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9837     
9838 (defun gnus-summary-generic-mark (n mark move unread)
9839   "Mark N articles with MARK."
9840   (unless (eq major-mode 'gnus-summary-mode)
9841     (error "This command can only be used in the summary buffer"))
9842   (gnus-summary-show-thread)
9843   (let ((nummove
9844          (cond
9845           ((eq move 'next) 1)
9846           ((eq move 'prev) -1)
9847           (t 0))))
9848     (if (zerop nummove)
9849         (setq n 1)
9850       (when (< n 0)
9851         (setq n (abs n)
9852               nummove (* -1 nummove))))
9853     (while (and (> n 0)
9854                 (gnus-summary-mark-article nil mark)
9855                 (zerop (gnus-summary-next-subject nummove unread t)))
9856       (setq n (1- n)))
9857     (when (/= 0 n)
9858       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9859     (gnus-summary-recenter)
9860     (gnus-summary-position-point)
9861     (gnus-set-mode-line 'summary)
9862     n))
9863
9864 (gnus-summary-make-all-marking-commands)
9865
9866 (gnus-ems-redefine)
9867
9868 (provide 'gnus-sum)
9869
9870 (run-hooks 'gnus-sum-load-hook)
9871
9872 ;;; gnus-sum.el ends here