* gnus-sum.el (gnus-summary-expire-articles): Save excursion.
[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 ;; Recursive :-(.
40 ;; (require 'gnus-art)
41 (require 'nnoo)
42 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
43 (autoload 'gnus-cache-write-active "gnus-cache")
44 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
45 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
46 (autoload 'mm-uu-dissect "mm-uu")
47
48 (defcustom gnus-kill-summary-on-exit t
49   "*If non-nil, kill the summary buffer when you exit from it.
50 If nil, the summary will become a \"*Dead Summary*\" buffer, and
51 it will be killed sometime later."
52   :group 'gnus-summary-exit
53   :type 'boolean)
54
55 (defcustom gnus-fetch-old-headers nil
56   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
57 If an unread article in the group refers to an older, already read (or
58 just marked as read) article, the old article will not normally be
59 displayed in the Summary buffer.  If this variable is non-nil, Gnus
60 will attempt to grab the headers to the old articles, and thereby
61 build complete threads.  If it has the value `some', only enough
62 headers to connect otherwise loose threads will be displayed.  This
63 variable can also be a number.  In that case, no more than that number
64 of old headers will be fetched.  If it has the value `invisible', all
65 old headers will be fetched, but none will be displayed.
66
67 The server has to support NOV for any of this to work."
68   :group 'gnus-thread
69   :type '(choice (const :tag "off" nil)
70                  (const some)
71                  number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-refer-thread-limit 200
75   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
76 If t, fetch all the available old headers."
77   :group 'gnus-thread
78   :type '(choice number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-summary-make-false-root 'adopt
82   "*nil means that Gnus won't gather loose threads.
83 If the root of a thread has expired or been read in a previous
84 session, the information necessary to build a complete thread has been
85 lost.  Instead of having many small sub-threads from this original thread
86 scattered all over the summary buffer, Gnus can gather them.
87
88 If non-nil, Gnus will try to gather all loose sub-threads from an
89 original thread into one large thread.
90
91 If this variable is non-nil, it should be one of `none', `adopt',
92 `dummy' or `empty'.
93
94 If this variable is `none', Gnus will not make a false root, but just
95 present the sub-threads after another.
96 If this variable is `dummy', Gnus will create a dummy root that will
97 have all the sub-threads as children.
98 If this variable is `adopt', Gnus will make one of the \"children\"
99 the parent and mark all the step-children as such.
100 If this variable is `empty', the \"children\" are printed with empty
101 subject fields.  (Or rather, they will be printed with a string
102 given by the `gnus-summary-same-subject' variable.)"
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const none)
106                  (const dummy)
107                  (const adopt)
108                  (const empty)))
109
110 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
111   "*A regexp to match subjects to be excluded from loose thread gathering.
112 As loose thread gathering is done on subjects only, that means that
113 there can be many false gatherings performed.  By rooting out certain
114 common subjects, gathering might become saner."
115   :group 'gnus-thread
116   :type 'regexp)
117
118 (defcustom gnus-summary-gather-subject-limit nil
119   "*Maximum length of subject comparisons when gathering loose threads.
120 Use nil to compare full subjects.  Setting this variable to a low
121 number will help gather threads that have been corrupted by
122 newsreaders chopping off subject lines, but it might also mean that
123 unrelated articles that have subject that happen to begin with the
124 same few characters will be incorrectly gathered.
125
126 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
127 comparing subjects."
128   :group 'gnus-thread
129   :type '(choice (const :tag "off" nil)
130                  (const fuzzy)
131                  (sexp :menu-tag "on" t)))
132
133 (defcustom gnus-simplify-subject-functions nil
134   "List of functions taking a string argument that simplify subjects.
135 The functions are applied recursively.
136
137 Useful functions to put in this list include: `gnus-simplify-subject-re',
138 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
139   :group 'gnus-thread
140   :type '(repeat function))
141
142 (defcustom gnus-simplify-ignored-prefixes nil
143   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  regexp))
147
148 (defcustom gnus-build-sparse-threads nil
149   "*If non-nil, fill in the gaps in threads.
150 If `some', only fill in the gaps that are needed to tie loose threads
151 together.  If `more', fill in all leaf nodes that Gnus can find.  If
152 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const some)
156                  (const more)
157                  (sexp :menu-tag "all" t)))
158
159 (defcustom gnus-summary-thread-gathering-function
160   'gnus-gather-threads-by-subject
161   "*Function used for gathering loose threads.
162 There are two pre-defined functions: `gnus-gather-threads-by-subject',
163 which only takes Subjects into consideration; and
164 `gnus-gather-threads-by-references', which compared the References
165 headers of the articles to find matches."
166   :group 'gnus-thread
167   :type '(radio (function-item gnus-gather-threads-by-subject)
168                 (function-item gnus-gather-threads-by-references)
169                 (function :tag "other")))
170
171 (defcustom gnus-summary-same-subject ""
172   "*String indicating that the current article has the same subject as the previous.
173 This variable will only be used if the value of
174 `gnus-summary-make-false-root' is `empty'."
175   :group 'gnus-summary-format
176   :type 'string)
177
178 (defcustom gnus-summary-goto-unread t
179   "*If t, many commands will go to the next unread article.
180 This applies to marking commands as well as other commands that
181 \"naturally\" select the next article, like, for instance, `SPC' at
182 the end of an article.
183
184 If nil, the marking commands do NOT go to the next unread article
185 (they go to the next article instead).  If `never', commands that
186 usually go to the next unread article, will go to the next article,
187 whether it is read or not."
188   :group 'gnus-summary-marks
189   :link '(custom-manual "(gnus)Setting Marks")
190   :type '(choice (const :tag "off" nil)
191                  (const never)
192                  (sexp :menu-tag "on" t)))
193
194 (defcustom gnus-summary-default-score 0
195   "*Default article score level.
196 All scores generated by the score files will be added to this score.
197 If this variable is nil, scoring will be disabled."
198   :group 'gnus-score-default
199   :type '(choice (const :tag "disable")
200                  integer))
201
202 (defcustom gnus-summary-zcore-fuzz 0
203   "*Fuzziness factor for the zcore in the summary buffer.
204 Articles with scores closer than this to `gnus-summary-default-score'
205 will not be marked."
206   :group 'gnus-summary-format
207   :type 'integer)
208
209 (defcustom gnus-simplify-subject-fuzzy-regexp nil
210   "*Strings to be removed when doing fuzzy matches.
211 This can either be a regular expression or list of regular expressions
212 that will be removed from subject strings if fuzzy subject
213 simplification is selected."
214   :group 'gnus-thread
215   :type '(repeat regexp))
216
217 (defcustom gnus-show-threads t
218   "*If non-nil, display threads in summary mode."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-hide-subtree nil
223   "*If non-nil, hide all threads initially.
224 If threads are hidden, you have to run the command
225 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
226 to expose hidden threads."
227   :group 'gnus-thread
228   :type 'boolean)
229
230 (defcustom gnus-thread-hide-killed t
231   "*If non-nil, hide killed threads automatically."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-ignore-subject t
236   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
237 If nil, articles that have different subjects from their parents will
238 start separate threads."
239   :group 'gnus-thread
240   :type 'boolean)
241
242 (defcustom gnus-thread-operation-ignore-subject t
243   "*If non-nil, subjects will be ignored when doing thread commands.
244 This affects commands like `gnus-summary-kill-thread' and
245 `gnus-summary-lower-thread'.
246
247 If this variable is nil, articles in the same thread with different
248 subjects will not be included in the operation in question.  If this
249 variable is `fuzzy', only articles that have subjects that are fuzzily
250 equal will be included."
251   :group 'gnus-thread
252   :type '(choice (const :tag "off" nil)
253                  (const fuzzy)
254                  (sexp :tag "on" t)))
255
256 (defcustom gnus-thread-indent-level 4
257   "*Number that says how much each sub-thread should be indented."
258   :group 'gnus-thread
259   :type 'integer)
260
261 (defcustom gnus-auto-extend-newsgroup t
262   "*If non-nil, extend newsgroup forward and backward when requested."
263   :group 'gnus-summary-choose
264   :type 'boolean)
265
266 (defcustom gnus-auto-select-first t
267   "*If nil, don't select the first unread article when entering a group.
268 If this variable is `best', select the highest-scored unread article
269 in the group.  If t, select the first unread article.
270
271 This variable can also be a function to place point on a likely
272 subject line.  Useful values include `gnus-summary-first-unread-subject',
273 `gnus-summary-first-unread-article' and
274 `gnus-summary-best-unread-article'.
275
276 If you want to prevent automatic selection of the first unread article
277 in some newsgroups, set the variable to nil in
278 `gnus-select-group-hook'."
279   :group 'gnus-group-select
280   :type '(choice (const :tag "none" nil)
281                  (const best)
282                  (sexp :menu-tag "first" t)
283                  (function-item gnus-summary-first-unread-subject)
284                  (function-item gnus-summary-first-unread-article)
285                  (function-item gnus-summary-best-unread-article)))
286
287 (defcustom gnus-auto-select-next t
288   "*If non-nil, offer to go to the next group from the end of the previous.
289 If the value is t and the next newsgroup is empty, Gnus will exit
290 summary mode and go back to group mode.  If the value is neither nil
291 nor t, Gnus will select the following unread newsgroup.  In
292 particular, if the value is the symbol `quietly', the next unread
293 newsgroup will be selected without any confirmation, and if it is
294 `almost-quietly', the next group will be selected without any
295 confirmation if you are located on the last article in the group.
296 Finally, if this variable is `slightly-quietly', the `Z n' command
297 will go to the next group without confirmation."
298   :group 'gnus-summary-maneuvering
299   :type '(choice (const :tag "off" nil)
300                  (const quietly)
301                  (const almost-quietly)
302                  (const slightly-quietly)
303                  (sexp :menu-tag "on" t)))
304
305 (defcustom gnus-auto-select-same nil
306   "*If non-nil, select the next article with the same subject.
307 If there are no more articles with the same subject, go to
308 the first unread article."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-summary-check-current nil
313   "*If non-nil, consider the current article when moving.
314 The \"unread\" movement commands will stay on the same line if the
315 current article is unread."
316   :group 'gnus-summary-maneuvering
317   :type 'boolean)
318
319 (defcustom gnus-auto-center-summary t
320   "*If non-nil, always center the current summary buffer.
321 In particular, if `vertical' do only vertical recentering.  If non-nil
322 and non-`vertical', do both horizontal and vertical recentering."
323   :group 'gnus-summary-maneuvering
324   :type '(choice (const :tag "none" nil)
325                  (const vertical)
326                  (integer :tag "height")
327                  (sexp :menu-tag "both" t)))
328
329 (defcustom gnus-show-all-headers nil
330   "*If non-nil, don't hide any headers."
331   :group 'gnus-article-hiding
332   :group 'gnus-article-headers
333   :type 'boolean)
334
335 (defcustom gnus-summary-ignore-duplicates nil
336   "*If non-nil, ignore articles with identical Message-ID headers."
337   :group 'gnus-summary
338   :type 'boolean)
339
340 (defcustom gnus-single-article-buffer t
341   "*If non-nil, display all articles in the same buffer.
342 If nil, each group will get its own article buffer."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-break-pages t
347   "*If non-nil, do page breaking on articles.
348 The page delimiter is specified by the `gnus-page-delimiter'
349 variable."
350   :group 'gnus-article-various
351   :type 'boolean)
352
353 (defcustom gnus-move-split-methods nil
354   "*Variable used to suggest where articles are to be moved to.
355 It uses the same syntax as the `gnus-split-methods' variable.
356 However, whereas `gnus-split-methods' specifies file names as targets,
357 this variable specifies group names."
358   :group 'gnus-summary-mail
359   :type '(repeat (choice (list :value (fun) function)
360                          (cons :value ("" "") regexp (repeat string))
361                          (sexp :value nil))))
362
363 (defcustom gnus-unread-mark ?  ;Whitespace
364   "*Mark used for unread articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-ticked-mark ?!
369   "*Mark used for ticked articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-dormant-mark ??
374   "*Mark used for dormant articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-del-mark ?r
379   "*Mark used for del'd articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-read-mark ?R
384   "*Mark used for read articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-expirable-mark ?E
389   "*Mark used for expirable articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-killed-mark ?K
394   "*Mark used for killed articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-souped-mark ?F
399   "*Mark used for killed articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-kill-file-mark ?X
404   "*Mark used for articles killed by kill files."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-low-score-mark ?Y
409   "*Mark used for articles with a low score."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-catchup-mark ?C
414   "*Mark used for articles that are caught up."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-replied-mark ?A
419   "*Mark used for articles that have been replied to."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-cached-mark ?*
424   "*Mark used for articles that are in the cache."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-saved-mark ?S
429   "*Mark used for articles that have been saved to."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-no-mark ?  ;Whitespace
434   "*Mark used for articles that have no other secondary mark."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-ancient-mark ?O
439   "*Mark used for ancient articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-sparse-mark ?Q
444   "*Mark used for sparsely reffed articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-canceled-mark ?G
449   "*Mark used for canceled articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-duplicate-mark ?M
454   "*Mark used for duplicate articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-undownloaded-mark ?@
459   "*Mark used for articles that weren't downloaded."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-downloadable-mark ?%
464   "*Mark used for articles that are to be downloaded."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-unsendable-mark ?=
469   "*Mark used for articles that won't be sent."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-score-over-mark ?+
474   "*Score mark used for articles with high scores."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-score-below-mark ?-
479   "*Score mark used for articles with low scores."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-empty-thread-mark ?  ;Whitespace
484   "*There is no thread under the article."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-not-empty-thread-mark ?=
489   "*There is a thread under the article."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-view-pseudo-asynchronously nil
494   "*If non-nil, Gnus will view pseudo-articles asynchronously."
495   :group 'gnus-extract-view
496   :type 'boolean)
497
498 (defcustom gnus-auto-expirable-marks
499   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
500         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
501         gnus-souped-mark gnus-duplicate-mark)
502   "*The list of marks converted into expiration if a group is auto-expirable."
503   :version "21.1"
504   :group 'gnus-summary
505   :type '(repeat character))
506
507 (defcustom gnus-inhibit-user-auto-expire t
508   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
509   :version "21.1"
510   :group 'gnus-summary
511   :type 'boolean)
512
513 (defcustom gnus-view-pseudos nil
514   "*If `automatic', pseudo-articles will be viewed automatically.
515 If `not-confirm', pseudos will be viewed automatically, and the user
516 will not be asked to confirm the command."
517   :group 'gnus-extract-view
518   :type '(choice (const :tag "off" nil)
519                  (const automatic)
520                  (const not-confirm)))
521
522 (defcustom gnus-view-pseudos-separately t
523   "*If non-nil, one pseudo-article will be created for each file to be viewed.
524 If nil, all files that use the same viewing command will be given as a
525 list of parameters to that command."
526   :group 'gnus-extract-view
527   :type 'boolean)
528
529 (defcustom gnus-insert-pseudo-articles t
530   "*If non-nil, insert pseudo-articles when decoding articles."
531   :group 'gnus-extract-view
532   :type 'boolean)
533
534 (defcustom gnus-summary-dummy-line-format
535   "  %(:                          :%) %S\n"
536   "*The format specification for the dummy roots in the summary buffer.
537 It works along the same lines as a normal formatting string,
538 with some simple extensions.
539
540 %S  The subject"
541   :group 'gnus-threading
542   :type 'string)
543
544 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
545   "*The format specification for the summary mode line.
546 It works along the same lines as a normal formatting string,
547 with some simple extensions:
548
549 %G  Group name
550 %p  Unprefixed group name
551 %A  Current article number
552 %z  Current article score
553 %V  Gnus version
554 %U  Number of unread articles in the group
555 %e  Number of unselected articles in the group
556 %Z  A string with unread/unselected article counts
557 %g  Shortish group name
558 %S  Subject of the current article
559 %u  User-defined spec
560 %s  Current score file name
561 %d  Number of dormant articles
562 %r  Number of articles that have been marked as read in this session
563 %E  Number of articles expunged by the score files"
564   :group 'gnus-summary-format
565   :type 'string)
566
567 (defcustom gnus-list-identifiers nil
568   "Regexp that matches list identifiers to be removed from subject.
569 This can also be a list of regexps."
570   :version "21.1"
571   :group 'gnus-summary-format
572   :group 'gnus-article-hiding
573   :type '(choice (const :tag "none" nil)
574                  (regexp :value ".*")
575                  (repeat :value (".*") regexp)))
576
577 (defcustom gnus-summary-mark-below 0
578   "*Mark all articles with a score below this variable as read.
579 This variable is local to each summary buffer and usually set by the
580 score file."
581   :group 'gnus-score-default
582   :type 'integer)
583
584 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
585   "*List of functions used for sorting articles in the summary buffer.
586 This variable is only used when not using a threaded display."
587   :group 'gnus-summary-sort
588   :type '(repeat (choice (function-item gnus-article-sort-by-number)
589                          (function-item gnus-article-sort-by-author)
590                          (function-item gnus-article-sort-by-subject)
591                          (function-item gnus-article-sort-by-date)
592                          (function-item gnus-article-sort-by-score)
593                          (function :tag "other"))))
594
595 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
596   "*List of functions used for sorting threads in the summary buffer.
597 By default, threads are sorted by article number.
598
599 Each function takes two threads and return non-nil if the first thread
600 should be sorted before the other.  If you use more than one function,
601 the primary sort function should be the last.  You should probably
602 always include `gnus-thread-sort-by-number' in the list of sorting
603 functions -- preferably first.
604
605 Ready-made functions include `gnus-thread-sort-by-number',
606 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
607 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
608 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
609   :group 'gnus-summary-sort
610   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
611                          (function-item gnus-thread-sort-by-author)
612                          (function-item gnus-thread-sort-by-subject)
613                          (function-item gnus-thread-sort-by-date)
614                          (function-item gnus-thread-sort-by-score)
615                          (function-item gnus-thread-sort-by-total-score)
616                          (function :tag "other"))))
617
618 (defcustom gnus-thread-score-function '+
619   "*Function used for calculating the total score of a thread.
620
621 The function is called with the scores of the article and each
622 subthread and should then return the score of the thread.
623
624 Some functions you can use are `+', `max', or `min'."
625   :group 'gnus-summary-sort
626   :type 'function)
627
628 (defcustom gnus-summary-expunge-below nil
629   "All articles that have a score less than this variable will be expunged.
630 This variable is local to the summary buffers."
631   :group 'gnus-score-default
632   :type '(choice (const :tag "off" nil)
633                  integer))
634
635 (defcustom gnus-thread-expunge-below nil
636   "All threads that have a total score less than this variable will be expunged.
637 See `gnus-thread-score-function' for en explanation of what a
638 \"thread score\" is.
639
640 This variable is local to the summary buffers."
641   :group 'gnus-threading
642   :group 'gnus-score-default
643   :type '(choice (const :tag "off" nil)
644                  integer))
645
646 (defcustom gnus-summary-mode-hook nil
647   "*A hook for Gnus summary mode.
648 This hook is run before any variables are set in the summary buffer."
649   :options '(turn-on-gnus-mailing-list-mode)
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-menu-hook nil
654   "*Hook run after the creation of the summary mode menu."
655   :group 'gnus-summary-visual
656   :type 'hook)
657
658 (defcustom gnus-summary-exit-hook nil
659   "*A hook called on exit from the summary buffer.
660 It will be called with point in the group buffer."
661   :group 'gnus-summary-exit
662   :type 'hook)
663
664 (defcustom gnus-summary-prepare-hook nil
665   "*A hook called after the summary buffer has been generated.
666 If you want to modify the summary buffer, you can use this hook."
667   :group 'gnus-summary-various
668   :type 'hook)
669
670 (defcustom gnus-summary-prepared-hook nil
671   "*A hook called as the last thing after the summary buffer has been generated."
672   :group 'gnus-summary-various
673   :type 'hook)
674
675 (defcustom gnus-summary-generate-hook nil
676   "*A hook run just before generating the summary buffer.
677 This hook is commonly used to customize threading variables and the
678 like."
679   :group 'gnus-summary-various
680   :type 'hook)
681
682 (defcustom gnus-select-group-hook nil
683   "*A hook called when a newsgroup is selected.
684
685 If you'd like to simplify subjects like the
686 `gnus-summary-next-same-subject' command does, you can use the
687 following hook:
688
689  (setq gnus-select-group-hook
690       (list
691         (lambda ()
692           (mapcar (lambda (header)
693                      (mail-header-set-subject
694                       header
695                       (gnus-simplify-subject
696                        (mail-header-subject header) 're-only)))
697                   gnus-newsgroup-headers))))"
698   :group 'gnus-group-select
699   :type 'hook)
700
701 (defcustom gnus-select-article-hook nil
702   "*A hook called when an article is selected."
703   :group 'gnus-summary-choose
704   :type 'hook)
705
706 (defcustom gnus-visual-mark-article-hook
707   (list 'gnus-highlight-selected-summary)
708   "*Hook run after selecting an article in the summary buffer.
709 It is meant to be used for highlighting the article in some way.  It
710 is not run if `gnus-visual' is nil."
711   :group 'gnus-summary-visual
712   :type 'hook)
713
714 (defcustom gnus-parse-headers-hook nil
715   "*A hook called before parsing the headers."
716   :group 'gnus-various
717   :type 'hook)
718
719 (defcustom gnus-exit-group-hook nil
720   "*A hook called when exiting summary mode.
721 This hook is not called from the non-updating exit commands like `Q'."
722   :group 'gnus-various
723   :type 'hook)
724
725 (defcustom gnus-summary-update-hook
726   (list 'gnus-summary-highlight-line)
727   "*A hook called when a summary line is changed.
728 The hook will not be called if `gnus-visual' is nil.
729
730 The default function `gnus-summary-highlight-line' will
731 highlight the line according to the `gnus-summary-highlight'
732 variable."
733   :group 'gnus-summary-visual
734   :type 'hook)
735
736 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
737   "*A hook called when an article is selected for the first time.
738 The hook is intended to mark an article as read (or unread)
739 automatically when it is selected."
740   :group 'gnus-summary-choose
741   :type 'hook)
742
743 (defcustom gnus-group-no-more-groups-hook nil
744   "*A hook run when returning to group mode having no more (unread) groups."
745   :group 'gnus-group-select
746   :type 'hook)
747
748 (defcustom gnus-ps-print-hook nil
749   "*A hook run before ps-printing something from Gnus."
750   :group 'gnus-summary
751   :type 'hook)
752
753 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
754   "Face used for highlighting the current article in the summary buffer."
755   :group 'gnus-summary-visual
756   :type 'face)
757
758 (defcustom gnus-summary-highlight
759   '(((= mark gnus-canceled-mark)
760      . gnus-summary-cancelled-face)
761     ((and (> score default)
762           (or (= mark gnus-dormant-mark)
763               (= mark gnus-ticked-mark)))
764      . gnus-summary-high-ticked-face)
765     ((and (< score default)
766           (or (= mark gnus-dormant-mark)
767               (= mark gnus-ticked-mark)))
768      . gnus-summary-low-ticked-face)
769     ((or (= mark gnus-dormant-mark)
770          (= mark gnus-ticked-mark))
771      . gnus-summary-normal-ticked-face)
772     ((and (> score default) (= mark gnus-ancient-mark))
773      . gnus-summary-high-ancient-face)
774     ((and (< score default) (= mark gnus-ancient-mark))
775      . gnus-summary-low-ancient-face)
776     ((= mark gnus-ancient-mark)
777      . gnus-summary-normal-ancient-face)
778     ((and (> score default) (= mark gnus-unread-mark))
779      . gnus-summary-high-unread-face)
780     ((and (< score default) (= mark gnus-unread-mark))
781      . gnus-summary-low-unread-face)
782     ((= mark gnus-unread-mark)
783      . gnus-summary-normal-unread-face)
784     ((and (> score default) (memq mark (list gnus-downloadable-mark
785                                              gnus-undownloaded-mark)))
786      . gnus-summary-high-unread-face)
787     ((and (< score default) (memq mark (list gnus-downloadable-mark
788                                              gnus-undownloaded-mark)))
789      . gnus-summary-low-unread-face)
790     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
791      . gnus-summary-normal-unread-face)
792     ((> score default)
793      . gnus-summary-high-read-face)
794     ((< score default)
795      . gnus-summary-low-read-face)
796     (t
797      . gnus-summary-normal-read-face))
798   "*Controls the highlighting of summary buffer lines.
799
800 A list of (FORM . FACE) pairs.  When deciding how a a particular
801 summary line should be displayed, each form is evaluated.  The content
802 of the face field after the first true form is used.  You can change
803 how those summary lines are displayed, by editing the face field.
804
805 You can use the following variables in the FORM field.
806
807 score:   The articles score
808 default: The default article score.
809 below:   The score below which articles are automatically marked as read.
810 mark:    The articles mark."
811   :group 'gnus-summary-visual
812   :type '(repeat (cons (sexp :tag "Form" nil)
813                        face)))
814
815 (defcustom gnus-alter-header-function nil
816   "Function called to allow alteration of article header structures.
817 The function is called with one parameter, the article header vector,
818 which it may alter in any way.")
819
820 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
821   "Variable that says which function should be used to decode a string with encoded words.")
822
823 (defcustom gnus-extra-headers nil
824   "*Extra headers to parse."
825   :version "21.1"
826   :group 'gnus-summary
827   :type '(repeat symbol))
828
829 (defcustom gnus-ignored-from-addresses
830   (and user-mail-address (regexp-quote user-mail-address))
831   "*Regexp of From headers that may be suppressed in favor of To headers."
832   :version "21.1"
833   :group 'gnus-summary
834   :type 'regexp)
835
836 (defcustom gnus-group-charset-alist
837   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
838     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
839     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
840     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
841     ("^relcom\\>" koi8-r)
842     ("^fido7\\>" koi8-r)
843     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
844     ("^israel\\>" iso-8859-1)
845     ("^han\\>" euc-kr)
846     ("^alt.chinese.text.big5\\>" chinese-big5)
847     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
848     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
849     (".*" iso-8859-1))
850   "Alist of regexps (to match group names) and default charsets to be used when reading."
851   :type '(repeat (list (regexp :tag "Group")
852                        (symbol :tag "Charset")))
853   :group 'gnus-charset)
854
855 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
856   "List of charsets that should be ignored.
857 When these charsets are used in the \"charset\" parameter, the
858 default charset will be used instead."
859   :version "21.1"
860   :type '(repeat symbol)
861   :group 'gnus-charset)
862
863 (defcustom gnus-group-ignored-charsets-alist
864   '(("alt\\.chinese\\.text" iso-8859-1))
865   "Alist of regexps (to match group names) and charsets that should be ignored.
866 When these charsets are used in the \"charset\" parameter, the
867 default charset will be used instead."
868   :type '(repeat (cons (regexp :tag "Group")
869                        (repeat symbol)))
870   :group 'gnus-charset)
871
872 (defcustom gnus-group-highlight-words-alist nil
873   "Alist of group regexps and highlight regexps.
874 This variable uses the same syntax as `gnus-emphasis-alist'."
875   :version "21.1"
876   :type '(repeat (cons (regexp :tag "Group")
877                        (repeat (list (regexp :tag "Highlight regexp")
878                                      (number :tag "Group for entire word" 0)
879                                      (number :tag "Group for displayed part" 0)
880                                      (symbol :tag "Face"
881                                              gnus-emphasis-highlight-words)))))
882   :group 'gnus-summary-visual)
883
884 (defcustom gnus-summary-show-article-charset-alist
885   nil
886   "Alist of number and charset.
887 The article will be shown with the charset corresponding to the
888 numbered argument.
889 For example: ((1 . cn-gb-2312) (2 . big5))."
890   :version "21.1"
891   :type '(repeat (cons (number :tag "Argument" 1)
892                        (symbol :tag "Charset")))
893   :group 'gnus-charset)
894
895 (defcustom gnus-preserve-marks t
896   "Whether marks are preserved when moving, copying and respooling messages."
897   :version "21.1"
898   :type 'boolean
899   :group 'gnus-summary-marks)
900
901 (defcustom gnus-alter-articles-to-read-function nil
902   "Function to be called to alter the list of articles to be selected."
903   :type 'function
904   :group 'gnus-summary)
905
906 (defcustom gnus-orphan-score nil
907   "*All orphans get this score added.  Set in the score file."
908   :group 'gnus-score-default
909   :type '(choice (const nil)
910                  integer))
911
912 (defcustom gnus-summary-save-parts-default-mime "image/.*"
913   "*A regexp to match MIME parts when saving multiple parts of a message
914 with gnus-summary-save-parts (X m). This regexp will be used by default
915 when prompting the user for which type of files to save."
916   :group 'gnus-summary
917   :type 'regexp)
918
919
920 (defcustom gnus-summary-save-parts-default-mime "image/.*"
921   "*A regexp to match MIME parts when saving multiple parts of a message
922 with gnus-summary-save-parts (X m). This regexp will be used by default
923 when prompting the user for which type of files to save."
924   :group 'gnus-summary
925   :type 'regexp)
926
927
928 ;;; Internal variables
929
930 (defvar gnus-article-mime-handles nil)
931 (defvar gnus-article-decoded-p nil)
932 (defvar gnus-article-charset nil)
933 (defvar gnus-article-ignored-charsets nil)
934 (defvar gnus-scores-exclude-files nil)
935 (defvar gnus-page-broken nil)
936 (defvar gnus-inhibit-mime-unbuttonizing nil)
937
938 (defvar gnus-original-article nil)
939 (defvar gnus-article-internal-prepare-hook nil)
940 (defvar gnus-newsgroup-process-stack nil)
941
942 (defvar gnus-thread-indent-array nil)
943 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
944 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
945   "Function called to sort the articles within a thread after it has been gathered together.")
946
947 (defvar gnus-summary-save-parts-type-history nil)
948 (defvar gnus-summary-save-parts-last-directory nil)
949
950 (defvar gnus-summary-save-parts-type-history nil)
951 (defvar gnus-summary-save-parts-last-directory nil)
952
953 ;; Avoid highlighting in kill files.
954 (defvar gnus-summary-inhibit-highlight nil)
955 (defvar gnus-newsgroup-selected-overlay nil)
956 (defvar gnus-inhibit-limiting nil)
957 (defvar gnus-newsgroup-adaptive-score-file nil)
958 (defvar gnus-current-score-file nil)
959 (defvar gnus-current-move-group nil)
960 (defvar gnus-current-copy-group nil)
961 (defvar gnus-current-crosspost-group nil)
962
963 (defvar gnus-newsgroup-dependencies nil)
964 (defvar gnus-newsgroup-adaptive nil)
965 (defvar gnus-summary-display-article-function nil)
966 (defvar gnus-summary-highlight-line-function nil
967   "Function called after highlighting a summary line.")
968
969 (defvar gnus-summary-line-format-alist
970   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
971     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
972     (?s gnus-tmp-subject-or-nil ?s)
973     (?n gnus-tmp-name ?s)
974     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
975         ?s)
976     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
977             gnus-tmp-from) ?s)
978     (?F gnus-tmp-from ?s)
979     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
980     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
981     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
982     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
983     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
984     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
985     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
986     (?L gnus-tmp-lines ?d)
987     (?I gnus-tmp-indentation ?s)
988     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
989     (?R gnus-tmp-replied ?c)
990     (?\[ gnus-tmp-opening-bracket ?c)
991     (?\] gnus-tmp-closing-bracket ?c)
992     (?\> (make-string gnus-tmp-level ? ) ?s)
993     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
994     (?i gnus-tmp-score ?d)
995     (?z gnus-tmp-score-char ?c)
996     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
997     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
998     (?U gnus-tmp-unread ?c)
999     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1000     (?t (gnus-summary-number-of-articles-in-thread
1001          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1002         ?d)
1003     (?e (gnus-summary-number-of-articles-in-thread
1004          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1005         ?c)
1006     (?u gnus-tmp-user-defined ?s)
1007     (?P (gnus-pick-line-number) ?d))
1008   "An alist of format specifications that can appear in summary lines.
1009 These are paired with what variables they correspond with, along with
1010 the type of the variable (string, integer, character, etc).")
1011
1012 (defvar gnus-summary-dummy-line-format-alist
1013   `((?S gnus-tmp-subject ?s)
1014     (?N gnus-tmp-number ?d)
1015     (?u gnus-tmp-user-defined ?s)))
1016
1017 (defvar gnus-summary-mode-line-format-alist
1018   `((?G gnus-tmp-group-name ?s)
1019     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1020     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1021     (?A gnus-tmp-article-number ?d)
1022     (?Z gnus-tmp-unread-and-unselected ?s)
1023     (?V gnus-version ?s)
1024     (?U gnus-tmp-unread-and-unticked ?d)
1025     (?S gnus-tmp-subject ?s)
1026     (?e gnus-tmp-unselected ?d)
1027     (?u gnus-tmp-user-defined ?s)
1028     (?d (length gnus-newsgroup-dormant) ?d)
1029     (?t (length gnus-newsgroup-marked) ?d)
1030     (?r (length gnus-newsgroup-reads) ?d)
1031     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1032     (?E gnus-newsgroup-expunged-tally ?d)
1033     (?s (gnus-current-score-file-nondirectory) ?s)))
1034
1035 (defvar gnus-last-search-regexp nil
1036   "Default regexp for article search command.")
1037
1038 (defvar gnus-last-shell-command nil
1039   "Default shell command on article.")
1040
1041 (defvar gnus-newsgroup-begin nil)
1042 (defvar gnus-newsgroup-end nil)
1043 (defvar gnus-newsgroup-last-rmail nil)
1044 (defvar gnus-newsgroup-last-mail nil)
1045 (defvar gnus-newsgroup-last-folder nil)
1046 (defvar gnus-newsgroup-last-file nil)
1047 (defvar gnus-newsgroup-auto-expire nil)
1048 (defvar gnus-newsgroup-active nil)
1049
1050 (defvar gnus-newsgroup-data nil)
1051 (defvar gnus-newsgroup-data-reverse nil)
1052 (defvar gnus-newsgroup-limit nil)
1053 (defvar gnus-newsgroup-limits nil)
1054
1055 (defvar gnus-newsgroup-unreads nil
1056   "List of unread articles in the current newsgroup.")
1057
1058 (defvar gnus-newsgroup-unselected nil
1059   "List of unselected unread articles in the current newsgroup.")
1060
1061 (defvar gnus-newsgroup-reads nil
1062   "Alist of read articles and article marks in the current newsgroup.")
1063
1064 (defvar gnus-newsgroup-expunged-tally nil)
1065
1066 (defvar gnus-newsgroup-marked nil
1067   "List of ticked articles in the current newsgroup (a subset of unread art).")
1068
1069 (defvar gnus-newsgroup-killed nil
1070   "List of ranges of articles that have been through the scoring process.")
1071
1072 (defvar gnus-newsgroup-cached nil
1073   "List of articles that come from the article cache.")
1074
1075 (defvar gnus-newsgroup-saved nil
1076   "List of articles that have been saved.")
1077
1078 (defvar gnus-newsgroup-kill-headers nil)
1079
1080 (defvar gnus-newsgroup-replied nil
1081   "List of articles that have been replied to in the current newsgroup.")
1082
1083 (defvar gnus-newsgroup-expirable nil
1084   "List of articles in the current newsgroup that can be expired.")
1085
1086 (defvar gnus-newsgroup-processable nil
1087   "List of articles in the current newsgroup that can be processed.")
1088
1089 (defvar gnus-newsgroup-downloadable nil
1090   "List of articles in the current newsgroup that can be processed.")
1091
1092 (defvar gnus-newsgroup-undownloaded nil
1093   "List of articles in the current newsgroup that haven't been downloaded..")
1094
1095 (defvar gnus-newsgroup-unsendable nil
1096   "List of articles in the current newsgroup that won't be sent.")
1097
1098 (defvar gnus-newsgroup-bookmarks nil
1099   "List of articles in the current newsgroup that have bookmarks.")
1100
1101 (defvar gnus-newsgroup-dormant nil
1102   "List of dormant articles in the current newsgroup.")
1103
1104 (defvar gnus-newsgroup-scored nil
1105   "List of scored articles in the current newsgroup.")
1106
1107 (defvar gnus-newsgroup-headers nil
1108   "List of article headers in the current newsgroup.")
1109
1110 (defvar gnus-newsgroup-threads nil)
1111
1112 (defvar gnus-newsgroup-prepared nil
1113   "Whether the current group has been prepared properly.")
1114
1115 (defvar gnus-newsgroup-ancient nil
1116   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1117
1118 (defvar gnus-newsgroup-sparse nil)
1119
1120 (defvar gnus-current-article nil)
1121 (defvar gnus-article-current nil)
1122 (defvar gnus-current-headers nil)
1123 (defvar gnus-have-all-headers nil)
1124 (defvar gnus-last-article nil)
1125 (defvar gnus-newsgroup-history nil)
1126 (defvar gnus-newsgroup-charset nil)
1127 (defvar gnus-newsgroup-ephemeral-charset nil)
1128 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1129
1130 (defvar gnus-article-before-search nil)
1131
1132 (defconst gnus-summary-local-variables
1133   '(gnus-newsgroup-name
1134     gnus-newsgroup-begin gnus-newsgroup-end
1135     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1136     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1137     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1138     gnus-newsgroup-unselected gnus-newsgroup-marked
1139     gnus-newsgroup-reads gnus-newsgroup-saved
1140     gnus-newsgroup-replied gnus-newsgroup-expirable
1141     gnus-newsgroup-processable gnus-newsgroup-killed
1142     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1143     gnus-newsgroup-unsendable
1144     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1145     gnus-newsgroup-headers gnus-newsgroup-threads
1146     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1147     gnus-current-article gnus-current-headers gnus-have-all-headers
1148     gnus-last-article gnus-article-internal-prepare-hook
1149     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1150     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1151     gnus-thread-expunge-below
1152     gnus-score-alist gnus-current-score-file
1153     (gnus-summary-expunge-below . global)
1154     (gnus-summary-mark-below . global)
1155     (gnus-orphan-score . global)
1156     gnus-newsgroup-active gnus-scores-exclude-files
1157     gnus-newsgroup-history gnus-newsgroup-ancient
1158     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1159     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1160     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1161     (gnus-newsgroup-expunged-tally . 0)
1162     gnus-cache-removable-articles gnus-newsgroup-cached
1163     gnus-newsgroup-data gnus-newsgroup-data-reverse
1164     gnus-newsgroup-limit gnus-newsgroup-limits
1165     gnus-newsgroup-charset)
1166   "Variables that are buffer-local to the summary buffers.")
1167
1168 (defvar gnus-newsgroup-variables nil
1169   "Variables that have separate values in the newsgroups.")
1170
1171 ;; Byte-compiler warning.
1172 (eval-when-compile (defvar gnus-article-mode-map))
1173
1174 ;; MIME stuff.
1175
1176 (defvar gnus-decode-encoded-word-methods
1177   '(mail-decode-encoded-word-string)
1178   "List of methods used to decode encoded words.
1179
1180 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1181 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1182 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1183 whose names match REGEXP.
1184
1185 For example:
1186 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1187  mail-decode-encoded-word-string
1188  (\"chinese\" . rfc1843-decode-string))")
1189
1190 (defvar gnus-decode-encoded-word-methods-cache nil)
1191
1192 (defun gnus-multi-decode-encoded-word-string (string)
1193   "Apply the functions from `gnus-encoded-word-methods' that match."
1194   (unless (and gnus-decode-encoded-word-methods-cache
1195                (eq gnus-newsgroup-name
1196                    (car gnus-decode-encoded-word-methods-cache)))
1197     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1198     (mapcar (lambda (x)
1199               (if (symbolp x)
1200                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1201                 (if (and gnus-newsgroup-name
1202                          (string-match (car x) gnus-newsgroup-name))
1203                     (nconc gnus-decode-encoded-word-methods-cache
1204                            (list (cdr x))))))
1205           gnus-decode-encoded-word-methods))
1206   (let ((xlist gnus-decode-encoded-word-methods-cache))
1207     (pop xlist)
1208     (while xlist
1209       (setq string (funcall (pop xlist) string))))
1210   string)
1211
1212 ;; Subject simplification.
1213
1214 (defun gnus-simplify-whitespace (str)
1215   "Remove excessive whitespace from STR."
1216   (let ((mystr str))
1217     ;; Multiple spaces.
1218     (while (string-match "[ \t][ \t]+" mystr)
1219       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1220                           " "
1221                           (substring mystr (match-end 0)))))
1222     ;; Leading spaces.
1223     (when (string-match "^[ \t]+" mystr)
1224       (setq mystr (substring mystr (match-end 0))))
1225     ;; Trailing spaces.
1226     (when (string-match "[ \t]+$" mystr)
1227       (setq mystr (substring mystr 0 (match-beginning 0))))
1228     mystr))
1229
1230 (defsubst gnus-simplify-subject-re (subject)
1231   "Remove \"Re:\" from subject lines."
1232   (if (string-match "^[Rr][Ee]: *" subject)
1233       (substring subject (match-end 0))
1234     subject))
1235
1236 (defun gnus-simplify-subject (subject &optional re-only)
1237   "Remove `Re:' and words in parentheses.
1238 If RE-ONLY is non-nil, strip leading `Re:'s only."
1239   (let ((case-fold-search t))           ;Ignore case.
1240     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1241     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1242       (setq subject (substring subject (match-end 0))))
1243     ;; Remove uninteresting prefixes.
1244     (when (and (not re-only)
1245                gnus-simplify-ignored-prefixes
1246                (string-match gnus-simplify-ignored-prefixes subject))
1247       (setq subject (substring subject (match-end 0))))
1248     ;; Remove words in parentheses from end.
1249     (unless re-only
1250       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1251         (setq subject (substring subject 0 (match-beginning 0)))))
1252     ;; Return subject string.
1253     subject))
1254
1255 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1256 ;; all whitespace.
1257 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1258   (goto-char (point-min))
1259   (while (re-search-forward regexp nil t)
1260     (replace-match (or newtext ""))))
1261
1262 (defun gnus-simplify-buffer-fuzzy ()
1263   "Simplify string in the buffer fuzzily.
1264 The string in the accessible portion of the current buffer is simplified.
1265 It is assumed to be a single-line subject.
1266 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1267 matter is removed.  Additional things can be deleted by setting
1268 `gnus-simplify-subject-fuzzy-regexp'."
1269   (let ((case-fold-search t)
1270         (modified-tick))
1271     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1272
1273     (while (not (eq modified-tick (buffer-modified-tick)))
1274       (setq modified-tick (buffer-modified-tick))
1275       (cond
1276        ((listp gnus-simplify-subject-fuzzy-regexp)
1277         (mapcar 'gnus-simplify-buffer-fuzzy-step
1278                 gnus-simplify-subject-fuzzy-regexp))
1279        (gnus-simplify-subject-fuzzy-regexp
1280         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1281       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1282       (gnus-simplify-buffer-fuzzy-step
1283        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1284       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1285
1286     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1287     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1288     (gnus-simplify-buffer-fuzzy-step " $")
1289     (gnus-simplify-buffer-fuzzy-step "^ +")))
1290
1291 (defun gnus-simplify-subject-fuzzy (subject)
1292   "Simplify a subject string fuzzily.
1293 See `gnus-simplify-buffer-fuzzy' for details."
1294   (save-excursion
1295     (gnus-set-work-buffer)
1296     (let ((case-fold-search t))
1297       ;; Remove uninteresting prefixes.
1298       (when (and gnus-simplify-ignored-prefixes
1299                  (string-match gnus-simplify-ignored-prefixes subject))
1300         (setq subject (substring subject (match-end 0))))
1301       (insert subject)
1302       (inline (gnus-simplify-buffer-fuzzy))
1303       (buffer-string))))
1304
1305 (defsubst gnus-simplify-subject-fully (subject)
1306   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1307   (cond
1308    (gnus-simplify-subject-functions
1309     (gnus-map-function gnus-simplify-subject-functions subject))
1310    ((null gnus-summary-gather-subject-limit)
1311     (gnus-simplify-subject-re subject))
1312    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1313     (gnus-simplify-subject-fuzzy subject))
1314    ((numberp gnus-summary-gather-subject-limit)
1315     (gnus-limit-string (gnus-simplify-subject-re subject)
1316                        gnus-summary-gather-subject-limit))
1317    (t
1318     subject)))
1319
1320 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1321   "Check whether two subjects are equal.
1322 If optional argument simple-first is t, first argument is already
1323 simplified."
1324   (cond
1325    ((null simple-first)
1326     (equal (gnus-simplify-subject-fully s1)
1327            (gnus-simplify-subject-fully s2)))
1328    (t
1329     (equal s1
1330            (gnus-simplify-subject-fully s2)))))
1331
1332 (defun gnus-summary-bubble-group ()
1333   "Increase the score of the current group.
1334 This is a handy function to add to `gnus-summary-exit-hook' to
1335 increase the score of each group you read."
1336   (gnus-group-add-score gnus-newsgroup-name))
1337
1338 \f
1339 ;;;
1340 ;;; Gnus summary mode
1341 ;;;
1342
1343 (put 'gnus-summary-mode 'mode-class 'special)
1344
1345 (when t
1346   ;; Non-orthogonal keys
1347
1348   (gnus-define-keys gnus-summary-mode-map
1349     " " gnus-summary-next-page
1350     "\177" gnus-summary-prev-page
1351     [delete] gnus-summary-prev-page
1352     [backspace] gnus-summary-prev-page
1353     "\r" gnus-summary-scroll-up
1354     "\M-\r" gnus-summary-scroll-down
1355     "n" gnus-summary-next-unread-article
1356     "p" gnus-summary-prev-unread-article
1357     "N" gnus-summary-next-article
1358     "P" gnus-summary-prev-article
1359     "\M-\C-n" gnus-summary-next-same-subject
1360     "\M-\C-p" gnus-summary-prev-same-subject
1361     "\M-n" gnus-summary-next-unread-subject
1362     "\M-p" gnus-summary-prev-unread-subject
1363     "." gnus-summary-first-unread-article
1364     "," gnus-summary-best-unread-article
1365     "\M-s" gnus-summary-search-article-forward
1366     "\M-r" gnus-summary-search-article-backward
1367     "<" gnus-summary-beginning-of-article
1368     ">" gnus-summary-end-of-article
1369     "j" gnus-summary-goto-article
1370     "^" gnus-summary-refer-parent-article
1371     "\M-^" gnus-summary-refer-article
1372     "u" gnus-summary-tick-article-forward
1373     "!" gnus-summary-tick-article-forward
1374     "U" gnus-summary-tick-article-backward
1375     "d" gnus-summary-mark-as-read-forward
1376     "D" gnus-summary-mark-as-read-backward
1377     "E" gnus-summary-mark-as-expirable
1378     "\M-u" gnus-summary-clear-mark-forward
1379     "\M-U" gnus-summary-clear-mark-backward
1380     "k" gnus-summary-kill-same-subject-and-select
1381     "\C-k" gnus-summary-kill-same-subject
1382     "\M-\C-k" gnus-summary-kill-thread
1383     "\M-\C-l" gnus-summary-lower-thread
1384     "e" gnus-summary-edit-article
1385     "#" gnus-summary-mark-as-processable
1386     "\M-#" gnus-summary-unmark-as-processable
1387     "\M-\C-t" gnus-summary-toggle-threads
1388     "\M-\C-s" gnus-summary-show-thread
1389     "\M-\C-h" gnus-summary-hide-thread
1390     "\M-\C-f" gnus-summary-next-thread
1391     "\M-\C-b" gnus-summary-prev-thread
1392     [(meta down)] gnus-summary-next-thread
1393     [(meta up)] gnus-summary-prev-thread
1394     "\M-\C-u" gnus-summary-up-thread
1395     "\M-\C-d" gnus-summary-down-thread
1396     "&" gnus-summary-execute-command
1397     "c" gnus-summary-catchup-and-exit
1398     "\C-w" gnus-summary-mark-region-as-read
1399     "\C-t" gnus-summary-toggle-truncation
1400     "?" gnus-summary-mark-as-dormant
1401     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1402     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1403     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1404     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1405     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1406     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1407     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1408     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1409     "=" gnus-summary-expand-window
1410     "\C-x\C-s" gnus-summary-reselect-current-group
1411     "\M-g" gnus-summary-rescan-group
1412     "w" gnus-summary-stop-page-breaking
1413     "\C-c\C-r" gnus-summary-caesar-message
1414     "f" gnus-summary-followup
1415     "F" gnus-summary-followup-with-original
1416     "C" gnus-summary-cancel-article
1417     "r" gnus-summary-reply
1418     "R" gnus-summary-reply-with-original
1419     "\C-c\C-f" gnus-summary-mail-forward
1420     "o" gnus-summary-save-article
1421     "\C-o" gnus-summary-save-article-mail
1422     "|" gnus-summary-pipe-output
1423     "\M-k" gnus-summary-edit-local-kill
1424     "\M-K" gnus-summary-edit-global-kill
1425     ;; "V" gnus-version
1426     "\C-c\C-d" gnus-summary-describe-group
1427     "q" gnus-summary-exit
1428     "Q" gnus-summary-exit-no-update
1429     "\C-c\C-i" gnus-info-find-node
1430     gnus-mouse-2 gnus-mouse-pick-article
1431     "m" gnus-summary-mail-other-window
1432     "a" gnus-summary-post-news
1433     "x" gnus-summary-limit-to-unread
1434     "s" gnus-summary-isearch-article
1435     "t" gnus-summary-toggle-header
1436     "g" gnus-summary-show-article
1437     "l" gnus-summary-goto-last-article
1438     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1439     "\C-d" gnus-summary-enter-digest-group
1440     "\M-\C-d" gnus-summary-read-document
1441     "\M-\C-e" gnus-summary-edit-parameters
1442     "\M-\C-a" gnus-summary-customize-parameters
1443     "\C-c\C-b" gnus-bug
1444     "*" gnus-cache-enter-article
1445     "\M-*" gnus-cache-remove-article
1446     "\M-&" gnus-summary-universal-argument
1447     "\C-l" gnus-recenter
1448     "I" gnus-summary-increase-score
1449     "L" gnus-summary-lower-score
1450     "\M-i" gnus-symbolic-argument
1451     "h" gnus-summary-select-article-buffer
1452
1453     "b" gnus-article-view-part
1454     "\M-t" gnus-summary-toggle-display-buttonized
1455
1456     "V" gnus-summary-score-map
1457     "X" gnus-uu-extract-map
1458     "S" gnus-summary-send-map)
1459
1460   ;; Sort of orthogonal keymap
1461   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1462     "t" gnus-summary-tick-article-forward
1463     "!" gnus-summary-tick-article-forward
1464     "d" gnus-summary-mark-as-read-forward
1465     "r" gnus-summary-mark-as-read-forward
1466     "c" gnus-summary-clear-mark-forward
1467     " " gnus-summary-clear-mark-forward
1468     "e" gnus-summary-mark-as-expirable
1469     "x" gnus-summary-mark-as-expirable
1470     "?" gnus-summary-mark-as-dormant
1471     "b" gnus-summary-set-bookmark
1472     "B" gnus-summary-remove-bookmark
1473     "#" gnus-summary-mark-as-processable
1474     "\M-#" gnus-summary-unmark-as-processable
1475     "S" gnus-summary-limit-include-expunged
1476     "C" gnus-summary-catchup
1477     "H" gnus-summary-catchup-to-here
1478     "\C-c" gnus-summary-catchup-all
1479     "k" gnus-summary-kill-same-subject-and-select
1480     "K" gnus-summary-kill-same-subject
1481     "P" gnus-uu-mark-map)
1482
1483   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1484     "c" gnus-summary-clear-above
1485     "u" gnus-summary-tick-above
1486     "m" gnus-summary-mark-above
1487     "k" gnus-summary-kill-below)
1488
1489   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1490     "/" gnus-summary-limit-to-subject
1491     "n" gnus-summary-limit-to-articles
1492     "w" gnus-summary-pop-limit
1493     "s" gnus-summary-limit-to-subject
1494     "a" gnus-summary-limit-to-author
1495     "u" gnus-summary-limit-to-unread
1496     "m" gnus-summary-limit-to-marks
1497     "M" gnus-summary-limit-exclude-marks
1498     "v" gnus-summary-limit-to-score
1499     "*" gnus-summary-limit-include-cached
1500     "D" gnus-summary-limit-include-dormant
1501     "T" gnus-summary-limit-include-thread
1502     "d" gnus-summary-limit-exclude-dormant
1503     "t" gnus-summary-limit-to-age
1504     "x" gnus-summary-limit-to-extra
1505     "E" gnus-summary-limit-include-expunged
1506     "c" gnus-summary-limit-exclude-childless-dormant
1507     "C" gnus-summary-limit-mark-excluded-as-read)
1508
1509   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1510     "n" gnus-summary-next-unread-article
1511     "p" gnus-summary-prev-unread-article
1512     "N" gnus-summary-next-article
1513     "P" gnus-summary-prev-article
1514     "\C-n" gnus-summary-next-same-subject
1515     "\C-p" gnus-summary-prev-same-subject
1516     "\M-n" gnus-summary-next-unread-subject
1517     "\M-p" gnus-summary-prev-unread-subject
1518     "f" gnus-summary-first-unread-article
1519     "b" gnus-summary-best-unread-article
1520     "j" gnus-summary-goto-article
1521     "g" gnus-summary-goto-subject
1522     "l" gnus-summary-goto-last-article
1523     "o" gnus-summary-pop-article)
1524
1525   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1526     "k" gnus-summary-kill-thread
1527     "l" gnus-summary-lower-thread
1528     "i" gnus-summary-raise-thread
1529     "T" gnus-summary-toggle-threads
1530     "t" gnus-summary-rethread-current
1531     "^" gnus-summary-reparent-thread
1532     "s" gnus-summary-show-thread
1533     "S" gnus-summary-show-all-threads
1534     "h" gnus-summary-hide-thread
1535     "H" gnus-summary-hide-all-threads
1536     "n" gnus-summary-next-thread
1537     "p" gnus-summary-prev-thread
1538     "u" gnus-summary-up-thread
1539     "o" gnus-summary-top-thread
1540     "d" gnus-summary-down-thread
1541     "#" gnus-uu-mark-thread
1542     "\M-#" gnus-uu-unmark-thread)
1543
1544   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1545     "g" gnus-summary-prepare
1546     "c" gnus-summary-insert-cached-articles)
1547
1548   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1549     "c" gnus-summary-catchup-and-exit
1550     "C" gnus-summary-catchup-all-and-exit
1551     "E" gnus-summary-exit-no-update
1552     "Q" gnus-summary-exit
1553     "Z" gnus-summary-exit
1554     "n" gnus-summary-catchup-and-goto-next-group
1555     "R" gnus-summary-reselect-current-group
1556     "G" gnus-summary-rescan-group
1557     "N" gnus-summary-next-group
1558     "s" gnus-summary-save-newsrc
1559     "P" gnus-summary-prev-group)
1560
1561   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1562     " " gnus-summary-next-page
1563     "n" gnus-summary-next-page
1564     "\177" gnus-summary-prev-page
1565     [delete] gnus-summary-prev-page
1566     "p" gnus-summary-prev-page
1567     "\r" gnus-summary-scroll-up
1568     "\M-\r" gnus-summary-scroll-down
1569     "<" gnus-summary-beginning-of-article
1570     ">" gnus-summary-end-of-article
1571     "b" gnus-summary-beginning-of-article
1572     "e" gnus-summary-end-of-article
1573     "^" gnus-summary-refer-parent-article
1574     "r" gnus-summary-refer-parent-article
1575     "D" gnus-summary-enter-digest-group
1576     "R" gnus-summary-refer-references
1577     "T" gnus-summary-refer-thread
1578     "g" gnus-summary-show-article
1579     "s" gnus-summary-isearch-article
1580     "P" gnus-summary-print-article
1581     "M" gnus-mailing-list-insinuate
1582     "t" gnus-article-babel)
1583
1584   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1585     "b" gnus-article-add-buttons
1586     "B" gnus-article-add-buttons-to-head
1587     "o" gnus-article-treat-overstrike
1588     "e" gnus-article-emphasize
1589     "w" gnus-article-fill-cited-article
1590     "Q" gnus-article-fill-long-lines
1591     "C" gnus-article-capitalize-sentences
1592     "c" gnus-article-remove-cr
1593     "q" gnus-article-de-quoted-unreadable
1594     "6" gnus-article-de-base64-unreadable
1595     "Z" gnus-article-decode-HZ
1596     "h" gnus-article-wash-html
1597     "s" gnus-summary-force-verify-and-decrypt
1598     "f" gnus-article-display-x-face
1599     "l" gnus-summary-stop-page-breaking
1600     "r" gnus-summary-caesar-message
1601     "t" gnus-summary-toggle-header
1602     "v" gnus-summary-verbose-headers
1603     "H" gnus-article-strip-headers-in-body
1604     "p" gnus-article-verify-x-pgp-sig
1605     "d" gnus-article-treat-dumbquotes)
1606
1607   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1608     "a" gnus-article-hide
1609     "h" gnus-article-hide-headers
1610     "b" gnus-article-hide-boring-headers
1611     "s" gnus-article-hide-signature
1612     "c" gnus-article-hide-citation
1613     "C" gnus-article-hide-citation-in-followups
1614     "l" gnus-article-hide-list-identifiers
1615     "p" gnus-article-hide-pgp
1616     "B" gnus-article-strip-banner
1617     "P" gnus-article-hide-pem
1618     "\C-c" gnus-article-hide-citation-maybe)
1619
1620   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1621     "a" gnus-article-highlight
1622     "h" gnus-article-highlight-headers
1623     "c" gnus-article-highlight-citation
1624     "s" gnus-article-highlight-signature)
1625
1626   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1627     "w" gnus-article-decode-mime-words
1628     "c" gnus-article-decode-charset
1629     "v" gnus-mime-view-all-parts
1630     "b" gnus-article-view-part)
1631
1632   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1633     "z" gnus-article-date-ut
1634     "u" gnus-article-date-ut
1635     "l" gnus-article-date-local
1636     "p" gnus-article-date-english
1637     "e" gnus-article-date-lapsed
1638     "o" gnus-article-date-original
1639     "i" gnus-article-date-iso8601
1640     "s" gnus-article-date-user)
1641
1642   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1643     "t" gnus-article-remove-trailing-blank-lines
1644     "l" gnus-article-strip-leading-blank-lines
1645     "m" gnus-article-strip-multiple-blank-lines
1646     "a" gnus-article-strip-blank-lines
1647     "A" gnus-article-strip-all-blank-lines
1648     "s" gnus-article-strip-leading-space
1649     "e" gnus-article-strip-trailing-space)
1650
1651   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1652     "v" gnus-version
1653     "f" gnus-summary-fetch-faq
1654     "d" gnus-summary-describe-group
1655     "h" gnus-summary-describe-briefly
1656     "i" gnus-info-find-node)
1657
1658   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1659     "e" gnus-summary-expire-articles
1660     "\M-\C-e" gnus-summary-expire-articles-now
1661     "\177" gnus-summary-delete-article
1662     [delete] gnus-summary-delete-article
1663     [backspace] gnus-summary-delete-article
1664     "m" gnus-summary-move-article
1665     "r" gnus-summary-respool-article
1666     "w" gnus-summary-edit-article
1667     "c" gnus-summary-copy-article
1668     "B" gnus-summary-crosspost-article
1669     "q" gnus-summary-respool-query
1670     "t" gnus-summary-respool-trace
1671     "i" gnus-summary-import-article
1672     "p" gnus-summary-article-posted-p)
1673
1674   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1675     "o" gnus-summary-save-article
1676     "m" gnus-summary-save-article-mail
1677     "F" gnus-summary-write-article-file
1678     "r" gnus-summary-save-article-rmail
1679     "f" gnus-summary-save-article-file
1680     "b" gnus-summary-save-article-body-file
1681     "h" gnus-summary-save-article-folder
1682     "v" gnus-summary-save-article-vm
1683     "p" gnus-summary-pipe-output
1684     "s" gnus-soup-add-article)
1685
1686   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1687     "b" gnus-summary-display-buttonized
1688     "m" gnus-summary-repair-multipart
1689     "v" gnus-article-view-part
1690     "o" gnus-article-save-part
1691     "c" gnus-article-copy-part
1692     "C" gnus-article-view-part-as-charset
1693     "e" gnus-article-externalize-part
1694     "E" gnus-article-encrypt-body
1695     "i" gnus-article-inline-part
1696     "|" gnus-article-pipe-part))
1697
1698 (defun gnus-summary-make-menu-bar ()
1699   (gnus-turn-off-edit-menu 'summary)
1700
1701   (unless (boundp 'gnus-summary-misc-menu)
1702
1703     (easy-menu-define
1704      gnus-summary-kill-menu gnus-summary-mode-map ""
1705      (cons
1706       "Score"
1707       (nconc
1708        (list
1709         ["Customize" gnus-score-customize t])
1710        (gnus-make-score-map 'increase)
1711        (gnus-make-score-map 'lower)
1712        '(("Mark"
1713           ["Kill below" gnus-summary-kill-below t]
1714           ["Mark above" gnus-summary-mark-above t]
1715           ["Tick above" gnus-summary-tick-above t]
1716           ["Clear above" gnus-summary-clear-above t])
1717          ["Current score" gnus-summary-current-score t]
1718          ["Set score" gnus-summary-set-score t]
1719          ["Switch current score file..." gnus-score-change-score-file t]
1720          ["Set mark below..." gnus-score-set-mark-below t]
1721          ["Set expunge below..." gnus-score-set-expunge-below t]
1722          ["Edit current score file" gnus-score-edit-current-scores t]
1723          ["Edit score file" gnus-score-edit-file t]
1724          ["Trace score" gnus-score-find-trace t]
1725          ["Find words" gnus-score-find-favourite-words t]
1726          ["Rescore buffer" gnus-summary-rescore t]
1727          ["Increase score..." gnus-summary-increase-score t]
1728          ["Lower score..." gnus-summary-lower-score t]))))
1729
1730     ;; Define both the Article menu in the summary buffer and the equivalent
1731     ;; Commands menu in the article buffer here for consistency.
1732     (let ((innards
1733            `(("Hide"
1734               ["All" gnus-article-hide t]
1735               ["Headers" gnus-article-hide-headers t]
1736               ["Signature" gnus-article-hide-signature t]
1737               ["Citation" gnus-article-hide-citation t]
1738               ["List identifiers" gnus-article-hide-list-identifiers t]
1739               ["PGP" gnus-article-hide-pgp t]
1740               ["Banner" gnus-article-strip-banner t]
1741               ["Boring headers" gnus-article-hide-boring-headers t])
1742              ("Highlight"
1743               ["All" gnus-article-highlight t]
1744               ["Headers" gnus-article-highlight-headers t]
1745               ["Signature" gnus-article-highlight-signature t]
1746               ["Citation" gnus-article-highlight-citation t])
1747              ("MIME"
1748               ["Words" gnus-article-decode-mime-words t]
1749               ["Charset" gnus-article-decode-charset t]
1750               ["QP" gnus-article-de-quoted-unreadable t]
1751               ["Base64" gnus-article-de-base64-unreadable t]
1752               ["View all" gnus-mime-view-all-parts t]
1753               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1754               ["Encrypt body" gnus-article-encrypt-body t])
1755              ("Date"
1756               ["Local" gnus-article-date-local t]
1757               ["ISO8601" gnus-article-date-iso8601 t]
1758               ["UT" gnus-article-date-ut t]
1759               ["Original" gnus-article-date-original t]
1760               ["Lapsed" gnus-article-date-lapsed t]
1761               ["User-defined" gnus-article-date-user t])
1762              ("Washing"
1763               ("Remove Blanks"
1764                ["Leading" gnus-article-strip-leading-blank-lines t]
1765                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1766                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1767                ["All of the above" gnus-article-strip-blank-lines t]
1768                ["All" gnus-article-strip-all-blank-lines t]
1769                ["Leading space" gnus-article-strip-leading-space t]
1770                ["Trailing space" gnus-article-strip-trailing-space t])
1771               ["Overstrike" gnus-article-treat-overstrike t]
1772               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1773               ["Emphasis" gnus-article-emphasize t]
1774               ["Word wrap" gnus-article-fill-cited-article t]
1775               ["Fill long lines" gnus-article-fill-long-lines t]
1776               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1777               ["CR" gnus-article-remove-cr t]
1778               ["Show X-Face" gnus-article-display-x-face t]
1779               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1780               ["Base64" gnus-article-de-base64-unreadable t]
1781               ["Rot 13" gnus-summary-caesar-message
1782                ,@(if (featurep 'xemacs) nil
1783                    '(:help "\"Caesar rotate\" article by 13"))]
1784               ["Unix pipe" gnus-summary-pipe-message t]
1785               ["Add buttons" gnus-article-add-buttons t]
1786               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1787               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1788               ["Verbose header" gnus-summary-verbose-headers t]
1789               ["Toggle header" gnus-summary-toggle-header t]
1790               ["Html" gnus-article-wash-html t]
1791               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1792               ["HZ" gnus-article-decode-HZ t])
1793              ("Output"
1794               ["Save in default format" gnus-summary-save-article
1795                ,@(if (featurep 'xemacs) nil
1796                    '(:help "Save article using default method"))]
1797               ["Save in file" gnus-summary-save-article-file
1798                ,@(if (featurep 'xemacs) nil
1799                    '(:help "Save article in file"))]
1800               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1801               ["Save in MH folder" gnus-summary-save-article-folder t]
1802               ["Save in VM folder" gnus-summary-save-article-vm t]
1803               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1804               ["Save body in file" gnus-summary-save-article-body-file t]
1805               ["Pipe through a filter" gnus-summary-pipe-output t]
1806               ["Add to SOUP packet" gnus-soup-add-article t]
1807               ["Print" gnus-summary-print-article t])
1808              ("Backend"
1809               ["Respool article..." gnus-summary-respool-article t]
1810               ["Move article..." gnus-summary-move-article
1811                (gnus-check-backend-function
1812                 'request-move-article gnus-newsgroup-name)]
1813               ["Copy article..." gnus-summary-copy-article t]
1814               ["Crosspost article..." gnus-summary-crosspost-article
1815                (gnus-check-backend-function
1816                 'request-replace-article gnus-newsgroup-name)]
1817               ["Import file..." gnus-summary-import-article t]
1818               ["Check if posted" gnus-summary-article-posted-p t]
1819               ["Edit article" gnus-summary-edit-article
1820                (not (gnus-group-read-only-p))]
1821               ["Delete article" gnus-summary-delete-article
1822                (gnus-check-backend-function
1823                 'request-expire-articles gnus-newsgroup-name)]
1824               ["Query respool" gnus-summary-respool-query t]
1825               ["Trace respool" gnus-summary-respool-trace t]
1826               ["Delete expirable articles" gnus-summary-expire-articles-now
1827                (gnus-check-backend-function
1828                 'request-expire-articles gnus-newsgroup-name)])
1829              ("Extract"
1830               ["Uudecode" gnus-uu-decode-uu
1831                ,@(if (featurep 'xemacs) nil
1832                    '(:help "Decode uuencoded article(s)"))]
1833               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1834               ["Unshar" gnus-uu-decode-unshar t]
1835               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1836               ["Save" gnus-uu-decode-save t]
1837               ["Binhex" gnus-uu-decode-binhex t]
1838               ["Postscript" gnus-uu-decode-postscript t])
1839              ("Cache"
1840               ["Enter article" gnus-cache-enter-article t]
1841               ["Remove article" gnus-cache-remove-article t])
1842              ["Translate" gnus-article-babel t]
1843              ["Select article buffer" gnus-summary-select-article-buffer t]
1844              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1845              ["Isearch article..." gnus-summary-isearch-article t]
1846              ["Beginning of the article" gnus-summary-beginning-of-article t]
1847              ["End of the article" gnus-summary-end-of-article t]
1848              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1849              ["Fetch referenced articles" gnus-summary-refer-references t]
1850              ["Fetch current thread" gnus-summary-refer-thread t]
1851              ["Fetch article with id..." gnus-summary-refer-article t]
1852              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1853              ["Redisplay" gnus-summary-show-article t])))
1854       (easy-menu-define
1855        gnus-summary-article-menu gnus-summary-mode-map ""
1856        (cons "Article" innards))
1857
1858       (easy-menu-define
1859        gnus-article-commands-menu gnus-article-mode-map ""
1860        (cons "Commands" innards)))
1861
1862     (easy-menu-define
1863      gnus-summary-thread-menu gnus-summary-mode-map ""
1864      '("Threads"
1865        ["Toggle threading" gnus-summary-toggle-threads t]
1866        ["Hide threads" gnus-summary-hide-all-threads t]
1867        ["Show threads" gnus-summary-show-all-threads t]
1868        ["Hide thread" gnus-summary-hide-thread t]
1869        ["Show thread" gnus-summary-show-thread t]
1870        ["Go to next thread" gnus-summary-next-thread t]
1871        ["Go to previous thread" gnus-summary-prev-thread t]
1872        ["Go down thread" gnus-summary-down-thread t]
1873        ["Go up thread" gnus-summary-up-thread t]
1874        ["Top of thread" gnus-summary-top-thread t]
1875        ["Mark thread as read" gnus-summary-kill-thread t]
1876        ["Lower thread score" gnus-summary-lower-thread t]
1877        ["Raise thread score" gnus-summary-raise-thread t]
1878        ["Rethread current" gnus-summary-rethread-current t]))
1879
1880     (easy-menu-define
1881      gnus-summary-post-menu gnus-summary-mode-map ""
1882      `("Post"
1883        ["Post an article" gnus-summary-post-news
1884         ,@(if (featurep 'xemacs) nil
1885             '(:help "Post an article"))]
1886        ["Followup" gnus-summary-followup
1887         ,@(if (featurep 'xemacs) nil
1888             '(:help "Post followup to this article"))]
1889        ["Followup and yank" gnus-summary-followup-with-original
1890         ,@(if (featurep 'xemacs) nil
1891             '(:help "Post followup to this article, quoting its contents"))]
1892        ["Supersede article" gnus-summary-supersede-article t]
1893        ["Cancel article" gnus-summary-cancel-article
1894         ,@(if (featurep 'xemacs) nil
1895             '(:help "Cancel an article you posted"))]
1896        ["Reply" gnus-summary-reply t]
1897        ["Reply and yank" gnus-summary-reply-with-original t]
1898        ["Wide reply" gnus-summary-wide-reply t]
1899        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1900         ,@(if (featurep 'xemacs) nil
1901             '(:help "Mail a reply, quoting this article"))]
1902        ["Mail forward" gnus-summary-mail-forward t]
1903        ["Post forward" gnus-summary-post-forward t]
1904        ["Digest and mail" gnus-uu-digest-mail-forward t]
1905        ["Digest and post" gnus-uu-digest-post-forward t]
1906        ["Resend message" gnus-summary-resend-message t]
1907        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1908        ["Send a mail" gnus-summary-mail-other-window t]
1909        ["Uuencode and post" gnus-uu-post-news
1910         ,@(if (featurep 'xemacs) nil
1911             '(:help "Post a uuencoded article"))]
1912        ["Followup via news" gnus-summary-followup-to-mail t]
1913        ["Followup via news and yank"
1914         gnus-summary-followup-to-mail-with-original t]
1915        ;;("Draft"
1916        ;;["Send" gnus-summary-send-draft t]
1917        ;;["Send bounced" gnus-resend-bounced-mail t])
1918        ))
1919
1920     (easy-menu-define
1921      gnus-summary-misc-menu gnus-summary-mode-map ""
1922      `("Misc"
1923        ("Mark Read"
1924         ["Mark as read" gnus-summary-mark-as-read-forward t]
1925         ["Mark same subject and select"
1926          gnus-summary-kill-same-subject-and-select t]
1927         ["Mark same subject" gnus-summary-kill-same-subject t]
1928         ["Catchup" gnus-summary-catchup
1929          ,@(if (featurep 'xemacs) nil
1930              '(:help "Mark unread articles in this group as read"))]
1931         ["Catchup all" gnus-summary-catchup-all t]
1932         ["Catchup to here" gnus-summary-catchup-to-here t]
1933         ["Catchup region" gnus-summary-mark-region-as-read t]
1934         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1935        ("Mark Various"
1936         ["Tick" gnus-summary-tick-article-forward t]
1937         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1938         ["Remove marks" gnus-summary-clear-mark-forward t]
1939         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1940         ["Set bookmark" gnus-summary-set-bookmark t]
1941         ["Remove bookmark" gnus-summary-remove-bookmark t])
1942        ("Mark Limit"
1943         ["Marks..." gnus-summary-limit-to-marks t]
1944         ["Subject..." gnus-summary-limit-to-subject t]
1945         ["Author..." gnus-summary-limit-to-author t]
1946         ["Age..." gnus-summary-limit-to-age t]
1947         ["Extra..." gnus-summary-limit-to-extra t]
1948         ["Score" gnus-summary-limit-to-score t]
1949         ["Unread" gnus-summary-limit-to-unread t]
1950         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1951         ["Articles" gnus-summary-limit-to-articles t]
1952         ["Pop limit" gnus-summary-pop-limit t]
1953         ["Show dormant" gnus-summary-limit-include-dormant t]
1954         ["Hide childless dormant"
1955          gnus-summary-limit-exclude-childless-dormant t]
1956         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1957         ["Hide marked" gnus-summary-limit-exclude-marks t]
1958         ["Show expunged" gnus-summary-show-all-expunged t])
1959        ("Process Mark"
1960         ["Set mark" gnus-summary-mark-as-processable t]
1961         ["Remove mark" gnus-summary-unmark-as-processable t]
1962         ["Remove all marks" gnus-summary-unmark-all-processable t]
1963         ["Mark above" gnus-uu-mark-over t]
1964         ["Mark series" gnus-uu-mark-series t]
1965         ["Mark region" gnus-uu-mark-region t]
1966         ["Unmark region" gnus-uu-unmark-region t]
1967         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1968         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1969         ["Mark all" gnus-uu-mark-all t]
1970         ["Mark buffer" gnus-uu-mark-buffer t]
1971         ["Mark sparse" gnus-uu-mark-sparse t]
1972         ["Mark thread" gnus-uu-mark-thread t]
1973         ["Unmark thread" gnus-uu-unmark-thread t]
1974         ("Process Mark Sets"
1975          ["Kill" gnus-summary-kill-process-mark t]
1976          ["Yank" gnus-summary-yank-process-mark
1977           gnus-newsgroup-process-stack]
1978          ["Save" gnus-summary-save-process-mark t]))
1979        ("Scroll article"
1980         ["Page forward" gnus-summary-next-page
1981          ,@(if (featurep 'xemacs) nil
1982              '(:help "Show next page of article"))]
1983         ["Page backward" gnus-summary-prev-page
1984          ,@(if (featurep 'xemacs) nil
1985              '(:help "Show previous page of article"))]
1986         ["Line forward" gnus-summary-scroll-up t])
1987        ("Move"
1988         ["Next unread article" gnus-summary-next-unread-article t]
1989         ["Previous unread article" gnus-summary-prev-unread-article t]
1990         ["Next article" gnus-summary-next-article t]
1991         ["Previous article" gnus-summary-prev-article t]
1992         ["Next unread subject" gnus-summary-next-unread-subject t]
1993         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1994         ["Next article same subject" gnus-summary-next-same-subject t]
1995         ["Previous article same subject" gnus-summary-prev-same-subject t]
1996         ["First unread article" gnus-summary-first-unread-article t]
1997         ["Best unread article" gnus-summary-best-unread-article t]
1998         ["Go to subject number..." gnus-summary-goto-subject t]
1999         ["Go to article number..." gnus-summary-goto-article t]
2000         ["Go to the last article" gnus-summary-goto-last-article t]
2001         ["Pop article off history" gnus-summary-pop-article t])
2002        ("Sort"
2003         ["Sort by number" gnus-summary-sort-by-number t]
2004         ["Sort by author" gnus-summary-sort-by-author t]
2005         ["Sort by subject" gnus-summary-sort-by-subject t]
2006         ["Sort by date" gnus-summary-sort-by-date t]
2007         ["Sort by score" gnus-summary-sort-by-score t]
2008         ["Sort by lines" gnus-summary-sort-by-lines t]
2009         ["Sort by characters" gnus-summary-sort-by-chars t])
2010        ("Help"
2011         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2012         ["Describe group" gnus-summary-describe-group t]
2013         ["Read manual" gnus-info-find-node t])
2014        ("Modes"
2015         ["Pick and read" gnus-pick-mode t]
2016         ["Binary" gnus-binary-mode t])
2017        ("Regeneration"
2018         ["Regenerate" gnus-summary-prepare t]
2019         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2020         ["Toggle threading" gnus-summary-toggle-threads t])
2021        ["Filter articles..." gnus-summary-execute-command t]
2022        ["Run command on subjects..." gnus-summary-universal-argument t]
2023        ["Search articles forward..." gnus-summary-search-article-forward t]
2024        ["Search articles backward..." gnus-summary-search-article-backward t]
2025        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2026        ["Expand window" gnus-summary-expand-window t]
2027        ["Expire expirable articles" gnus-summary-expire-articles
2028         (gnus-check-backend-function
2029          'request-expire-articles gnus-newsgroup-name)]
2030        ["Edit local kill file" gnus-summary-edit-local-kill t]
2031        ["Edit main kill file" gnus-summary-edit-global-kill t]
2032        ["Edit group parameters" gnus-summary-edit-parameters t]
2033        ["Customize group parameters" gnus-summary-customize-parameters t]
2034        ["Send a bug report" gnus-bug t]
2035        ("Exit"
2036         ["Catchup and exit" gnus-summary-catchup-and-exit
2037          ,@(if (featurep 'xemacs) nil
2038              '(:help "Mark unread articles in this group as read, then exit"))]
2039         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2040         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2041         ["Exit group" gnus-summary-exit
2042          ,@(if (featurep 'xemacs) nil
2043              '(:help "Exit current group, return to group selection mode"))]
2044         ["Exit group without updating" gnus-summary-exit-no-update t]
2045         ["Exit and goto next group" gnus-summary-next-group t]
2046         ["Exit and goto prev group" gnus-summary-prev-group t]
2047         ["Reselect group" gnus-summary-reselect-current-group t]
2048         ["Rescan group" gnus-summary-rescan-group t]
2049         ["Update dribble" gnus-summary-save-newsrc t])))
2050
2051     (gnus-run-hooks 'gnus-summary-menu-hook)))
2052
2053 (defvar gnus-summary-tool-bar-map nil)
2054
2055 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2056 (defun gnus-summary-make-tool-bar ()
2057   (if (and (fboundp 'tool-bar-add-item-from-menu)
2058            (default-value 'tool-bar-mode)
2059            (not gnus-summary-tool-bar-map))
2060       (setq gnus-summary-tool-bar-map
2061             (let ((tool-bar-map (make-sparse-keymap))
2062                   (load-path (mm-image-load-path)))
2063               (tool-bar-add-item-from-menu
2064                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2065               (tool-bar-add-item-from-menu
2066                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2067               (tool-bar-add-item-from-menu
2068                'gnus-summary-post-news "post" gnus-summary-mode-map)
2069               (tool-bar-add-item-from-menu
2070                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2071               (tool-bar-add-item-from-menu
2072                'gnus-summary-followup "followup" gnus-summary-mode-map)
2073               (tool-bar-add-item-from-menu
2074                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2075               (tool-bar-add-item-from-menu
2076                'gnus-summary-reply "reply" gnus-summary-mode-map)
2077               (tool-bar-add-item-from-menu
2078                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2079               (tool-bar-add-item-from-menu
2080                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2081               (tool-bar-add-item-from-menu
2082                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2083               (tool-bar-add-item-from-menu
2084                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2085               (tool-bar-add-item-from-menu
2086                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2087               (tool-bar-add-item-from-menu
2088                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2089               (tool-bar-add-item-from-menu
2090                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2091               (tool-bar-add-item-from-menu
2092                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2093               tool-bar-map)))
2094   (if gnus-summary-tool-bar-map
2095       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2096
2097 (defun gnus-score-set-default (var value)
2098   "A version of set that updates the GNU Emacs menu-bar."
2099   (set var value)
2100   ;; It is the message that forces the active status to be updated.
2101   (message ""))
2102
2103 (defun gnus-make-score-map (type)
2104   "Make a summary score map of type TYPE."
2105   (if t
2106       nil
2107     (let ((headers '(("author" "from" string)
2108                      ("subject" "subject" string)
2109                      ("article body" "body" string)
2110                      ("article head" "head" string)
2111                      ("xref" "xref" string)
2112                      ("extra header" "extra" string)
2113                      ("lines" "lines" number)
2114                      ("followups to author" "followup" string)))
2115           (types '((number ("less than" <)
2116                            ("greater than" >)
2117                            ("equal" =))
2118                    (string ("substring" s)
2119                            ("exact string" e)
2120                            ("fuzzy string" f)
2121                            ("regexp" r))))
2122           (perms '(("temporary" (current-time-string))
2123                    ("permanent" nil)
2124                    ("immediate" now)))
2125           header)
2126       (list
2127        (apply
2128         'nconc
2129         (list
2130          (if (eq type 'lower)
2131              "Lower score"
2132            "Increase score"))
2133         (let (outh)
2134           (while headers
2135             (setq header (car headers))
2136             (setq outh
2137                   (cons
2138                    (apply
2139                     'nconc
2140                     (list (car header))
2141                     (let ((ts (cdr (assoc (nth 2 header) types)))
2142                           outt)
2143                       (while ts
2144                         (setq outt
2145                               (cons
2146                                (apply
2147                                 'nconc
2148                                 (list (caar ts))
2149                                 (let ((ps perms)
2150                                       outp)
2151                                   (while ps
2152                                     (setq outp
2153                                           (cons
2154                                            (vector
2155                                             (caar ps)
2156                                             (list
2157                                              'gnus-summary-score-entry
2158                                              (nth 1 header)
2159                                              (if (or (string= (nth 1 header)
2160                                                               "head")
2161                                                      (string= (nth 1 header)
2162                                                               "body"))
2163                                                  ""
2164                                                (list 'gnus-summary-header
2165                                                      (nth 1 header)))
2166                                              (list 'quote (nth 1 (car ts)))
2167                                              (list 'gnus-score-delta-default
2168                                                    nil)
2169                                              (nth 1 (car ps))
2170                                              t)
2171                                             t)
2172                                            outp))
2173                                     (setq ps (cdr ps)))
2174                                   (list (nreverse outp))))
2175                                outt))
2176                         (setq ts (cdr ts)))
2177                       (list (nreverse outt))))
2178                    outh))
2179             (setq headers (cdr headers)))
2180           (list (nreverse outh))))))))
2181
2182 \f
2183
2184 (defun gnus-summary-mode (&optional group)
2185   "Major mode for reading articles.
2186
2187 All normal editing commands are switched off.
2188 \\<gnus-summary-mode-map>
2189 Each line in this buffer represents one article.  To read an
2190 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2191 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2192 respectively.
2193
2194 You can also post articles and send mail from this buffer.  To
2195 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2196 of an article, type `\\[gnus-summary-reply]'.
2197
2198 There are approx. one gazillion commands you can execute in this
2199 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2200
2201 The following commands are available:
2202
2203 \\{gnus-summary-mode-map}"
2204   (interactive)
2205   (kill-all-local-variables)
2206   (when (gnus-visual-p 'summary-menu 'menu)
2207     (gnus-summary-make-menu-bar)
2208     (gnus-summary-make-tool-bar))
2209   (gnus-summary-make-local-variables)
2210   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2211     (gnus-summary-make-local-variables))
2212   (gnus-make-thread-indent-array)
2213   (gnus-simplify-mode-line)
2214   (setq major-mode 'gnus-summary-mode)
2215   (setq mode-name "Summary")
2216   (make-local-variable 'minor-mode-alist)
2217   (use-local-map gnus-summary-mode-map)
2218   (buffer-disable-undo)
2219   (setq buffer-read-only t)             ;Disable modification
2220   (setq truncate-lines t)
2221   (setq selective-display t)
2222   (setq selective-display-ellipses t)   ;Display `...'
2223   (gnus-summary-set-display-table)
2224   (gnus-set-default-directory)
2225   (setq gnus-newsgroup-name group)
2226   (make-local-variable 'gnus-summary-line-format)
2227   (make-local-variable 'gnus-summary-line-format-spec)
2228   (make-local-variable 'gnus-summary-dummy-line-format)
2229   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2230   (make-local-variable 'gnus-summary-mark-positions)
2231   (make-local-hook 'pre-command-hook)
2232   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2233   (gnus-run-hooks 'gnus-summary-mode-hook)
2234   (turn-on-gnus-mailing-list-mode)
2235   (mm-enable-multibyte-mule4)
2236   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2237   (gnus-update-summary-mark-positions))
2238
2239 (defun gnus-summary-make-local-variables ()
2240   "Make all the local summary buffer variables."
2241   (let (global)
2242     (dolist (local gnus-summary-local-variables)
2243       (if (consp local)
2244           (progn
2245             (if (eq (cdr local) 'global)
2246                 ;; Copy the global value of the variable.
2247                 (setq global (symbol-value (car local)))
2248               ;; Use the value from the list.
2249               (setq global (eval (cdr local))))
2250             (set (make-local-variable (car local)) global))
2251         ;; Simple nil-valued local variable.
2252         (set (make-local-variable local) nil)))))
2253
2254 (defun gnus-summary-clear-local-variables ()
2255   (let ((locals gnus-summary-local-variables))
2256     (while locals
2257       (if (consp (car locals))
2258           (and (vectorp (caar locals))
2259                (set (caar locals) nil))
2260         (and (vectorp (car locals))
2261              (set (car locals) nil)))
2262       (setq locals (cdr locals)))))
2263
2264 ;; Summary data functions.
2265
2266 (defmacro gnus-data-number (data)
2267   `(car ,data))
2268
2269 (defmacro gnus-data-set-number (data number)
2270   `(setcar ,data ,number))
2271
2272 (defmacro gnus-data-mark (data)
2273   `(nth 1 ,data))
2274
2275 (defmacro gnus-data-set-mark (data mark)
2276   `(setcar (nthcdr 1 ,data) ,mark))
2277
2278 (defmacro gnus-data-pos (data)
2279   `(nth 2 ,data))
2280
2281 (defmacro gnus-data-set-pos (data pos)
2282   `(setcar (nthcdr 2 ,data) ,pos))
2283
2284 (defmacro gnus-data-header (data)
2285   `(nth 3 ,data))
2286
2287 (defmacro gnus-data-set-header (data header)
2288   `(setf (nth 3 ,data) ,header))
2289
2290 (defmacro gnus-data-level (data)
2291   `(nth 4 ,data))
2292
2293 (defmacro gnus-data-unread-p (data)
2294   `(= (nth 1 ,data) gnus-unread-mark))
2295
2296 (defmacro gnus-data-read-p (data)
2297   `(/= (nth 1 ,data) gnus-unread-mark))
2298
2299 (defmacro gnus-data-pseudo-p (data)
2300   `(consp (nth 3 ,data)))
2301
2302 (defmacro gnus-data-find (number)
2303   `(assq ,number gnus-newsgroup-data))
2304
2305 (defmacro gnus-data-find-list (number &optional data)
2306   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2307      (memq (assq ,number bdata)
2308            bdata)))
2309
2310 (defmacro gnus-data-make (number mark pos header level)
2311   `(list ,number ,mark ,pos ,header ,level))
2312
2313 (defun gnus-data-enter (after-article number mark pos header level offset)
2314   (let ((data (gnus-data-find-list after-article)))
2315     (unless data
2316       (error "No such article: %d" after-article))
2317     (setcdr data (cons (gnus-data-make number mark pos header level)
2318                        (cdr data)))
2319     (setq gnus-newsgroup-data-reverse nil)
2320     (gnus-data-update-list (cddr data) offset)))
2321
2322 (defun gnus-data-enter-list (after-article list &optional offset)
2323   (when list
2324     (let ((data (and after-article (gnus-data-find-list after-article)))
2325           (ilist list))
2326       (if (not (or data
2327                    after-article))
2328           (let ((odata gnus-newsgroup-data))
2329             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2330             (when offset
2331               (gnus-data-update-list odata offset)))
2332         ;; Find the last element in the list to be spliced into the main
2333         ;; list.
2334         (while (cdr list)
2335           (setq list (cdr list)))
2336         (if (not data)
2337             (progn
2338               (setcdr list gnus-newsgroup-data)
2339               (setq gnus-newsgroup-data ilist)
2340               (when offset
2341                 (gnus-data-update-list (cdr list) offset)))
2342           (setcdr list (cdr data))
2343           (setcdr data ilist)
2344           (when offset
2345             (gnus-data-update-list (cdr list) offset))))
2346       (setq gnus-newsgroup-data-reverse nil))))
2347
2348 (defun gnus-data-remove (article &optional offset)
2349   (let ((data gnus-newsgroup-data))
2350     (if (= (gnus-data-number (car data)) article)
2351         (progn
2352           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2353                 gnus-newsgroup-data-reverse nil)
2354           (when offset
2355             (gnus-data-update-list gnus-newsgroup-data offset)))
2356       (while (cdr data)
2357         (when (= (gnus-data-number (cadr data)) article)
2358           (setcdr data (cddr data))
2359           (when offset
2360             (gnus-data-update-list (cdr data) offset))
2361           (setq data nil
2362                 gnus-newsgroup-data-reverse nil))
2363         (setq data (cdr data))))))
2364
2365 (defmacro gnus-data-list (backward)
2366   `(if ,backward
2367        (or gnus-newsgroup-data-reverse
2368            (setq gnus-newsgroup-data-reverse
2369                  (reverse gnus-newsgroup-data)))
2370      gnus-newsgroup-data))
2371
2372 (defun gnus-data-update-list (data offset)
2373   "Add OFFSET to the POS of all data entries in DATA."
2374   (setq gnus-newsgroup-data-reverse nil)
2375   (while data
2376     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2377     (setq data (cdr data))))
2378
2379 (defun gnus-summary-article-pseudo-p (article)
2380   "Say whether this article is a pseudo article or not."
2381   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2382
2383 (defmacro gnus-summary-article-sparse-p (article)
2384   "Say whether this article is a sparse article or not."
2385   `(memq ,article gnus-newsgroup-sparse))
2386
2387 (defmacro gnus-summary-article-ancient-p (article)
2388   "Say whether this article is a sparse article or not."
2389   `(memq ,article gnus-newsgroup-ancient))
2390
2391 (defun gnus-article-parent-p (number)
2392   "Say whether this article is a parent or not."
2393   (let ((data (gnus-data-find-list number)))
2394     (and (cdr data)                     ; There has to be an article after...
2395          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2396             (gnus-data-level (nth 1 data))))))
2397
2398 (defun gnus-article-children (number)
2399   "Return a list of all children to NUMBER."
2400   (let* ((data (gnus-data-find-list number))
2401          (level (gnus-data-level (car data)))
2402          children)
2403     (setq data (cdr data))
2404     (while (and data
2405                 (= (gnus-data-level (car data)) (1+ level)))
2406       (push (gnus-data-number (car data)) children)
2407       (setq data (cdr data)))
2408     children))
2409
2410 (defmacro gnus-summary-skip-intangible ()
2411   "If the current article is intangible, then jump to a different article."
2412   '(let ((to (get-text-property (point) 'gnus-intangible)))
2413      (and to (gnus-summary-goto-subject to))))
2414
2415 (defmacro gnus-summary-article-intangible-p ()
2416   "Say whether this article is intangible or not."
2417   '(get-text-property (point) 'gnus-intangible))
2418
2419 (defun gnus-article-read-p (article)
2420   "Say whether ARTICLE is read or not."
2421   (not (or (memq article gnus-newsgroup-marked)
2422            (memq article gnus-newsgroup-unreads)
2423            (memq article gnus-newsgroup-unselected)
2424            (memq article gnus-newsgroup-dormant))))
2425
2426 ;; Some summary mode macros.
2427
2428 (defmacro gnus-summary-article-number ()
2429   "The article number of the article on the current line.
2430 If there isn's an article number here, then we return the current
2431 article number."
2432   '(progn
2433      (gnus-summary-skip-intangible)
2434      (or (get-text-property (point) 'gnus-number)
2435          (gnus-summary-last-subject))))
2436
2437 (defmacro gnus-summary-article-header (&optional number)
2438   "Return the header of article NUMBER."
2439   `(gnus-data-header (gnus-data-find
2440                       ,(or number '(gnus-summary-article-number)))))
2441
2442 (defmacro gnus-summary-thread-level (&optional number)
2443   "Return the level of thread that starts with article NUMBER."
2444   `(if (and (eq gnus-summary-make-false-root 'dummy)
2445             (get-text-property (point) 'gnus-intangible))
2446        0
2447      (gnus-data-level (gnus-data-find
2448                        ,(or number '(gnus-summary-article-number))))))
2449
2450 (defmacro gnus-summary-article-mark (&optional number)
2451   "Return the mark of article NUMBER."
2452   `(gnus-data-mark (gnus-data-find
2453                     ,(or number '(gnus-summary-article-number)))))
2454
2455 (defmacro gnus-summary-article-pos (&optional number)
2456   "Return the position of the line of article NUMBER."
2457   `(gnus-data-pos (gnus-data-find
2458                    ,(or number '(gnus-summary-article-number)))))
2459
2460 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2461 (defmacro gnus-summary-article-subject (&optional number)
2462   "Return current subject string or nil if nothing."
2463   `(let ((headers
2464           ,(if number
2465                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2466              '(gnus-data-header (assq (gnus-summary-article-number)
2467                                       gnus-newsgroup-data)))))
2468      (and headers
2469           (vectorp headers)
2470           (mail-header-subject headers))))
2471
2472 (defmacro gnus-summary-article-score (&optional number)
2473   "Return current article score."
2474   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2475                   gnus-newsgroup-scored))
2476        gnus-summary-default-score 0))
2477
2478 (defun gnus-summary-article-children (&optional number)
2479   "Return a list of article numbers that are children of article NUMBER."
2480   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2481          (level (gnus-data-level (car data)))
2482          l children)
2483     (while (and (setq data (cdr data))
2484                 (> (setq l (gnus-data-level (car data))) level))
2485       (and (= (1+ level) l)
2486            (push (gnus-data-number (car data))
2487                  children)))
2488     (nreverse children)))
2489
2490 (defun gnus-summary-article-parent (&optional number)
2491   "Return the article number of the parent of article NUMBER."
2492   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2493                                     (gnus-data-list t)))
2494          (level (gnus-data-level (car data))))
2495     (if (zerop level)
2496         ()                              ; This is a root.
2497       ;; We search until we find an article with a level less than
2498       ;; this one.  That function has to be the parent.
2499       (while (and (setq data (cdr data))
2500                   (not (< (gnus-data-level (car data)) level))))
2501       (and data (gnus-data-number (car data))))))
2502
2503 (defun gnus-unread-mark-p (mark)
2504   "Say whether MARK is the unread mark."
2505   (= mark gnus-unread-mark))
2506
2507 (defun gnus-read-mark-p (mark)
2508   "Say whether MARK is one of the marks that mark as read.
2509 This is all marks except unread, ticked, dormant, and expirable."
2510   (not (or (= mark gnus-unread-mark)
2511            (= mark gnus-ticked-mark)
2512            (= mark gnus-dormant-mark)
2513            (= mark gnus-expirable-mark))))
2514
2515 (defmacro gnus-article-mark (number)
2516   "Return the MARK of article NUMBER.
2517 This macro should only be used when computing the mark the \"first\"
2518 time; i.e., when generating the summary lines.  After that,
2519 `gnus-summary-article-mark' should be used to examine the
2520 marks of articles."
2521   `(cond
2522     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2523     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2524     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2525     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2526     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2527     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2528     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2529     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2530            gnus-ancient-mark))))
2531
2532 ;; Saving hidden threads.
2533
2534 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2535 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2536
2537 (defmacro gnus-save-hidden-threads (&rest forms)
2538   "Save hidden threads, eval FORMS, and restore the hidden threads."
2539   (let ((config (make-symbol "config")))
2540     `(let ((,config (gnus-hidden-threads-configuration)))
2541        (unwind-protect
2542            (save-excursion
2543              ,@forms)
2544          (gnus-restore-hidden-threads-configuration ,config)))))
2545
2546 (defun gnus-data-compute-positions ()
2547   "Compute the positions of all articles."
2548   (setq gnus-newsgroup-data-reverse nil)
2549   (let ((data gnus-newsgroup-data))
2550     (save-excursion
2551       (gnus-save-hidden-threads
2552         (gnus-summary-show-all-threads)
2553         (goto-char (point-min))
2554         (while data
2555           (while (get-text-property (point) 'gnus-intangible)
2556             (forward-line 1))
2557           (gnus-data-set-pos (car data) (+ (point) 3))
2558           (setq data (cdr data))
2559           (forward-line 1))))))
2560
2561 (defun gnus-hidden-threads-configuration ()
2562   "Return the current hidden threads configuration."
2563   (save-excursion
2564     (let (config)
2565       (goto-char (point-min))
2566       (while (search-forward "\r" nil t)
2567         (push (1- (point)) config))
2568       config)))
2569
2570 (defun gnus-restore-hidden-threads-configuration (config)
2571   "Restore hidden threads configuration from CONFIG."
2572   (save-excursion
2573     (let (point buffer-read-only)
2574       (while (setq point (pop config))
2575         (when (and (< point (point-max))
2576                    (goto-char point)
2577                    (eq (char-after) ?\n))
2578           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2579
2580 ;; Various summary mode internalish functions.
2581
2582 (defun gnus-mouse-pick-article (e)
2583   (interactive "e")
2584   (mouse-set-point e)
2585   (gnus-summary-next-page nil t))
2586
2587 (defun gnus-summary-set-display-table ()
2588   "Change the display table.
2589 Odd characters have a tendency to mess
2590 up nicely formatted displays - we make all possible glyphs
2591 display only a single character."
2592
2593   ;; We start from the standard display table, if any.
2594   (let ((table (or (copy-sequence standard-display-table)
2595                    (make-display-table)))
2596         (i 32))
2597     ;; Nix out all the control chars...
2598     (while (>= (setq i (1- i)) 0)
2599       (aset table i [??]))
2600     ;; ... but not newline and cr, of course.  (cr is necessary for the
2601     ;; selective display).
2602     (aset table ?\n nil)
2603     (aset table ?\r nil)
2604     ;; We keep TAB as well.
2605     (aset table ?\t nil)
2606     ;; We nix out any glyphs over 126 that are not set already.
2607     (let ((i 256))
2608       (while (>= (setq i (1- i)) 127)
2609         ;; Only modify if the entry is nil.
2610         (unless (aref table i)
2611           (aset table i [??]))))
2612     (setq buffer-display-table table)))
2613
2614 (defun gnus-summary-buffer-name (group)
2615   "Return the summary buffer name of GROUP."
2616   (concat "*Summary " group "*"))
2617
2618 (defun gnus-summary-setup-buffer (group)
2619   "Initialize summary buffer."
2620   (let ((buffer (gnus-summary-buffer-name group)))
2621     (if (get-buffer buffer)
2622         (progn
2623           (set-buffer buffer)
2624           (setq gnus-summary-buffer (current-buffer))
2625           (not gnus-newsgroup-prepared))
2626       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2627       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2628       (gnus-summary-mode group)
2629       (when gnus-carpal
2630         (gnus-carpal-setup-buffer 'summary))
2631       (unless gnus-single-article-buffer
2632         (make-local-variable 'gnus-article-buffer)
2633         (make-local-variable 'gnus-article-current)
2634         (make-local-variable 'gnus-original-article-buffer))
2635       (setq gnus-newsgroup-name group)
2636       t)))
2637
2638 (defun gnus-set-global-variables ()
2639   "Set the global equivalents of the buffer-local variables.
2640 They are set to the latest values they had.  These reflect the summary
2641 buffer that was in action when the last article was fetched."
2642   (when (eq major-mode 'gnus-summary-mode)
2643     (setq gnus-summary-buffer (current-buffer))
2644     (let ((name gnus-newsgroup-name)
2645           (marked gnus-newsgroup-marked)
2646           (unread gnus-newsgroup-unreads)
2647           (headers gnus-current-headers)
2648           (data gnus-newsgroup-data)
2649           (summary gnus-summary-buffer)
2650           (article-buffer gnus-article-buffer)
2651           (original gnus-original-article-buffer)
2652           (gac gnus-article-current)
2653           (reffed gnus-reffed-article-number)
2654           (score-file gnus-current-score-file)
2655           (default-charset gnus-newsgroup-charset)
2656           vlist)
2657       (let ((locals gnus-newsgroup-variables))
2658         (while locals
2659           (if (consp (car locals))
2660               (push (eval (caar locals)) vlist)
2661             (push (eval (car locals)) vlist))
2662           (setq locals (cdr locals)))
2663         (setq vlist (nreverse vlist)))
2664       (save-excursion
2665         (set-buffer gnus-group-buffer)
2666         (setq gnus-newsgroup-name name
2667               gnus-newsgroup-marked marked
2668               gnus-newsgroup-unreads unread
2669               gnus-current-headers headers
2670               gnus-newsgroup-data data
2671               gnus-article-current gac
2672               gnus-summary-buffer summary
2673               gnus-article-buffer article-buffer
2674               gnus-original-article-buffer original
2675               gnus-reffed-article-number reffed
2676               gnus-current-score-file score-file
2677               gnus-newsgroup-charset default-charset)
2678         (let ((locals gnus-newsgroup-variables))
2679           (while locals
2680             (if (consp (car locals))
2681                 (set (caar locals) (pop vlist))
2682               (set (car locals) (pop vlist)))
2683             (setq locals (cdr locals))))
2684         ;; The article buffer also has local variables.
2685         (when (gnus-buffer-live-p gnus-article-buffer)
2686           (set-buffer gnus-article-buffer)
2687           (setq gnus-summary-buffer summary))))))
2688
2689 (defun gnus-summary-article-unread-p (article)
2690   "Say whether ARTICLE is unread or not."
2691   (memq article gnus-newsgroup-unreads))
2692
2693 (defun gnus-summary-first-article-p (&optional article)
2694   "Return whether ARTICLE is the first article in the buffer."
2695   (if (not (setq article (or article (gnus-summary-article-number))))
2696       nil
2697     (eq article (caar gnus-newsgroup-data))))
2698
2699 (defun gnus-summary-last-article-p (&optional article)
2700   "Return whether ARTICLE is the last article in the buffer."
2701   (if (not (setq article (or article (gnus-summary-article-number))))
2702       ;; All non-existent numbers are the last article.  :-)
2703       t
2704     (not (cdr (gnus-data-find-list article)))))
2705
2706 (defun gnus-make-thread-indent-array ()
2707   (let ((n 200))
2708     (unless (and gnus-thread-indent-array
2709                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2710       (setq gnus-thread-indent-array (make-vector 201 "")
2711             gnus-thread-indent-array-level gnus-thread-indent-level)
2712       (while (>= n 0)
2713         (aset gnus-thread-indent-array n
2714               (make-string (* n gnus-thread-indent-level) ? ))
2715         (setq n (1- n))))))
2716
2717 (defun gnus-update-summary-mark-positions ()
2718   "Compute where the summary marks are to go."
2719   (save-excursion
2720     (when (gnus-buffer-exists-p gnus-summary-buffer)
2721       (set-buffer gnus-summary-buffer))
2722     (let ((gnus-replied-mark 129)
2723           (gnus-score-below-mark 130)
2724           (gnus-score-over-mark 130)
2725           (gnus-download-mark 131)
2726           (spec gnus-summary-line-format-spec)
2727           gnus-visual pos)
2728       (save-excursion
2729         (gnus-set-work-buffer)
2730         (let ((gnus-summary-line-format-spec spec)
2731               (gnus-newsgroup-downloadable '((0 . t))))
2732           (gnus-summary-insert-line
2733            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2734           (goto-char (point-min))
2735           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2736                                              (- (point) 2)))))
2737           (goto-char (point-min))
2738           (push (cons 'replied (and (search-forward "\201" nil t)
2739                                     (- (point) 2)))
2740                 pos)
2741           (goto-char (point-min))
2742           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2743                 pos)
2744           (goto-char (point-min))
2745           (push (cons 'download
2746                       (and (search-forward "\203" nil t) (- (point) 2)))
2747                 pos)))
2748       (setq gnus-summary-mark-positions pos))))
2749
2750 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2751   "Insert a dummy root in the summary buffer."
2752   (beginning-of-line)
2753   (gnus-add-text-properties
2754    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2755    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2756
2757 (defun gnus-summary-from-or-to-or-newsgroups (header)
2758   (let ((to (cdr (assq 'To (mail-header-extra header))))
2759         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2760         (mail-parse-charset gnus-newsgroup-charset)
2761         (mail-parse-ignored-charsets
2762          (save-excursion (set-buffer gnus-summary-buffer)
2763                          gnus-newsgroup-ignored-charsets)))
2764     (cond
2765      ((and to
2766            gnus-ignored-from-addresses
2767            (string-match gnus-ignored-from-addresses
2768                          (mail-header-from header)))
2769       (concat "-> "
2770               (or (car (funcall gnus-extract-address-components
2771                                 (funcall
2772                                  gnus-decode-encoded-word-function to)))
2773                   (funcall gnus-decode-encoded-word-function to))))
2774      ((and newsgroups
2775            gnus-ignored-from-addresses
2776            (string-match gnus-ignored-from-addresses
2777                          (mail-header-from header)))
2778       (concat "=> " newsgroups))
2779      (t
2780       (or (car (funcall gnus-extract-address-components
2781                         (mail-header-from header)))
2782           (mail-header-from header))))))
2783
2784 (defun gnus-summary-insert-line (gnus-tmp-header
2785                                  gnus-tmp-level gnus-tmp-current
2786                                  gnus-tmp-unread gnus-tmp-replied
2787                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2788                                  &optional gnus-tmp-dummy gnus-tmp-score
2789                                  gnus-tmp-process)
2790   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2791          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2792          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2793          (gnus-tmp-score-char
2794           (if (or (null gnus-summary-default-score)
2795                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2796                       gnus-summary-zcore-fuzz))
2797               ?  ;Whitespace
2798             (if (< gnus-tmp-score gnus-summary-default-score)
2799                 gnus-score-below-mark gnus-score-over-mark)))
2800          (gnus-tmp-replied
2801           (cond (gnus-tmp-process gnus-process-mark)
2802                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2803                  gnus-cached-mark)
2804                 (gnus-tmp-replied gnus-replied-mark)
2805                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2806                  gnus-saved-mark)
2807                 (t gnus-no-mark)))
2808          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2809          (gnus-tmp-name
2810           (cond
2811            ((string-match "<[^>]+> *$" gnus-tmp-from)
2812             (let ((beg (match-beginning 0)))
2813               (or (and (string-match "^\".+\"" gnus-tmp-from)
2814                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2815                   (substring gnus-tmp-from 0 beg))))
2816            ((string-match "(.+)" gnus-tmp-from)
2817             (substring gnus-tmp-from
2818                        (1+ (match-beginning 0)) (1- (match-end 0))))
2819            (t gnus-tmp-from)))
2820          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2821          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2822          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2823          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2824          (buffer-read-only nil))
2825     (when (string= gnus-tmp-name "")
2826       (setq gnus-tmp-name gnus-tmp-from))
2827     (unless (numberp gnus-tmp-lines)
2828       (setq gnus-tmp-lines 0))
2829     (gnus-put-text-property
2830      (point)
2831      (progn (eval gnus-summary-line-format-spec) (point))
2832      'gnus-number gnus-tmp-number)
2833     (when (gnus-visual-p 'summary-highlight 'highlight)
2834       (forward-line -1)
2835       (gnus-run-hooks 'gnus-summary-update-hook)
2836       (forward-line 1))))
2837
2838 (defun gnus-summary-update-line (&optional dont-update)
2839   "Update summary line after change."
2840   (when (and gnus-summary-default-score
2841              (not gnus-summary-inhibit-highlight))
2842     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2843            (article (gnus-summary-article-number))
2844            (score (gnus-summary-article-score article)))
2845       (unless dont-update
2846         (if (and gnus-summary-mark-below
2847                  (< (gnus-summary-article-score)
2848                     gnus-summary-mark-below))
2849             ;; This article has a low score, so we mark it as read.
2850             (when (memq article gnus-newsgroup-unreads)
2851               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2852           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2853             ;; This article was previously marked as read on account
2854             ;; of a low score, but now it has risen, so we mark it as
2855             ;; unread.
2856             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2857         (gnus-summary-update-mark
2858          (if (or (null gnus-summary-default-score)
2859                  (<= (abs (- score gnus-summary-default-score))
2860                      gnus-summary-zcore-fuzz))
2861              ?  ;Whitespace
2862            (if (< score gnus-summary-default-score)
2863                gnus-score-below-mark gnus-score-over-mark))
2864          'score))
2865       ;; Do visual highlighting.
2866       (when (gnus-visual-p 'summary-highlight 'highlight)
2867         (gnus-run-hooks 'gnus-summary-update-hook)))))
2868
2869 (defvar gnus-tmp-new-adopts nil)
2870
2871 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2872   "Return the number of articles in THREAD.
2873 This may be 0 in some cases -- if none of the articles in
2874 the thread are to be displayed."
2875   (let* ((number
2876           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2877           (cond
2878            ((not (listp thread))
2879             1)
2880            ((and (consp thread) (cdr thread))
2881             (apply
2882              '+ 1 (mapcar
2883                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2884            ((null thread)
2885             1)
2886            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2887             1)
2888            (t 0))))
2889     (when (and level (zerop level) gnus-tmp-new-adopts)
2890       (incf number
2891             (apply '+ (mapcar
2892                        'gnus-summary-number-of-articles-in-thread
2893                        gnus-tmp-new-adopts))))
2894     (if char
2895         (if (> number 1) gnus-not-empty-thread-mark
2896           gnus-empty-thread-mark)
2897       number)))
2898
2899 (defun gnus-summary-set-local-parameters (group)
2900   "Go through the local params of GROUP and set all variable specs in that list."
2901   (let ((params (gnus-group-find-parameter group))
2902         elem)
2903     (while params
2904       (setq elem (car params)
2905             params (cdr params))
2906       (and (consp elem)                 ; Has to be a cons.
2907            (consp (cdr elem))           ; The cdr has to be a list.
2908            (symbolp (car elem))         ; Has to be a symbol in there.
2909            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2910            (ignore-errors               ; So we set it.
2911              (make-local-variable (car elem))
2912              (set (car elem) (eval (nth 1 elem))))))))
2913
2914 (defun gnus-summary-read-group (group &optional show-all no-article
2915                                       kill-buffer no-display backward
2916                                       select-articles)
2917   "Start reading news in newsgroup GROUP.
2918 If SHOW-ALL is non-nil, already read articles are also listed.
2919 If NO-ARTICLE is non-nil, no article is selected initially.
2920 If NO-DISPLAY, don't generate a summary buffer."
2921   (let (result)
2922     (while (and group
2923                 (null (setq result
2924                             (let ((gnus-auto-select-next nil))
2925                               (or (gnus-summary-read-group-1
2926                                    group show-all no-article
2927                                    kill-buffer no-display
2928                                    select-articles)
2929                                   (setq show-all nil
2930                                         select-articles nil)))))
2931                 (eq gnus-auto-select-next 'quietly))
2932       (set-buffer gnus-group-buffer)
2933       ;; The entry function called above goes to the next
2934       ;; group automatically, so we go two groups back
2935       ;; if we are searching for the previous group.
2936       (when backward
2937         (gnus-group-prev-unread-group 2))
2938       (if (not (equal group (gnus-group-group-name)))
2939           (setq group (gnus-group-group-name))
2940         (setq group nil)))
2941     result))
2942
2943 (defun gnus-summary-read-group-1 (group show-all no-article
2944                                         kill-buffer no-display
2945                                         &optional select-articles)
2946   ;; Killed foreign groups can't be entered.
2947   (when (and (not (gnus-group-native-p group))
2948              (not (gnus-gethash group gnus-newsrc-hashtb)))
2949     (error "Dead non-native groups can't be entered"))
2950   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2951   (let* ((new-group (gnus-summary-setup-buffer group))
2952          (quit-config (gnus-group-quit-config group))
2953          (did-select (and new-group (gnus-select-newsgroup
2954                                      group show-all select-articles))))
2955     (cond
2956      ;; This summary buffer exists already, so we just select it.
2957      ((not new-group)
2958       (gnus-set-global-variables)
2959       (when kill-buffer
2960         (gnus-kill-or-deaden-summary kill-buffer))
2961       (gnus-configure-windows 'summary 'force)
2962       (gnus-set-mode-line 'summary)
2963       (gnus-summary-position-point)
2964       (message "")
2965       t)
2966      ;; We couldn't select this group.
2967      ((null did-select)
2968       (when (and (eq major-mode 'gnus-summary-mode)
2969                  (not (equal (current-buffer) kill-buffer)))
2970         (kill-buffer (current-buffer))
2971         (if (not quit-config)
2972             (progn
2973               ;; Update the info -- marks might need to be removed,
2974               ;; for instance.
2975               (gnus-summary-update-info)
2976               (set-buffer gnus-group-buffer)
2977               (gnus-group-jump-to-group group)
2978               (gnus-group-next-unread-group 1))
2979           (gnus-handle-ephemeral-exit quit-config)))
2980       (gnus-message 3 "Can't select group")
2981       nil)
2982      ;; The user did a `C-g' while prompting for number of articles,
2983      ;; so we exit this group.
2984      ((eq did-select 'quit)
2985       (and (eq major-mode 'gnus-summary-mode)
2986            (not (equal (current-buffer) kill-buffer))
2987            (kill-buffer (current-buffer)))
2988       (when kill-buffer
2989         (gnus-kill-or-deaden-summary kill-buffer))
2990       (if (not quit-config)
2991           (progn
2992             (set-buffer gnus-group-buffer)
2993             (gnus-group-jump-to-group group)
2994             (gnus-group-next-unread-group 1)
2995             (gnus-configure-windows 'group 'force))
2996         (gnus-handle-ephemeral-exit quit-config))
2997       ;; Finally signal the quit.
2998       (signal 'quit nil))
2999      ;; The group was successfully selected.
3000      (t
3001       (gnus-set-global-variables)
3002       ;; Save the active value in effect when the group was entered.
3003       (setq gnus-newsgroup-active
3004             (gnus-copy-sequence
3005              (gnus-active gnus-newsgroup-name)))
3006       ;; You can change the summary buffer in some way with this hook.
3007       (gnus-run-hooks 'gnus-select-group-hook)
3008       ;; Set any local variables in the group parameters.
3009       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3010       (gnus-update-format-specifications
3011        nil 'summary 'summary-mode 'summary-dummy)
3012       (gnus-update-summary-mark-positions)
3013       ;; Do score processing.
3014       (when gnus-use-scoring
3015         (gnus-possibly-score-headers))
3016       ;; Check whether to fill in the gaps in the threads.
3017       (when gnus-build-sparse-threads
3018         (gnus-build-sparse-threads))
3019       ;; Find the initial limit.
3020       (if gnus-show-threads
3021           (if show-all
3022               (let ((gnus-newsgroup-dormant nil))
3023                 (gnus-summary-initial-limit show-all))
3024             (gnus-summary-initial-limit show-all))
3025         ;; When untreaded, all articles are always shown.
3026         (setq gnus-newsgroup-limit
3027               (mapcar
3028                (lambda (header) (mail-header-number header))
3029                gnus-newsgroup-headers)))
3030       ;; Generate the summary buffer.
3031       (unless no-display
3032         (gnus-summary-prepare))
3033       (when gnus-use-trees
3034         (gnus-tree-open group)
3035         (setq gnus-summary-highlight-line-function
3036               'gnus-tree-highlight-article))
3037       ;; If the summary buffer is empty, but there are some low-scored
3038       ;; articles or some excluded dormants, we include these in the
3039       ;; buffer.
3040       (when (and (zerop (buffer-size))
3041                  (not no-display))
3042         (cond (gnus-newsgroup-dormant
3043                (gnus-summary-limit-include-dormant))
3044               ((and gnus-newsgroup-scored show-all)
3045                (gnus-summary-limit-include-expunged t))))
3046       ;; Function `gnus-apply-kill-file' must be called in this hook.
3047       (gnus-run-hooks 'gnus-apply-kill-hook)
3048       (if (and (zerop (buffer-size))
3049                (not no-display))
3050           (progn
3051             ;; This newsgroup is empty.
3052             (gnus-summary-catchup-and-exit nil t)
3053             (gnus-message 6 "No unread news")
3054             (when kill-buffer
3055               (gnus-kill-or-deaden-summary kill-buffer))
3056             ;; Return nil from this function.
3057             nil)
3058         ;; Hide conversation thread subtrees.  We cannot do this in
3059         ;; gnus-summary-prepare-hook since kill processing may not
3060         ;; work with hidden articles.
3061         (and gnus-show-threads
3062              gnus-thread-hide-subtree
3063              (gnus-summary-hide-all-threads))
3064         (when kill-buffer
3065           (gnus-kill-or-deaden-summary kill-buffer))
3066         ;; Show first unread article if requested.
3067         (if (and (not no-article)
3068                  (not no-display)
3069                  gnus-newsgroup-unreads
3070                  gnus-auto-select-first)
3071             (progn
3072               (gnus-configure-windows 'summary)
3073               (cond
3074                ((eq gnus-auto-select-first 'best)
3075                 (gnus-summary-best-unread-article))
3076                ((eq gnus-auto-select-first t)
3077                 (gnus-summary-first-unread-article))
3078                ((gnus-functionp gnus-auto-select-first)
3079                 (funcall gnus-auto-select-first))))
3080           ;; Don't select any articles, just move point to the first
3081           ;; article in the group.
3082           (goto-char (point-min))
3083           (gnus-summary-position-point)
3084           (gnus-configure-windows 'summary 'force)
3085           (gnus-set-mode-line 'summary))
3086         (when (get-buffer-window gnus-group-buffer t)
3087           ;; Gotta use windows, because recenter does weird stuff if
3088           ;; the current buffer ain't the displayed window.
3089           (let ((owin (selected-window)))
3090             (select-window (get-buffer-window gnus-group-buffer t))
3091             (when (gnus-group-goto-group group)
3092               (recenter))
3093             (select-window owin)))
3094         ;; Mark this buffer as "prepared".
3095         (setq gnus-newsgroup-prepared t)
3096         (gnus-run-hooks 'gnus-summary-prepared-hook)
3097         t)))))
3098
3099 (defun gnus-summary-prepare ()
3100   "Generate the summary buffer."
3101   (interactive)
3102   (let ((buffer-read-only nil))
3103     (erase-buffer)
3104     (setq gnus-newsgroup-data nil
3105           gnus-newsgroup-data-reverse nil)
3106     (gnus-run-hooks 'gnus-summary-generate-hook)
3107     ;; Generate the buffer, either with threads or without.
3108     (when gnus-newsgroup-headers
3109       (gnus-summary-prepare-threads
3110        (if gnus-show-threads
3111            (gnus-sort-gathered-threads
3112             (funcall gnus-summary-thread-gathering-function
3113                      (gnus-sort-threads
3114                       (gnus-cut-threads (gnus-make-threads)))))
3115          ;; Unthreaded display.
3116          (gnus-sort-articles gnus-newsgroup-headers))))
3117     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3118     ;; Call hooks for modifying summary buffer.
3119     (goto-char (point-min))
3120     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3121
3122 (defsubst gnus-general-simplify-subject (subject)
3123   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3124   (setq subject
3125         (cond
3126          ;; Truncate the subject.
3127          (gnus-simplify-subject-functions
3128           (gnus-map-function gnus-simplify-subject-functions subject))
3129          ((numberp gnus-summary-gather-subject-limit)
3130           (setq subject (gnus-simplify-subject-re subject))
3131           (if (> (length subject) gnus-summary-gather-subject-limit)
3132               (substring subject 0 gnus-summary-gather-subject-limit)
3133             subject))
3134          ;; Fuzzily simplify it.
3135          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3136           (gnus-simplify-subject-fuzzy subject))
3137          ;; Just remove the leading "Re:".
3138          (t
3139           (gnus-simplify-subject-re subject))))
3140
3141   (if (and gnus-summary-gather-exclude-subject
3142            (string-match gnus-summary-gather-exclude-subject subject))
3143       nil                               ; This article shouldn't be gathered
3144     subject))
3145
3146 (defun gnus-summary-simplify-subject-query ()
3147   "Query where the respool algorithm would put this article."
3148   (interactive)
3149   (gnus-summary-select-article)
3150   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3151
3152 (defun gnus-gather-threads-by-subject (threads)
3153   "Gather threads by looking at Subject headers."
3154   (if (not gnus-summary-make-false-root)
3155       threads
3156     (let ((hashtb (gnus-make-hashtable 1024))
3157           (prev threads)
3158           (result threads)
3159           subject hthread whole-subject)
3160       (while threads
3161         (setq subject (gnus-general-simplify-subject
3162                        (setq whole-subject (mail-header-subject
3163                                             (caar threads)))))
3164         (when subject
3165           (if (setq hthread (gnus-gethash subject hashtb))
3166               (progn
3167                 ;; We enter a dummy root into the thread, if we
3168                 ;; haven't done that already.
3169                 (unless (stringp (caar hthread))
3170                   (setcar hthread (list whole-subject (car hthread))))
3171                 ;; We add this new gathered thread to this gathered
3172                 ;; thread.
3173                 (setcdr (car hthread)
3174                         (nconc (cdar hthread) (list (car threads))))
3175                 ;; Remove it from the list of threads.
3176                 (setcdr prev (cdr threads))
3177                 (setq threads prev))
3178             ;; Enter this thread into the hash table.
3179             (gnus-sethash subject threads hashtb)))
3180         (setq prev threads)
3181         (setq threads (cdr threads)))
3182       result)))
3183
3184 (defun gnus-gather-threads-by-references (threads)
3185   "Gather threads by looking at References headers."
3186   (let ((idhashtb (gnus-make-hashtable 1024))
3187         (thhashtb (gnus-make-hashtable 1024))
3188         (prev threads)
3189         (result threads)
3190         ids references id gthread gid entered ref)
3191     (while threads
3192       (when (setq references (mail-header-references (caar threads)))
3193         (setq id (mail-header-id (caar threads))
3194               ids (gnus-split-references references)
3195               entered nil)
3196         (while (setq ref (pop ids))
3197           (setq ids (delete ref ids))
3198           (if (not (setq gid (gnus-gethash ref idhashtb)))
3199               (progn
3200                 (gnus-sethash ref id idhashtb)
3201                 (gnus-sethash id threads thhashtb))
3202             (setq gthread (gnus-gethash gid thhashtb))
3203             (unless entered
3204               ;; We enter a dummy root into the thread, if we
3205               ;; haven't done that already.
3206               (unless (stringp (caar gthread))
3207                 (setcar gthread (list (mail-header-subject (caar gthread))
3208                                       (car gthread))))
3209               ;; We add this new gathered thread to this gathered
3210               ;; thread.
3211               (setcdr (car gthread)
3212                       (nconc (cdar gthread) (list (car threads)))))
3213             ;; Add it into the thread hash table.
3214             (gnus-sethash id gthread thhashtb)
3215             (setq entered t)
3216             ;; Remove it from the list of threads.
3217             (setcdr prev (cdr threads))
3218             (setq threads prev))))
3219       (setq prev threads)
3220       (setq threads (cdr threads)))
3221     result))
3222
3223 (defun gnus-sort-gathered-threads (threads)
3224   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3225   (let ((result threads))
3226     (while threads
3227       (when (stringp (caar threads))
3228         (setcdr (car threads)
3229                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3230       (setq threads (cdr threads)))
3231     result))
3232
3233 (defun gnus-thread-loop-p (root thread)
3234   "Say whether ROOT is in THREAD."
3235   (let ((stack (list thread))
3236         (infloop 0)
3237         th)
3238     (while (setq thread (pop stack))
3239       (setq th (cdr thread))
3240       (while (and th
3241                   (not (eq (caar th) root)))
3242         (pop th))
3243       (if th
3244           ;; We have found a loop.
3245           (let (ref-dep)
3246             (setcdr thread (delq (car th) (cdr thread)))
3247             (if (boundp (setq ref-dep (intern "none"
3248                                               gnus-newsgroup-dependencies)))
3249                 (setcdr (symbol-value ref-dep)
3250                         (nconc (cdr (symbol-value ref-dep))
3251                                (list (car th))))
3252               (set ref-dep (list nil (car th))))
3253             (setq infloop 1
3254                   stack nil))
3255         ;; Push all the subthreads onto the stack.
3256         (push (cdr thread) stack)))
3257     infloop))
3258
3259 (defun gnus-make-threads ()
3260   "Go through the dependency hashtb and find the roots.  Return all threads."
3261   (let (threads)
3262     (while (catch 'infloop
3263              (mapatoms
3264               (lambda (refs)
3265                 ;; Deal with self-referencing References loops.
3266                 (when (and (car (symbol-value refs))
3267                            (not (zerop
3268                                  (apply
3269                                   '+
3270                                   (mapcar
3271                                    (lambda (thread)
3272                                      (gnus-thread-loop-p
3273                                       (car (symbol-value refs)) thread))
3274                                    (cdr (symbol-value refs)))))))
3275                   (setq threads nil)
3276                   (throw 'infloop t))
3277                 (unless (car (symbol-value refs))
3278                   ;; These threads do not refer back to any other articles,
3279                   ;; so they're roots.
3280                   (setq threads (append (cdr (symbol-value refs)) threads))))
3281               gnus-newsgroup-dependencies)))
3282     threads))
3283
3284 ;; Build the thread tree.
3285 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3286   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3287
3288 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3289 if it was already present.
3290
3291 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3292 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3293 Message-IDs will be renamed be renamed to a unique Message-ID before
3294 being entered.
3295
3296 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3297   (let* ((id (mail-header-id header))
3298          (id-dep (and id (intern id dependencies)))
3299          ref ref-dep ref-header)
3300     ;; Enter this `header' in the `dependencies' table.
3301     (cond
3302      ((not id-dep)
3303       (setq header nil))
3304      ;; The first two cases do the normal part: enter a new `header'
3305      ;; in the `dependencies' table.
3306      ((not (boundp id-dep))
3307       (set id-dep (list header)))
3308      ((null (car (symbol-value id-dep)))
3309       (setcar (symbol-value id-dep) header))
3310
3311      ;; From here the `header' was already present in the
3312      ;; `dependencies' table.
3313      (force-new
3314       ;; Overrides an existing entry;
3315       ;; just set the header part of the entry.
3316       (setcar (symbol-value id-dep) header))
3317
3318      ;; Renames the existing `header' to a unique Message-ID.
3319      ((not gnus-summary-ignore-duplicates)
3320       ;; An article with this Message-ID has already been seen.
3321       ;; We rename the Message-ID.
3322       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3323            (list header))
3324       (mail-header-set-id header id))
3325
3326      ;; The last case ignores an existing entry, except it adds any
3327      ;; additional Xrefs (in case the two articles came from different
3328      ;; servers.
3329      ;; Also sets `header' to `nil' meaning that the `dependencies'
3330      ;; table was *not* modified.
3331      (t
3332       (mail-header-set-xref
3333        (car (symbol-value id-dep))
3334        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3335                    "")
3336                (or (mail-header-xref header) "")))
3337       (setq header nil)))
3338
3339     (when header
3340       ;; First check if that we are not creating a References loop.
3341       (setq ref (gnus-parent-id (mail-header-references header)))
3342       (while (and ref
3343                   (setq ref-dep (intern-soft ref dependencies))
3344                   (boundp ref-dep)
3345                   (setq ref-header (car (symbol-value ref-dep))))
3346         (if (string= id ref)
3347             ;; Yuk!  This is a reference loop.  Make the article be a
3348             ;; root article.
3349             (progn
3350               (mail-header-set-references (car (symbol-value id-dep)) "none")
3351               (setq ref nil))
3352           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3353       (setq ref (gnus-parent-id (mail-header-references header)))
3354       (setq ref-dep (intern (or ref "none") dependencies))
3355       (if (boundp ref-dep)
3356           (setcdr (symbol-value ref-dep)
3357                   (nconc (cdr (symbol-value ref-dep))
3358                          (list (symbol-value id-dep))))
3359         (set ref-dep (list nil (symbol-value id-dep)))))
3360     header))
3361
3362 (defun gnus-build-sparse-threads ()
3363   (let ((headers gnus-newsgroup-headers)
3364         (mail-parse-charset gnus-newsgroup-charset)
3365         (gnus-summary-ignore-duplicates t)
3366         header references generation relations
3367         subject child end new-child date)
3368     ;; First we create an alist of generations/relations, where
3369     ;; generations is how much we trust the relation, and the relation
3370     ;; is parent/child.
3371     (gnus-message 7 "Making sparse threads...")
3372     (save-excursion
3373       (nnheader-set-temp-buffer " *gnus sparse threads*")
3374       (while (setq header (pop headers))
3375         (when (and (setq references (mail-header-references header))
3376                    (not (string= references "")))
3377           (insert references)
3378           (setq child (mail-header-id header)
3379                 subject (mail-header-subject header)
3380                 date (mail-header-date header)
3381                 generation 0)
3382           (while (search-backward ">" nil t)
3383             (setq end (1+ (point)))
3384             (when (search-backward "<" nil t)
3385               (setq new-child (buffer-substring (point) end))
3386               (push (list (incf generation)
3387                           child (setq child new-child)
3388                           subject date)
3389                     relations)))
3390           (when child
3391             (push (list (1+ generation) child nil subject) relations))
3392           (erase-buffer)))
3393       (kill-buffer (current-buffer)))
3394     ;; Sort over trustworthiness.
3395     (mapcar
3396      (lambda (relation)
3397        (when (gnus-dependencies-add-header
3398               (make-full-mail-header
3399                gnus-reffed-article-number
3400                (nth 3 relation) "" (or (nth 4 relation) "")
3401                (nth 1 relation)
3402                (or (nth 2 relation) "") 0 0 "")
3403               gnus-newsgroup-dependencies nil)
3404          (push gnus-reffed-article-number gnus-newsgroup-limit)
3405          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3406          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3407                gnus-newsgroup-reads)
3408          (decf gnus-reffed-article-number)))
3409      (sort relations 'car-less-than-car))
3410     (gnus-message 7 "Making sparse threads...done")))
3411
3412 (defun gnus-build-old-threads ()
3413   ;; Look at all the articles that refer back to old articles, and
3414   ;; fetch the headers for the articles that aren't there.  This will
3415   ;; build complete threads - if the roots haven't been expired by the
3416   ;; server, that is.
3417   (let ((mail-parse-charset gnus-newsgroup-charset)
3418         id heads)
3419     (mapatoms
3420      (lambda (refs)
3421        (when (not (car (symbol-value refs)))
3422          (setq heads (cdr (symbol-value refs)))
3423          (while heads
3424            (if (memq (mail-header-number (caar heads))
3425                      gnus-newsgroup-dormant)
3426                (setq heads (cdr heads))
3427              (setq id (symbol-name refs))
3428              (while (and (setq id (gnus-build-get-header id))
3429                          (not (car (gnus-id-to-thread id)))))
3430              (setq heads nil)))))
3431      gnus-newsgroup-dependencies)))
3432
3433 ;; This function has to be called with point after the article number
3434 ;; on the beginning of the line.
3435 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3436   (let ((eol (gnus-point-at-eol))
3437         (buffer (current-buffer))
3438         header)
3439
3440     ;; overview: [num subject from date id refs chars lines misc]
3441     (unwind-protect
3442         (progn
3443           (narrow-to-region (point) eol)
3444           (unless (eobp)
3445             (forward-char))
3446
3447           (setq header
3448                 (make-full-mail-header
3449                  number                 ; number
3450                  (funcall gnus-decode-encoded-word-function
3451                           (nnheader-nov-field)) ; subject
3452                  (funcall gnus-decode-encoded-word-function
3453                           (nnheader-nov-field)) ; from
3454                  (nnheader-nov-field)   ; date
3455                  (nnheader-nov-read-message-id) ; id
3456                  (nnheader-nov-field)   ; refs
3457                  (nnheader-nov-read-integer) ; chars
3458                  (nnheader-nov-read-integer) ; lines
3459                  (unless (eobp)
3460                    (if (looking-at "Xref: ")
3461                        (goto-char (match-end 0)))
3462                    (nnheader-nov-field)) ; Xref
3463                  (nnheader-nov-parse-extra)))) ; extra
3464
3465       (widen))
3466
3467     (when gnus-alter-header-function
3468       (funcall gnus-alter-header-function header))
3469     (gnus-dependencies-add-header header dependencies force-new)))
3470
3471 (defun gnus-build-get-header (id)
3472   "Look through the buffer of NOV lines and find the header to ID.
3473 Enter this line into the dependencies hash table, and return
3474 the id of the parent article (if any)."
3475   (let ((deps gnus-newsgroup-dependencies)
3476         found header)
3477     (prog1
3478         (save-excursion
3479           (set-buffer nntp-server-buffer)
3480           (let ((case-fold-search nil))
3481             (goto-char (point-min))
3482             (while (and (not found)
3483                         (search-forward id nil t))
3484               (beginning-of-line)
3485               (setq found (looking-at
3486                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3487                                    (regexp-quote id))))
3488               (or found (beginning-of-line 2)))
3489             (when found
3490               (beginning-of-line)
3491               (and
3492                (setq header (gnus-nov-parse-line
3493                              (read (current-buffer)) deps))
3494                (gnus-parent-id (mail-header-references header))))))
3495       (when header
3496         (let ((number (mail-header-number header)))
3497           (push number gnus-newsgroup-limit)
3498           (push header gnus-newsgroup-headers)
3499           (if (memq number gnus-newsgroup-unselected)
3500               (progn
3501                 (push number gnus-newsgroup-unreads)
3502                 (setq gnus-newsgroup-unselected
3503                       (delq number gnus-newsgroup-unselected)))
3504             (push number gnus-newsgroup-ancient)))))))
3505
3506 (defun gnus-build-all-threads ()
3507   "Read all the headers."
3508   (let ((gnus-summary-ignore-duplicates t)
3509         (mail-parse-charset gnus-newsgroup-charset)
3510         (dependencies gnus-newsgroup-dependencies)
3511         header article)
3512     (save-excursion
3513       (set-buffer nntp-server-buffer)
3514       (let ((case-fold-search nil))
3515         (goto-char (point-min))
3516         (while (not (eobp))
3517           (ignore-errors
3518             (setq article (read (current-buffer))
3519                   header (gnus-nov-parse-line article dependencies)))
3520           (when header
3521             (save-excursion
3522               (set-buffer gnus-summary-buffer)
3523               (push header gnus-newsgroup-headers)
3524               (if (memq (setq article (mail-header-number header))
3525                         gnus-newsgroup-unselected)
3526                   (progn
3527                     (push article gnus-newsgroup-unreads)
3528                     (setq gnus-newsgroup-unselected
3529                           (delq article gnus-newsgroup-unselected)))
3530                 (push article gnus-newsgroup-ancient)))
3531             (forward-line 1)))))))
3532
3533 (defun gnus-summary-update-article-line (article header)
3534   "Update the line for ARTICLE using HEADERS."
3535   (let* ((id (mail-header-id header))
3536          (thread (gnus-id-to-thread id)))
3537     (unless thread
3538       (error "Article in no thread"))
3539     ;; Update the thread.
3540     (setcar thread header)
3541     (gnus-summary-goto-subject article)
3542     (let* ((datal (gnus-data-find-list article))
3543            (data (car datal))
3544            (length (when (cdr datal)
3545                      (- (gnus-data-pos data)
3546                         (gnus-data-pos (cadr datal)))))
3547            (buffer-read-only nil)
3548            (level (gnus-summary-thread-level)))
3549       (gnus-delete-line)
3550       (gnus-summary-insert-line
3551        header level nil (gnus-article-mark article)
3552        (memq article gnus-newsgroup-replied)
3553        (memq article gnus-newsgroup-expirable)
3554        ;; Only insert the Subject string when it's different
3555        ;; from the previous Subject string.
3556        (if (and
3557             gnus-show-threads
3558             (gnus-subject-equal
3559              (condition-case ()
3560                  (mail-header-subject
3561                   (gnus-data-header
3562                    (cadr
3563                     (gnus-data-find-list
3564                      article
3565                      (gnus-data-list t)))))
3566                ;; Error on the side of excessive subjects.
3567                (error ""))
3568              (mail-header-subject header)))
3569            ""
3570          (mail-header-subject header))
3571        nil (cdr (assq article gnus-newsgroup-scored))
3572        (memq article gnus-newsgroup-processable))
3573       (when length
3574         (gnus-data-update-list
3575          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3576
3577 (defun gnus-summary-update-article (article &optional iheader)
3578   "Update ARTICLE in the summary buffer."
3579   (set-buffer gnus-summary-buffer)
3580   (let* ((header (gnus-summary-article-header article))
3581          (id (mail-header-id header))
3582          (data (gnus-data-find article))
3583          (thread (gnus-id-to-thread id))
3584          (references (mail-header-references header))
3585          (parent
3586           (gnus-id-to-thread
3587            (or (gnus-parent-id
3588                 (when (and references
3589                            (not (equal "" references)))
3590                   references))
3591                "none")))
3592          (buffer-read-only nil)
3593          (old (car thread)))
3594     (when thread
3595       (unless iheader
3596         (setcar thread nil)
3597         (when parent
3598           (delq thread parent)))
3599       (if (gnus-summary-insert-subject id header)
3600           ;; Set the (possibly) new article number in the data structure.
3601           (gnus-data-set-number data (gnus-id-to-article id))
3602         (setcar thread old)
3603         nil))))
3604
3605 (defun gnus-rebuild-thread (id &optional line)
3606   "Rebuild the thread containing ID.
3607 If LINE, insert the rebuilt thread starting on line LINE."
3608   (let ((buffer-read-only nil)
3609         old-pos current thread data)
3610     (if (not gnus-show-threads)
3611         (setq thread (list (car (gnus-id-to-thread id))))
3612       ;; Get the thread this article is part of.
3613       (setq thread (gnus-remove-thread id)))
3614     (setq old-pos (gnus-point-at-bol))
3615     (setq current (save-excursion
3616                     (and (zerop (forward-line -1))
3617                          (gnus-summary-article-number))))
3618     ;; If this is a gathered thread, we have to go some re-gathering.
3619     (when (stringp (car thread))
3620       (let ((subject (car thread))
3621             roots thr)
3622         (setq thread (cdr thread))
3623         (while thread
3624           (unless (memq (setq thr (gnus-id-to-thread
3625                                    (gnus-root-id
3626                                     (mail-header-id (caar thread)))))
3627                         roots)
3628             (push thr roots))
3629           (setq thread (cdr thread)))
3630         ;; We now have all (unique) roots.
3631         (if (= (length roots) 1)
3632             ;; All the loose roots are now one solid root.
3633             (setq thread (car roots))
3634           (setq thread (cons subject (gnus-sort-threads roots))))))
3635     (let (threads)
3636       ;; We then insert this thread into the summary buffer.
3637       (when line
3638         (goto-char (point-min))
3639         (forward-line (1- line)))
3640       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3641         (if gnus-show-threads
3642             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3643           (gnus-summary-prepare-unthreaded thread))
3644         (setq data (nreverse gnus-newsgroup-data))
3645         (setq threads gnus-newsgroup-threads))
3646       ;; We splice the new data into the data structure.
3647       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3648       ;;!!! then we want to insert at the beginning of the buffer.
3649       ;;!!! That happens to be true with Gnus now, but that may
3650       ;;!!! change in the future.  Perhaps.
3651       (gnus-data-enter-list
3652        (if line nil current) data (- (point) old-pos))
3653       (setq gnus-newsgroup-threads
3654             (nconc threads gnus-newsgroup-threads))
3655       (gnus-data-compute-positions))))
3656
3657 (defun gnus-number-to-header (number)
3658   "Return the header for article NUMBER."
3659   (let ((headers gnus-newsgroup-headers))
3660     (while (and headers
3661                 (not (= number (mail-header-number (car headers)))))
3662       (pop headers))
3663     (when headers
3664       (car headers))))
3665
3666 (defun gnus-parent-headers (in-headers &optional generation)
3667   "Return the headers of the GENERATIONeth parent of HEADERS."
3668   (unless generation
3669     (setq generation 1))
3670   (let ((parent t)
3671         (headers in-headers)
3672         references)
3673     (while (and parent
3674                 (not (zerop generation))
3675                 (setq references (mail-header-references headers)))
3676       (setq headers (if (and references
3677                              (setq parent (gnus-parent-id references)))
3678                         (car (gnus-id-to-thread parent))
3679                       nil))
3680       (decf generation))
3681     (and (not (eq headers in-headers))
3682          headers)))
3683
3684 (defun gnus-id-to-thread (id)
3685   "Return the (sub-)thread where ID appears."
3686   (gnus-gethash id gnus-newsgroup-dependencies))
3687
3688 (defun gnus-id-to-article (id)
3689   "Return the article number of ID."
3690   (let ((thread (gnus-id-to-thread id)))
3691     (when (and thread
3692                (car thread))
3693       (mail-header-number (car thread)))))
3694
3695 (defun gnus-id-to-header (id)
3696   "Return the article headers of ID."
3697   (car (gnus-id-to-thread id)))
3698
3699 (defun gnus-article-displayed-root-p (article)
3700   "Say whether ARTICLE is a root(ish) article."
3701   (let ((level (gnus-summary-thread-level article))
3702         (refs (mail-header-references  (gnus-summary-article-header article)))
3703         particle)
3704     (cond
3705      ((null level) nil)
3706      ((zerop level) t)
3707      ((null refs) t)
3708      ((null (gnus-parent-id refs)) t)
3709      ((and (= 1 level)
3710            (null (setq particle (gnus-id-to-article
3711                                  (gnus-parent-id refs))))
3712            (null (gnus-summary-thread-level particle)))))))
3713
3714 (defun gnus-root-id (id)
3715   "Return the id of the root of the thread where ID appears."
3716   (let (last-id prev)
3717     (while (and id (setq prev (car (gnus-id-to-thread id))))
3718       (setq last-id id
3719             id (gnus-parent-id (mail-header-references prev))))
3720     last-id))
3721
3722 (defun gnus-articles-in-thread (thread)
3723   "Return the list of articles in THREAD."
3724   (cons (mail-header-number (car thread))
3725         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3726
3727 (defun gnus-remove-thread (id &optional dont-remove)
3728   "Remove the thread that has ID in it."
3729   (let (headers thread last-id)
3730     ;; First go up in this thread until we find the root.
3731     (setq last-id (gnus-root-id id)
3732           headers (message-flatten-list (gnus-id-to-thread last-id)))
3733     ;; We have now found the real root of this thread.  It might have
3734     ;; been gathered into some loose thread, so we have to search
3735     ;; through the threads to find the thread we wanted.
3736     (let ((threads gnus-newsgroup-threads)
3737           sub)
3738       (while threads
3739         (setq sub (car threads))
3740         (if (stringp (car sub))
3741             ;; This is a gathered thread, so we look at the roots
3742             ;; below it to find whether this article is in this
3743             ;; gathered root.
3744             (progn
3745               (setq sub (cdr sub))
3746               (while sub
3747                 (when (member (caar sub) headers)
3748                   (setq thread (car threads)
3749                         threads nil
3750                         sub nil))
3751                 (setq sub (cdr sub))))
3752           ;; It's an ordinary thread, so we check it.
3753           (when (eq (car sub) (car headers))
3754             (setq thread sub
3755                   threads nil)))
3756         (setq threads (cdr threads)))
3757       ;; If this article is in no thread, then it's a root.
3758       (if thread
3759           (unless dont-remove
3760             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3761         (setq thread (gnus-id-to-thread last-id)))
3762       (when thread
3763         (prog1
3764             thread                      ; We return this thread.
3765           (unless dont-remove
3766             (if (stringp (car thread))
3767                 (progn
3768                   ;; If we use dummy roots, then we have to remove the
3769                   ;; dummy root as well.
3770                   (when (eq gnus-summary-make-false-root 'dummy)
3771                     ;; We go to the dummy root by going to
3772                     ;; the first sub-"thread", and then one line up.
3773                     (gnus-summary-goto-article
3774                      (mail-header-number (caadr thread)))
3775                     (forward-line -1)
3776                     (gnus-delete-line)
3777                     (gnus-data-compute-positions))
3778                   (setq thread (cdr thread))
3779                   (while thread
3780                     (gnus-remove-thread-1 (car thread))
3781                     (setq thread (cdr thread))))
3782               (gnus-remove-thread-1 thread))))))))
3783
3784 (defun gnus-remove-thread-1 (thread)
3785   "Remove the thread THREAD recursively."
3786   (let ((number (mail-header-number (pop thread)))
3787         d)
3788     (setq thread (reverse thread))
3789     (while thread
3790       (gnus-remove-thread-1 (pop thread)))
3791     (when (setq d (gnus-data-find number))
3792       (goto-char (gnus-data-pos d))
3793       (gnus-summary-show-thread)
3794       (gnus-data-remove
3795        number
3796        (- (gnus-point-at-bol)
3797           (prog1
3798               (1+ (gnus-point-at-eol))
3799             (gnus-delete-line)))))))
3800
3801 (defun gnus-sort-threads-1 (threads func)
3802   (sort (mapcar (lambda (thread)
3803                   (cons (car thread)
3804                         (and (cdr thread)
3805                              (gnus-sort-threads-1 (cdr thread) func))))
3806                 threads) func))
3807
3808 (defun gnus-sort-threads (threads)
3809   "Sort THREADS."
3810   (if (not gnus-thread-sort-functions)
3811       threads
3812     (gnus-message 8 "Sorting threads...")
3813     (prog1
3814         (gnus-sort-threads-1 
3815          threads 
3816          (gnus-make-sort-function gnus-thread-sort-functions))
3817       (gnus-message 8 "Sorting threads...done"))))
3818
3819 (defun gnus-sort-articles (articles)
3820   "Sort ARTICLES."
3821   (when gnus-article-sort-functions
3822     (gnus-message 7 "Sorting articles...")
3823     (prog1
3824         (setq gnus-newsgroup-headers
3825               (sort articles (gnus-make-sort-function
3826                               gnus-article-sort-functions)))
3827       (gnus-message 7 "Sorting articles...done"))))
3828
3829 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3830 (defmacro gnus-thread-header (thread)
3831   "Return header of first article in THREAD.
3832 Note that THREAD must never, ever be anything else than a variable -
3833 using some other form will lead to serious barfage."
3834   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3835   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3836   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3837         (vector thread) 2))
3838
3839 (defsubst gnus-article-sort-by-number (h1 h2)
3840   "Sort articles by article number."
3841   (< (mail-header-number h1)
3842      (mail-header-number h2)))
3843
3844 (defun gnus-thread-sort-by-number (h1 h2)
3845   "Sort threads by root article number."
3846   (gnus-article-sort-by-number
3847    (gnus-thread-header h1) (gnus-thread-header h2)))
3848
3849 (defsubst gnus-article-sort-by-lines (h1 h2)
3850   "Sort articles by article Lines header."
3851   (< (mail-header-lines h1)
3852      (mail-header-lines h2)))
3853
3854 (defun gnus-thread-sort-by-lines (h1 h2)
3855   "Sort threads by root article Lines header."
3856   (gnus-article-sort-by-lines
3857    (gnus-thread-header h1) (gnus-thread-header h2)))
3858
3859 (defsubst gnus-article-sort-by-chars (h1 h2)
3860   "Sort articles by octet length."
3861   (< (mail-header-chars h1)
3862      (mail-header-chars h2)))
3863
3864 (defun gnus-thread-sort-by-chars (h1 h2)
3865   "Sort threads by root article octet length."
3866   (gnus-article-sort-by-chars
3867    (gnus-thread-header h1) (gnus-thread-header h2)))
3868
3869 (defsubst gnus-article-sort-by-author (h1 h2)
3870   "Sort articles by root author."
3871   (string-lessp
3872    (let ((extract (funcall
3873                    gnus-extract-address-components
3874                    (mail-header-from h1))))
3875      (or (car extract) (cadr extract) ""))
3876    (let ((extract (funcall
3877                    gnus-extract-address-components
3878                    (mail-header-from h2))))
3879      (or (car extract) (cadr extract) ""))))
3880
3881 (defun gnus-thread-sort-by-author (h1 h2)
3882   "Sort threads by root author."
3883   (gnus-article-sort-by-author
3884    (gnus-thread-header h1)  (gnus-thread-header h2)))
3885
3886 (defsubst gnus-article-sort-by-subject (h1 h2)
3887   "Sort articles by root subject."
3888   (string-lessp
3889    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3890    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3891
3892 (defun gnus-thread-sort-by-subject (h1 h2)
3893   "Sort threads by root subject."
3894   (gnus-article-sort-by-subject
3895    (gnus-thread-header h1) (gnus-thread-header h2)))
3896
3897 (defsubst gnus-article-sort-by-date (h1 h2)
3898   "Sort articles by root article date."
3899   (time-less-p
3900    (gnus-date-get-time (mail-header-date h1))
3901    (gnus-date-get-time (mail-header-date h2))))
3902
3903 (defun gnus-thread-sort-by-date (h1 h2)
3904   "Sort threads by root article date."
3905   (gnus-article-sort-by-date
3906    (gnus-thread-header h1) (gnus-thread-header h2)))
3907
3908 (defsubst gnus-article-sort-by-score (h1 h2)
3909   "Sort articles by root article score.
3910 Unscored articles will be counted as having a score of zero."
3911   (> (or (cdr (assq (mail-header-number h1)
3912                     gnus-newsgroup-scored))
3913          gnus-summary-default-score 0)
3914      (or (cdr (assq (mail-header-number h2)
3915                     gnus-newsgroup-scored))
3916          gnus-summary-default-score 0)))
3917
3918 (defun gnus-thread-sort-by-score (h1 h2)
3919   "Sort threads by root article score."
3920   (gnus-article-sort-by-score
3921    (gnus-thread-header h1) (gnus-thread-header h2)))
3922
3923 (defun gnus-thread-sort-by-total-score (h1 h2)
3924   "Sort threads by the sum of all scores in the thread.
3925 Unscored articles will be counted as having a score of zero."
3926   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3927
3928 (defun gnus-thread-total-score (thread)
3929   ;; This function find the total score of THREAD.
3930   (cond ((null thread)
3931          0)
3932         ((consp thread)
3933          (if (stringp (car thread))
3934              (apply gnus-thread-score-function 0
3935                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3936            (gnus-thread-total-score-1 thread)))
3937         (t
3938          (gnus-thread-total-score-1 (list thread)))))
3939
3940 (defun gnus-thread-total-score-1 (root)
3941   ;; This function find the total score of the thread below ROOT.
3942   (setq root (car root))
3943   (apply gnus-thread-score-function
3944          (or (append
3945               (mapcar 'gnus-thread-total-score
3946                       (cdr (gnus-id-to-thread (mail-header-id root))))
3947               (when (> (mail-header-number root) 0)
3948                 (list (or (cdr (assq (mail-header-number root)
3949                                      gnus-newsgroup-scored))
3950                           gnus-summary-default-score 0))))
3951              (list gnus-summary-default-score)
3952              '(0))))
3953
3954 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3955 (defvar gnus-tmp-prev-subject nil)
3956 (defvar gnus-tmp-false-parent nil)
3957 (defvar gnus-tmp-root-expunged nil)
3958 (defvar gnus-tmp-dummy-line nil)
3959
3960 (eval-when-compile (defvar gnus-tmp-header))
3961 (defun gnus-extra-header (type &optional header)
3962   "Return the extra header of TYPE."
3963   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3964       ""))
3965
3966 (defun gnus-summary-prepare-threads (threads)
3967   "Prepare summary buffer from THREADS and indentation LEVEL.
3968 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3969 or a straight list of headers."
3970   (gnus-message 7 "Generating summary...")
3971
3972   (setq gnus-newsgroup-threads threads)
3973   (beginning-of-line)
3974
3975   (let ((gnus-tmp-level 0)
3976         (default-score (or gnus-summary-default-score 0))
3977         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3978         thread number subject stack state gnus-tmp-gathered beg-match
3979         new-roots gnus-tmp-new-adopts thread-end
3980         gnus-tmp-header gnus-tmp-unread
3981         gnus-tmp-replied gnus-tmp-subject-or-nil
3982         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3983         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3984         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3985
3986     (setq gnus-tmp-prev-subject nil)
3987
3988     (if (vectorp (car threads))
3989         ;; If this is a straight (sic) list of headers, then a
3990         ;; threaded summary display isn't required, so we just create
3991         ;; an unthreaded one.
3992         (gnus-summary-prepare-unthreaded threads)
3993
3994       ;; Do the threaded display.
3995
3996       (while (or threads stack gnus-tmp-new-adopts new-roots)
3997
3998         (if (and (= gnus-tmp-level 0)
3999                  (or (not stack)
4000                      (= (caar stack) 0))
4001                  (not gnus-tmp-false-parent)
4002                  (or gnus-tmp-new-adopts new-roots))
4003             (if gnus-tmp-new-adopts
4004                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4005                       thread (list (car gnus-tmp-new-adopts))
4006                       gnus-tmp-header (caar thread)
4007                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4008               (when new-roots
4009                 (setq thread (list (car new-roots))
4010                       gnus-tmp-header (caar thread)
4011                       new-roots (cdr new-roots))))
4012
4013           (if threads
4014               ;; If there are some threads, we do them before the
4015               ;; threads on the stack.
4016               (setq thread threads
4017                     gnus-tmp-header (caar thread))
4018             ;; There were no current threads, so we pop something off
4019             ;; the stack.
4020             (setq state (car stack)
4021                   gnus-tmp-level (car state)
4022                   thread (cdr state)
4023                   stack (cdr stack)
4024                   gnus-tmp-header (caar thread))))
4025
4026         (setq gnus-tmp-false-parent nil)
4027         (setq gnus-tmp-root-expunged nil)
4028         (setq thread-end nil)
4029
4030         (if (stringp gnus-tmp-header)
4031             ;; The header is a dummy root.
4032             (cond
4033              ((eq gnus-summary-make-false-root 'adopt)
4034               ;; We let the first article adopt the rest.
4035               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4036                                                (cddar thread)))
4037               (setq gnus-tmp-gathered
4038                     (nconc (mapcar
4039                             (lambda (h) (mail-header-number (car h)))
4040                             (cddar thread))
4041                            gnus-tmp-gathered))
4042               (setq thread (cons (list (caar thread)
4043                                        (cadar thread))
4044                                  (cdr thread)))
4045               (setq gnus-tmp-level -1
4046                     gnus-tmp-false-parent t))
4047              ((eq gnus-summary-make-false-root 'empty)
4048               ;; We print adopted articles with empty subject fields.
4049               (setq gnus-tmp-gathered
4050                     (nconc (mapcar
4051                             (lambda (h) (mail-header-number (car h)))
4052                             (cddar thread))
4053                            gnus-tmp-gathered))
4054               (setq gnus-tmp-level -1))
4055              ((eq gnus-summary-make-false-root 'dummy)
4056               ;; We remember that we probably want to output a dummy
4057               ;; root.
4058               (setq gnus-tmp-dummy-line gnus-tmp-header)
4059               (setq gnus-tmp-prev-subject gnus-tmp-header))
4060              (t
4061               ;; We do not make a root for the gathered
4062               ;; sub-threads at all.
4063               (setq gnus-tmp-level -1)))
4064
4065           (setq number (mail-header-number gnus-tmp-header)
4066                 subject (mail-header-subject gnus-tmp-header))
4067
4068           (cond
4069            ;; If the thread has changed subject, we might want to make
4070            ;; this subthread into a root.
4071            ((and (null gnus-thread-ignore-subject)
4072                  (not (zerop gnus-tmp-level))
4073                  gnus-tmp-prev-subject
4074                  (not (inline
4075                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4076             (setq new-roots (nconc new-roots (list (car thread)))
4077                   thread-end t
4078                   gnus-tmp-header nil))
4079            ;; If the article lies outside the current limit,
4080            ;; then we do not display it.
4081            ((not (memq number gnus-newsgroup-limit))
4082             (setq gnus-tmp-gathered
4083                   (nconc (mapcar
4084                           (lambda (h) (mail-header-number (car h)))
4085                           (cdar thread))
4086                          gnus-tmp-gathered))
4087             (setq gnus-tmp-new-adopts (if (cdar thread)
4088                                           (append gnus-tmp-new-adopts
4089                                                   (cdar thread))
4090                                         gnus-tmp-new-adopts)
4091                   thread-end t
4092                   gnus-tmp-header nil)
4093             (when (zerop gnus-tmp-level)
4094               (setq gnus-tmp-root-expunged t)))
4095            ;; Perhaps this article is to be marked as read?
4096            ((and gnus-summary-mark-below
4097                  (< (or (cdr (assq number gnus-newsgroup-scored))
4098                         default-score)
4099                     gnus-summary-mark-below)
4100                  ;; Don't touch sparse articles.
4101                  (not (gnus-summary-article-sparse-p number))
4102                  (not (gnus-summary-article-ancient-p number)))
4103             (setq gnus-newsgroup-unreads
4104                   (delq number gnus-newsgroup-unreads))
4105             (if gnus-newsgroup-auto-expire
4106                 (push number gnus-newsgroup-expirable)
4107               (push (cons number gnus-low-score-mark)
4108                     gnus-newsgroup-reads))))
4109
4110           (when gnus-tmp-header
4111             ;; We may have an old dummy line to output before this
4112             ;; article.
4113             (when (and gnus-tmp-dummy-line
4114                        (gnus-subject-equal
4115                         gnus-tmp-dummy-line
4116                         (mail-header-subject gnus-tmp-header)))
4117               (gnus-summary-insert-dummy-line
4118                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4119               (setq gnus-tmp-dummy-line nil))
4120
4121             ;; Compute the mark.
4122             (setq gnus-tmp-unread (gnus-article-mark number))
4123
4124             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4125                                   gnus-tmp-header gnus-tmp-level)
4126                   gnus-newsgroup-data)
4127
4128             ;; Actually insert the line.
4129             (setq
4130              gnus-tmp-subject-or-nil
4131              (cond
4132               ((and gnus-thread-ignore-subject
4133                     gnus-tmp-prev-subject
4134                     (not (inline (gnus-subject-equal
4135                                   gnus-tmp-prev-subject subject))))
4136                subject)
4137               ((zerop gnus-tmp-level)
4138                (if (and (eq gnus-summary-make-false-root 'empty)
4139                         (memq number gnus-tmp-gathered)
4140                         gnus-tmp-prev-subject
4141                         (inline (gnus-subject-equal
4142                                  gnus-tmp-prev-subject subject)))
4143                    gnus-summary-same-subject
4144                  subject))
4145               (t gnus-summary-same-subject)))
4146             (if (and (eq gnus-summary-make-false-root 'adopt)
4147                      (= gnus-tmp-level 1)
4148                      (memq number gnus-tmp-gathered))
4149                 (setq gnus-tmp-opening-bracket ?\<
4150                       gnus-tmp-closing-bracket ?\>)
4151               (setq gnus-tmp-opening-bracket ?\[
4152                     gnus-tmp-closing-bracket ?\]))
4153             (setq
4154              gnus-tmp-indentation
4155              (aref gnus-thread-indent-array gnus-tmp-level)
4156              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4157              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4158                                 gnus-summary-default-score 0)
4159              gnus-tmp-score-char
4160              (if (or (null gnus-summary-default-score)
4161                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4162                          gnus-summary-zcore-fuzz))
4163                  ?  ;Whitespace
4164                (if (< gnus-tmp-score gnus-summary-default-score)
4165                    gnus-score-below-mark gnus-score-over-mark))
4166              gnus-tmp-replied
4167              (cond ((memq number gnus-newsgroup-processable)
4168                     gnus-process-mark)
4169                    ((memq number gnus-newsgroup-cached)
4170                     gnus-cached-mark)
4171                    ((memq number gnus-newsgroup-replied)
4172                     gnus-replied-mark)
4173                    ((memq number gnus-newsgroup-saved)
4174                     gnus-saved-mark)
4175                    (t gnus-no-mark))
4176              gnus-tmp-from (mail-header-from gnus-tmp-header)
4177              gnus-tmp-name
4178              (cond
4179               ((string-match "<[^>]+> *$" gnus-tmp-from)
4180                (setq beg-match (match-beginning 0))
4181                (or (and (string-match "^\".+\"" gnus-tmp-from)
4182                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4183                    (substring gnus-tmp-from 0 beg-match)))
4184               ((string-match "(.+)" gnus-tmp-from)
4185                (substring gnus-tmp-from
4186                           (1+ (match-beginning 0)) (1- (match-end 0))))
4187               (t gnus-tmp-from)))
4188             (when (string= gnus-tmp-name "")
4189               (setq gnus-tmp-name gnus-tmp-from))
4190             (unless (numberp gnus-tmp-lines)
4191               (setq gnus-tmp-lines 0))
4192             (gnus-put-text-property
4193              (point)
4194              (progn (eval gnus-summary-line-format-spec) (point))
4195              'gnus-number number)
4196             (when gnus-visual-p
4197               (forward-line -1)
4198               (gnus-run-hooks 'gnus-summary-update-hook)
4199               (forward-line 1))
4200
4201             (setq gnus-tmp-prev-subject subject)))
4202
4203         (when (nth 1 thread)
4204           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4205         (incf gnus-tmp-level)
4206         (setq threads (if thread-end nil (cdar thread)))
4207         (unless threads
4208           (setq gnus-tmp-level 0)))))
4209   (gnus-message 7 "Generating summary...done"))
4210
4211 (defun gnus-summary-prepare-unthreaded (headers)
4212   "Generate an unthreaded summary buffer based on HEADERS."
4213   (let (header number mark)
4214
4215     (beginning-of-line)
4216
4217     (while headers
4218       ;; We may have to root out some bad articles...
4219       (when (memq (setq number (mail-header-number
4220                                 (setq header (pop headers))))
4221                   gnus-newsgroup-limit)
4222         ;; Mark article as read when it has a low score.
4223         (when (and gnus-summary-mark-below
4224                    (< (or (cdr (assq number gnus-newsgroup-scored))
4225                           gnus-summary-default-score 0)
4226                       gnus-summary-mark-below)
4227                    (not (gnus-summary-article-ancient-p number)))
4228           (setq gnus-newsgroup-unreads
4229                 (delq number gnus-newsgroup-unreads))
4230           (if gnus-newsgroup-auto-expire
4231               (push number gnus-newsgroup-expirable)
4232             (push (cons number gnus-low-score-mark)
4233                   gnus-newsgroup-reads)))
4234
4235         (setq mark (gnus-article-mark number))
4236         (push (gnus-data-make number mark (1+ (point)) header 0)
4237               gnus-newsgroup-data)
4238         (gnus-summary-insert-line
4239          header 0 number
4240          mark (memq number gnus-newsgroup-replied)
4241          (memq number gnus-newsgroup-expirable)
4242          (mail-header-subject header) nil
4243          (cdr (assq number gnus-newsgroup-scored))
4244          (memq number gnus-newsgroup-processable))))))
4245
4246 (defun gnus-summary-remove-list-identifiers ()
4247   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4248   (let ((regexp (if (stringp gnus-list-identifiers)
4249                     gnus-list-identifiers
4250                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4251     (dolist (header gnus-newsgroup-headers)
4252       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4253                                   " *\\)\\)+\\(Re: +\\)?\\)")
4254                           (mail-header-subject header))
4255         (mail-header-set-subject
4256          header (concat (substring (mail-header-subject header)
4257                                    0 (match-beginning 1))
4258                         (or
4259                          (match-string 3 (mail-header-subject header))
4260                          (match-string 5 (mail-header-subject header)))
4261                         (substring (mail-header-subject header)
4262                                    (match-end 1))))))))
4263
4264 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4265   "Select newsgroup GROUP.
4266 If READ-ALL is non-nil, all articles in the group are selected.
4267 If SELECT-ARTICLES, only select those articles from GROUP."
4268   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4269          ;;!!! Dirty hack; should be removed.
4270          (gnus-summary-ignore-duplicates
4271           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4272               t
4273             gnus-summary-ignore-duplicates))
4274          (info (nth 2 entry))
4275          articles fetched-articles cached)
4276
4277     (unless (gnus-check-server
4278              (setq gnus-current-select-method
4279                    (gnus-find-method-for-group group)))
4280       (error "Couldn't open server"))
4281
4282     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4283         (gnus-activate-group group)     ; Or we can activate it...
4284         (progn                          ; Or we bug out.
4285           (when (equal major-mode 'gnus-summary-mode)
4286             (kill-buffer (current-buffer)))
4287           (error "Couldn't activate group %s: %s"
4288                  group (gnus-status-message group))))
4289
4290     (unless (gnus-request-group group t)
4291       (when (equal major-mode 'gnus-summary-mode)
4292         (kill-buffer (current-buffer)))
4293       (error "Couldn't request group %s: %s"
4294              group (gnus-status-message group)))
4295
4296     (setq gnus-newsgroup-name group)
4297     (setq gnus-newsgroup-unselected nil)
4298     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4299     (gnus-summary-setup-default-charset)
4300
4301     ;; Adjust and set lists of article marks.
4302     (when info
4303       (gnus-adjust-marked-articles info))
4304
4305     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4306     (when (gnus-virtual-group-p group)
4307       (setq cached gnus-newsgroup-cached))
4308
4309     (setq gnus-newsgroup-unreads
4310           (gnus-set-difference
4311            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4312            gnus-newsgroup-dormant))
4313
4314     (setq gnus-newsgroup-processable nil)
4315
4316     (gnus-update-read-articles group gnus-newsgroup-unreads)
4317
4318     (if (setq articles select-articles)
4319         (setq gnus-newsgroup-unselected
4320               (gnus-sorted-intersection
4321                gnus-newsgroup-unreads
4322                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4323       (setq articles (gnus-articles-to-read group read-all)))
4324
4325     (cond
4326      ((null articles)
4327       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4328       'quit)
4329      ((eq articles 0) nil)
4330      (t
4331       ;; Init the dependencies hash table.
4332       (setq gnus-newsgroup-dependencies
4333             (gnus-make-hashtable (length articles)))
4334       (gnus-set-global-variables)
4335       ;; Retrieve the headers and read them in.
4336       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4337       (setq gnus-newsgroup-headers
4338             (if (eq 'nov
4339                     (setq gnus-headers-retrieved-by
4340                           (gnus-retrieve-headers
4341                            articles gnus-newsgroup-name
4342                            ;; We might want to fetch old headers, but
4343                            ;; not if there is only 1 article.
4344                            (and (or (and
4345                                      (not (eq gnus-fetch-old-headers 'some))
4346                                      (not (numberp gnus-fetch-old-headers)))
4347                                     (> (length articles) 1))
4348                                 gnus-fetch-old-headers))))
4349                 (gnus-get-newsgroup-headers-xover
4350                  articles nil nil gnus-newsgroup-name t)
4351               (gnus-get-newsgroup-headers)))
4352       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4353
4354       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4355       (when cached
4356         (setq gnus-newsgroup-cached cached))
4357
4358       ;; Suppress duplicates?
4359       (when gnus-suppress-duplicates
4360         (gnus-dup-suppress-articles))
4361
4362       ;; Set the initial limit.
4363       (setq gnus-newsgroup-limit (copy-sequence articles))
4364       ;; Remove canceled articles from the list of unread articles.
4365       (setq gnus-newsgroup-unreads
4366             (gnus-set-sorted-intersection
4367              gnus-newsgroup-unreads
4368              (setq fetched-articles
4369                    (mapcar (lambda (headers) (mail-header-number headers))
4370                            gnus-newsgroup-headers))))
4371       ;; Removed marked articles that do not exist.
4372       (gnus-update-missing-marks
4373        (gnus-sorted-complement fetched-articles articles))
4374       ;; We might want to build some more threads first.
4375       (when (and gnus-fetch-old-headers
4376                  (eq gnus-headers-retrieved-by 'nov))
4377         (if (eq gnus-fetch-old-headers 'invisible)
4378             (gnus-build-all-threads)
4379           (gnus-build-old-threads)))
4380       ;; Let the Gnus agent mark articles as read.
4381       (when gnus-agent
4382         (gnus-agent-get-undownloaded-list))
4383       ;; Remove list identifiers from subject
4384       (when gnus-list-identifiers
4385         (gnus-summary-remove-list-identifiers))
4386       ;; Check whether auto-expire is to be done in this group.
4387       (setq gnus-newsgroup-auto-expire
4388             (gnus-group-auto-expirable-p group))
4389       ;; Set up the article buffer now, if necessary.
4390       (unless gnus-single-article-buffer
4391         (gnus-article-setup-buffer))
4392       ;; First and last article in this newsgroup.
4393       (when gnus-newsgroup-headers
4394         (setq gnus-newsgroup-begin
4395               (mail-header-number (car gnus-newsgroup-headers))
4396               gnus-newsgroup-end
4397               (mail-header-number
4398                (gnus-last-element gnus-newsgroup-headers))))
4399       ;; GROUP is successfully selected.
4400       (or gnus-newsgroup-headers t)))))
4401
4402 (defun gnus-articles-to-read (group &optional read-all)
4403   "Find out what articles the user wants to read."
4404   (let* ((articles
4405           ;; Select all articles if `read-all' is non-nil, or if there
4406           ;; are no unread articles.
4407           (if (or read-all
4408                   (and (zerop (length gnus-newsgroup-marked))
4409                        (zerop (length gnus-newsgroup-unreads)))
4410                   (eq (gnus-group-find-parameter group 'display)
4411                       'all))
4412               (or
4413                (gnus-uncompress-range (gnus-active group))
4414                (gnus-cache-articles-in-group group))
4415             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4416                           (copy-sequence gnus-newsgroup-unreads))
4417                   '<)))
4418          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4419          (scored (length scored-list))
4420          (number (length articles))
4421          (marked (+ (length gnus-newsgroup-marked)
4422                     (length gnus-newsgroup-dormant)))
4423          (select
4424           (cond
4425            ((numberp read-all)
4426             read-all)
4427            (t
4428             (condition-case ()
4429                 (cond
4430                  ((and (or (<= scored marked) (= scored number))
4431                        (numberp gnus-large-newsgroup)
4432                        (> number gnus-large-newsgroup))
4433                   (let ((input
4434                          (read-string
4435                           (format
4436                            "How many articles from %s (default %d): "
4437                            (gnus-limit-string gnus-newsgroup-name 35)
4438                            number))))
4439                     (if (string-match "^[ \t]*$" input) number input)))
4440                  ((and (> scored marked) (< scored number)
4441                        (> (- scored number) 20))
4442                   (let ((input
4443                          (read-string
4444                           (format "%s %s (%d scored, %d total): "
4445                                   "How many articles from"
4446                                   group scored number))))
4447                     (if (string-match "^[ \t]*$" input)
4448                         number input)))
4449                  (t number))
4450               (quit
4451                (message "Quit getting the articles to read")
4452                nil))))))
4453     (setq select (if (stringp select) (string-to-number select) select))
4454     (if (or (null select) (zerop select))
4455         select
4456       (if (and (not (zerop scored)) (<= (abs select) scored))
4457           (progn
4458             (setq articles (sort scored-list '<))
4459             (setq number (length articles)))
4460         (setq articles (copy-sequence articles)))
4461
4462       (when (< (abs select) number)
4463         (if (< select 0)
4464             ;; Select the N oldest articles.
4465             (setcdr (nthcdr (1- (abs select)) articles) nil)
4466           ;; Select the N most recent articles.
4467           (setq articles (nthcdr (- number select) articles))))
4468       (setq gnus-newsgroup-unselected
4469             (gnus-sorted-intersection
4470              gnus-newsgroup-unreads
4471              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4472       (when gnus-alter-articles-to-read-function
4473         (setq gnus-newsgroup-unreads
4474               (sort 
4475                (funcall gnus-alter-articles-to-read-function
4476                         gnus-newsgroup-name gnus-newsgroup-unreads)
4477                '<)))
4478       articles)))
4479
4480 (defun gnus-killed-articles (killed articles)
4481   (let (out)
4482     (while articles
4483       (when (inline (gnus-member-of-range (car articles) killed))
4484         (push (car articles) out))
4485       (setq articles (cdr articles)))
4486     out))
4487
4488 (defun gnus-uncompress-marks (marks)
4489   "Uncompress the mark ranges in MARKS."
4490   (let ((uncompressed '(score bookmark))
4491         out)
4492     (while marks
4493       (if (memq (caar marks) uncompressed)
4494           (push (car marks) out)
4495         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4496       (setq marks (cdr marks)))
4497     out))
4498
4499 (defun gnus-adjust-marked-articles (info)
4500   "Set all article lists and remove all marks that are no longer valid."
4501   (let* ((marked-lists (gnus-info-marks info))
4502          (active (gnus-active (gnus-info-group info)))
4503          (min (car active))
4504          (max (cdr active))
4505          (types gnus-article-mark-lists)
4506          (uncompressed '(score bookmark killed))
4507          marks var articles article mark)
4508
4509     (while marked-lists
4510       (setq marks (pop marked-lists))
4511       (set (setq var (intern (format "gnus-newsgroup-%s"
4512                                      (car (rassq (setq mark (car marks))
4513                                                  types)))))
4514            (if (memq (car marks) uncompressed) (cdr marks)
4515              (gnus-uncompress-range (cdr marks))))
4516
4517       (setq articles (symbol-value var))
4518
4519       ;; All articles have to be subsets of the active articles.
4520       (cond
4521        ;; Adjust "simple" lists.
4522        ((memq mark '(tick dormant expire reply save))
4523         (while articles
4524           (when (or (< (setq article (pop articles)) min) (> article max))
4525             (set var (delq article (symbol-value var))))))
4526        ;; Adjust assocs.
4527        ((memq mark uncompressed)
4528         (when (not (listp (cdr (symbol-value var))))
4529           (set var (list (symbol-value var))))
4530         (when (not (listp (cdr articles)))
4531           (setq articles (list articles)))
4532         (while articles
4533           (when (or (not (consp (setq article (pop articles))))
4534                     (< (car article) min)
4535                     (> (car article) max))
4536             (set var (delq article (symbol-value var))))))))))
4537
4538 (defun gnus-update-missing-marks (missing)
4539   "Go through the list of MISSING articles and remove them from the mark lists."
4540   (when missing
4541     (let ((types gnus-article-mark-lists)
4542           var m)
4543       ;; Go through all types.
4544       (while types
4545         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4546         (when (symbol-value var)
4547           ;; This list has articles.  So we delete all missing articles
4548           ;; from it.
4549           (setq m missing)
4550           (while m
4551             (set var (delq (pop m) (symbol-value var)))))))))
4552
4553 (defun gnus-update-marks ()
4554   "Enter the various lists of marked articles into the newsgroup info list."
4555   (let ((types gnus-article-mark-lists)
4556         (info (gnus-get-info gnus-newsgroup-name))
4557         (uncompressed '(score bookmark killed))
4558         type list newmarked symbol delta-marks)
4559     (when info
4560       ;; Add all marks lists to the list of marks lists.
4561       (while (setq type (pop types))
4562         (setq list (symbol-value
4563                     (setq symbol
4564                           (intern (format "gnus-newsgroup-%s"
4565                                           (car type))))))
4566
4567         (when list
4568           ;; Get rid of the entries of the articles that have the
4569           ;; default score.
4570           (when (and (eq (cdr type) 'score)
4571                      gnus-save-score
4572                      list)
4573             (let* ((arts list)
4574                    (prev (cons nil list))
4575                    (all prev))
4576               (while arts
4577                 (if (or (not (consp (car arts)))
4578                         (= (cdar arts) gnus-summary-default-score))
4579                     (setcdr prev (cdr arts))
4580                   (setq prev arts))
4581                 (setq arts (cdr arts)))
4582               (setq list (cdr all)))))
4583
4584         (unless (memq (cdr type) uncompressed)
4585           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4586        
4587         (when (gnus-check-backend-function
4588                'request-set-mark gnus-newsgroup-name)
4589           ;; propagate flags to server, with the following exceptions:
4590           ;; uncompressed:s are not proper flags (they are cons cells)
4591           ;; cache is a internal gnus flag
4592           ;; download are local to one gnus installation (well)
4593           ;; unsend are for nndraft groups only
4594           ;; xxx: generality of this?  this suits nnimap anyway
4595           (unless (memq (cdr type) (append '(cache download unsend)
4596                                            uncompressed))
4597             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4598                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4599                    (add (gnus-remove-from-range
4600                          (gnus-copy-sequence list) old)))
4601               (when add
4602                 (push (list add 'add (list (cdr type))) delta-marks))
4603               (when del
4604                 (push (list del 'del (list (cdr type))) delta-marks)))))
4605           
4606         (when list
4607           (push (cons (cdr type) list) newmarked)))
4608
4609       (when delta-marks
4610         (unless (gnus-check-group gnus-newsgroup-name)
4611           (error "Can't open server for %s" gnus-newsgroup-name))
4612         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4613           
4614       ;; Enter these new marks into the info of the group.
4615       (if (nthcdr 3 info)
4616           (setcar (nthcdr 3 info) newmarked)
4617         ;; Add the marks lists to the end of the info.
4618         (when newmarked
4619           (setcdr (nthcdr 2 info) (list newmarked))))
4620
4621       ;; Cut off the end of the info if there's nothing else there.
4622       (let ((i 5))
4623         (while (and (> i 2)
4624                     (not (nth i info)))
4625           (when (nthcdr (decf i) info)
4626             (setcdr (nthcdr i info) nil)))))))
4627
4628 (defun gnus-set-mode-line (where)
4629   "Set the mode line of the article or summary buffers.
4630 If WHERE is `summary', the summary mode line format will be used."
4631   ;; Is this mode line one we keep updated?
4632   (when (and (memq where gnus-updated-mode-lines)
4633              (symbol-value
4634               (intern (format "gnus-%s-mode-line-format-spec" where))))
4635     (let (mode-string)
4636       (save-excursion
4637         ;; We evaluate this in the summary buffer since these
4638         ;; variables are buffer-local to that buffer.
4639         (set-buffer gnus-summary-buffer)
4640         ;; We bind all these variables that are used in the `eval' form
4641         ;; below.
4642         (let* ((mformat (symbol-value
4643                          (intern
4644                           (format "gnus-%s-mode-line-format-spec" where))))
4645                (gnus-tmp-group-name (gnus-group-name-decode 
4646                                      gnus-newsgroup-name
4647                                      (gnus-group-name-charset 
4648                                       nil
4649                                       gnus-newsgroup-name)))
4650                (gnus-tmp-article-number (or gnus-current-article 0))
4651                (gnus-tmp-unread gnus-newsgroup-unreads)
4652                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4653                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4654                (gnus-tmp-unread-and-unselected
4655                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4656                             (zerop gnus-tmp-unselected))
4657                        "")
4658                       ((zerop gnus-tmp-unselected)
4659                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4660                       (t (format "{%d(+%d) more}"
4661                                  gnus-tmp-unread-and-unticked
4662                                  gnus-tmp-unselected))))
4663                (gnus-tmp-subject
4664                 (if (and gnus-current-headers
4665                          (vectorp gnus-current-headers))
4666                     (gnus-mode-string-quote
4667                      (mail-header-subject gnus-current-headers))
4668                   ""))
4669                bufname-length max-len
4670                gnus-tmp-header);; passed as argument to any user-format-funcs
4671           (setq mode-string (eval mformat))
4672           (setq bufname-length (if (string-match "%b" mode-string)
4673                                    (- (length
4674                                        (buffer-name
4675                                         (if (eq where 'summary)
4676                                             nil
4677                                           (get-buffer gnus-article-buffer))))
4678                                       2)
4679                                  0))
4680           (setq max-len (max 4 (if gnus-mode-non-string-length
4681                                    (- (window-width)
4682                                       gnus-mode-non-string-length
4683                                       bufname-length)
4684                                  (length mode-string))))
4685           ;; We might have to chop a bit of the string off...
4686           (when (> (length mode-string) max-len)
4687             (setq mode-string
4688                   (concat (truncate-string-to-width mode-string (- max-len 3))
4689                           "...")))
4690           ;; Pad the mode string a bit.
4691           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4692       ;; Update the mode line.
4693       (setq mode-line-buffer-identification
4694             (gnus-mode-line-buffer-identification (list mode-string)))
4695       (set-buffer-modified-p t))))
4696
4697 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4698   "Go through the HEADERS list and add all Xrefs to a hash table.
4699 The resulting hash table is returned, or nil if no Xrefs were found."
4700   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4701          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4702          (xref-hashtb (gnus-make-hashtable))
4703          start group entry number xrefs header)
4704     (while headers
4705       (setq header (pop headers))
4706       (when (and (setq xrefs (mail-header-xref header))
4707                  (not (memq (setq number (mail-header-number header))
4708                             unreads)))
4709         (setq start 0)
4710         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4711           (setq start (match-end 0))
4712           (setq group (if prefix
4713                           (concat prefix (substring xrefs (match-beginning 1)
4714                                                     (match-end 1)))
4715                         (substring xrefs (match-beginning 1) (match-end 1))))
4716           (setq number
4717                 (string-to-int (substring xrefs (match-beginning 2)
4718                                           (match-end 2))))
4719           (if (setq entry (gnus-gethash group xref-hashtb))
4720               (setcdr entry (cons number (cdr entry)))
4721             (gnus-sethash group (cons number nil) xref-hashtb)))))
4722     (and start xref-hashtb)))
4723
4724 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4725   "Look through all the headers and mark the Xrefs as read."
4726   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4727         name entry info xref-hashtb idlist method nth4)
4728     (save-excursion
4729       (set-buffer gnus-group-buffer)
4730       (when (setq xref-hashtb
4731                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4732         (mapatoms
4733          (lambda (group)
4734            (unless (string= from-newsgroup (setq name (symbol-name group)))
4735              (setq idlist (symbol-value group))
4736              ;; Dead groups are not updated.
4737              (and (prog1
4738                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4739                             info (nth 2 entry))
4740                     (when (stringp (setq nth4 (gnus-info-method info)))
4741                       (setq nth4 (gnus-server-to-method nth4))))
4742                   ;; Only do the xrefs if the group has the same
4743                   ;; select method as the group we have just read.
4744                   (or (gnus-methods-equal-p
4745                        nth4 (gnus-find-method-for-group from-newsgroup))
4746                       virtual
4747                       (equal nth4 (setq method (gnus-find-method-for-group
4748                                                 from-newsgroup)))
4749                       (and (equal (car nth4) (car method))
4750                            (equal (nth 1 nth4) (nth 1 method))))
4751                   gnus-use-cross-reference
4752                   (or (not (eq gnus-use-cross-reference t))
4753                       virtual
4754                       ;; Only do cross-references on subscribed
4755                       ;; groups, if that is what is wanted.
4756                       (<= (gnus-info-level info) gnus-level-subscribed))
4757                   (gnus-group-make-articles-read name idlist))))
4758          xref-hashtb)))))
4759
4760 (defun gnus-compute-read-articles (group articles)
4761   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4762          (info (nth 2 entry))
4763          (active (gnus-active group))
4764          ninfo)
4765     (when entry
4766       ;; First peel off all invalid article numbers.
4767       (when active
4768         (let ((ids articles)
4769               id first)
4770           (while (setq id (pop ids))
4771             (when (and first (> id (cdr active)))
4772               ;; We'll end up in this situation in one particular
4773               ;; obscure situation.  If you re-scan a group and get
4774               ;; a new article that is cross-posted to a different
4775               ;; group that has not been re-scanned, you might get
4776               ;; crossposted article that has a higher number than
4777               ;; Gnus believes possible.  So we re-activate this
4778               ;; group as well.  This might mean doing the
4779               ;; crossposting thingy will *increase* the number
4780               ;; of articles in some groups.  Tsk, tsk.
4781               (setq active (or (gnus-activate-group group) active)))
4782             (when (or (> id (cdr active))
4783                       (< id (car active)))
4784               (setq articles (delq id articles))))))
4785       ;; If the read list is nil, we init it.
4786       (if (and active
4787                (null (gnus-info-read info))
4788                (> (car active) 1))
4789           (setq ninfo (cons 1 (1- (car active))))
4790         (setq ninfo (gnus-info-read info)))
4791       ;; Then we add the read articles to the range.
4792       (gnus-add-to-range
4793        ninfo (setq articles (sort articles '<))))))
4794
4795 (defun gnus-group-make-articles-read (group articles)
4796   "Update the info of GROUP to say that ARTICLES are read."
4797   (let* ((num 0)
4798          (entry (gnus-gethash group gnus-newsrc-hashtb))
4799          (info (nth 2 entry))
4800          (active (gnus-active group))
4801          range)
4802     (when entry
4803       (setq range (gnus-compute-read-articles group articles))
4804       (save-excursion
4805         (set-buffer gnus-group-buffer)
4806         (gnus-undo-register
4807           `(progn
4808              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4809              (gnus-info-set-read ',info ',(gnus-info-read info))
4810              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4811              (gnus-group-update-group ,group t))))
4812       ;; Add the read articles to the range.
4813       (gnus-info-set-read info range)
4814       ;; Then we have to re-compute how many unread
4815       ;; articles there are in this group.
4816       (when active
4817         (cond
4818          ((not range)
4819           (setq num (- (1+ (cdr active)) (car active))))
4820          ((not (listp (cdr range)))
4821           (setq num (- (cdr active) (- (1+ (cdr range))
4822                                        (car range)))))
4823          (t
4824           (while range
4825             (if (numberp (car range))
4826                 (setq num (1+ num))
4827               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4828             (setq range (cdr range)))
4829           (setq num (- (cdr active) num))))
4830         ;; Update the number of unread articles.
4831         (setcar entry num)
4832         ;; Update the group buffer.
4833         (gnus-group-update-group group t)))))
4834
4835 (defvar gnus-newsgroup-none-id 0)
4836
4837 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4838   (let ((cur nntp-server-buffer)
4839         (dependencies
4840          (or dependencies
4841              (save-excursion (set-buffer gnus-summary-buffer)
4842                              gnus-newsgroup-dependencies)))
4843         headers id end ref
4844         (mail-parse-charset gnus-newsgroup-charset)
4845         (mail-parse-ignored-charsets
4846          (save-excursion (condition-case nil
4847                              (set-buffer gnus-summary-buffer)
4848                            (error))
4849                          gnus-newsgroup-ignored-charsets)))
4850     (save-excursion
4851       (set-buffer nntp-server-buffer)
4852       ;; Translate all TAB characters into SPACE characters.
4853       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4854       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4855       (gnus-run-hooks 'gnus-parse-headers-hook)
4856       (let ((case-fold-search t)
4857             in-reply-to header p lines chars)
4858         (goto-char (point-min))
4859         ;; Search to the beginning of the next header.  Error messages
4860         ;; do not begin with 2 or 3.
4861         (while (re-search-forward "^[23][0-9]+ " nil t)
4862           (setq id nil
4863                 ref nil)
4864           ;; This implementation of this function, with nine
4865           ;; search-forwards instead of the one re-search-forward and
4866           ;; a case (which basically was the old function) is actually
4867           ;; about twice as fast, even though it looks messier.  You
4868           ;; can't have everything, I guess.  Speed and elegance
4869           ;; doesn't always go hand in hand.
4870           (setq
4871            header
4872            (vector
4873             ;; Number.
4874             (prog1
4875                 (read cur)
4876               (end-of-line)
4877               (setq p (point))
4878               (narrow-to-region (point)
4879                                 (or (and (search-forward "\n.\n" nil t)
4880                                          (- (point) 2))
4881                                     (point))))
4882             ;; Subject.
4883             (progn
4884               (goto-char p)
4885               (if (search-forward "\nsubject: " nil t)
4886                   (funcall gnus-decode-encoded-word-function
4887                            (nnheader-header-value))
4888                 "(none)"))
4889             ;; From.
4890             (progn
4891               (goto-char p)
4892               (if (or (search-forward "\nfrom: " nil t)
4893                       (search-forward "\nfrom:" nil t))
4894                   (funcall gnus-decode-encoded-word-function
4895                            (nnheader-header-value))
4896                 "(nobody)"))
4897             ;; Date.
4898             (progn
4899               (goto-char p)
4900               (if (search-forward "\ndate: " nil t)
4901                   (nnheader-header-value) ""))
4902             ;; Message-ID.
4903             (progn
4904               (goto-char p)
4905               (setq id (if (re-search-forward
4906                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4907                            ;; We do it this way to make sure the Message-ID
4908                            ;; is (somewhat) syntactically valid.
4909                            (buffer-substring (match-beginning 1)
4910                                              (match-end 1))
4911                          ;; If there was no message-id, we just fake one
4912                          ;; to make subsequent routines simpler.
4913                          (nnheader-generate-fake-message-id))))
4914             ;; References.
4915             (progn
4916               (goto-char p)
4917               (if (search-forward "\nreferences: " nil t)
4918                   (progn
4919                     (setq end (point))
4920                     (prog1
4921                         (nnheader-header-value)
4922                       (setq ref
4923                             (buffer-substring
4924                              (progn
4925                                (end-of-line)
4926                                (search-backward ">" end t)
4927                                (1+ (point)))
4928                              (progn
4929                                (search-backward "<" end t)
4930                                (point))))))
4931                 ;; Get the references from the in-reply-to header if there
4932                 ;; were no references and the in-reply-to header looks
4933                 ;; promising.
4934                 (if (and (search-forward "\nin-reply-to: " nil t)
4935                          (setq in-reply-to (nnheader-header-value))
4936                          (string-match "<[^>]+>" in-reply-to))
4937                     (let (ref2)
4938                       (setq ref (substring in-reply-to (match-beginning 0)
4939                                            (match-end 0)))
4940                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4941                         (setq ref2 (substring in-reply-to (match-beginning 0)
4942                                               (match-end 0)))
4943                         (when (> (length ref2) (length ref))
4944                           (setq ref ref2)))
4945                       ref)
4946                   (setq ref nil))))
4947             ;; Chars.
4948             (progn
4949               (goto-char p)
4950               (if (search-forward "\nchars: " nil t)
4951                   (if (numberp (setq chars (ignore-errors (read cur))))
4952                       chars 0)
4953                 0))
4954             ;; Lines.
4955             (progn
4956               (goto-char p)
4957               (if (search-forward "\nlines: " nil t)
4958                   (if (numberp (setq lines (ignore-errors (read cur))))
4959                       lines 0)
4960                 0))
4961             ;; Xref.
4962             (progn
4963               (goto-char p)
4964               (and (search-forward "\nxref: " nil t)
4965                    (nnheader-header-value)))
4966             ;; Extra.
4967             (when gnus-extra-headers
4968               (let ((extra gnus-extra-headers)
4969                     out)
4970                 (while extra
4971                   (goto-char p)
4972                   (when (search-forward
4973                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4974                     (push (cons (car extra) (nnheader-header-value))
4975                           out))
4976                   (pop extra))
4977                 out))))
4978           (when (equal id ref)
4979             (setq ref nil))
4980
4981           (when gnus-alter-header-function
4982             (funcall gnus-alter-header-function header)
4983             (setq id (mail-header-id header)
4984                   ref (gnus-parent-id (mail-header-references header))))
4985
4986           (when (setq header
4987                       (gnus-dependencies-add-header
4988                        header dependencies force-new))
4989             (push header headers))
4990           (goto-char (point-max))
4991           (widen))
4992         (nreverse headers)))))
4993
4994 ;; Goes through the xover lines and returns a list of vectors
4995 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4996                                                   force-new dependencies
4997                                                   group also-fetch-heads)
4998   "Parse the news overview data in the server buffer.
4999 Return a list of headers that match SEQUENCE (see
5000 `nntp-retrieve-headers')."
5001   ;; Get the Xref when the users reads the articles since most/some
5002   ;; NNTP servers do not include Xrefs when using XOVER.
5003   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5004   (let ((mail-parse-charset gnus-newsgroup-charset)
5005         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5006         (cur nntp-server-buffer)
5007         (dependencies (or dependencies gnus-newsgroup-dependencies))
5008         number headers header)
5009     (save-excursion
5010       (set-buffer nntp-server-buffer)
5011       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5012       ;; Allow the user to mangle the headers before parsing them.
5013       (gnus-run-hooks 'gnus-parse-headers-hook)
5014       (goto-char (point-min))
5015       (while (not (eobp))
5016         (condition-case ()
5017             (while (and sequence (not (eobp)))
5018               (setq number (read cur))
5019               (while (and sequence
5020                           (< (car sequence) number))
5021                 (setq sequence (cdr sequence)))
5022               (and sequence
5023                    (eq number (car sequence))
5024                    (progn
5025                      (setq sequence (cdr sequence))
5026                      (setq header (inline
5027                                     (gnus-nov-parse-line
5028                                      number dependencies force-new))))
5029                    (push header headers))
5030               (forward-line 1))
5031           (error
5032            (gnus-error 4 "Strange nov line (%d)"
5033                        (count-lines (point-min) (point)))))
5034         (forward-line 1))
5035       ;; A common bug in inn is that if you have posted an article and
5036       ;; then retrieves the active file, it will answer correctly --
5037       ;; the new article is included.  However, a NOV entry for the
5038       ;; article may not have been generated yet, so this may fail.
5039       ;; We work around this problem by retrieving the last few
5040       ;; headers using HEAD.
5041       (if (or (not also-fetch-heads)
5042               (not sequence))
5043           ;; We (probably) got all the headers.
5044           (nreverse headers)
5045         (let ((gnus-nov-is-evil t))
5046           (nconc
5047            (nreverse headers)
5048            (when (gnus-retrieve-headers sequence group)
5049              (gnus-get-newsgroup-headers))))))))
5050
5051 (defun gnus-article-get-xrefs ()
5052   "Fill in the Xref value in `gnus-current-headers', if necessary.
5053 This is meant to be called in `gnus-article-internal-prepare-hook'."
5054   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5055                                  gnus-current-headers)))
5056     (or (not gnus-use-cross-reference)
5057         (not headers)
5058         (and (mail-header-xref headers)
5059              (not (string= (mail-header-xref headers) "")))
5060         (let ((case-fold-search t)
5061               xref)
5062           (save-restriction
5063             (nnheader-narrow-to-headers)
5064             (goto-char (point-min))
5065             (when (or (and (not (eobp))
5066                            (eq (downcase (char-after)) ?x)
5067                            (looking-at "Xref:"))
5068                       (search-forward "\nXref:" nil t))
5069               (goto-char (1+ (match-end 0)))
5070               (setq xref (buffer-substring (point)
5071                                            (progn (end-of-line) (point))))
5072               (mail-header-set-xref headers xref)))))))
5073
5074 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5075   "Find article ID and insert the summary line for that article.
5076 OLD-HEADER can either be a header or a line number to insert
5077 the subject line on."
5078   (let* ((line (and (numberp old-header) old-header))
5079          (old-header (and (vectorp old-header) old-header))
5080          (header (cond ((and old-header use-old-header)
5081                         old-header)
5082                        ((and (numberp id)
5083                              (gnus-number-to-header id))
5084                         (gnus-number-to-header id))
5085                        (t
5086                         (gnus-read-header id))))
5087          (number (and (numberp id) id))
5088          d)
5089     (when header
5090       ;; Rebuild the thread that this article is part of and go to the
5091       ;; article we have fetched.
5092       (when (and (not gnus-show-threads)
5093                  old-header)
5094         (when (and number
5095                    (setq d (gnus-data-find (mail-header-number old-header))))
5096           (goto-char (gnus-data-pos d))
5097           (gnus-data-remove
5098            number
5099            (- (gnus-point-at-bol)
5100               (prog1
5101                   (1+ (gnus-point-at-eol))
5102                 (gnus-delete-line))))))
5103       (when old-header
5104         (mail-header-set-number header (mail-header-number old-header)))
5105       (setq gnus-newsgroup-sparse
5106             (delq (setq number (mail-header-number header))
5107                   gnus-newsgroup-sparse))
5108       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5109       (push number gnus-newsgroup-limit)
5110       (gnus-rebuild-thread (mail-header-id header) line)
5111       (gnus-summary-goto-subject number nil t))
5112     (when (and (numberp number)
5113                (> number 0))
5114       ;; We have to update the boundaries even if we can't fetch the
5115       ;; article if ID is a number -- so that the next `P' or `N'
5116       ;; command will fetch the previous (or next) article even
5117       ;; if the one we tried to fetch this time has been canceled.
5118       (when (> number gnus-newsgroup-end)
5119         (setq gnus-newsgroup-end number))
5120       (when (< number gnus-newsgroup-begin)
5121         (setq gnus-newsgroup-begin number))
5122       (setq gnus-newsgroup-unselected
5123             (delq number gnus-newsgroup-unselected)))
5124     ;; Report back a success?
5125     (and header (mail-header-number header))))
5126
5127 ;;; Process/prefix in the summary buffer
5128
5129 (defun gnus-summary-work-articles (n)
5130   "Return a list of articles to be worked upon.
5131 The prefix argument, the list of process marked articles, and the
5132 current article will be taken into consideration."
5133   (save-excursion
5134     (set-buffer gnus-summary-buffer)
5135     (cond
5136      (n
5137       ;; A numerical prefix has been given.
5138       (setq n (prefix-numeric-value n))
5139       (let ((backward (< n 0))
5140             (n (abs (prefix-numeric-value n)))
5141             articles article)
5142         (save-excursion
5143           (while
5144               (and (> n 0)
5145                    (push (setq article (gnus-summary-article-number))
5146                          articles)
5147                    (if backward
5148                        (gnus-summary-find-prev nil article)
5149                      (gnus-summary-find-next nil article)))
5150             (decf n)))
5151         (nreverse articles)))
5152      ((and (gnus-region-active-p) (mark))
5153       (message "region active")
5154       ;; Work on the region between point and mark.
5155       (let ((max (max (point) (mark)))
5156             articles article)
5157         (save-excursion
5158           (goto-char (min (min (point) (mark))))
5159           (while
5160               (and
5161                (push (setq article (gnus-summary-article-number)) articles)
5162                (gnus-summary-find-next nil article)
5163                (< (point) max)))
5164           (nreverse articles))))
5165      (gnus-newsgroup-processable
5166       ;; There are process-marked articles present.
5167       ;; Save current state.
5168       (gnus-summary-save-process-mark)
5169       ;; Return the list.
5170       (reverse gnus-newsgroup-processable))
5171      (t
5172       ;; Just return the current article.
5173       (list (gnus-summary-article-number))))))
5174
5175 (defmacro gnus-summary-iterate (arg &rest forms)
5176   "Iterate over the process/prefixed articles and do FORMS.
5177 ARG is the interactive prefix given to the command.  FORMS will be
5178 executed with point over the summary line of the articles."
5179   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5180     `(let ((,articles (gnus-summary-work-articles ,arg)))
5181        (while ,articles
5182          (gnus-summary-goto-subject (car ,articles))
5183          ,@forms
5184          (pop ,articles)))))
5185
5186 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5187 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5188
5189 (defun gnus-summary-save-process-mark ()
5190   "Push the current set of process marked articles on the stack."
5191   (interactive)
5192   (push (copy-sequence gnus-newsgroup-processable)
5193         gnus-newsgroup-process-stack))
5194
5195 (defun gnus-summary-kill-process-mark ()
5196   "Push the current set of process marked articles on the stack and unmark."
5197   (interactive)
5198   (gnus-summary-save-process-mark)
5199   (gnus-summary-unmark-all-processable))
5200
5201 (defun gnus-summary-yank-process-mark ()
5202   "Pop the last process mark state off the stack and restore it."
5203   (interactive)
5204   (unless gnus-newsgroup-process-stack
5205     (error "Empty mark stack"))
5206   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5207
5208 (defun gnus-summary-process-mark-set (set)
5209   "Make SET into the current process marked articles."
5210   (gnus-summary-unmark-all-processable)
5211   (while set
5212     (gnus-summary-set-process-mark (pop set))))
5213
5214 ;;; Searching and stuff
5215
5216 (defun gnus-summary-search-group (&optional backward use-level)
5217   "Search for next unread newsgroup.
5218 If optional argument BACKWARD is non-nil, search backward instead."
5219   (save-excursion
5220     (set-buffer gnus-group-buffer)
5221     (when (gnus-group-search-forward
5222            backward nil (if use-level (gnus-group-group-level) nil))
5223       (gnus-group-group-name))))
5224
5225 (defun gnus-summary-best-group (&optional exclude-group)
5226   "Find the name of the best unread group.
5227 If EXCLUDE-GROUP, do not go to this group."
5228   (save-excursion
5229     (set-buffer gnus-group-buffer)
5230     (save-excursion
5231       (gnus-group-best-unread-group exclude-group))))
5232
5233 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5234   (if backward (gnus-summary-find-prev)
5235     (let* ((dummy (gnus-summary-article-intangible-p))
5236            (article (or article (gnus-summary-article-number)))
5237            (arts (gnus-data-find-list article))
5238            result)
5239       (when (and (not dummy)
5240                  (or (not gnus-summary-check-current)
5241                      (not unread)
5242                      (not (gnus-data-unread-p (car arts)))))
5243         (setq arts (cdr arts)))
5244       (when (setq result
5245                   (if unread
5246                       (progn
5247                         (while arts
5248                           (when (or (and undownloaded
5249                                          (eq gnus-undownloaded-mark
5250                                              (gnus-data-mark (car arts))))
5251                                     (gnus-data-unread-p (car arts)))
5252                             (setq result (car arts)
5253                                   arts nil))
5254                           (setq arts (cdr arts)))
5255                         result)
5256                     (car arts)))
5257         (goto-char (gnus-data-pos result))
5258         (gnus-data-number result)))))
5259
5260 (defun gnus-summary-find-prev (&optional unread article)
5261   (let* ((eobp (eobp))
5262          (article (or article (gnus-summary-article-number)))
5263          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5264          result)
5265     (when (and (not eobp)
5266                (or (not gnus-summary-check-current)
5267                    (not unread)
5268                    (not (gnus-data-unread-p (car arts)))))
5269       (setq arts (cdr arts)))
5270     (when (setq result
5271                 (if unread
5272                     (progn
5273                       (while arts
5274                         (when (gnus-data-unread-p (car arts))
5275                           (setq result (car arts)
5276                                 arts nil))
5277                         (setq arts (cdr arts)))
5278                       result)
5279                   (car arts)))
5280       (goto-char (gnus-data-pos result))
5281       (gnus-data-number result))))
5282
5283 (defun gnus-summary-find-subject (subject &optional unread backward article)
5284   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5285          (article (or article (gnus-summary-article-number)))
5286          (articles (gnus-data-list backward))
5287          (arts (gnus-data-find-list article articles))
5288          result)
5289     (when (or (not gnus-summary-check-current)
5290               (not unread)
5291               (not (gnus-data-unread-p (car arts))))
5292       (setq arts (cdr arts)))
5293     (while arts
5294       (and (or (not unread)
5295                (gnus-data-unread-p (car arts)))
5296            (vectorp (gnus-data-header (car arts)))
5297            (gnus-subject-equal
5298             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5299            (setq result (car arts)
5300                  arts nil))
5301       (setq arts (cdr arts)))
5302     (and result
5303          (goto-char (gnus-data-pos result))
5304          (gnus-data-number result))))
5305
5306 (defun gnus-summary-search-forward (&optional unread subject backward)
5307   "Search forward for an article.
5308 If UNREAD, look for unread articles.  If SUBJECT, look for
5309 articles with that subject.  If BACKWARD, search backward instead."
5310   (cond (subject (gnus-summary-find-subject subject unread backward))
5311         (backward (gnus-summary-find-prev unread))
5312         (t (gnus-summary-find-next unread))))
5313
5314 (defun gnus-recenter (&optional n)
5315   "Center point in window and redisplay frame.
5316 Also do horizontal recentering."
5317   (interactive "P")
5318   (when (and gnus-auto-center-summary
5319              (not (eq gnus-auto-center-summary 'vertical)))
5320     (gnus-horizontal-recenter))
5321   (recenter n))
5322
5323 (defun gnus-summary-recenter ()
5324   "Center point in the summary window.
5325 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5326 displayed, no centering will be performed."
5327   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5328   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5329   (interactive)
5330   (let* ((top (cond ((< (window-height) 4) 0)
5331                     ((< (window-height) 7) 1)
5332                     (t (if (numberp gnus-auto-center-summary)
5333                            gnus-auto-center-summary
5334                          2))))
5335          (height (1- (window-height)))
5336          (bottom (save-excursion (goto-char (point-max))
5337                                  (forward-line (- height))
5338                                  (point)))
5339          (window (get-buffer-window (current-buffer))))
5340     ;; The user has to want it.
5341     (when gnus-auto-center-summary
5342       (when (get-buffer-window gnus-article-buffer)
5343         ;; Only do recentering when the article buffer is displayed,
5344         ;; Set the window start to either `bottom', which is the biggest
5345         ;; possible valid number, or the second line from the top,
5346         ;; whichever is the least.
5347         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5348           (if (> bottom top-pos)
5349               ;; Keep the second line from the top visible
5350               (set-window-start window top-pos t)
5351             ;; Try to keep the bottom line visible; if it's partially
5352             ;; obscured, either scroll one more line to make it fully
5353             ;; visible, or revert to using TOP-POS.
5354             (save-excursion
5355               (goto-char (point-max))
5356               (forward-line -1)
5357               (let ((last-line-start (point)))
5358                 (goto-char bottom)
5359                 (set-window-start window (point) t)
5360                 (when (not (pos-visible-in-window-p last-line-start window))
5361                   (forward-line 1)
5362                   (set-window-start window (min (point) top-pos) t)))))))
5363       ;; Do horizontal recentering while we're at it.
5364       (when (and (get-buffer-window (current-buffer) t)
5365                  (not (eq gnus-auto-center-summary 'vertical)))
5366         (let ((selected (selected-window)))
5367           (select-window (get-buffer-window (current-buffer) t))
5368           (gnus-summary-position-point)
5369           (gnus-horizontal-recenter)
5370           (select-window selected))))))
5371
5372 (defun gnus-summary-jump-to-group (newsgroup)
5373   "Move point to NEWSGROUP in group mode buffer."
5374   ;; Keep update point of group mode buffer if visible.
5375   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5376       (save-window-excursion
5377         ;; Take care of tree window mode.
5378         (when (get-buffer-window gnus-group-buffer)
5379           (pop-to-buffer gnus-group-buffer))
5380         (gnus-group-jump-to-group newsgroup))
5381     (save-excursion
5382       ;; Take care of tree window mode.
5383       (if (get-buffer-window gnus-group-buffer)
5384           (pop-to-buffer gnus-group-buffer)
5385         (set-buffer gnus-group-buffer))
5386       (gnus-group-jump-to-group newsgroup))))
5387
5388 ;; This function returns a list of article numbers based on the
5389 ;; difference between the ranges of read articles in this group and
5390 ;; the range of active articles.
5391 (defun gnus-list-of-unread-articles (group)
5392   (let* ((read (gnus-info-read (gnus-get-info group)))
5393          (active (or (gnus-active group) (gnus-activate-group group)))
5394          (last (cdr active))
5395          first nlast unread)
5396     ;; If none are read, then all are unread.
5397     (if (not read)
5398         (setq first (car active))
5399       ;; If the range of read articles is a single range, then the
5400       ;; first unread article is the article after the last read
5401       ;; article.  Sounds logical, doesn't it?
5402       (if (and (not (listp (cdr read)))
5403                (or (< (car read) (car active))
5404                    (progn (setq read (list read))
5405                           nil)))
5406           (setq first (max (car active) (1+ (cdr read))))
5407         ;; `read' is a list of ranges.
5408         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5409                                   (caar read)))
5410                   1)
5411           (setq first (car active)))
5412         (while read
5413           (when first
5414             (while (< first nlast)
5415               (push first unread)
5416               (setq first (1+ first))))
5417           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5418           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5419           (setq read (cdr read)))))
5420     ;; And add the last unread articles.
5421     (while (<= first last)
5422       (push first unread)
5423       (setq first (1+ first)))
5424     ;; Return the list of unread articles.
5425     (delq 0 (nreverse unread))))
5426
5427 (defun gnus-list-of-read-articles (group)
5428   "Return a list of unread, unticked and non-dormant articles."
5429   (let* ((info (gnus-get-info group))
5430          (marked (gnus-info-marks info))
5431          (active (gnus-active group)))
5432     (and info active
5433          (gnus-set-difference
5434           (gnus-sorted-complement
5435            (gnus-uncompress-range active)
5436            (gnus-list-of-unread-articles group))
5437           (append
5438            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5439            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5440
5441 ;; Various summary commands
5442
5443 (defun gnus-summary-select-article-buffer ()
5444   "Reconfigure windows to show article buffer."
5445   (interactive)
5446   (if (not (gnus-buffer-live-p gnus-article-buffer))
5447       (error "There is no article buffer for this summary buffer")
5448     (gnus-configure-windows 'article)
5449     (select-window (get-buffer-window gnus-article-buffer))))
5450
5451 (defun gnus-summary-universal-argument (arg)
5452   "Perform any operation on all articles that are process/prefixed."
5453   (interactive "P")
5454   (let ((articles (gnus-summary-work-articles arg))
5455         func article)
5456     (if (eq
5457          (setq
5458           func
5459           (key-binding
5460            (read-key-sequence
5461             (substitute-command-keys
5462              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5463          'undefined)
5464         (gnus-error 1 "Undefined key")
5465       (save-excursion
5466         (while articles
5467           (gnus-summary-goto-subject (setq article (pop articles)))
5468           (let (gnus-newsgroup-processable)
5469             (command-execute func))
5470           (gnus-summary-remove-process-mark article)))))
5471   (gnus-summary-position-point))
5472
5473 (defun gnus-summary-toggle-truncation (&optional arg)
5474   "Toggle truncation of summary lines.
5475 With arg, turn line truncation on iff arg is positive."
5476   (interactive "P")
5477   (setq truncate-lines
5478         (if (null arg) (not truncate-lines)
5479           (> (prefix-numeric-value arg) 0)))
5480   (redraw-display))
5481
5482 (defun gnus-summary-reselect-current-group (&optional all rescan)
5483   "Exit and then reselect the current newsgroup.
5484 The prefix argument ALL means to select all articles."
5485   (interactive "P")
5486   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5487     (error "Ephemeral groups can't be reselected"))
5488   (let ((current-subject (gnus-summary-article-number))
5489         (group gnus-newsgroup-name))
5490     (setq gnus-newsgroup-begin nil)
5491     (gnus-summary-exit)
5492     ;; We have to adjust the point of group mode buffer because
5493     ;; point was moved to the next unread newsgroup by exiting.
5494     (gnus-summary-jump-to-group group)
5495     (when rescan
5496       (save-excursion
5497         (gnus-group-get-new-news-this-group 1)))
5498     (gnus-group-read-group all t)
5499     (gnus-summary-goto-subject current-subject nil t)))
5500
5501 (defun gnus-summary-rescan-group (&optional all)
5502   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5503   (interactive "P")
5504   (gnus-summary-reselect-current-group all t))
5505
5506 (defun gnus-summary-update-info (&optional non-destructive)
5507   (save-excursion
5508     (let ((group gnus-newsgroup-name))
5509       (when group
5510         (when gnus-newsgroup-kill-headers
5511           (setq gnus-newsgroup-killed
5512                 (gnus-compress-sequence
5513                  (nconc
5514                   (gnus-set-sorted-intersection
5515                    (gnus-uncompress-range gnus-newsgroup-killed)
5516                    (setq gnus-newsgroup-unselected
5517                          (sort gnus-newsgroup-unselected '<)))
5518                   (setq gnus-newsgroup-unreads
5519                         (sort gnus-newsgroup-unreads '<)))
5520                  t)))
5521         (unless (listp (cdr gnus-newsgroup-killed))
5522           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5523         (let ((headers gnus-newsgroup-headers))
5524           ;; Set the new ranges of read articles.
5525           (save-excursion
5526             (set-buffer gnus-group-buffer)
5527             (gnus-undo-force-boundary))
5528           (gnus-update-read-articles
5529            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5530           ;; Set the current article marks.
5531           (let ((gnus-newsgroup-scored
5532                  (if (and (not gnus-save-score)
5533                           (not non-destructive))
5534                      nil
5535                    gnus-newsgroup-scored)))
5536             (save-excursion
5537               (gnus-update-marks)))
5538           ;; Do the cross-ref thing.
5539           (when gnus-use-cross-reference
5540             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5541           ;; Do not switch windows but change the buffer to work.
5542           (set-buffer gnus-group-buffer)
5543           (unless (gnus-ephemeral-group-p group)
5544             (gnus-group-update-group group)))))))
5545
5546 (defun gnus-summary-save-newsrc (&optional force)
5547   "Save the current number of read/marked articles in the dribble buffer.
5548 The dribble buffer will then be saved.
5549 If FORCE (the prefix), also save the .newsrc file(s)."
5550   (interactive "P")
5551   (gnus-summary-update-info t)
5552   (if force
5553       (gnus-save-newsrc-file)
5554     (gnus-dribble-save)))
5555
5556 (defun gnus-summary-exit (&optional temporary)
5557   "Exit reading current newsgroup, and then return to group selection mode.
5558 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5559   (interactive)
5560   (gnus-set-global-variables)
5561   (when (gnus-buffer-live-p gnus-article-buffer)
5562     (save-excursion
5563       (set-buffer gnus-article-buffer)
5564       (mm-destroy-parts gnus-article-mime-handles)
5565       ;; Set it to nil for safety reason.
5566       (setq gnus-article-mime-handle-alist nil)
5567       (setq gnus-article-mime-handles nil)))
5568   (gnus-kill-save-kill-buffer)
5569   (gnus-async-halt-prefetch)
5570   (let* ((group gnus-newsgroup-name)
5571          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5572          (mode major-mode)
5573          (group-point nil)
5574          (buf (current-buffer)))
5575     (unless quit-config
5576       ;; Do adaptive scoring, and possibly save score files.
5577       (when gnus-newsgroup-adaptive
5578         (gnus-score-adaptive))
5579       (when gnus-use-scoring
5580         (gnus-score-save)))
5581     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5582     ;; If we have several article buffers, we kill them at exit.
5583     (unless gnus-single-article-buffer
5584       (gnus-kill-buffer gnus-original-article-buffer)
5585       (setq gnus-article-current nil))
5586     (when gnus-use-cache
5587       (gnus-cache-possibly-remove-articles)
5588       (gnus-cache-save-buffers))
5589     (gnus-async-prefetch-remove-group group)
5590     (when gnus-suppress-duplicates
5591       (gnus-dup-enter-articles))
5592     (when gnus-use-trees
5593       (gnus-tree-close group))
5594     (when gnus-use-cache
5595       (gnus-cache-write-active))
5596     ;; Remove entries for this group.
5597     (nnmail-purge-split-history (gnus-group-real-name group))
5598     ;; Make all changes in this group permanent.
5599     (unless quit-config
5600       (gnus-run-hooks 'gnus-exit-group-hook)
5601       (gnus-summary-update-info))
5602     (gnus-close-group group)
5603     ;; Make sure where we were, and go to next newsgroup.
5604     (set-buffer gnus-group-buffer)
5605     (unless quit-config
5606       (gnus-group-jump-to-group group))
5607     (gnus-run-hooks 'gnus-summary-exit-hook)
5608     (unless (or quit-config
5609                 ;; If this group has disappeared from the summary
5610                 ;; buffer, don't skip forwards.
5611                 (not (string= group (gnus-group-group-name))))
5612       (gnus-group-next-unread-group 1))
5613     (setq group-point (point))
5614     (if temporary
5615         nil                             ;Nothing to do.
5616       ;; If we have several article buffers, we kill them at exit.
5617       (unless gnus-single-article-buffer
5618         (gnus-kill-buffer gnus-article-buffer)
5619         (gnus-kill-buffer gnus-original-article-buffer)
5620         (setq gnus-article-current nil))
5621       (set-buffer buf)
5622       (if (not gnus-kill-summary-on-exit)
5623           (gnus-deaden-summary)
5624         ;; We set all buffer-local variables to nil.  It is unclear why
5625         ;; this is needed, but if we don't, buffer-local variables are
5626         ;; not garbage-collected, it seems.  This would the lead to en
5627         ;; ever-growing Emacs.
5628         (gnus-summary-clear-local-variables)
5629         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5630           (gnus-summary-clear-local-variables))
5631         (when (get-buffer gnus-article-buffer)
5632           (bury-buffer gnus-article-buffer))
5633         ;; We clear the global counterparts of the buffer-local
5634         ;; variables as well, just to be on the safe side.
5635         (set-buffer gnus-group-buffer)
5636         (gnus-summary-clear-local-variables)
5637         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5638           (gnus-summary-clear-local-variables))
5639         ;; Return to group mode buffer.
5640         (when (eq mode 'gnus-summary-mode)
5641           (gnus-kill-buffer buf)))
5642       (setq gnus-current-select-method gnus-select-method)
5643       (pop-to-buffer gnus-group-buffer)
5644       (if (not quit-config)
5645           (progn
5646             (goto-char group-point)
5647             (gnus-configure-windows 'group 'force))
5648         (gnus-handle-ephemeral-exit quit-config))
5649       ;; Clear the current group name.
5650       (unless quit-config
5651         (setq gnus-newsgroup-name nil)))))
5652
5653 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5654 (defun gnus-summary-exit-no-update (&optional no-questions)
5655   "Quit reading current newsgroup without updating read article info."
5656   (interactive)
5657   (let* ((group gnus-newsgroup-name)
5658          (quit-config (gnus-group-quit-config group)))
5659     (when (or no-questions
5660               gnus-expert-user
5661               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5662       (gnus-async-halt-prefetch)
5663       (mapcar 'funcall
5664               (delq 'gnus-summary-expire-articles
5665                     (copy-sequence gnus-summary-prepare-exit-hook)))
5666       (when (gnus-buffer-live-p gnus-article-buffer)
5667         (save-excursion
5668           (set-buffer gnus-article-buffer)
5669           (mm-destroy-parts gnus-article-mime-handles)
5670           ;; Set it to nil for safety reason.
5671           (setq gnus-article-mime-handle-alist nil)
5672           (setq gnus-article-mime-handles nil)))
5673       ;; If we have several article buffers, we kill them at exit.
5674       (unless gnus-single-article-buffer
5675         (gnus-kill-buffer gnus-article-buffer)
5676         (gnus-kill-buffer gnus-original-article-buffer)
5677         (setq gnus-article-current nil))
5678       (if (not gnus-kill-summary-on-exit)
5679           (gnus-deaden-summary)
5680         (gnus-close-group group)
5681         (gnus-summary-clear-local-variables)
5682         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5683           (gnus-summary-clear-local-variables))
5684         (set-buffer gnus-group-buffer)
5685         (gnus-summary-clear-local-variables)
5686         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5687           (gnus-summary-clear-local-variables))
5688         (when (get-buffer gnus-summary-buffer)
5689           (kill-buffer gnus-summary-buffer)))
5690       (unless gnus-single-article-buffer
5691         (setq gnus-article-current nil))
5692       (when gnus-use-trees
5693         (gnus-tree-close group))
5694       (gnus-async-prefetch-remove-group group)
5695       (when (get-buffer gnus-article-buffer)
5696         (bury-buffer gnus-article-buffer))
5697       ;; Return to the group buffer.
5698       (gnus-configure-windows 'group 'force)
5699       ;; Clear the current group name.
5700       (setq gnus-newsgroup-name nil)
5701       (when (equal (gnus-group-group-name) group)
5702         (gnus-group-next-unread-group 1))
5703       (when quit-config
5704         (gnus-handle-ephemeral-exit quit-config)))))
5705
5706 (defun gnus-handle-ephemeral-exit (quit-config)
5707   "Handle movement when leaving an ephemeral group.
5708 The state which existed when entering the ephemeral is reset."
5709   (if (not (buffer-name (car quit-config)))
5710       (gnus-configure-windows 'group 'force)
5711     (set-buffer (car quit-config))
5712     (cond ((eq major-mode 'gnus-summary-mode)
5713            (gnus-set-global-variables))
5714           ((eq major-mode 'gnus-article-mode)
5715            (save-excursion
5716              ;; The `gnus-summary-buffer' variable may point
5717              ;; to the old summary buffer when using a single
5718              ;; article buffer.
5719              (unless (gnus-buffer-live-p gnus-summary-buffer)
5720                (set-buffer gnus-group-buffer))
5721              (set-buffer gnus-summary-buffer)
5722              (gnus-set-global-variables))))
5723     (if (or (eq (cdr quit-config) 'article)
5724             (eq (cdr quit-config) 'pick))
5725         (progn
5726           ;; The current article may be from the ephemeral group
5727           ;; thus it is best that we reload this article
5728           (gnus-summary-show-article)
5729           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5730               (gnus-configure-windows 'pick 'force)
5731             (gnus-configure-windows (cdr quit-config) 'force)))
5732       (gnus-configure-windows (cdr quit-config) 'force))
5733     (when (eq major-mode 'gnus-summary-mode)
5734       (gnus-summary-next-subject 1 nil t)
5735       (gnus-summary-recenter)
5736       (gnus-summary-position-point))))
5737
5738 ;;; Dead summaries.
5739
5740 (defvar gnus-dead-summary-mode-map nil)
5741
5742 (unless gnus-dead-summary-mode-map
5743   (setq gnus-dead-summary-mode-map (make-keymap))
5744   (suppress-keymap gnus-dead-summary-mode-map)
5745   (substitute-key-definition
5746    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5747   (let ((keys '("\C-d" "\r" "\177" [delete])))
5748     (while keys
5749       (define-key gnus-dead-summary-mode-map
5750         (pop keys) 'gnus-summary-wake-up-the-dead))))
5751
5752 (defvar gnus-dead-summary-mode nil
5753   "Minor mode for Gnus summary buffers.")
5754
5755 (defun gnus-dead-summary-mode (&optional arg)
5756   "Minor mode for Gnus summary buffers."
5757   (interactive "P")
5758   (when (eq major-mode 'gnus-summary-mode)
5759     (make-local-variable 'gnus-dead-summary-mode)
5760     (setq gnus-dead-summary-mode
5761           (if (null arg) (not gnus-dead-summary-mode)
5762             (> (prefix-numeric-value arg) 0)))
5763     (when gnus-dead-summary-mode
5764       (gnus-add-minor-mode
5765        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5766
5767 (defun gnus-deaden-summary ()
5768   "Make the current summary buffer into a dead summary buffer."
5769   ;; Kill any previous dead summary buffer.
5770   (when (and gnus-dead-summary
5771              (buffer-name gnus-dead-summary))
5772     (save-excursion
5773       (set-buffer gnus-dead-summary)
5774       (when gnus-dead-summary-mode
5775         (kill-buffer (current-buffer)))))
5776   ;; Make this the current dead summary.
5777   (setq gnus-dead-summary (current-buffer))
5778   (gnus-dead-summary-mode 1)
5779   (let ((name (buffer-name)))
5780     (when (string-match "Summary" name)
5781       (rename-buffer
5782        (concat (substring name 0 (match-beginning 0)) "Dead "
5783                (substring name (match-beginning 0)))
5784        t)
5785       (bury-buffer))))
5786
5787 (defun gnus-kill-or-deaden-summary (buffer)
5788   "Kill or deaden the summary BUFFER."
5789   (save-excursion
5790     (when (and (buffer-name buffer)
5791                (not gnus-single-article-buffer))
5792       (save-excursion
5793         (set-buffer buffer)
5794         (gnus-kill-buffer gnus-article-buffer)
5795         (gnus-kill-buffer gnus-original-article-buffer)))
5796     (cond (gnus-kill-summary-on-exit
5797            (when (and gnus-use-trees
5798                       (gnus-buffer-exists-p buffer))
5799              (save-excursion
5800                (set-buffer buffer)
5801                (gnus-tree-close gnus-newsgroup-name)))
5802            (gnus-kill-buffer buffer))
5803           ((gnus-buffer-exists-p buffer)
5804            (save-excursion
5805              (set-buffer buffer)
5806              (gnus-deaden-summary))))))
5807
5808 (defun gnus-summary-wake-up-the-dead (&rest args)
5809   "Wake up the dead summary buffer."
5810   (interactive)
5811   (gnus-dead-summary-mode -1)
5812   (let ((name (buffer-name)))
5813     (when (string-match "Dead " name)
5814       (rename-buffer
5815        (concat (substring name 0 (match-beginning 0))
5816                (substring name (match-end 0)))
5817        t)))
5818   (gnus-message 3 "This dead summary is now alive again"))
5819
5820 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5821 (defun gnus-summary-fetch-faq (&optional faq-dir)
5822   "Fetch the FAQ for the current group.
5823 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5824 in."
5825   (interactive
5826    (list
5827     (when current-prefix-arg
5828       (completing-read
5829        "Faq dir: " (and (listp gnus-group-faq-directory)
5830                         (mapcar (lambda (file) (list file))
5831                                 gnus-group-faq-directory))))))
5832   (let (gnus-faq-buffer)
5833     (when (setq gnus-faq-buffer
5834                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5835       (gnus-configure-windows 'summary-faq))))
5836
5837 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5838 (defun gnus-summary-describe-group (&optional force)
5839   "Describe the current newsgroup."
5840   (interactive "P")
5841   (gnus-group-describe-group force gnus-newsgroup-name))
5842
5843 (defun gnus-summary-describe-briefly ()
5844   "Describe summary mode commands briefly."
5845   (interactive)
5846   (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")))
5847
5848 ;; Walking around group mode buffer from summary mode.
5849
5850 (defun gnus-summary-next-group (&optional no-article target-group backward)
5851   "Exit current newsgroup and then select next unread newsgroup.
5852 If prefix argument NO-ARTICLE is non-nil, no article is selected
5853 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5854 previous group instead."
5855   (interactive "P")
5856   ;; Stop pre-fetching.
5857   (gnus-async-halt-prefetch)
5858   (let ((current-group gnus-newsgroup-name)
5859         (current-buffer (current-buffer))
5860         entered)
5861     ;; First we semi-exit this group to update Xrefs and all variables.
5862     ;; We can't do a real exit, because the window conf must remain
5863     ;; the same in case the user is prompted for info, and we don't
5864     ;; want the window conf to change before that...
5865     (gnus-summary-exit t)
5866     (while (not entered)
5867       ;; Then we find what group we are supposed to enter.
5868       (set-buffer gnus-group-buffer)
5869       (gnus-group-jump-to-group current-group)
5870       (setq target-group
5871             (or target-group
5872                 (if (eq gnus-keep-same-level 'best)
5873                     (gnus-summary-best-group gnus-newsgroup-name)
5874                   (gnus-summary-search-group backward gnus-keep-same-level))))
5875       (if (not target-group)
5876           ;; There are no further groups, so we return to the group
5877           ;; buffer.
5878           (progn
5879             (gnus-message 5 "Returning to the group buffer")
5880             (setq entered t)
5881             (when (gnus-buffer-live-p current-buffer)
5882               (set-buffer current-buffer)
5883               (gnus-summary-exit))
5884             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5885         ;; We try to enter the target group.
5886         (gnus-group-jump-to-group target-group)
5887         (let ((unreads (gnus-group-group-unread)))
5888           (if (and (or (eq t unreads)
5889                        (and unreads (not (zerop unreads))))
5890                    (gnus-summary-read-group
5891                     target-group nil no-article
5892                     (and (buffer-name current-buffer) current-buffer)
5893                     nil backward))
5894               (setq entered t)
5895             (setq current-group target-group
5896                   target-group nil)))))))
5897
5898 (defun gnus-summary-prev-group (&optional no-article)
5899   "Exit current newsgroup and then select previous unread newsgroup.
5900 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5901   (interactive "P")
5902   (gnus-summary-next-group no-article nil t))
5903
5904 ;; Walking around summary lines.
5905
5906 (defun gnus-summary-first-subject (&optional unread undownloaded)
5907   "Go to the first unread subject.
5908 If UNREAD is non-nil, go to the first unread article.
5909 Returns the article selected or nil if there are no unread articles."
5910   (interactive "P")
5911   (prog1
5912       (cond
5913        ;; Empty summary.
5914        ((null gnus-newsgroup-data)
5915         (gnus-message 3 "No articles in the group")
5916         nil)
5917        ;; Pick the first article.
5918        ((not unread)
5919         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5920         (gnus-data-number (car gnus-newsgroup-data)))
5921        ;; No unread articles.
5922        ((null gnus-newsgroup-unreads)
5923         (gnus-message 3 "No more unread articles")
5924         nil)
5925        ;; Find the first unread article.
5926        (t
5927         (let ((data gnus-newsgroup-data))
5928           (while (and data
5929                       (and (not (and undownloaded
5930                                      (eq gnus-undownloaded-mark
5931                                          (gnus-data-mark (car data)))))
5932                            (not (gnus-data-unread-p (car data)))))
5933             (setq data (cdr data)))
5934           (when data
5935             (goto-char (gnus-data-pos (car data)))
5936             (gnus-data-number (car data))))))
5937     (gnus-summary-position-point)))
5938
5939 (defun gnus-summary-next-subject (n &optional unread dont-display)
5940   "Go to next N'th summary line.
5941 If N is negative, go to the previous N'th subject line.
5942 If UNREAD is non-nil, only unread articles are selected.
5943 The difference between N and the actual number of steps taken is
5944 returned."
5945   (interactive "p")
5946   (let ((backward (< n 0))
5947         (n (abs n)))
5948     (while (and (> n 0)
5949                 (if backward
5950                     (gnus-summary-find-prev unread)
5951                   (gnus-summary-find-next unread)))
5952       (unless (zerop (setq n (1- n)))
5953         (gnus-summary-show-thread)))
5954     (when (/= 0 n)
5955       (gnus-message 7 "No more%s articles"
5956                     (if unread " unread" "")))
5957     (unless dont-display
5958       (gnus-summary-recenter)
5959       (gnus-summary-position-point))
5960     n))
5961
5962 (defun gnus-summary-next-unread-subject (n)
5963   "Go to next N'th unread summary line."
5964   (interactive "p")
5965   (gnus-summary-next-subject n t))
5966
5967 (defun gnus-summary-prev-subject (n &optional unread)
5968   "Go to previous N'th summary line.
5969 If optional argument UNREAD is non-nil, only unread article is selected."
5970   (interactive "p")
5971   (gnus-summary-next-subject (- n) unread))
5972
5973 (defun gnus-summary-prev-unread-subject (n)
5974   "Go to previous N'th unread summary line."
5975   (interactive "p")
5976   (gnus-summary-next-subject (- n) t))
5977
5978 (defun gnus-summary-goto-subject (article &optional force silent)
5979   "Go the subject line of ARTICLE.
5980 If FORCE, also allow jumping to articles not currently shown."
5981   (interactive "nArticle number: ")
5982   (let ((b (point))
5983         (data (gnus-data-find article)))
5984     ;; We read in the article if we have to.
5985     (and (not data)
5986          force
5987          (gnus-summary-insert-subject
5988           article
5989           (if (or (numberp force) (vectorp force)) force)
5990           t)
5991          (setq data (gnus-data-find article)))
5992     (goto-char b)
5993     (if (not data)
5994         (progn
5995           (unless silent
5996             (gnus-message 3 "Can't find article %d" article))
5997           nil)
5998       (goto-char (gnus-data-pos data))
5999       (gnus-summary-position-point)
6000       article)))
6001
6002 ;; Walking around summary lines with displaying articles.
6003
6004 (defun gnus-summary-expand-window (&optional arg)
6005   "Make the summary buffer take up the entire Emacs frame.
6006 Given a prefix, will force an `article' buffer configuration."
6007   (interactive "P")
6008   (if arg
6009       (gnus-configure-windows 'article 'force)
6010     (gnus-configure-windows 'summary 'force)))
6011
6012 (defun gnus-summary-display-article (article &optional all-header)
6013   "Display ARTICLE in article buffer."
6014   (when (gnus-buffer-live-p gnus-article-buffer)
6015     (with-current-buffer gnus-article-buffer
6016       (mm-enable-multibyte-mule4)))
6017   (gnus-set-global-variables)
6018   (when (gnus-buffer-live-p gnus-article-buffer)
6019     (with-current-buffer gnus-article-buffer
6020       (setq gnus-article-charset gnus-newsgroup-charset)
6021       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6022       (mm-enable-multibyte-mule4)))
6023   (if (null article)
6024       nil
6025     (prog1
6026         (if gnus-summary-display-article-function
6027             (funcall gnus-summary-display-article-function article all-header)
6028           (gnus-article-prepare article all-header))
6029       (gnus-run-hooks 'gnus-select-article-hook)
6030       (when (and gnus-current-article
6031                  (not (zerop gnus-current-article)))
6032         (gnus-summary-goto-subject gnus-current-article))
6033       (gnus-summary-recenter)
6034       (when (and gnus-use-trees gnus-show-threads)
6035         (gnus-possibly-generate-tree article)
6036         (gnus-highlight-selected-tree article))
6037       ;; Successfully display article.
6038       (gnus-article-set-window-start
6039        (cdr (assq article gnus-newsgroup-bookmarks))))))
6040
6041 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6042   "Select the current article.
6043 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6044 non-nil, the article will be re-fetched even if it already present in
6045 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6046 be displayed."
6047   ;; Make sure we are in the summary buffer to work around bbdb bug.
6048   (unless (eq major-mode 'gnus-summary-mode)
6049     (set-buffer gnus-summary-buffer))
6050   (let ((article (or article (gnus-summary-article-number)))
6051         (all-headers (not (not all-headers))) ;Must be T or NIL.
6052         gnus-summary-display-article-function)
6053     (and (not pseudo)
6054          (gnus-summary-article-pseudo-p article)
6055          (error "This is a pseudo-article"))
6056     (save-excursion
6057       (set-buffer gnus-summary-buffer)
6058       (if (or (and gnus-single-article-buffer
6059                    (or (null gnus-current-article)
6060                        (null gnus-article-current)
6061                        (null (get-buffer gnus-article-buffer))
6062                        (not (eq article (cdr gnus-article-current)))
6063                        (not (equal (car gnus-article-current)
6064                                    gnus-newsgroup-name))))
6065               (and (not gnus-single-article-buffer)
6066                    (or (null gnus-current-article)
6067                        (not (eq gnus-current-article article))))
6068               force)
6069           ;; The requested article is different from the current article.
6070           (progn
6071             (gnus-summary-display-article article all-headers)
6072             (when (gnus-buffer-live-p gnus-article-buffer)
6073                (with-current-buffer gnus-article-buffer
6074                 (if (not gnus-article-decoded-p) ;; a local variable
6075                     (mm-disable-multibyte-mule4))))
6076             (when (or all-headers gnus-show-all-headers)
6077               (gnus-article-show-all-headers))
6078             (gnus-article-set-window-start
6079              (cdr (assq article gnus-newsgroup-bookmarks)))
6080             article)
6081         (when (or all-headers gnus-show-all-headers)
6082           (gnus-article-show-all-headers))
6083         'old))))
6084
6085 (defun gnus-summary-force-verify-and-decrypt ()
6086   (interactive)
6087   (let ((mm-verify-option 'known)
6088         (mm-decrypt-option 'known))
6089     (gnus-summary-select-article nil 'force)))
6090
6091 (defun gnus-summary-set-current-mark (&optional current-mark)
6092   "Obsolete function."
6093   nil)
6094
6095 (defun gnus-summary-next-article (&optional unread subject backward push)
6096   "Select the next article.
6097 If UNREAD, only unread articles are selected.
6098 If SUBJECT, only articles with SUBJECT are selected.
6099 If BACKWARD, the previous article is selected instead of the next."
6100   (interactive "P")
6101   (cond
6102    ;; Is there such an article?
6103    ((and (gnus-summary-search-forward unread subject backward)
6104          (or (gnus-summary-display-article (gnus-summary-article-number))
6105              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6106     (gnus-summary-position-point))
6107    ;; If not, we try the first unread, if that is wanted.
6108    ((and subject
6109          gnus-auto-select-same
6110          (gnus-summary-first-unread-article))
6111     (gnus-summary-position-point)
6112     (gnus-message 6 "Wrapped"))
6113    ;; Try to get next/previous article not displayed in this group.
6114    ((and gnus-auto-extend-newsgroup
6115          (not unread) (not subject))
6116     (gnus-summary-goto-article
6117      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6118      nil (count-lines (point-min) (point))))
6119    ;; Go to next/previous group.
6120    (t
6121     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6122       (gnus-summary-jump-to-group gnus-newsgroup-name))
6123     (let ((cmd last-command-char)
6124           (point
6125            (save-excursion
6126              (set-buffer gnus-group-buffer)
6127              (point)))
6128           (group
6129            (if (eq gnus-keep-same-level 'best)
6130                (gnus-summary-best-group gnus-newsgroup-name)
6131              (gnus-summary-search-group backward gnus-keep-same-level))))
6132       ;; For some reason, the group window gets selected.  We change
6133       ;; it back.
6134       (select-window (get-buffer-window (current-buffer)))
6135       ;; Select next unread newsgroup automagically.
6136       (cond
6137        ((or (not gnus-auto-select-next)
6138             (not cmd))
6139         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6140        ((or (eq gnus-auto-select-next 'quietly)
6141             (and (eq gnus-auto-select-next 'slightly-quietly)
6142                  push)
6143             (and (eq gnus-auto-select-next 'almost-quietly)
6144                  (gnus-summary-last-article-p)))
6145         ;; Select quietly.
6146         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6147             (gnus-summary-exit)
6148           (gnus-message 7 "No more%s articles (%s)..."
6149                         (if unread " unread" "")
6150                         (if group (concat "selecting " group)
6151                           "exiting"))
6152           (gnus-summary-next-group nil group backward)))
6153        (t
6154         (when (gnus-key-press-event-p last-input-event)
6155           (gnus-summary-walk-group-buffer
6156            gnus-newsgroup-name cmd unread backward point))))))))
6157
6158 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6159   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6160                       (?\C-p (gnus-group-prev-unread-group 1))))
6161         (cursor-in-echo-area t)
6162         keve key group ended)
6163     (save-excursion
6164       (set-buffer gnus-group-buffer)
6165       (goto-char start)
6166       (setq group
6167             (if (eq gnus-keep-same-level 'best)
6168                 (gnus-summary-best-group gnus-newsgroup-name)
6169               (gnus-summary-search-group backward gnus-keep-same-level))))
6170     (while (not ended)
6171       (gnus-message
6172        5 "No more%s articles%s" (if unread " unread" "")
6173        (if (and group
6174                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6175            (format " (Type %s for %s [%s])"
6176                    (single-key-description cmd) group
6177                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6178          (format " (Type %s to exit %s)"
6179                  (single-key-description cmd)
6180                  gnus-newsgroup-name)))
6181       ;; Confirm auto selection.
6182       (setq key (car (setq keve (gnus-read-event-char))))
6183       (setq ended t)
6184       (cond
6185        ((assq key keystrokes)
6186         (let ((obuf (current-buffer)))
6187           (switch-to-buffer gnus-group-buffer)
6188           (when group
6189             (gnus-group-jump-to-group group))
6190           (eval (cadr (assq key keystrokes)))
6191           (setq group (gnus-group-group-name))
6192           (switch-to-buffer obuf))
6193         (setq ended nil))
6194        ((equal key cmd)
6195         (if (or (not group)
6196                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6197             (gnus-summary-exit)
6198           (gnus-summary-next-group nil group backward)))
6199        (t
6200         (push (cdr keve) unread-command-events))))))
6201
6202 (defun gnus-summary-next-unread-article ()
6203   "Select unread article after current one."
6204   (interactive)
6205   (gnus-summary-next-article
6206    (or (not (eq gnus-summary-goto-unread 'never))
6207        (gnus-summary-last-article-p (gnus-summary-article-number)))
6208    (and gnus-auto-select-same
6209         (gnus-summary-article-subject))))
6210
6211 (defun gnus-summary-prev-article (&optional unread subject)
6212   "Select the article after the current one.
6213 If UNREAD is non-nil, only unread articles are selected."
6214   (interactive "P")
6215   (gnus-summary-next-article unread subject t))
6216
6217 (defun gnus-summary-prev-unread-article ()
6218   "Select unread article before current one."
6219   (interactive)
6220   (gnus-summary-prev-article
6221    (or (not (eq gnus-summary-goto-unread 'never))
6222        (gnus-summary-first-article-p (gnus-summary-article-number)))
6223    (and gnus-auto-select-same
6224         (gnus-summary-article-subject))))
6225
6226 (defun gnus-summary-next-page (&optional lines circular)
6227   "Show next page of the selected article.
6228 If at the end of the current article, select the next article.
6229 LINES says how many lines should be scrolled up.
6230
6231 If CIRCULAR is non-nil, go to the start of the article instead of
6232 selecting the next article when reaching the end of the current
6233 article."
6234   (interactive "P")
6235   (setq gnus-summary-buffer (current-buffer))
6236   (gnus-set-global-variables)
6237   (let ((article (gnus-summary-article-number))
6238         (article-window (get-buffer-window gnus-article-buffer t))
6239         endp)
6240     ;; If the buffer is empty, we have no article.
6241     (unless article
6242       (error "No article to select"))
6243     (gnus-configure-windows 'article)
6244     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6245         (if (and (eq gnus-summary-goto-unread 'never)
6246                  (not (gnus-summary-last-article-p article)))
6247             (gnus-summary-next-article)
6248           (gnus-summary-next-unread-article))
6249       (if (or (null gnus-current-article)
6250               (null gnus-article-current)
6251               (/= article (cdr gnus-article-current))
6252               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6253           ;; Selected subject is different from current article's.
6254           (gnus-summary-display-article article)
6255         (when article-window
6256           (gnus-eval-in-buffer-window gnus-article-buffer
6257             (setq endp (gnus-article-next-page lines)))
6258           (when endp
6259             (cond (circular
6260                    (gnus-summary-beginning-of-article))
6261                   (lines
6262                    (gnus-message 3 "End of message"))
6263                   ((null lines)
6264                    (if (and (eq gnus-summary-goto-unread 'never)
6265                             (not (gnus-summary-last-article-p article)))
6266                        (gnus-summary-next-article)
6267                      (gnus-summary-next-unread-article))))))))
6268     (gnus-summary-recenter)
6269     (gnus-summary-position-point)))
6270
6271 (defun gnus-summary-prev-page (&optional lines move)
6272   "Show previous page of selected article.
6273 Argument LINES specifies lines to be scrolled down.
6274 If MOVE, move to the previous unread article if point is at
6275 the beginning of the buffer."
6276   (interactive "P")
6277   (let ((article (gnus-summary-article-number))
6278         (article-window (get-buffer-window gnus-article-buffer t))
6279         endp)
6280     (gnus-configure-windows 'article)
6281     (if (or (null gnus-current-article)
6282             (null gnus-article-current)
6283             (/= article (cdr gnus-article-current))
6284             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6285         ;; Selected subject is different from current article's.
6286         (gnus-summary-display-article article)
6287       (gnus-summary-recenter)
6288       (when article-window
6289         (gnus-eval-in-buffer-window gnus-article-buffer
6290           (setq endp (gnus-article-prev-page lines)))
6291         (when (and move endp)
6292           (cond (lines
6293                  (gnus-message 3 "Beginning of message"))
6294                 ((null lines)
6295                  (if (and (eq gnus-summary-goto-unread 'never)
6296                           (not (gnus-summary-first-article-p article)))
6297                      (gnus-summary-prev-article)
6298                    (gnus-summary-prev-unread-article))))))))
6299   (gnus-summary-position-point))
6300
6301 (defun gnus-summary-prev-page-or-article (&optional lines)
6302   "Show previous page of selected article.
6303 Argument LINES specifies lines to be scrolled down.
6304 If at the beginning of the article, go to the next article."
6305   (interactive "P")
6306   (gnus-summary-prev-page lines t))
6307
6308 (defun gnus-summary-scroll-up (lines)
6309   "Scroll up (or down) one line current article.
6310 Argument LINES specifies lines to be scrolled up (or down if negative)."
6311   (interactive "p")
6312   (gnus-configure-windows 'article)
6313   (gnus-summary-show-thread)
6314   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6315     (gnus-eval-in-buffer-window gnus-article-buffer
6316       (cond ((> lines 0)
6317              (when (gnus-article-next-page lines)
6318                (gnus-message 3 "End of message")))
6319             ((< lines 0)
6320              (gnus-article-prev-page (- lines))))))
6321   (gnus-summary-recenter)
6322   (gnus-summary-position-point))
6323
6324 (defun gnus-summary-scroll-down (lines)
6325   "Scroll down (or up) one line current article.
6326 Argument LINES specifies lines to be scrolled down (or up if negative)."
6327   (interactive "p")
6328   (gnus-summary-scroll-up (- lines)))
6329
6330 (defun gnus-summary-next-same-subject ()
6331   "Select next article which has the same subject as current one."
6332   (interactive)
6333   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6334
6335 (defun gnus-summary-prev-same-subject ()
6336   "Select previous article which has the same subject as current one."
6337   (interactive)
6338   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6339
6340 (defun gnus-summary-next-unread-same-subject ()
6341   "Select next unread article which has the same subject as current one."
6342   (interactive)
6343   (gnus-summary-next-article t (gnus-summary-article-subject)))
6344
6345 (defun gnus-summary-prev-unread-same-subject ()
6346   "Select previous unread article which has the same subject as current one."
6347   (interactive)
6348   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6349
6350 (defun gnus-summary-first-unread-article ()
6351   "Select the first unread article.
6352 Return nil if there are no unread articles."
6353   (interactive)
6354   (prog1
6355       (when (gnus-summary-first-subject t)
6356         (gnus-summary-show-thread)
6357         (gnus-summary-first-subject t)
6358         (gnus-summary-display-article (gnus-summary-article-number)))
6359     (gnus-summary-position-point)))
6360
6361 (defun gnus-summary-first-unread-subject ()
6362   "Place the point on the subject line of the first unread article.
6363 Return nil if there are no unread articles."
6364   (interactive)
6365   (prog1
6366       (when (gnus-summary-first-subject t)
6367         (gnus-summary-show-thread)
6368         (gnus-summary-first-subject t))
6369     (gnus-summary-position-point)))
6370
6371 (defun gnus-summary-first-article ()
6372   "Select the first article.
6373 Return nil if there are no articles."
6374   (interactive)
6375   (prog1
6376       (when (gnus-summary-first-subject)
6377         (gnus-summary-show-thread)
6378         (gnus-summary-first-subject)
6379         (gnus-summary-display-article (gnus-summary-article-number)))
6380     (gnus-summary-position-point)))
6381
6382 (defun gnus-summary-best-unread-article ()
6383   "Select the unread article with the highest score."
6384   (interactive)
6385   (let ((best -1000000)
6386         (data gnus-newsgroup-data)
6387         article score)
6388     (while data
6389       (and (gnus-data-unread-p (car data))
6390            (> (setq score
6391                     (gnus-summary-article-score (gnus-data-number (car data))))
6392               best)
6393            (setq best score
6394                  article (gnus-data-number (car data))))
6395       (setq data (cdr data)))
6396     (prog1
6397         (if article
6398             (gnus-summary-goto-article article)
6399           (error "No unread articles"))
6400       (gnus-summary-position-point))))
6401
6402 (defun gnus-summary-last-subject ()
6403   "Go to the last displayed subject line in the group."
6404   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6405     (when article
6406       (gnus-summary-goto-subject article))))
6407
6408 (defun gnus-summary-goto-article (article &optional all-headers force)
6409   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6410 If ALL-HEADERS is non-nil, no header lines are hidden.
6411 If FORCE, go to the article even if it isn't displayed.  If FORCE
6412 is a number, it is the line the article is to be displayed on."
6413   (interactive
6414    (list
6415     (completing-read
6416      "Article number or Message-ID: "
6417      (mapcar (lambda (number) (list (int-to-string number)))
6418              gnus-newsgroup-limit))
6419     current-prefix-arg
6420     t))
6421   (prog1
6422       (if (and (stringp article)
6423                (string-match "@" article))
6424           (gnus-summary-refer-article article)
6425         (when (stringp article)
6426           (setq article (string-to-number article)))
6427         (if (gnus-summary-goto-subject article force)
6428             (gnus-summary-display-article article all-headers)
6429           (gnus-message 4 "Couldn't go to article %s" article) nil))
6430     (gnus-summary-position-point)))
6431
6432 (defun gnus-summary-goto-last-article ()
6433   "Go to the previously read article."
6434   (interactive)
6435   (prog1
6436       (when gnus-last-article
6437         (gnus-summary-goto-article gnus-last-article nil t))
6438     (gnus-summary-position-point)))
6439
6440 (defun gnus-summary-pop-article (number)
6441   "Pop one article off the history and go to the previous.
6442 NUMBER articles will be popped off."
6443   (interactive "p")
6444   (let (to)
6445     (setq gnus-newsgroup-history
6446           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6447     (if to
6448         (gnus-summary-goto-article (car to) nil t)
6449       (error "Article history empty")))
6450   (gnus-summary-position-point))
6451
6452 ;; Summary commands and functions for limiting the summary buffer.
6453
6454 (defun gnus-summary-limit-to-articles (n)
6455   "Limit the summary buffer to the next N articles.
6456 If not given a prefix, use the process marked articles instead."
6457   (interactive "P")
6458   (prog1
6459       (let ((articles (gnus-summary-work-articles n)))
6460         (setq gnus-newsgroup-processable nil)
6461         (gnus-summary-limit articles))
6462     (gnus-summary-position-point)))
6463
6464 (defun gnus-summary-pop-limit (&optional total)
6465   "Restore the previous limit.
6466 If given a prefix, remove all limits."
6467   (interactive "P")
6468   (when total
6469     (setq gnus-newsgroup-limits
6470           (list (mapcar (lambda (h) (mail-header-number h))
6471                         gnus-newsgroup-headers))))
6472   (unless gnus-newsgroup-limits
6473     (error "No limit to pop"))
6474   (prog1
6475       (gnus-summary-limit nil 'pop)
6476     (gnus-summary-position-point)))
6477
6478 (defun gnus-summary-limit-to-subject (subject &optional header)
6479   "Limit the summary buffer to articles that have subjects that match a regexp."
6480   (interactive "sLimit to subject (regexp): ")
6481   (unless header
6482     (setq header "subject"))
6483   (when (not (equal "" subject))
6484     (prog1
6485         (let ((articles (gnus-summary-find-matching
6486                          (or header "subject") subject 'all)))
6487           (unless articles
6488             (error "Found no matches for \"%s\"" subject))
6489           (gnus-summary-limit articles))
6490       (gnus-summary-position-point))))
6491
6492 (defun gnus-summary-limit-to-author (from)
6493   "Limit the summary buffer to articles that have authors that match a regexp."
6494   (interactive "sLimit to author (regexp): ")
6495   (gnus-summary-limit-to-subject from "from"))
6496
6497 (defun gnus-summary-limit-to-age (age &optional younger-p)
6498   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6499 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6500 articles that are younger than AGE days."
6501   (interactive
6502    (let ((younger current-prefix-arg)
6503          (days-got nil)
6504          days)
6505      (while (not days-got)
6506        (setq days (if younger
6507                       (read-string "Limit to articles within (in days): ")
6508                     (read-string "Limit to articles older than (in days): ")))
6509        (when (> (length days) 0)
6510          (setq days (read days)))
6511        (if (numberp days)
6512            (setq days-got t)
6513          (message "Please enter a number.")
6514          (sleep-for 1)))
6515      (list days younger)))
6516   (prog1
6517       (let ((data gnus-newsgroup-data)
6518             (cutoff (days-to-time age))
6519             articles d date is-younger)
6520         (while (setq d (pop data))
6521           (when (and (vectorp (gnus-data-header d))
6522                      (setq date (mail-header-date (gnus-data-header d))))
6523             (setq is-younger (time-less-p
6524                               (time-since (condition-case ()
6525                                               (date-to-time date)
6526                                             (error '(0 0))))
6527                               cutoff))
6528             (when (if younger-p
6529                       is-younger
6530                     (not is-younger))
6531               (push (gnus-data-number d) articles))))
6532         (gnus-summary-limit (nreverse articles)))
6533     (gnus-summary-position-point)))
6534
6535 (defun gnus-summary-limit-to-extra (header regexp)
6536   "Limit the summary buffer to articles that match an 'extra' header."
6537   (interactive
6538    (let ((header
6539           (intern
6540            (gnus-completing-read
6541             (symbol-name (car gnus-extra-headers))
6542             "Limit extra header:"
6543             (mapcar (lambda (x)
6544                       (cons (symbol-name x) x))
6545                     gnus-extra-headers)
6546             nil
6547             t))))
6548      (list header
6549            (read-string (format "Limit to header %s (regexp): " header)))))
6550   (when (not (equal "" regexp))
6551     (prog1
6552         (let ((articles (gnus-summary-find-matching
6553                          (cons 'extra header) regexp 'all)))
6554           (unless articles
6555             (error "Found no matches for \"%s\"" regexp))
6556           (gnus-summary-limit articles))
6557       (gnus-summary-position-point))))
6558
6559 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6560 (make-obsolete
6561  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6562
6563 (defun gnus-summary-limit-to-unread (&optional all)
6564   "Limit the summary buffer to articles that are not marked as read.
6565 If ALL is non-nil, limit strictly to unread articles."
6566   (interactive "P")
6567   (if all
6568       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6569     (gnus-summary-limit-to-marks
6570      ;; Concat all the marks that say that an article is read and have
6571      ;; those removed.
6572      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6573            gnus-killed-mark gnus-kill-file-mark
6574            gnus-low-score-mark gnus-expirable-mark
6575            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6576            gnus-duplicate-mark gnus-souped-mark)
6577      'reverse)))
6578
6579 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6580 (make-obsolete 'gnus-summary-delete-marked-with
6581                'gnus-summary-limit-exlude-marks)
6582
6583 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6584   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6585 If REVERSE, limit the summary buffer to articles that are marked
6586 with MARKS.  MARKS can either be a string of marks or a list of marks.
6587 Returns how many articles were removed."
6588   (interactive "sMarks: ")
6589   (gnus-summary-limit-to-marks marks t))
6590
6591 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6592   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6593 If REVERSE (the prefix), limit the summary buffer to articles that are
6594 not marked with MARKS.  MARKS can either be a string of marks or a
6595 list of marks.
6596 Returns how many articles were removed."
6597   (interactive "sMarks: \nP")
6598   (prog1
6599       (let ((data gnus-newsgroup-data)
6600             (marks (if (listp marks) marks
6601                      (append marks nil))) ; Transform to list.
6602             articles)
6603         (while data
6604           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6605                   (memq (gnus-data-mark (car data)) marks))
6606             (push (gnus-data-number (car data)) articles))
6607           (setq data (cdr data)))
6608         (gnus-summary-limit articles))
6609     (gnus-summary-position-point)))
6610
6611 (defun gnus-summary-limit-to-score (&optional score)
6612   "Limit to articles with score at or above SCORE."
6613   (interactive "P")
6614   (setq score (if score
6615                   (prefix-numeric-value score)
6616                 (or gnus-summary-default-score 0)))
6617   (let ((data gnus-newsgroup-data)
6618         articles)
6619     (while data
6620       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6621                 score)
6622         (push (gnus-data-number (car data)) articles))
6623       (setq data (cdr data)))
6624     (prog1
6625         (gnus-summary-limit articles)
6626       (gnus-summary-position-point))))
6627
6628 (defun gnus-summary-limit-include-thread (id)
6629   "Display all the hidden articles that in the current thread."
6630   (interactive (list (mail-header-id (gnus-summary-article-header))))
6631   (let ((articles (gnus-articles-in-thread
6632                    (gnus-id-to-thread (gnus-root-id id)))))
6633     (prog1
6634         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6635       (gnus-summary-position-point))))
6636
6637 (defun gnus-summary-limit-include-dormant ()
6638   "Display all the hidden articles that are marked as dormant.
6639 Note that this command only works on a subset of the articles currently
6640 fetched for this group."
6641   (interactive)
6642   (unless gnus-newsgroup-dormant
6643     (error "There are no dormant articles in this group"))
6644   (prog1
6645       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6646     (gnus-summary-position-point)))
6647
6648 (defun gnus-summary-limit-exclude-dormant ()
6649   "Hide all dormant articles."
6650   (interactive)
6651   (prog1
6652       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6653     (gnus-summary-position-point)))
6654
6655 (defun gnus-summary-limit-exclude-childless-dormant ()
6656   "Hide all dormant articles that have no children."
6657   (interactive)
6658   (let ((data (gnus-data-list t))
6659         articles d children)
6660     ;; Find all articles that are either not dormant or have
6661     ;; children.
6662     (while (setq d (pop data))
6663       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6664                 (and (setq children
6665                            (gnus-article-children (gnus-data-number d)))
6666                      (let (found)
6667                        (while children
6668                          (when (memq (car children) articles)
6669                            (setq children nil
6670                                  found t))
6671                          (pop children))
6672                        found)))
6673         (push (gnus-data-number d) articles)))
6674     ;; Do the limiting.
6675     (prog1
6676         (gnus-summary-limit articles)
6677       (gnus-summary-position-point))))
6678
6679 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6680   "Mark all unread excluded articles as read.
6681 If ALL, mark even excluded ticked and dormants as read."
6682   (interactive "P")
6683   (let ((articles (gnus-sorted-complement
6684                    (sort
6685                     (mapcar (lambda (h) (mail-header-number h))
6686                             gnus-newsgroup-headers)
6687                     '<)
6688                    (sort gnus-newsgroup-limit '<)))
6689         article)
6690     (setq gnus-newsgroup-unreads
6691           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6692     (if all
6693         (setq gnus-newsgroup-dormant nil
6694               gnus-newsgroup-marked nil
6695               gnus-newsgroup-reads
6696               (nconc
6697                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6698                gnus-newsgroup-reads))
6699       (while (setq article (pop articles))
6700         (unless (or (memq article gnus-newsgroup-dormant)
6701                     (memq article gnus-newsgroup-marked))
6702           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6703
6704 (defun gnus-summary-limit (articles &optional pop)
6705   (if pop
6706       ;; We pop the previous limit off the stack and use that.
6707       (setq articles (car gnus-newsgroup-limits)
6708             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6709     ;; We use the new limit, so we push the old limit on the stack.
6710     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6711   ;; Set the limit.
6712   (setq gnus-newsgroup-limit articles)
6713   (let ((total (length gnus-newsgroup-data))
6714         (data (gnus-data-find-list (gnus-summary-article-number)))
6715         (gnus-summary-mark-below nil)   ; Inhibit this.
6716         found)
6717     ;; This will do all the work of generating the new summary buffer
6718     ;; according to the new limit.
6719     (gnus-summary-prepare)
6720     ;; Hide any threads, possibly.
6721     (and gnus-show-threads
6722          gnus-thread-hide-subtree
6723          (gnus-summary-hide-all-threads))
6724     ;; Try to return to the article you were at, or one in the
6725     ;; neighborhood.
6726     (when data
6727       ;; We try to find some article after the current one.
6728       (while data
6729         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6730           (setq data nil
6731                 found t))
6732         (setq data (cdr data))))
6733     (unless found
6734       ;; If there is no data, that means that we were after the last
6735       ;; article.  The same goes when we can't find any articles
6736       ;; after the current one.
6737       (goto-char (point-max))
6738       (gnus-summary-find-prev))
6739     (gnus-set-mode-line 'summary)
6740     ;; We return how many articles were removed from the summary
6741     ;; buffer as a result of the new limit.
6742     (- total (length gnus-newsgroup-data))))
6743
6744 (defsubst gnus-invisible-cut-children (threads)
6745   (let ((num 0))
6746     (while threads
6747       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6748         (incf num))
6749       (pop threads))
6750     (< num 2)))
6751
6752 (defsubst gnus-cut-thread (thread)
6753   "Go forwards in the thread until we find an article that we want to display."
6754   (when (or (eq gnus-fetch-old-headers 'some)
6755             (eq gnus-fetch-old-headers 'invisible)
6756             (numberp gnus-fetch-old-headers)
6757             (eq gnus-build-sparse-threads 'some)
6758             (eq gnus-build-sparse-threads 'more))
6759     ;; Deal with old-fetched headers and sparse threads.
6760     (while (and
6761             thread
6762             (or
6763              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6764              (gnus-summary-article-ancient-p
6765               (mail-header-number (car thread))))
6766             (if (or (<= (length (cdr thread)) 1)
6767                     (eq gnus-fetch-old-headers 'invisible))
6768                 (setq gnus-newsgroup-limit
6769                       (delq (mail-header-number (car thread))
6770                             gnus-newsgroup-limit)
6771                       thread (cadr thread))
6772               (when (gnus-invisible-cut-children (cdr thread))
6773                 (let ((th (cdr thread)))
6774                   (while th
6775                     (if (memq (mail-header-number (caar th))
6776                               gnus-newsgroup-limit)
6777                         (setq thread (car th)
6778                               th nil)
6779                       (setq th (cdr th))))))))))
6780   thread)
6781
6782 (defun gnus-cut-threads (threads)
6783   "Cut off all uninteresting articles from the beginning of threads."
6784   (when (or (eq gnus-fetch-old-headers 'some)
6785             (eq gnus-fetch-old-headers 'invisible)
6786             (numberp gnus-fetch-old-headers)
6787             (eq gnus-build-sparse-threads 'some)
6788             (eq gnus-build-sparse-threads 'more))
6789     (let ((th threads))
6790       (while th
6791         (setcar th (gnus-cut-thread (car th)))
6792         (setq th (cdr th)))))
6793   ;; Remove nixed out threads.
6794   (delq nil threads))
6795
6796 (defun gnus-summary-initial-limit (&optional show-if-empty)
6797   "Figure out what the initial limit is supposed to be on group entry.
6798 This entails weeding out unwanted dormants, low-scored articles,
6799 fetch-old-headers verbiage, and so on."
6800   ;; Most groups have nothing to remove.
6801   (if (or gnus-inhibit-limiting
6802           (and (null gnus-newsgroup-dormant)
6803                (not (eq gnus-fetch-old-headers 'some))
6804                (not (numberp gnus-fetch-old-headers))
6805                (not (eq gnus-fetch-old-headers 'invisible))
6806                (null gnus-summary-expunge-below)
6807                (not (eq gnus-build-sparse-threads 'some))
6808                (not (eq gnus-build-sparse-threads 'more))
6809                (null gnus-thread-expunge-below)
6810                (not gnus-use-nocem)))
6811       ()                                ; Do nothing.
6812     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6813     (setq gnus-newsgroup-limit nil)
6814     (mapatoms
6815      (lambda (node)
6816        (unless (car (symbol-value node))
6817          ;; These threads have no parents -- they are roots.
6818          (let ((nodes (cdr (symbol-value node)))
6819                thread)
6820            (while nodes
6821              (if (and gnus-thread-expunge-below
6822                       (< (gnus-thread-total-score (car nodes))
6823                          gnus-thread-expunge-below))
6824                  (gnus-expunge-thread (pop nodes))
6825                (setq thread (pop nodes))
6826                (gnus-summary-limit-children thread))))))
6827      gnus-newsgroup-dependencies)
6828     ;; If this limitation resulted in an empty group, we might
6829     ;; pop the previous limit and use it instead.
6830     (when (and (not gnus-newsgroup-limit)
6831                show-if-empty)
6832       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6833     gnus-newsgroup-limit))
6834
6835 (defun gnus-summary-limit-children (thread)
6836   "Return 1 if this subthread is visible and 0 if it is not."
6837   ;; First we get the number of visible children to this thread.  This
6838   ;; is done by recursing down the thread using this function, so this
6839   ;; will really go down to a leaf article first, before slowly
6840   ;; working its way up towards the root.
6841   (when thread
6842     (let ((children
6843            (if (cdr thread)
6844                (apply '+ (mapcar 'gnus-summary-limit-children
6845                                  (cdr thread)))
6846              0))
6847           (number (mail-header-number (car thread)))
6848           score)
6849       (if (and
6850            (not (memq number gnus-newsgroup-marked))
6851            (or
6852             ;; If this article is dormant and has absolutely no visible
6853             ;; children, then this article isn't visible.
6854             (and (memq number gnus-newsgroup-dormant)
6855                  (zerop children))
6856             ;; If this is "fetch-old-headered" and there is no
6857             ;; visible children, then we don't want this article.
6858             (and (or (eq gnus-fetch-old-headers 'some)
6859                      (numberp gnus-fetch-old-headers))
6860                  (gnus-summary-article-ancient-p number)
6861                  (zerop children))
6862             ;; If this is "fetch-old-headered" and `invisible', then
6863             ;; we don't want this article.
6864             (and (eq gnus-fetch-old-headers 'invisible)
6865                  (gnus-summary-article-ancient-p number))
6866             ;; If this is a sparsely inserted article with no children,
6867             ;; we don't want it.
6868             (and (eq gnus-build-sparse-threads 'some)
6869                  (gnus-summary-article-sparse-p number)
6870                  (zerop children))
6871             ;; If we use expunging, and this article is really
6872             ;; low-scored, then we don't want this article.
6873             (when (and gnus-summary-expunge-below
6874                        (< (setq score
6875                                 (or (cdr (assq number gnus-newsgroup-scored))
6876                                     gnus-summary-default-score))
6877                           gnus-summary-expunge-below))
6878               ;; We increase the expunge-tally here, but that has
6879               ;; nothing to do with the limits, really.
6880               (incf gnus-newsgroup-expunged-tally)
6881               ;; We also mark as read here, if that's wanted.
6882               (when (and gnus-summary-mark-below
6883                          (< score gnus-summary-mark-below))
6884                 (setq gnus-newsgroup-unreads
6885                       (delq number gnus-newsgroup-unreads))
6886                 (if gnus-newsgroup-auto-expire
6887                     (push number gnus-newsgroup-expirable)
6888                   (push (cons number gnus-low-score-mark)
6889                         gnus-newsgroup-reads)))
6890               t)
6891             ;; Check NoCeM things.
6892             (if (and gnus-use-nocem
6893                      (gnus-nocem-unwanted-article-p
6894                       (mail-header-id (car thread))))
6895                 (progn
6896                   (setq gnus-newsgroup-unreads
6897                         (delq number gnus-newsgroup-unreads))
6898                   t))))
6899           ;; Nope, invisible article.
6900           0
6901         ;; Ok, this article is to be visible, so we add it to the limit
6902         ;; and return 1.
6903         (push number gnus-newsgroup-limit)
6904         1))))
6905
6906 (defun gnus-expunge-thread (thread)
6907   "Mark all articles in THREAD as read."
6908   (let* ((number (mail-header-number (car thread))))
6909     (incf gnus-newsgroup-expunged-tally)
6910     ;; We also mark as read here, if that's wanted.
6911     (setq gnus-newsgroup-unreads
6912           (delq number gnus-newsgroup-unreads))
6913     (if gnus-newsgroup-auto-expire
6914         (push number gnus-newsgroup-expirable)
6915       (push (cons number gnus-low-score-mark)
6916             gnus-newsgroup-reads)))
6917   ;; Go recursively through all subthreads.
6918   (mapcar 'gnus-expunge-thread (cdr thread)))
6919
6920 ;; Summary article oriented commands
6921
6922 (defun gnus-summary-refer-parent-article (n)
6923   "Refer parent article N times.
6924 If N is negative, go to ancestor -N instead.
6925 The difference between N and the number of articles fetched is returned."
6926   (interactive "p")
6927   (let ((skip 1)
6928         error header ref)
6929     (when (not (natnump n))
6930       (setq skip (abs n)
6931             n 1))
6932     (while (and (> n 0)
6933                 (not error))
6934       (setq header (gnus-summary-article-header))
6935       (if (and (eq (mail-header-number header)
6936                    (cdr gnus-article-current))
6937                (equal gnus-newsgroup-name
6938                       (car gnus-article-current)))
6939           ;; If we try to find the parent of the currently
6940           ;; displayed article, then we take a look at the actual
6941           ;; References header, since this is slightly more
6942           ;; reliable than the References field we got from the
6943           ;; server.
6944           (save-excursion
6945             (set-buffer gnus-original-article-buffer)
6946             (nnheader-narrow-to-headers)
6947             (unless (setq ref (message-fetch-field "references"))
6948               (setq ref (message-fetch-field "in-reply-to")))
6949             (widen))
6950         (setq ref
6951               ;; It's not the current article, so we take a bet on
6952               ;; the value we got from the server.
6953               (mail-header-references header)))
6954       (if (and ref
6955                (not (equal ref "")))
6956           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6957             (gnus-message 1 "Couldn't find parent"))
6958         (gnus-message 1 "No references in article %d"
6959                       (gnus-summary-article-number))
6960         (setq error t))
6961       (decf n))
6962     (gnus-summary-position-point)
6963     n))
6964
6965 (defun gnus-summary-refer-references ()
6966   "Fetch all articles mentioned in the References header.
6967 Return the number of articles fetched."
6968   (interactive)
6969   (let ((ref (mail-header-references (gnus-summary-article-header)))
6970         (current (gnus-summary-article-number))
6971         (n 0))
6972     (if (or (not ref)
6973             (equal ref ""))
6974         (error "No References in the current article")
6975       ;; For each Message-ID in the References header...
6976       (while (string-match "<[^>]*>" ref)
6977         (incf n)
6978         ;; ... fetch that article.
6979         (gnus-summary-refer-article
6980          (prog1 (match-string 0 ref)
6981            (setq ref (substring ref (match-end 0))))))
6982       (gnus-summary-goto-subject current)
6983       (gnus-summary-position-point)
6984       n)))
6985
6986 (defun gnus-summary-refer-thread (&optional limit)
6987   "Fetch all articles in the current thread.
6988 If LIMIT (the numerical prefix), fetch that many old headers instead
6989 of what's specified by the `gnus-refer-thread-limit' variable."
6990   (interactive "P")
6991   (let ((id (mail-header-id (gnus-summary-article-header)))
6992         (limit (if limit (prefix-numeric-value limit)
6993                  gnus-refer-thread-limit)))
6994     ;; We want to fetch LIMIT *old* headers, but we also have to
6995     ;; re-fetch all the headers in the current buffer, because many of
6996     ;; them may be undisplayed.  So we adjust LIMIT.
6997     (when (numberp limit)
6998       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6999     (unless (eq gnus-fetch-old-headers 'invisible)
7000       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7001       ;; Retrieve the headers and read them in.
7002       (if (eq (gnus-retrieve-headers
7003                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7004               'nov)
7005           (gnus-build-all-threads)
7006         (error "Can't fetch thread from backends that don't support NOV"))
7007       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7008     (gnus-summary-limit-include-thread id)))
7009
7010 (defun gnus-summary-refer-article (message-id)
7011   "Fetch an article specified by MESSAGE-ID."
7012   (interactive "sMessage-ID: ")
7013   (when (and (stringp message-id)
7014              (not (zerop (length message-id))))
7015     ;; Construct the correct Message-ID if necessary.
7016     ;; Suggested by tale@pawl.rpi.edu.
7017     (unless (string-match "^<" message-id)
7018       (setq message-id (concat "<" message-id)))
7019     (unless (string-match ">$" message-id)
7020       (setq message-id (concat message-id ">")))
7021     (let* ((header (gnus-id-to-header message-id))
7022            (sparse (and header
7023                         (gnus-summary-article-sparse-p
7024                          (mail-header-number header))
7025                         (memq (mail-header-number header)
7026                               gnus-newsgroup-limit)))
7027            number)
7028       (cond
7029        ;; If the article is present in the buffer we just go to it.
7030        ((and header
7031              (or (not (gnus-summary-article-sparse-p
7032                        (mail-header-number header)))
7033                  sparse))
7034         (prog1
7035             (gnus-summary-goto-article
7036              (mail-header-number header) nil t)
7037           (when sparse
7038             (gnus-summary-update-article (mail-header-number header)))))
7039        (t
7040         ;; We fetch the article.
7041         (catch 'found
7042           (dolist (gnus-override-method (gnus-refer-article-methods))
7043             (gnus-check-server gnus-override-method)
7044             ;; Fetch the header, and display the article.
7045             (when (setq number (gnus-summary-insert-subject message-id))
7046               (gnus-summary-select-article nil nil nil number)
7047               (throw 'found t)))
7048           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7049
7050 (defun gnus-refer-article-methods ()
7051   "Return a list of referrable methods."
7052   (cond
7053    ;; No method, so we default to current and native.
7054    ((null gnus-refer-article-method)
7055     (list gnus-current-select-method gnus-select-method))
7056    ;; Current.
7057    ((eq 'current gnus-refer-article-method)
7058     (list gnus-current-select-method))
7059    ;; List of select methods.
7060    ((not (and (symbolp (car gnus-refer-article-method))
7061               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7062     (let (out)
7063       (dolist (method gnus-refer-article-method)
7064         (push (if (eq 'current method)
7065                   gnus-current-select-method
7066                 method)
7067               out))
7068       (nreverse out)))
7069    ;; One single select method.
7070    (t
7071     (list gnus-refer-article-method))))
7072
7073 (defun gnus-summary-edit-parameters ()
7074   "Edit the group parameters of the current group."
7075   (interactive)
7076   (gnus-group-edit-group gnus-newsgroup-name 'params))
7077
7078 (defun gnus-summary-customize-parameters ()
7079   "Customize the group parameters of the current group."
7080   (interactive)
7081   (gnus-group-customize gnus-newsgroup-name))
7082
7083 (defun gnus-summary-enter-digest-group (&optional force)
7084   "Enter an nndoc group based on the current article.
7085 If FORCE, force a digest interpretation.  If not, try
7086 to guess what the document format is."
7087   (interactive "P")
7088   (let ((conf gnus-current-window-configuration))
7089     (save-excursion
7090       (gnus-summary-select-article))
7091     (setq gnus-current-window-configuration conf)
7092     (let* ((name (format "%s-%d"
7093                          (gnus-group-prefixed-name
7094                           gnus-newsgroup-name (list 'nndoc ""))
7095                          (save-excursion
7096                            (set-buffer gnus-summary-buffer)
7097                            gnus-current-article)))
7098            (ogroup gnus-newsgroup-name)
7099            (params (append (gnus-info-params (gnus-get-info ogroup))
7100                            (list (cons 'to-group ogroup))
7101                            (list (cons 'save-article-group ogroup))))
7102            (case-fold-search t)
7103            (buf (current-buffer))
7104            dig to-address)
7105       (save-excursion
7106         (set-buffer gnus-original-article-buffer)
7107         ;; Have the digest group inherit the main mail address of
7108         ;; the parent article.
7109         (when (setq to-address (or (message-fetch-field "reply-to")
7110                                    (message-fetch-field "from")))
7111           (setq params (append 
7112                         (list (cons 'to-address 
7113                                     (funcall gnus-decode-encoded-word-function
7114                                              to-address))))))
7115         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7116         (insert-buffer-substring gnus-original-article-buffer)
7117         ;; Remove lines that may lead nndoc to misinterpret the
7118         ;; document type.
7119         (narrow-to-region
7120          (goto-char (point-min))
7121          (or (search-forward "\n\n" nil t) (point)))
7122         (goto-char (point-min))
7123         (delete-matching-lines "^Path:\\|^From ")
7124         (widen))
7125       (unwind-protect
7126           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7127                     (gnus-newsgroup-ephemeral-ignored-charsets
7128                      gnus-newsgroup-ignored-charsets))
7129                 (gnus-group-read-ephemeral-group
7130                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7131                               (nndoc-article-type
7132                                ,(if force 'mbox 'guess))) t))
7133               ;; Make all postings to this group go to the parent group.
7134               (nconc (gnus-info-params (gnus-get-info name))
7135                      params)
7136             ;; Couldn't select this doc group.
7137             (switch-to-buffer buf)
7138             (gnus-set-global-variables)
7139             (gnus-configure-windows 'summary)
7140             (gnus-message 3 "Article couldn't be entered?"))
7141         (kill-buffer dig)))))
7142
7143 (defun gnus-summary-read-document (n)
7144   "Open a new group based on the current article(s).
7145 This will allow you to read digests and other similar
7146 documents as newsgroups.
7147 Obeys the standard process/prefix convention."
7148   (interactive "P")
7149   (let* ((articles (gnus-summary-work-articles n))
7150          (ogroup gnus-newsgroup-name)
7151          (params (append (gnus-info-params (gnus-get-info ogroup))
7152                          (list (cons 'to-group ogroup))))
7153          article group egroup groups vgroup)
7154     (while (setq article (pop articles))
7155       (setq group (format "%s-%d" gnus-newsgroup-name article))
7156       (gnus-summary-remove-process-mark article)
7157       (when (gnus-summary-display-article article)
7158         (save-excursion
7159           (with-temp-buffer
7160             (insert-buffer-substring gnus-original-article-buffer)
7161             ;; Remove some headers that may lead nndoc to make
7162             ;; the wrong guess.
7163             (message-narrow-to-head)
7164             (goto-char (point-min))
7165             (delete-matching-lines "^\\(Path\\):\\|^From ")
7166             (widen)
7167             (if (setq egroup
7168                       (gnus-group-read-ephemeral-group
7169                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7170                                      (nndoc-article-type guess))
7171                        t nil t))
7172                 (progn
7173                   ;; Make all postings to this group go to the parent group.
7174                   (nconc (gnus-info-params (gnus-get-info egroup))
7175                          params)
7176                   (push egroup groups))
7177               ;; Couldn't select this doc group.
7178               (gnus-error 3 "Article couldn't be entered"))))))
7179     ;; Now we have selected all the documents.
7180     (cond
7181      ((not groups)
7182       (error "None of the articles could be interpreted as documents"))
7183      ((gnus-group-read-ephemeral-group
7184        (setq vgroup (format
7185                      "nnvirtual:%s-%s" gnus-newsgroup-name
7186                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7187        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7188        t
7189        (cons (current-buffer) 'summary)))
7190      (t
7191       (error "Couldn't select virtual nndoc group")))))
7192
7193 (defun gnus-summary-isearch-article (&optional regexp-p)
7194   "Do incremental search forward on the current article.
7195 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7196   (interactive "P")
7197   (gnus-summary-select-article)
7198   (gnus-configure-windows 'article)
7199   (gnus-eval-in-buffer-window gnus-article-buffer
7200     (save-restriction
7201       (widen)
7202       (isearch-forward regexp-p))))
7203
7204 (defun gnus-summary-search-article-forward (regexp &optional backward)
7205   "Search for an article containing REGEXP forward.
7206 If BACKWARD, search backward instead."
7207   (interactive
7208    (list (read-string
7209           (format "Search article %s (regexp%s): "
7210                   (if current-prefix-arg "backward" "forward")
7211                   (if gnus-last-search-regexp
7212                       (concat ", default " gnus-last-search-regexp)
7213                     "")))
7214          current-prefix-arg))
7215   (if (string-equal regexp "")
7216       (setq regexp (or gnus-last-search-regexp ""))
7217     (setq gnus-last-search-regexp regexp)
7218     (setq gnus-article-before-search gnus-current-article))
7219   ;; Intentionally set gnus-last-article.
7220   (setq gnus-last-article gnus-article-before-search)
7221   (let ((gnus-last-article gnus-last-article))
7222     (if (gnus-summary-search-article regexp backward)
7223         (gnus-summary-show-thread)
7224       (error "Search failed: \"%s\"" regexp))))
7225
7226 (defun gnus-summary-search-article-backward (regexp)
7227   "Search for an article containing REGEXP backward."
7228   (interactive
7229    (list (read-string
7230           (format "Search article backward (regexp%s): "
7231                   (if gnus-last-search-regexp
7232                       (concat ", default " gnus-last-search-regexp)
7233                     "")))))
7234   (gnus-summary-search-article-forward regexp 'backward))
7235
7236 (defun gnus-summary-search-article (regexp &optional backward)
7237   "Search for an article containing REGEXP.
7238 Optional argument BACKWARD means do search for backward.
7239 `gnus-select-article-hook' is not called during the search."
7240   ;; We have to require this here to make sure that the following
7241   ;; dynamic binding isn't shadowed by autoloading.
7242   (require 'gnus-async)
7243   (require 'gnus-art)
7244   (let ((gnus-select-article-hook nil)  ;Disable hook.
7245         (gnus-article-prepare-hook nil)
7246         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7247         (gnus-use-article-prefetch nil)
7248         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7249         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7250         (sum (current-buffer))
7251         (gnus-display-mime-function nil)
7252         (found nil)
7253         point)
7254     (gnus-save-hidden-threads
7255       (gnus-summary-select-article)
7256       (set-buffer gnus-article-buffer)
7257       (goto-char (window-point (get-buffer-window (current-buffer))))
7258       (when backward
7259         (forward-line -1))
7260       (while (not found)
7261         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7262         (if (if backward
7263                 (re-search-backward regexp nil t)
7264               (re-search-forward regexp nil t))
7265             ;; We found the regexp.
7266             (progn
7267               (setq found 'found)
7268               (beginning-of-line)
7269               (set-window-start
7270                (get-buffer-window (current-buffer))
7271                (point))
7272               (forward-line 1)
7273               (set-window-point
7274                (get-buffer-window (current-buffer))
7275                (point))
7276               (set-buffer sum)
7277               (setq point (point)))
7278           ;; We didn't find it, so we go to the next article.
7279           (set-buffer sum)
7280           (setq found 'not)
7281           (while (eq found 'not)
7282             (if (not (if backward (gnus-summary-find-prev)
7283                        (gnus-summary-find-next)))
7284                 ;; No more articles.
7285                 (setq found t)
7286               ;; Select the next article and adjust point.
7287               (unless (gnus-summary-article-sparse-p
7288                        (gnus-summary-article-number))
7289                 (setq found nil)
7290                 (gnus-summary-select-article)
7291                 (set-buffer gnus-article-buffer)
7292                 (widen)
7293                 (goto-char (if backward (point-max) (point-min))))))))
7294       (gnus-message 7 ""))
7295     ;; Return whether we found the regexp.
7296     (when (eq found 'found)
7297       (goto-char point)
7298       (gnus-summary-show-thread)
7299       (gnus-summary-goto-subject gnus-current-article)
7300       (gnus-summary-position-point)
7301       t)))
7302
7303 (defun gnus-summary-find-matching (header regexp &optional backward unread
7304                                           not-case-fold)
7305   "Return a list of all articles that match REGEXP on HEADER.
7306 The search stars on the current article and goes forwards unless
7307 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7308 If UNREAD is non-nil, only unread articles will
7309 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7310 in the comparisons."
7311   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7312                 (gnus-data-find-list
7313                  (gnus-summary-article-number) (gnus-data-list backward))))
7314         (case-fold-search (not not-case-fold))
7315         articles d func)
7316     (if (consp header)
7317         (if (eq (car header) 'extra)
7318             (setq func
7319                   `(lambda (h)
7320                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7321                          "")))
7322           (error "%s is an invalid header" header))
7323       (unless (fboundp (intern (concat "mail-header-" header)))
7324         (error "%s is not a valid header" header))
7325       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7326     (while data
7327       (setq d (car data))
7328       (and (or (not unread)             ; We want all articles...
7329                (gnus-data-unread-p d))  ; Or just unreads.
7330            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7331            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7332            (push (gnus-data-number d) articles)) ; Success!
7333       (setq data (cdr data)))
7334     (nreverse articles)))
7335
7336 (defun gnus-summary-execute-command (header regexp command &optional backward)
7337   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7338 If HEADER is an empty string (or nil), the match is done on the entire
7339 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7340   (interactive
7341    (list (let ((completion-ignore-case t))
7342            (completing-read
7343             "Header name: "
7344             (mapcar (lambda (string) (list string))
7345                     '("Number" "Subject" "From" "Lines" "Date"
7346                       "Message-ID" "Xref" "References" "Body"))
7347             nil 'require-match))
7348          (read-string "Regexp: ")
7349          (read-key-sequence "Command: ")
7350          current-prefix-arg))
7351   (when (equal header "Body")
7352     (setq header ""))
7353   ;; Hidden thread subtrees must be searched as well.
7354   (gnus-summary-show-all-threads)
7355   ;; We don't want to change current point nor window configuration.
7356   (save-excursion
7357     (save-window-excursion
7358       (gnus-message 6 "Executing %s..." (key-description command))
7359       ;; We'd like to execute COMMAND interactively so as to give arguments.
7360       (gnus-execute header regexp
7361                     `(call-interactively ',(key-binding command))
7362                     backward)
7363       (gnus-message 6 "Executing %s...done" (key-description command)))))
7364
7365 (defun gnus-summary-beginning-of-article ()
7366   "Scroll the article back to the beginning."
7367   (interactive)
7368   (gnus-summary-select-article)
7369   (gnus-configure-windows 'article)
7370   (gnus-eval-in-buffer-window gnus-article-buffer
7371     (widen)
7372     (goto-char (point-min))
7373     (when gnus-page-broken
7374       (gnus-narrow-to-page))))
7375
7376 (defun gnus-summary-end-of-article ()
7377   "Scroll to the end of the article."
7378   (interactive)
7379   (gnus-summary-select-article)
7380   (gnus-configure-windows 'article)
7381   (gnus-eval-in-buffer-window gnus-article-buffer
7382     (widen)
7383     (goto-char (point-max))
7384     (recenter -3)
7385     (when gnus-page-broken
7386       (gnus-narrow-to-page))))
7387
7388 (defun gnus-summary-print-article (&optional filename n)
7389   "Generate and print a PostScript image of the N next (mail) articles.
7390
7391 If N is negative, print the N previous articles.  If N is nil and articles
7392 have been marked with the process mark, print these instead.
7393
7394 If the optional first argument FILENAME is nil, send the image to the
7395 printer.  If FILENAME is a string, save the PostScript image in a file with
7396 that name.  If FILENAME is a number, prompt the user for the name of the file
7397 to save in."
7398   (interactive (list (ps-print-preprint current-prefix-arg)
7399                      current-prefix-arg))
7400   (dolist (article (gnus-summary-work-articles n))
7401     (gnus-summary-select-article nil nil 'pseudo article)
7402     (gnus-eval-in-buffer-window gnus-article-buffer
7403       (let ((buffer (generate-new-buffer " *print*")))
7404         (unwind-protect
7405             (progn
7406               (copy-to-buffer buffer (point-min) (point-max))
7407               (set-buffer buffer)
7408               (gnus-article-delete-invisible-text)
7409               (let ((ps-left-header
7410                      (list
7411                       (concat "("
7412                               (mail-header-subject gnus-current-headers) ")")
7413                       (concat "("
7414                               (mail-header-from gnus-current-headers) ")")))
7415                     (ps-right-header
7416                      (list
7417                       "/pagenumberstring load"
7418                       (concat "("
7419                               (mail-header-date gnus-current-headers) ")"))))
7420                 (gnus-run-hooks 'gnus-ps-print-hook)
7421                 (save-excursion
7422                   (ps-print-buffer-with-faces filename))))
7423           (kill-buffer buffer))))))
7424
7425 (defun gnus-summary-show-article (&optional arg)
7426   "Force re-fetching of the current article.
7427 If ARG (the prefix) is a number, show the article with the charset
7428 defined in `gnus-summary-show-article-charset-alist', or the charset
7429 inputed.
7430 If ARG (the prefix) is non-nil and not a number, show the raw article
7431 without any article massaging functions being run."
7432   (interactive "P")
7433   (cond
7434    ((numberp arg)
7435     (let ((gnus-newsgroup-charset
7436            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7437                (read-coding-system "Charset: ")))
7438           (gnus-newsgroup-ignored-charsets 'gnus-all))
7439       (gnus-summary-select-article nil 'force)
7440       (let ((deps gnus-newsgroup-dependencies)
7441             head header)
7442         (save-excursion
7443           (set-buffer gnus-original-article-buffer)
7444           (save-restriction
7445             (message-narrow-to-head)
7446             (setq head (buffer-string)))
7447           (with-temp-buffer
7448             (insert (format "211 %d Article retrieved.\n"
7449                             (cdr gnus-article-current)))
7450             (insert head)
7451             (insert ".\n")
7452             (let ((nntp-server-buffer (current-buffer)))
7453               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7454         (gnus-data-set-header
7455          (gnus-data-find (cdr gnus-article-current))
7456          header)
7457         (gnus-summary-update-article-line
7458          (cdr gnus-article-current) header))))
7459    ((not arg)
7460     ;; Select the article the normal way.
7461     (gnus-summary-select-article nil 'force))
7462    (t
7463     ;; We have to require this here to make sure that the following
7464     ;; dynamic binding isn't shadowed by autoloading.
7465     (require 'gnus-async)
7466     (require 'gnus-art)
7467     ;; Bind the article treatment functions to nil.
7468     (let ((gnus-have-all-headers t)
7469           gnus-article-prepare-hook
7470           gnus-article-decode-hook
7471           gnus-display-mime-function
7472           gnus-break-pages)
7473       ;; Destroy any MIME parts.
7474       (when (gnus-buffer-live-p gnus-article-buffer)
7475         (save-excursion
7476           (set-buffer gnus-article-buffer)
7477           (mm-destroy-parts gnus-article-mime-handles)
7478           ;; Set it to nil for safety reason.
7479           (setq gnus-article-mime-handle-alist nil)
7480           (setq gnus-article-mime-handles nil)))
7481       (gnus-summary-select-article nil 'force))))
7482   (gnus-summary-goto-subject gnus-current-article)
7483   (gnus-summary-position-point))
7484
7485 (defun gnus-summary-verbose-headers (&optional arg)
7486   "Toggle permanent full header display.
7487 If ARG is a positive number, turn header display on.
7488 If ARG is a negative number, turn header display off."
7489   (interactive "P")
7490   (setq gnus-show-all-headers
7491         (cond ((or (not (numberp arg))
7492                    (zerop arg))
7493                (not gnus-show-all-headers))
7494               ((natnump arg)
7495                t)))
7496   (gnus-summary-show-article))
7497
7498 (defun gnus-summary-toggle-header (&optional arg)
7499   "Show the headers if they are hidden, or hide them if they are shown.
7500 If ARG is a positive number, show the entire header.
7501 If ARG is a negative number, hide the unwanted header lines."
7502   (interactive "P")
7503   (save-excursion
7504     (set-buffer gnus-article-buffer)
7505     (save-restriction
7506       (let* ((buffer-read-only nil)
7507              (inhibit-point-motion-hooks t)
7508              hidden e)
7509         (setq hidden
7510               (if (numberp arg)
7511                   (>= arg 0)
7512                 (save-restriction
7513                   (article-narrow-to-head)
7514                   (gnus-article-hidden-text-p 'headers))))
7515         (goto-char (point-min))
7516         (when (search-forward "\n\n" nil t)
7517           (delete-region (point-min) (1- (point))))
7518         (goto-char (point-min))
7519         (save-excursion
7520           (set-buffer gnus-original-article-buffer)
7521           (goto-char (point-min))
7522           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7523         (insert-buffer-substring gnus-original-article-buffer 1 e)
7524         (save-restriction
7525           (narrow-to-region (point-min) (point))
7526           (article-decode-encoded-words)
7527           (if  hidden
7528               (let ((gnus-treat-hide-headers nil)
7529                     (gnus-treat-hide-boring-headers nil))
7530                 (setq gnus-article-wash-types
7531                       (delq 'headers gnus-article-wash-types))
7532                 (gnus-treat-article 'head))
7533             (gnus-treat-article 'head)))
7534         (gnus-set-mode-line 'article)))))
7535
7536 (defun gnus-summary-show-all-headers ()
7537   "Make all header lines visible."
7538   (interactive)
7539   (gnus-article-show-all-headers))
7540
7541 (defun gnus-summary-caesar-message (&optional arg)
7542   "Caesar rotate the current article by 13.
7543 The numerical prefix specifies how many places to rotate each letter
7544 forward."
7545   (interactive "P")
7546   (gnus-summary-select-article)
7547   (let ((mail-header-separator ""))
7548     (gnus-eval-in-buffer-window gnus-article-buffer
7549       (save-restriction
7550         (widen)
7551         (let ((start (window-start))
7552               buffer-read-only)
7553           (message-caesar-buffer-body arg)
7554           (set-window-start (get-buffer-window (current-buffer)) start))))))
7555
7556 (defun gnus-summary-stop-page-breaking ()
7557   "Stop page breaking in the current article."
7558   (interactive)
7559   (gnus-summary-select-article)
7560   (gnus-eval-in-buffer-window gnus-article-buffer
7561     (widen)
7562     (when (gnus-visual-p 'page-marker)
7563       (let ((buffer-read-only nil))
7564         (gnus-remove-text-with-property 'gnus-prev)
7565         (gnus-remove-text-with-property 'gnus-next))
7566       (setq gnus-page-broken nil))))
7567
7568 (defun gnus-summary-move-article (&optional n to-newsgroup
7569                                             select-method action)
7570   "Move the current article to a different newsgroup.
7571 If N is a positive number, move the N next articles.
7572 If N is a negative number, move the N previous articles.
7573 If N is nil and any articles have been marked with the process mark,
7574 move those articles instead.
7575 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7576 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7577 re-spool using this method.
7578
7579 For this function to work, both the current newsgroup and the
7580 newsgroup that you want to move to have to support the `request-move'
7581 and `request-accept' functions.
7582
7583 ACTION can be either `move' (the default), `crosspost' or `copy'."
7584   (interactive "P")
7585   (unless action
7586     (setq action 'move))
7587   ;; Disable marking as read.
7588   (let (gnus-mark-article-hook)
7589     (save-window-excursion
7590       (gnus-summary-select-article)))
7591   ;; Check whether the source group supports the required functions.
7592   (cond ((and (eq action 'move)
7593               (not (gnus-check-backend-function
7594                     'request-move-article gnus-newsgroup-name)))
7595          (error "The current group does not support article moving"))
7596         ((and (eq action 'crosspost)
7597               (not (gnus-check-backend-function
7598                     'request-replace-article gnus-newsgroup-name)))
7599          (error "The current group does not support article editing")))
7600   (let ((articles (gnus-summary-work-articles n))
7601         (prefix (if (gnus-check-backend-function
7602                     'request-move-article gnus-newsgroup-name)
7603                     (gnus-group-real-prefix gnus-newsgroup-name)
7604                   ""))
7605         (names '((move "Move" "Moving")
7606                  (copy "Copy" "Copying")
7607                  (crosspost "Crosspost" "Crossposting")))
7608         (copy-buf (save-excursion
7609                     (nnheader-set-temp-buffer " *copy article*")))
7610         art-group to-method new-xref article to-groups)
7611     (unless (assq action names)
7612       (error "Unknown action %s" action))
7613     ;; Read the newsgroup name.
7614     (when (and (not to-newsgroup)
7615                (not select-method))
7616       (setq to-newsgroup
7617             (gnus-read-move-group-name
7618              (cadr (assq action names))
7619              (symbol-value (intern (format "gnus-current-%s-group" action)))
7620              articles prefix))
7621       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7622     (setq to-method (or select-method
7623                         (gnus-server-to-method
7624                          (gnus-group-method to-newsgroup))))
7625     ;; Check the method we are to move this article to...
7626     (unless (gnus-check-backend-function
7627              'request-accept-article (car to-method))
7628       (error "%s does not support article copying" (car to-method)))
7629     (unless (gnus-check-server to-method)
7630       (error "Can't open server %s" (car to-method)))
7631     (gnus-message 6 "%s to %s: %s..."
7632                   (caddr (assq action names))
7633                   (or (car select-method) to-newsgroup) articles)
7634     (while articles
7635       (setq article (pop articles))
7636       (setq
7637        art-group
7638        (cond
7639         ;; Move the article.
7640         ((eq action 'move)
7641          ;; Remove this article from future suppression.
7642          (gnus-dup-unsuppress-article article)
7643          (gnus-request-move-article
7644           article                       ; Article to move
7645           gnus-newsgroup-name           ; From newsgroup
7646           (nth 1 (gnus-find-method-for-group
7647                   gnus-newsgroup-name)) ; Server
7648           (list 'gnus-request-accept-article
7649                 to-newsgroup (list 'quote select-method)
7650                 (not articles) t)       ; Accept form
7651           (not articles)))              ; Only save nov last time
7652         ;; Copy the article.
7653         ((eq action 'copy)
7654          (save-excursion
7655            (set-buffer copy-buf)
7656            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7657              (gnus-request-accept-article
7658               to-newsgroup select-method (not articles) t))))
7659         ;; Crosspost the article.
7660         ((eq action 'crosspost)
7661          (let ((xref (message-tokenize-header
7662                       (mail-header-xref (gnus-summary-article-header article))
7663                       " ")))
7664            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7665                                   ":" article))
7666            (unless xref
7667              (setq xref (list (system-name))))
7668            (setq new-xref
7669                  (concat
7670                   (mapconcat 'identity
7671                              (delete "Xref:" (delete new-xref xref))
7672                              " ")
7673                   " " new-xref))
7674            (save-excursion
7675              (set-buffer copy-buf)
7676              ;; First put the article in the destination group.
7677              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7678              (when (consp (setq art-group
7679                                 (gnus-request-accept-article
7680                                  to-newsgroup select-method (not articles))))
7681                (setq new-xref (concat new-xref " " (car art-group)
7682                                       ":" (cdr art-group)))
7683                ;; Now we have the new Xrefs header, so we insert
7684                ;; it and replace the new article.
7685                (nnheader-replace-header "Xref" new-xref)
7686                (gnus-request-replace-article
7687                 (cdr art-group) to-newsgroup (current-buffer))
7688                art-group))))))
7689       (cond
7690        ((not art-group)
7691         (gnus-message 1 "Couldn't %s article %s: %s"
7692                       (cadr (assq action names)) article
7693                       (nnheader-get-report (car to-method))))
7694        ((eq art-group 'junk)
7695         (when (eq action 'move)
7696           (gnus-summary-mark-article article gnus-canceled-mark)
7697           (gnus-message 4 "Deleted article %s" article)))
7698        (t
7699         (let* ((pto-group (gnus-group-prefixed-name
7700                            (car art-group) to-method))
7701                (entry
7702                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7703                (info (nth 2 entry))
7704                (to-group (gnus-info-group info))
7705                to-marks)
7706           ;; Update the group that has been moved to.
7707           (when (and info
7708                      (memq action '(move copy)))
7709             (unless (member to-group to-groups)
7710               (push to-group to-groups))
7711
7712             (unless (memq article gnus-newsgroup-unreads)
7713               (push 'read to-marks)
7714               (gnus-info-set-read
7715                info (gnus-add-to-range (gnus-info-read info)
7716                                        (list (cdr art-group)))))
7717
7718             ;; See whether the article is to be put in the cache.
7719             (let ((marks gnus-article-mark-lists)
7720                   (to-article (cdr art-group)))
7721
7722               ;; Enter the article into the cache in the new group,
7723               ;; if that is required.
7724               (when gnus-use-cache
7725                 (gnus-cache-possibly-enter-article
7726                  to-group to-article
7727                  (memq article gnus-newsgroup-marked)
7728                  (memq article gnus-newsgroup-dormant)
7729                  (memq article gnus-newsgroup-unreads)))
7730
7731               (when gnus-preserve-marks
7732                 ;; Copy any marks over to the new group.
7733                 (when (and (equal to-group gnus-newsgroup-name)
7734                            (not (memq article gnus-newsgroup-unreads)))
7735                   ;; Mark this article as read in this group.
7736                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7737                   (setcdr (gnus-active to-group) to-article)
7738                   (setcdr gnus-newsgroup-active to-article))
7739
7740                 (while marks
7741                   (when (memq article (symbol-value
7742                                        (intern (format "gnus-newsgroup-%s"
7743                                                        (caar marks)))))
7744                     (push (cdar marks) to-marks)
7745                     ;; If the other group is the same as this group,
7746                     ;; then we have to add the mark to the list.
7747                     (when (equal to-group gnus-newsgroup-name)
7748                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7749                            (cons to-article
7750                                  (symbol-value
7751                                   (intern (format "gnus-newsgroup-%s"
7752                                                   (caar marks)))))))
7753                     ;; Copy the marks to other group.
7754                     (gnus-add-marked-articles
7755                      to-group (cdar marks) (list to-article) info))
7756                   (setq marks (cdr marks)))
7757
7758                 (gnus-request-set-mark to-group (list (list (list to-article)
7759                                                             'set
7760                                                             to-marks))))
7761
7762               (gnus-dribble-enter
7763                (concat "(gnus-group-set-info '"
7764                        (gnus-prin1-to-string (gnus-get-info to-group))
7765                        ")"))))
7766
7767           ;; Update the Xref header in this article to point to
7768           ;; the new crossposted article we have just created.
7769           (when (eq action 'crosspost)
7770             (save-excursion
7771               (set-buffer copy-buf)
7772               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7773               (nnheader-replace-header "Xref" new-xref)
7774               (gnus-request-replace-article
7775                article gnus-newsgroup-name (current-buffer)))))
7776
7777         ;;;!!!Why is this necessary?
7778         (set-buffer gnus-summary-buffer)
7779
7780         (gnus-summary-goto-subject article)
7781         (when (eq action 'move)
7782           (gnus-summary-mark-article article gnus-canceled-mark))))
7783       (gnus-summary-remove-process-mark article))
7784     ;; Re-activate all groups that have been moved to.
7785     (while to-groups
7786       (save-excursion
7787         (set-buffer gnus-group-buffer)
7788         (when (gnus-group-goto-group (car to-groups) t)
7789           (gnus-group-get-new-news-this-group 1 t))
7790         (pop to-groups)))
7791
7792     (gnus-kill-buffer copy-buf)
7793     (gnus-summary-position-point)
7794     (gnus-set-mode-line 'summary)))
7795
7796 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7797   "Move the current article to a different newsgroup.
7798 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7799 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7800 re-spool using this method."
7801   (interactive "P")
7802   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7803
7804 (defun gnus-summary-crosspost-article (&optional n)
7805   "Crosspost the current article to some other group."
7806   (interactive "P")
7807   (gnus-summary-move-article n nil nil 'crosspost))
7808
7809 (defcustom gnus-summary-respool-default-method nil
7810   "Default method for respooling an article.
7811 If nil, use to the current newsgroup method."
7812   :type '(choice (gnus-select-method :value (nnml ""))
7813                  (const nil))
7814   :group 'gnus-summary-mail)
7815
7816 (defun gnus-summary-respool-article (&optional n method)
7817   "Respool the current article.
7818 The article will be squeezed through the mail spooling process again,
7819 which means that it will be put in some mail newsgroup or other
7820 depending on `nnmail-split-methods'.
7821 If N is a positive number, respool the N next articles.
7822 If N is a negative number, respool the N previous articles.
7823 If N is nil and any articles have been marked with the process mark,
7824 respool those articles instead.
7825
7826 Respooling can be done both from mail groups and \"real\" newsgroups.
7827 In the former case, the articles in question will be moved from the
7828 current group into whatever groups they are destined to.  In the
7829 latter case, they will be copied into the relevant groups."
7830   (interactive
7831    (list current-prefix-arg
7832          (let* ((methods (gnus-methods-using 'respool))
7833                 (methname
7834                  (symbol-name (or gnus-summary-respool-default-method
7835                                   (car (gnus-find-method-for-group
7836                                         gnus-newsgroup-name)))))
7837                 (method
7838                  (gnus-completing-read
7839                   methname "What backend do you want to use when respooling?"
7840                   methods nil t nil 'gnus-mail-method-history))
7841                 ms)
7842            (cond
7843             ((zerop (length (setq ms (gnus-servers-using-backend
7844                                       (intern method)))))
7845              (list (intern method) ""))
7846             ((= 1 (length ms))
7847              (car ms))
7848             (t
7849              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7850                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7851                            ms-alist))))))))
7852   (unless method
7853     (error "No method given for respooling"))
7854   (if (assoc (symbol-name
7855               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7856              (gnus-methods-using 'respool))
7857       (gnus-summary-move-article n nil method)
7858     (gnus-summary-copy-article n nil method)))
7859
7860 (defun gnus-summary-import-article (file)
7861   "Import an arbitrary file into a mail newsgroup."
7862   (interactive "fImport file: ")
7863   (let ((group gnus-newsgroup-name)
7864         (now (current-time))
7865         atts lines)
7866     (unless (gnus-check-backend-function 'request-accept-article group)
7867       (error "%s does not support article importing" group))
7868     (or (file-readable-p file)
7869         (not (file-regular-p file))
7870         (error "Can't read %s" file))
7871     (save-excursion
7872       (set-buffer (gnus-get-buffer-create " *import file*"))
7873       (erase-buffer)
7874       (nnheader-insert-file-contents file)
7875       (goto-char (point-min))
7876       (unless (nnheader-article-p)
7877         ;; This doesn't look like an article, so we fudge some headers.
7878         (setq atts (file-attributes file)
7879               lines (count-lines (point-min) (point-max)))
7880         (insert "From: " (read-string "From: ") "\n"
7881                 "Subject: " (read-string "Subject: ") "\n"
7882                 "Date: " (message-make-date (nth 5 atts))
7883                 "\n"
7884                 "Message-ID: " (message-make-message-id) "\n"
7885                 "Lines: " (int-to-string lines) "\n"
7886                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7887       (gnus-request-accept-article group nil t)
7888       (kill-buffer (current-buffer)))))
7889
7890 (defun gnus-summary-article-posted-p ()
7891   "Say whether the current (mail) article is available from news as well.
7892 This will be the case if the article has both been mailed and posted."
7893   (interactive)
7894   (let ((id (mail-header-references (gnus-summary-article-header)))
7895         (gnus-override-method (car (gnus-refer-article-methods))))
7896     (if (gnus-request-head id "")
7897         (gnus-message 2 "The current message was found on %s"
7898                       gnus-override-method)
7899       (gnus-message 2 "The current message couldn't be found on %s"
7900                     gnus-override-method)
7901       nil)))
7902
7903 (defun gnus-summary-expire-articles (&optional now)
7904   "Expire all articles that are marked as expirable in the current group."
7905   (interactive)
7906   (when (gnus-check-backend-function
7907          'request-expire-articles gnus-newsgroup-name)
7908     ;; This backend supports expiry.
7909     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7910            (expirable (if total
7911                           (progn
7912                             ;; We need to update the info for
7913                             ;; this group for `gnus-list-of-read-articles'
7914                             ;; to give us the right answer.
7915                             (gnus-run-hooks 'gnus-exit-group-hook)
7916                             (gnus-summary-update-info)
7917                             (gnus-list-of-read-articles gnus-newsgroup-name))
7918                         (setq gnus-newsgroup-expirable
7919                               (sort gnus-newsgroup-expirable '<))))
7920            (expiry-wait (if now 'immediate
7921                           (gnus-group-find-parameter
7922                            gnus-newsgroup-name 'expiry-wait)))
7923            (nnmail-expiry-target
7924             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7925                 nnmail-expiry-target))
7926            es)
7927       (when expirable
7928         ;; There are expirable articles in this group, so we run them
7929         ;; through the expiry process.
7930         (save-excursion
7931           (gnus-message 6 "Expiring articles...")
7932           (unless (gnus-check-group gnus-newsgroup-name)
7933             (error "Can't open server for %s" gnus-newsgroup-name))
7934           ;; The list of articles that weren't expired is returned.
7935           (save-excursion
7936             (if expiry-wait
7937                 (let ((nnmail-expiry-wait-function nil)
7938                       (nnmail-expiry-wait expiry-wait))
7939                   (setq es (gnus-request-expire-articles
7940                             expirable gnus-newsgroup-name)))
7941               (setq es (gnus-request-expire-articles
7942                         expirable gnus-newsgroup-name)))
7943             (unless total
7944               (setq gnus-newsgroup-expirable es))
7945             ;; We go through the old list of expirable, and mark all
7946             ;; really expired articles as nonexistent.
7947             (unless (eq es expirable)   ;If nothing was expired, we don't mark.
7948               (let ((gnus-use-cache nil))
7949                 (while expirable
7950                   (unless (memq (car expirable) es)
7951                     (when (gnus-data-find (car expirable))
7952                       (gnus-summary-mark-article
7953                        (car expirable) gnus-canceled-mark)))
7954                   (setq expirable (cdr expirable))))))
7955           (gnus-message 6 "Expiring articles...done"))))))
7956
7957 (defun gnus-summary-expire-articles-now ()
7958   "Expunge all expirable articles in the current group.
7959 This means that *all* articles that are marked as expirable will be
7960 deleted forever, right now."
7961   (interactive)
7962   (or gnus-expert-user
7963       (gnus-yes-or-no-p
7964        "Are you really, really, really sure you want to delete all these messages? ")
7965       (error "Phew!"))
7966   (gnus-summary-expire-articles t))
7967
7968 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7969 (defun gnus-summary-delete-article (&optional n)
7970   "Delete the N next (mail) articles.
7971 This command actually deletes articles.  This is not a marking
7972 command.  The article will disappear forever from your life, never to
7973 return.
7974 If N is negative, delete backwards.
7975 If N is nil and articles have been marked with the process mark,
7976 delete these instead."
7977   (interactive "P")
7978   (unless (gnus-check-backend-function 'request-expire-articles
7979                                        gnus-newsgroup-name)
7980     (error "The current newsgroup does not support article deletion"))
7981   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7982     (error "Couldn't open server"))
7983   ;; Compute the list of articles to delete.
7984   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7985         not-deleted)
7986     (if (and gnus-novice-user
7987              (not (gnus-yes-or-no-p
7988                    (format "Do you really want to delete %s forever? "
7989                            (if (> (length articles) 1)
7990                                (format "these %s articles" (length articles))
7991                              "this article")))))
7992         ()
7993       ;; Delete the articles.
7994       (setq not-deleted (gnus-request-expire-articles
7995                          articles gnus-newsgroup-name 'force))
7996       (while articles
7997         (gnus-summary-remove-process-mark (car articles))
7998         ;; The backend might not have been able to delete the article
7999         ;; after all.
8000         (unless (memq (car articles) not-deleted)
8001           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8002         (setq articles (cdr articles)))
8003       (when not-deleted
8004         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8005     (gnus-summary-position-point)
8006     (gnus-set-mode-line 'summary)
8007     not-deleted))
8008
8009 (defun gnus-summary-edit-article (&optional arg)
8010   "Edit the current article.
8011 This will have permanent effect only in mail groups.
8012 If ARG is nil, edit the decoded articles.
8013 If ARG is 1, edit the raw articles. 
8014 If ARG is 2, edit the raw articles even in read-only groups.
8015 If ARG is 3, edit the articles with the current handles.
8016 Otherwise, allow editing of articles even in read-only
8017 groups."
8018   (interactive "P")
8019   (let (force raw current-handles)
8020     (cond 
8021      ((null arg))
8022      ((eq arg 1) (setq raw t))
8023      ((eq arg 2) (setq raw t
8024                        force t))
8025      ((eq arg 3) (setq current-handles 
8026                        (and (gnus-buffer-live-p gnus-article-buffer)
8027                             (with-current-buffer gnus-article-buffer
8028                               (prog1
8029                                   gnus-article-mime-handles
8030                                   (setq gnus-article-mime-handles nil))))))
8031      (t (setq force t)))
8032     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8033         (error "Can't edit the raw article in group nndraft:drafts."))
8034     (save-excursion
8035       (set-buffer gnus-summary-buffer)
8036       (let ((mail-parse-charset gnus-newsgroup-charset)
8037             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8038         (gnus-set-global-variables)
8039         (when (and (not force)
8040                    (gnus-group-read-only-p))
8041           (error "The current newsgroup does not support article editing"))
8042         (gnus-summary-show-article t)
8043         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8044           (with-current-buffer gnus-article-buffer
8045             (mm-enable-multibyte-mule4)))
8046         (if (equal gnus-newsgroup-name "nndraft:drafts")
8047             (setq raw t))
8048         (gnus-article-edit-article
8049          (if raw 'ignore 
8050            `(lambda () 
8051               (let ((mbl mml-buffer-list))
8052                 (setq mml-buffer-list nil)
8053                 (mime-to-mml ,'current-handles)
8054                 (make-local-hook 'kill-buffer-hook)
8055                 (let ((mbl1 mml-buffer-list))
8056                   (setq mml-buffer-list mbl)
8057                   (set (make-local-variable 'mml-buffer-list) mbl1))
8058                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8059          `(lambda (no-highlight)
8060             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8061                   (message-options message-options)
8062                   (message-options-set-recipient)
8063                   (mail-parse-ignored-charsets 
8064                    ',gnus-newsgroup-ignored-charsets))
8065               ,(if (not raw) '(progn 
8066                                 (mml-to-mime)
8067                                 (mml-destroy-buffers)
8068                                 (remove-hook 'kill-buffer-hook 
8069                                              'mml-destroy-buffers t)
8070                                 (kill-local-variable 'mml-buffer-list)))
8071               (gnus-summary-edit-article-done
8072                ,(or (mail-header-references gnus-current-headers) "")
8073                ,(gnus-group-read-only-p) 
8074                ,gnus-summary-buffer no-highlight))))))))
8075
8076 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8077
8078 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8079                                                  no-highlight)
8080   "Make edits to the current article permanent."
8081   (interactive)
8082   (save-excursion
8083     ;; The buffer restriction contains the entire article if it exists.
8084     (when (article-goto-body)
8085       (let ((lines (count-lines (point) (point-max)))
8086             (length (- (point-max) (point)))
8087             (case-fold-search t)
8088             (body (copy-marker (point))))
8089         (goto-char (point-min))
8090         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8091           (delete-region (match-beginning 1) (match-end 1))
8092           (insert (number-to-string length)))
8093         (goto-char (point-min))
8094         (when (re-search-forward
8095                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8096           (delete-region (match-beginning 1) (match-end 1))
8097           (insert (number-to-string length)))
8098         (goto-char (point-min))
8099         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8100           (delete-region (match-beginning 1) (match-end 1))
8101           (insert (number-to-string lines))))))
8102   ;; Replace the article.
8103   (let ((buf (current-buffer)))
8104     (with-temp-buffer
8105       (insert-buffer-substring buf)
8106       
8107       (if (and (not read-only)
8108                (not (gnus-request-replace-article
8109                      (cdr gnus-article-current) (car gnus-article-current)
8110                      (current-buffer) t)))
8111           (error "Couldn't replace article")
8112         ;; Update the summary buffer.
8113         (if (and references
8114                  (equal (message-tokenize-header references " ")
8115                         (message-tokenize-header
8116                          (or (message-fetch-field "references") "") " ")))
8117             ;; We only have to update this line.
8118             (save-excursion
8119               (save-restriction
8120                 (message-narrow-to-head)
8121                 (let ((head (buffer-string))
8122                       header)
8123                   (with-temp-buffer
8124                     (insert (format "211 %d Article retrieved.\n"
8125                                     (cdr gnus-article-current)))
8126                     (insert head)
8127                     (insert ".\n")
8128                     (let ((nntp-server-buffer (current-buffer)))
8129                       (setq header (car (gnus-get-newsgroup-headers
8130                                          (save-excursion
8131                                            (set-buffer gnus-summary-buffer)
8132                                            gnus-newsgroup-dependencies)
8133                                          t))))
8134                     (save-excursion
8135                       (set-buffer gnus-summary-buffer)
8136                       (gnus-data-set-header
8137                        (gnus-data-find (cdr gnus-article-current))
8138                        header)
8139                       (gnus-summary-update-article-line
8140                        (cdr gnus-article-current) header))))))
8141           ;; Update threads.
8142           (set-buffer (or buffer gnus-summary-buffer))
8143           (gnus-summary-update-article (cdr gnus-article-current)))
8144         ;; Prettify the article buffer again.
8145         (unless no-highlight
8146           (save-excursion
8147             (set-buffer gnus-article-buffer)
8148             ;;;!!! Fix this -- article should be rehighlighted.
8149             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8150             (set-buffer gnus-original-article-buffer)
8151             (gnus-request-article
8152              (cdr gnus-article-current)
8153              (car gnus-article-current) (current-buffer))))
8154         ;; Prettify the summary buffer line.
8155         (when (gnus-visual-p 'summary-highlight 'highlight)
8156           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8157
8158 (defun gnus-summary-edit-wash (key)
8159   "Perform editing command KEY in the article buffer."
8160   (interactive
8161    (list
8162     (progn
8163       (message "%s" (concat (this-command-keys) "- "))
8164       (read-char))))
8165   (message "")
8166   (gnus-summary-edit-article)
8167   (execute-kbd-macro (concat (this-command-keys) key))
8168   (gnus-article-edit-done))
8169
8170 ;;; Respooling
8171
8172 (defun gnus-summary-respool-query (&optional silent trace)
8173   "Query where the respool algorithm would put this article."
8174   (interactive)
8175   (let (gnus-mark-article-hook)
8176     (gnus-summary-select-article)
8177     (save-excursion
8178       (set-buffer gnus-original-article-buffer)
8179       (save-restriction
8180         (message-narrow-to-head)
8181         (let ((groups (nnmail-article-group 'identity trace)))
8182           (unless silent
8183             (if groups
8184                 (message "This message would go to %s"
8185                          (mapconcat 'car groups ", "))
8186               (message "This message would go to no groups"))
8187             groups))))))
8188
8189 (defun gnus-summary-respool-trace ()
8190   "Trace where the respool algorithm would put this article.
8191 Display a buffer showing all fancy splitting patterns which matched."
8192   (interactive)
8193   (gnus-summary-respool-query nil t))
8194
8195 ;; Summary marking commands.
8196
8197 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8198   "Mark articles which has the same subject as read, and then select the next.
8199 If UNMARK is positive, remove any kind of mark.
8200 If UNMARK is negative, tick articles."
8201   (interactive "P")
8202   (when unmark
8203     (setq unmark (prefix-numeric-value unmark)))
8204   (let ((count
8205          (gnus-summary-mark-same-subject
8206           (gnus-summary-article-subject) unmark)))
8207     ;; Select next unread article.  If auto-select-same mode, should
8208     ;; select the first unread article.
8209     (gnus-summary-next-article t (and gnus-auto-select-same
8210                                       (gnus-summary-article-subject)))
8211     (gnus-message 7 "%d article%s marked as %s"
8212                   count (if (= count 1) " is" "s are")
8213                   (if unmark "unread" "read"))))
8214
8215 (defun gnus-summary-kill-same-subject (&optional unmark)
8216   "Mark articles which has the same subject as read.
8217 If UNMARK is positive, remove any kind of mark.
8218 If UNMARK is negative, tick articles."
8219   (interactive "P")
8220   (when unmark
8221     (setq unmark (prefix-numeric-value unmark)))
8222   (let ((count
8223          (gnus-summary-mark-same-subject
8224           (gnus-summary-article-subject) unmark)))
8225     ;; If marked as read, go to next unread subject.
8226     (when (null unmark)
8227       ;; Go to next unread subject.
8228       (gnus-summary-next-subject 1 t))
8229     (gnus-message 7 "%d articles are marked as %s"
8230                   count (if unmark "unread" "read"))))
8231
8232 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8233   "Mark articles with same SUBJECT as read, and return marked number.
8234 If optional argument UNMARK is positive, remove any kinds of marks.
8235 If optional argument UNMARK is negative, mark articles as unread instead."
8236   (let ((count 1))
8237     (save-excursion
8238       (cond
8239        ((null unmark)                   ; Mark as read.
8240         (while (and
8241                 (progn
8242                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8243                   (gnus-summary-show-thread) t)
8244                 (gnus-summary-find-subject subject))
8245           (setq count (1+ count))))
8246        ((> unmark 0)                    ; Tick.
8247         (while (and
8248                 (progn
8249                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8250                   (gnus-summary-show-thread) t)
8251                 (gnus-summary-find-subject subject))
8252           (setq count (1+ count))))
8253        (t                               ; Mark as unread.
8254         (while (and
8255                 (progn
8256                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8257                   (gnus-summary-show-thread) t)
8258                 (gnus-summary-find-subject subject))
8259           (setq count (1+ count)))))
8260       (gnus-set-mode-line 'summary)
8261       ;; Return the number of marked articles.
8262       count)))
8263
8264 (defun gnus-summary-mark-as-processable (n &optional unmark)
8265   "Set the process mark on the next N articles.
8266 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8267 the process mark instead.  The difference between N and the actual
8268 number of articles marked is returned."
8269   (interactive "p")
8270   (let ((backward (< n 0))
8271         (n (abs n)))
8272     (while (and
8273             (> n 0)
8274             (if unmark
8275                 (gnus-summary-remove-process-mark
8276                  (gnus-summary-article-number))
8277               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8278             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8279       (setq n (1- n)))
8280     (when (/= 0 n)
8281       (gnus-message 7 "No more articles"))
8282     (gnus-summary-recenter)
8283     (gnus-summary-position-point)
8284     n))
8285
8286 (defun gnus-summary-unmark-as-processable (n)
8287   "Remove the process mark from the next N articles.
8288 If N is negative, unmark backward instead.  The difference between N and
8289 the actual number of articles unmarked is returned."
8290   (interactive "p")
8291   (gnus-summary-mark-as-processable n t))
8292
8293 (defun gnus-summary-unmark-all-processable ()
8294   "Remove the process mark from all articles."
8295   (interactive)
8296   (save-excursion
8297     (while gnus-newsgroup-processable
8298       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8299   (gnus-summary-position-point))
8300
8301 (defun gnus-summary-add-mark (article type)
8302   "Mark ARTICLE with a mark of TYPE."
8303   (let ((vtype (car (assq type gnus-article-mark-lists)))
8304         var)
8305     (if (not vtype)
8306         (error "No such mark type: %s" type)
8307       (setq var (intern (format "gnus-newsgroup-%s" type)))
8308       (set var (cons article (symbol-value var)))
8309       (if (memq type '(processable cached replied saved))
8310           (gnus-summary-update-secondary-mark article)
8311         ;;; !!! This is bobus.  We should find out what primary
8312         ;;; !!! mark we want to set.
8313         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8314         
8315 (defun gnus-summary-mark-as-expirable (n)
8316   "Mark N articles forward as expirable.
8317 If N is negative, mark backward instead.  The difference between N and
8318 the actual number of articles marked is returned."
8319   (interactive "p")
8320   (gnus-summary-mark-forward n gnus-expirable-mark))
8321
8322 (defun gnus-summary-mark-article-as-replied (article)
8323   "Mark ARTICLE replied and update the summary line."
8324   (push article gnus-newsgroup-replied)
8325   (let ((buffer-read-only nil))
8326     (when (gnus-summary-goto-subject article nil t)
8327       (gnus-summary-update-secondary-mark article))))
8328
8329 (defun gnus-summary-set-bookmark (article)
8330   "Set a bookmark in current article."
8331   (interactive (list (gnus-summary-article-number)))
8332   (when (or (not (get-buffer gnus-article-buffer))
8333             (not gnus-current-article)
8334             (not gnus-article-current)
8335             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8336     (error "No current article selected"))
8337   ;; Remove old bookmark, if one exists.
8338   (let ((old (assq article gnus-newsgroup-bookmarks)))
8339     (when old
8340       (setq gnus-newsgroup-bookmarks
8341             (delq old gnus-newsgroup-bookmarks))))
8342   ;; Set the new bookmark, which is on the form
8343   ;; (article-number . line-number-in-body).
8344   (push
8345    (cons article
8346          (save-excursion
8347            (set-buffer gnus-article-buffer)
8348            (count-lines
8349             (min (point)
8350                  (save-excursion
8351                    (goto-char (point-min))
8352                    (search-forward "\n\n" nil t)
8353                    (point)))
8354             (point))))
8355    gnus-newsgroup-bookmarks)
8356   (gnus-message 6 "A bookmark has been added to the current article."))
8357
8358 (defun gnus-summary-remove-bookmark (article)
8359   "Remove the bookmark from the current article."
8360   (interactive (list (gnus-summary-article-number)))
8361   ;; Remove old bookmark, if one exists.
8362   (let ((old (assq article gnus-newsgroup-bookmarks)))
8363     (if old
8364         (progn
8365           (setq gnus-newsgroup-bookmarks
8366                 (delq old gnus-newsgroup-bookmarks))
8367           (gnus-message 6 "Removed bookmark."))
8368       (gnus-message 6 "No bookmark in current article."))))
8369
8370 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8371 (defun gnus-summary-mark-as-dormant (n)
8372   "Mark N articles forward as dormant.
8373 If N is negative, mark backward instead.  The difference between N and
8374 the actual number of articles marked is returned."
8375   (interactive "p")
8376   (gnus-summary-mark-forward n gnus-dormant-mark))
8377
8378 (defun gnus-summary-set-process-mark (article)
8379   "Set the process mark on ARTICLE and update the summary line."
8380   (setq gnus-newsgroup-processable
8381         (cons article
8382               (delq article gnus-newsgroup-processable)))
8383   (when (gnus-summary-goto-subject article)
8384     (gnus-summary-show-thread)
8385     (gnus-summary-goto-subject article)
8386     (gnus-summary-update-secondary-mark article)))
8387
8388 (defun gnus-summary-remove-process-mark (article)
8389   "Remove the process mark from ARTICLE and update the summary line."
8390   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8391   (when (gnus-summary-goto-subject article)
8392     (gnus-summary-show-thread)
8393     (gnus-summary-goto-subject article)
8394     (gnus-summary-update-secondary-mark article)))
8395
8396 (defun gnus-summary-set-saved-mark (article)
8397   "Set the process mark on ARTICLE and update the summary line."
8398   (push article gnus-newsgroup-saved)
8399   (when (gnus-summary-goto-subject article)
8400     (gnus-summary-update-secondary-mark article)))
8401
8402 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8403   "Mark N articles as read forwards.
8404 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8405 The difference between N and the actual number of articles marked is
8406 returned.
8407 Iff NO-EXPIRE, auto-expiry will be inhibited."
8408   (interactive "p")
8409   (gnus-summary-show-thread)
8410   (let ((backward (< n 0))
8411         (gnus-summary-goto-unread
8412          (and gnus-summary-goto-unread
8413               (not (eq gnus-summary-goto-unread 'never))
8414               (not (memq mark (list gnus-unread-mark
8415                                     gnus-ticked-mark gnus-dormant-mark)))))
8416         (n (abs n))
8417         (mark (or mark gnus-del-mark)))
8418     (while (and (> n 0)
8419                 (gnus-summary-mark-article nil mark no-expire)
8420                 (zerop (gnus-summary-next-subject
8421                         (if backward -1 1)
8422                         (and gnus-summary-goto-unread
8423                              (not (eq gnus-summary-goto-unread 'never)))
8424                         t)))
8425       (setq n (1- n)))
8426     (when (/= 0 n)
8427       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8428     (gnus-summary-recenter)
8429     (gnus-summary-position-point)
8430     (gnus-set-mode-line 'summary)
8431     n))
8432
8433 (defun gnus-summary-mark-article-as-read (mark)
8434   "Mark the current article quickly as read with MARK."
8435   (let ((article (gnus-summary-article-number)))
8436     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8437     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8438     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8439     (push (cons article mark) gnus-newsgroup-reads)
8440     ;; Possibly remove from cache, if that is used.
8441     (when gnus-use-cache
8442       (gnus-cache-enter-remove-article article))
8443     ;; Allow the backend to change the mark.
8444     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8445     ;; Check for auto-expiry.
8446     (when (and gnus-newsgroup-auto-expire
8447                (memq mark gnus-auto-expirable-marks))
8448       (setq mark gnus-expirable-mark)
8449       ;; Let the backend know about the mark change.
8450       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8451       (push article gnus-newsgroup-expirable))
8452     ;; Set the mark in the buffer.
8453     (gnus-summary-update-mark mark 'unread)
8454     t))
8455
8456 (defun gnus-summary-mark-article-as-unread (mark)
8457   "Mark the current article quickly as unread with MARK."
8458   (let* ((article (gnus-summary-article-number))
8459          (old-mark (gnus-summary-article-mark article)))
8460     ;; Allow the backend to change the mark.
8461     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8462     (if (eq mark old-mark)
8463         t
8464       (if (<= article 0)
8465           (progn
8466             (gnus-error 1 "Can't mark negative article numbers")
8467             nil)
8468         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8469         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8470         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8471         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8472         (cond ((= mark gnus-ticked-mark)
8473                (push article gnus-newsgroup-marked))
8474               ((= mark gnus-dormant-mark)
8475                (push article gnus-newsgroup-dormant))
8476               (t
8477                (push article gnus-newsgroup-unreads)))
8478         (gnus-pull article gnus-newsgroup-reads)
8479
8480         ;; See whether the article is to be put in the cache.
8481         (and gnus-use-cache
8482              (vectorp (gnus-summary-article-header article))
8483              (save-excursion
8484                (gnus-cache-possibly-enter-article
8485                 gnus-newsgroup-name article
8486                 (= mark gnus-ticked-mark)
8487                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8488
8489         ;; Fix the mark.
8490         (gnus-summary-update-mark mark 'unread)
8491         t))))
8492
8493 (defun gnus-summary-mark-article (&optional article mark no-expire)
8494   "Mark ARTICLE with MARK.  MARK can be any character.
8495 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8496 `??' (dormant) and `?E' (expirable).
8497 If MARK is nil, then the default character `?r' is used.
8498 If ARTICLE is nil, then the article on the current line will be
8499 marked.
8500 Iff NO-EXPIRE, auto-expiry will be inhibited."
8501   ;; The mark might be a string.
8502   (when (stringp mark)
8503     (setq mark (aref mark 0)))
8504   ;; If no mark is given, then we check auto-expiring.
8505   (when (null mark)
8506     (setq mark gnus-del-mark))
8507   (when (and (not no-expire)
8508              gnus-newsgroup-auto-expire
8509              (memq mark gnus-auto-expirable-marks))
8510     (setq mark gnus-expirable-mark))
8511   (let ((article (or article (gnus-summary-article-number)))
8512         (old-mark (gnus-summary-article-mark article)))
8513     ;; Allow the backend to change the mark.
8514     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8515     (if (eq mark old-mark)
8516         t
8517       (unless article
8518         (error "No article on current line"))
8519       (if (not (if (or (= mark gnus-unread-mark)
8520                        (= mark gnus-ticked-mark)
8521                        (= mark gnus-dormant-mark))
8522                    (gnus-mark-article-as-unread article mark)
8523                  (gnus-mark-article-as-read article mark)))
8524           t
8525         ;; See whether the article is to be put in the cache.
8526         (and gnus-use-cache
8527              (not (= mark gnus-canceled-mark))
8528              (vectorp (gnus-summary-article-header article))
8529              (save-excursion
8530                (gnus-cache-possibly-enter-article
8531                 gnus-newsgroup-name article
8532                 (= mark gnus-ticked-mark)
8533                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8534
8535         (when (gnus-summary-goto-subject article nil t)
8536           (let ((buffer-read-only nil))
8537             (gnus-summary-show-thread)
8538             ;; Fix the mark.
8539             (gnus-summary-update-mark mark 'unread)
8540             t))))))
8541
8542 (defun gnus-summary-update-secondary-mark (article)
8543   "Update the secondary (read, process, cache) mark."
8544   (gnus-summary-update-mark
8545    (cond ((memq article gnus-newsgroup-processable)
8546           gnus-process-mark)
8547          ((memq article gnus-newsgroup-cached)
8548           gnus-cached-mark)
8549          ((memq article gnus-newsgroup-replied)
8550           gnus-replied-mark)
8551          ((memq article gnus-newsgroup-saved)
8552           gnus-saved-mark)
8553          (t gnus-no-mark))
8554    'replied)
8555   (when (gnus-visual-p 'summary-highlight 'highlight)
8556     (gnus-run-hooks 'gnus-summary-update-hook))
8557   t)
8558
8559 (defun gnus-summary-update-mark (mark type)
8560   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8561         (buffer-read-only nil))
8562     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8563     (when forward
8564       (when (looking-at "\r")
8565         (incf forward))
8566       (when (<= (+ forward (point)) (point-max))
8567         ;; Go to the right position on the line.
8568         (goto-char (+ forward (point)))
8569         ;; Replace the old mark with the new mark.
8570         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8571         ;; Optionally update the marks by some user rule.
8572         (when (eq type 'unread)
8573           (gnus-data-set-mark
8574            (gnus-data-find (gnus-summary-article-number)) mark)
8575           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8576
8577 (defun gnus-mark-article-as-read (article &optional mark)
8578   "Enter ARTICLE in the pertinent lists and remove it from others."
8579   ;; Make the article expirable.
8580   (let ((mark (or mark gnus-del-mark)))
8581     (if (= mark gnus-expirable-mark)
8582         (push article gnus-newsgroup-expirable)
8583       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8584     ;; Remove from unread and marked lists.
8585     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8586     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8587     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8588     (push (cons article mark) gnus-newsgroup-reads)
8589     ;; Possibly remove from cache, if that is used.
8590     (when gnus-use-cache
8591       (gnus-cache-enter-remove-article article))
8592     t))
8593
8594 (defun gnus-mark-article-as-unread (article &optional mark)
8595   "Enter ARTICLE in the pertinent lists and remove it from others."
8596   (let ((mark (or mark gnus-ticked-mark)))
8597     (if (<= article 0)
8598         (progn
8599           (gnus-error 1 "Can't mark negative article numbers")
8600           nil)
8601       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8602             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8603             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8604             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8605
8606       ;; Unsuppress duplicates?
8607       (when gnus-suppress-duplicates
8608         (gnus-dup-unsuppress-article article))
8609
8610       (cond ((= mark gnus-ticked-mark)
8611              (push article gnus-newsgroup-marked))
8612             ((= mark gnus-dormant-mark)
8613              (push article gnus-newsgroup-dormant))
8614             (t
8615              (push article gnus-newsgroup-unreads)))
8616       (gnus-pull article gnus-newsgroup-reads)
8617       t)))
8618
8619 (defalias 'gnus-summary-mark-as-unread-forward
8620   'gnus-summary-tick-article-forward)
8621 (make-obsolete 'gnus-summary-mark-as-unread-forward
8622                'gnus-summary-tick-article-forward)
8623 (defun gnus-summary-tick-article-forward (n)
8624   "Tick N articles forwards.
8625 If N is negative, tick backwards instead.
8626 The difference between N and the number of articles ticked is returned."
8627   (interactive "p")
8628   (gnus-summary-mark-forward n gnus-ticked-mark))
8629
8630 (defalias 'gnus-summary-mark-as-unread-backward
8631   'gnus-summary-tick-article-backward)
8632 (make-obsolete 'gnus-summary-mark-as-unread-backward
8633                'gnus-summary-tick-article-backward)
8634 (defun gnus-summary-tick-article-backward (n)
8635   "Tick N articles backwards.
8636 The difference between N and the number of articles ticked is returned."
8637   (interactive "p")
8638   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8639
8640 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8641 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8642 (defun gnus-summary-tick-article (&optional article clear-mark)
8643   "Mark current article as unread.
8644 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8645 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8646   (interactive)
8647   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8648                                        gnus-ticked-mark)))
8649
8650 (defun gnus-summary-mark-as-read-forward (n)
8651   "Mark N articles as read forwards.
8652 If N is negative, mark backwards instead.
8653 The difference between N and the actual number of articles marked is
8654 returned."
8655   (interactive "p")
8656   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8657
8658 (defun gnus-summary-mark-as-read-backward (n)
8659   "Mark the N articles as read backwards.
8660 The difference between N and the actual number of articles marked is
8661 returned."
8662   (interactive "p")
8663   (gnus-summary-mark-forward
8664    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8665
8666 (defun gnus-summary-mark-as-read (&optional article mark)
8667   "Mark current article as read.
8668 ARTICLE specifies the article to be marked as read.
8669 MARK specifies a string to be inserted at the beginning of the line."
8670   (gnus-summary-mark-article article mark))
8671
8672 (defun gnus-summary-clear-mark-forward (n)
8673   "Clear marks from N articles forward.
8674 If N is negative, clear backward instead.
8675 The difference between N and the number of marks cleared is returned."
8676   (interactive "p")
8677   (gnus-summary-mark-forward n gnus-unread-mark))
8678
8679 (defun gnus-summary-clear-mark-backward (n)
8680   "Clear marks from N articles backward.
8681 The difference between N and the number of marks cleared is returned."
8682   (interactive "p")
8683   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8684
8685 (defun gnus-summary-mark-unread-as-read ()
8686   "Intended to be used by `gnus-summary-mark-article-hook'."
8687   (when (memq gnus-current-article gnus-newsgroup-unreads)
8688     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8689
8690 (defun gnus-summary-mark-read-and-unread-as-read ()
8691   "Intended to be used by `gnus-summary-mark-article-hook'."
8692   (let ((mark (gnus-summary-article-mark)))
8693     (when (or (gnus-unread-mark-p mark)
8694               (gnus-read-mark-p mark))
8695       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8696
8697 (defun gnus-summary-mark-unread-as-ticked ()
8698    "Intended to be used by `gnus-summary-mark-article-hook'."
8699   (when (memq gnus-current-article gnus-newsgroup-unreads)
8700     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8701
8702 (defun gnus-summary-mark-region-as-read (point mark all)
8703   "Mark all unread articles between point and mark as read.
8704 If given a prefix, mark all articles between point and mark as read,
8705 even ticked and dormant ones."
8706   (interactive "r\nP")
8707   (save-excursion
8708     (let (article)
8709       (goto-char point)
8710       (beginning-of-line)
8711       (while (and
8712               (< (point) mark)
8713               (progn
8714                 (when (or all
8715                           (memq (setq article (gnus-summary-article-number))
8716                                 gnus-newsgroup-unreads))
8717                   (gnus-summary-mark-article article gnus-del-mark))
8718                 t)
8719               (gnus-summary-find-next))))))
8720
8721 (defun gnus-summary-mark-below (score mark)
8722   "Mark articles with score less than SCORE with MARK."
8723   (interactive "P\ncMark: ")
8724   (setq score (if score
8725                   (prefix-numeric-value score)
8726                 (or gnus-summary-default-score 0)))
8727   (save-excursion
8728     (set-buffer gnus-summary-buffer)
8729     (goto-char (point-min))
8730     (while
8731         (progn
8732           (and (< (gnus-summary-article-score) score)
8733                (gnus-summary-mark-article nil mark))
8734           (gnus-summary-find-next)))))
8735
8736 (defun gnus-summary-kill-below (&optional score)
8737   "Mark articles with score below SCORE as read."
8738   (interactive "P")
8739   (gnus-summary-mark-below score gnus-killed-mark))
8740
8741 (defun gnus-summary-clear-above (&optional score)
8742   "Clear all marks from articles with score above SCORE."
8743   (interactive "P")
8744   (gnus-summary-mark-above score gnus-unread-mark))
8745
8746 (defun gnus-summary-tick-above (&optional score)
8747   "Tick all articles with score above SCORE."
8748   (interactive "P")
8749   (gnus-summary-mark-above score gnus-ticked-mark))
8750
8751 (defun gnus-summary-mark-above (score mark)
8752   "Mark articles with score over SCORE with MARK."
8753   (interactive "P\ncMark: ")
8754   (setq score (if score
8755                   (prefix-numeric-value score)
8756                 (or gnus-summary-default-score 0)))
8757   (save-excursion
8758     (set-buffer gnus-summary-buffer)
8759     (goto-char (point-min))
8760     (while (and (progn
8761                   (when (> (gnus-summary-article-score) score)
8762                     (gnus-summary-mark-article nil mark))
8763                   t)
8764                 (gnus-summary-find-next)))))
8765
8766 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8767 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8768 (defun gnus-summary-limit-include-expunged (&optional no-error)
8769   "Display all the hidden articles that were expunged for low scores."
8770   (interactive)
8771   (let ((buffer-read-only nil))
8772     (let ((scored gnus-newsgroup-scored)
8773           headers h)
8774       (while scored
8775         (unless (gnus-number-to-header (caar scored))
8776           (and (setq h (gnus-summary-article-header (caar scored)))
8777                (< (cdar scored) gnus-summary-expunge-below)
8778                (push h headers)))
8779         (setq scored (cdr scored)))
8780       (if (not headers)
8781           (when (not no-error)
8782             (error "No expunged articles hidden"))
8783         (goto-char (point-min))
8784         (gnus-summary-prepare-unthreaded (nreverse headers))
8785         (goto-char (point-min))
8786         (gnus-summary-position-point)
8787         t))))
8788
8789 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8790   "Mark all unread articles in this newsgroup as read.
8791 If prefix argument ALL is non-nil, ticked and dormant articles will
8792 also be marked as read.
8793 If QUIETLY is non-nil, no questions will be asked.
8794 If TO-HERE is non-nil, it should be a point in the buffer.  All
8795 articles before this point will be marked as read.
8796 Note that this function will only catch up the unread article
8797 in the current summary buffer limitation.
8798 The number of articles marked as read is returned."
8799   (interactive "P")
8800   (prog1
8801       (save-excursion
8802         (when (or quietly
8803                   (not gnus-interactive-catchup) ;Without confirmation?
8804                   gnus-expert-user
8805                   (gnus-y-or-n-p
8806                    (if all
8807                        "Mark absolutely all articles as read? "
8808                      "Mark all unread articles as read? ")))
8809           (if (and not-mark
8810                    (not gnus-newsgroup-adaptive)
8811                    (not gnus-newsgroup-auto-expire)
8812                    (not gnus-suppress-duplicates)
8813                    (or (not gnus-use-cache)
8814                        (eq gnus-use-cache 'passive)))
8815               (progn
8816                 (when all
8817                   (setq gnus-newsgroup-marked nil
8818                         gnus-newsgroup-dormant nil))
8819                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8820             ;; We actually mark all articles as canceled, which we
8821             ;; have to do when using auto-expiry or adaptive scoring.
8822             (gnus-summary-show-all-threads)
8823             (when (gnus-summary-first-subject (not all) t)
8824               (while (and
8825                       (if to-here (< (point) to-here) t)
8826                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8827                       (gnus-summary-find-next (not all) nil nil t))))
8828             (gnus-set-mode-line 'summary))
8829           t))
8830     (gnus-summary-position-point)))
8831
8832 (defun gnus-summary-catchup-to-here (&optional all)
8833   "Mark all unticked articles before the current one as read.
8834 If ALL is non-nil, also mark ticked and dormant articles as read."
8835   (interactive "P")
8836   (save-excursion
8837     (gnus-save-hidden-threads
8838       (let ((beg (point)))
8839         ;; We check that there are unread articles.
8840         (when (or all (gnus-summary-find-prev))
8841           (gnus-summary-catchup all t beg)))))
8842   (gnus-summary-position-point))
8843
8844 (defun gnus-summary-catchup-all (&optional quietly)
8845   "Mark all articles in this newsgroup as read."
8846   (interactive "P")
8847   (gnus-summary-catchup t quietly))
8848
8849 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8850   "Mark all unread articles in this group as read, then exit.
8851 If prefix argument ALL is non-nil, all articles are marked as read."
8852   (interactive "P")
8853   (when (gnus-summary-catchup all quietly nil 'fast)
8854     ;; Select next newsgroup or exit.
8855     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8856              (eq gnus-auto-select-next 'quietly))
8857         (gnus-summary-next-group nil)
8858       (gnus-summary-exit))))
8859
8860 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8861   "Mark all articles in this newsgroup as read, and then exit."
8862   (interactive "P")
8863   (gnus-summary-catchup-and-exit t quietly))
8864
8865 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8866   "Mark all articles in this group as read and select the next group.
8867 If given a prefix, mark all articles, unread as well as ticked, as
8868 read."
8869   (interactive "P")
8870   (save-excursion
8871     (gnus-summary-catchup all))
8872   (gnus-summary-next-group))
8873
8874 ;;;
8875 ;;; with article
8876 ;;;
8877
8878 (defmacro gnus-with-article (article &rest forms)
8879   "Select ARTICLE and perform FORMS in the original article buffer.
8880 Then replace the article with the result."
8881   `(progn
8882      ;; We don't want the article to be marked as read.
8883      (let (gnus-mark-article-hook)
8884        (gnus-summary-select-article t t nil ,article))
8885      (set-buffer gnus-original-article-buffer)
8886      ,@forms
8887      (if (not (gnus-check-backend-function
8888                'request-replace-article (car gnus-article-current)))
8889          (gnus-message 5 "Read-only group; not replacing")
8890        (unless (gnus-request-replace-article
8891                 ,article (car gnus-article-current)
8892                 (current-buffer) t)
8893          (error "Couldn't replace article")))
8894      ;; The cache and backlog have to be flushed somewhat.
8895      (when gnus-keep-backlog
8896        (gnus-backlog-remove-article
8897         (car gnus-article-current) (cdr gnus-article-current)))
8898      (when gnus-use-cache
8899        (gnus-cache-update-article
8900         (car gnus-article-current) (cdr gnus-article-current)))))
8901
8902 (put 'gnus-with-article 'lisp-indent-function 1)
8903 (put 'gnus-with-article 'edebug-form-spec '(form body))
8904
8905 ;; Thread-based commands.
8906
8907 (defun gnus-summary-articles-in-thread (&optional article)
8908   "Return a list of all articles in the current thread.
8909 If ARTICLE is non-nil, return all articles in the thread that starts
8910 with that article."
8911   (let* ((article (or article (gnus-summary-article-number)))
8912          (data (gnus-data-find-list article))
8913          (top-level (gnus-data-level (car data)))
8914          (top-subject
8915           (cond ((null gnus-thread-operation-ignore-subject)
8916                  (gnus-simplify-subject-re
8917                   (mail-header-subject (gnus-data-header (car data)))))
8918                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8919                  (gnus-simplify-subject-fuzzy
8920                   (mail-header-subject (gnus-data-header (car data)))))
8921                 (t nil)))
8922          (end-point (save-excursion
8923                       (if (gnus-summary-go-to-next-thread)
8924                           (point) (point-max))))
8925          articles)
8926     (while (and data
8927                 (< (gnus-data-pos (car data)) end-point))
8928       (when (or (not top-subject)
8929                 (string= top-subject
8930                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8931                              (gnus-simplify-subject-fuzzy
8932                               (mail-header-subject
8933                                (gnus-data-header (car data))))
8934                            (gnus-simplify-subject-re
8935                             (mail-header-subject
8936                              (gnus-data-header (car data)))))))
8937         (push (gnus-data-number (car data)) articles))
8938       (unless (and (setq data (cdr data))
8939                    (> (gnus-data-level (car data)) top-level))
8940         (setq data nil)))
8941     ;; Return the list of articles.
8942     (nreverse articles)))
8943
8944 (defun gnus-summary-rethread-current ()
8945   "Rethread the thread the current article is part of."
8946   (interactive)
8947   (let* ((gnus-show-threads t)
8948          (article (gnus-summary-article-number))
8949          (id (mail-header-id (gnus-summary-article-header)))
8950          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8951     (unless id
8952       (error "No article on the current line"))
8953     (gnus-rebuild-thread id)
8954     (gnus-summary-goto-subject article)))
8955
8956 (defun gnus-summary-reparent-thread ()
8957   "Make the current article child of the marked (or previous) article.
8958
8959 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8960 is non-nil or the Subject: of both articles are the same."
8961   (interactive)
8962   (unless (not (gnus-group-read-only-p))
8963     (error "The current newsgroup does not support article editing"))
8964   (unless (<= (length gnus-newsgroup-processable) 1)
8965     (error "No more than one article may be marked"))
8966   (save-window-excursion
8967     (let ((gnus-article-buffer " *reparent*")
8968           (current-article (gnus-summary-article-number))
8969           ;; First grab the marked article, otherwise one line up.
8970           (parent-article (if (not (null gnus-newsgroup-processable))
8971                               (car gnus-newsgroup-processable)
8972                             (save-excursion
8973                               (if (eq (forward-line -1) 0)
8974                                   (gnus-summary-article-number)
8975                                 (error "Beginning of summary buffer"))))))
8976       (unless (not (eq current-article parent-article))
8977         (error "An article may not be self-referential"))
8978       (let ((message-id (mail-header-id
8979                          (gnus-summary-article-header parent-article))))
8980         (unless (and message-id (not (equal message-id "")))
8981           (error "No message-id in desired parent"))
8982         (gnus-with-article current-article
8983           (save-restriction
8984             (goto-char (point-min))
8985             (message-narrow-to-head)
8986             (if (re-search-forward "^References: " nil t)
8987                 (progn
8988                   (re-search-forward "^[^ \t]" nil t)
8989                   (forward-line -1)
8990                   (end-of-line)
8991                   (insert " " message-id))
8992               (insert "References: " message-id "\n"))))
8993         (set-buffer gnus-summary-buffer)
8994         (gnus-summary-unmark-all-processable)
8995         (gnus-summary-update-article current-article)
8996         (gnus-summary-rethread-current)
8997         (gnus-message 3 "Article %d is now the child of article %d"
8998                       current-article parent-article)))))
8999
9000 (defun gnus-summary-toggle-threads (&optional arg)
9001   "Toggle showing conversation threads.
9002 If ARG is positive number, turn showing conversation threads on."
9003   (interactive "P")
9004   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9005     (setq gnus-show-threads
9006           (if (null arg) (not gnus-show-threads)
9007             (> (prefix-numeric-value arg) 0)))
9008     (gnus-summary-prepare)
9009     (gnus-summary-goto-subject current)
9010     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9011     (gnus-summary-position-point)))
9012
9013 (defun gnus-summary-show-all-threads ()
9014   "Show all threads."
9015   (interactive)
9016   (save-excursion
9017     (let ((buffer-read-only nil))
9018       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9019   (gnus-summary-position-point))
9020
9021 (defun gnus-summary-show-thread ()
9022   "Show thread subtrees.
9023 Returns nil if no thread was there to be shown."
9024   (interactive)
9025   (let ((buffer-read-only nil)
9026         (orig (point))
9027         ;; first goto end then to beg, to have point at beg after let
9028         (end (progn (end-of-line) (point)))
9029         (beg (progn (beginning-of-line) (point))))
9030     (prog1
9031         ;; Any hidden lines here?
9032         (search-forward "\r" end t)
9033       (subst-char-in-region beg end ?\^M ?\n t)
9034       (goto-char orig)
9035       (gnus-summary-position-point))))
9036
9037 (defun gnus-summary-hide-all-threads ()
9038   "Hide all thread subtrees."
9039   (interactive)
9040   (save-excursion
9041     (goto-char (point-min))
9042     (gnus-summary-hide-thread)
9043     (while (zerop (gnus-summary-next-thread 1 t))
9044       (gnus-summary-hide-thread)))
9045   (gnus-summary-position-point))
9046
9047 (defun gnus-summary-hide-thread ()
9048   "Hide thread subtrees.
9049 Returns nil if no threads were there to be hidden."
9050   (interactive)
9051   (let ((buffer-read-only nil)
9052         (start (point))
9053         (article (gnus-summary-article-number)))
9054     (goto-char start)
9055     ;; Go forward until either the buffer ends or the subthread
9056     ;; ends.
9057     (when (and (not (eobp))
9058                (or (zerop (gnus-summary-next-thread 1 t))
9059                    (goto-char (point-max))))
9060       (prog1
9061           (if (and (> (point) start)
9062                    (search-backward "\n" start t))
9063               (progn
9064                 (subst-char-in-region start (point) ?\n ?\^M)
9065                 (gnus-summary-goto-subject article))
9066             (goto-char start)
9067             nil)))))
9068
9069 (defun gnus-summary-go-to-next-thread (&optional previous)
9070   "Go to the same level (or less) next thread.
9071 If PREVIOUS is non-nil, go to previous thread instead.
9072 Return the article number moved to, or nil if moving was impossible."
9073   (let ((level (gnus-summary-thread-level))
9074         (way (if previous -1 1))
9075         (beg (point)))
9076     (forward-line way)
9077     (while (and (not (eobp))
9078                 (< level (gnus-summary-thread-level)))
9079       (forward-line way))
9080     (if (eobp)
9081         (progn
9082           (goto-char beg)
9083           nil)
9084       (setq beg (point))
9085       (prog1
9086           (gnus-summary-article-number)
9087         (goto-char beg)))))
9088
9089 (defun gnus-summary-next-thread (n &optional silent)
9090   "Go to the same level next N'th thread.
9091 If N is negative, search backward instead.
9092 Returns the difference between N and the number of skips actually
9093 done.
9094
9095 If SILENT, don't output messages."
9096   (interactive "p")
9097   (let ((backward (< n 0))
9098         (n (abs n)))
9099     (while (and (> n 0)
9100                 (gnus-summary-go-to-next-thread backward))
9101       (decf n))
9102     (unless silent
9103       (gnus-summary-position-point))
9104     (when (and (not silent) (/= 0 n))
9105       (gnus-message 7 "No more threads"))
9106     n))
9107
9108 (defun gnus-summary-prev-thread (n)
9109   "Go to the same level previous N'th thread.
9110 Returns the difference between N and the number of skips actually
9111 done."
9112   (interactive "p")
9113   (gnus-summary-next-thread (- n)))
9114
9115 (defun gnus-summary-go-down-thread ()
9116   "Go down one level in the current thread."
9117   (let ((children (gnus-summary-article-children)))
9118     (when children
9119       (gnus-summary-goto-subject (car children)))))
9120
9121 (defun gnus-summary-go-up-thread ()
9122   "Go up one level in the current thread."
9123   (let ((parent (gnus-summary-article-parent)))
9124     (when parent
9125       (gnus-summary-goto-subject parent))))
9126
9127 (defun gnus-summary-down-thread (n)
9128   "Go down thread N steps.
9129 If N is negative, go up instead.
9130 Returns the difference between N and how many steps down that were
9131 taken."
9132   (interactive "p")
9133   (let ((up (< n 0))
9134         (n (abs n)))
9135     (while (and (> n 0)
9136                 (if up (gnus-summary-go-up-thread)
9137                   (gnus-summary-go-down-thread)))
9138       (setq n (1- n)))
9139     (gnus-summary-position-point)
9140     (when (/= 0 n)
9141       (gnus-message 7 "Can't go further"))
9142     n))
9143
9144 (defun gnus-summary-up-thread (n)
9145   "Go up thread N steps.
9146 If N is negative, go up instead.
9147 Returns the difference between N and how many steps down that were
9148 taken."
9149   (interactive "p")
9150   (gnus-summary-down-thread (- n)))
9151
9152 (defun gnus-summary-top-thread ()
9153   "Go to the top of the thread."
9154   (interactive)
9155   (while (gnus-summary-go-up-thread))
9156   (gnus-summary-article-number))
9157
9158 (defun gnus-summary-kill-thread (&optional unmark)
9159   "Mark articles under current thread as read.
9160 If the prefix argument is positive, remove any kinds of marks.
9161 If the prefix argument is negative, tick articles instead."
9162   (interactive "P")
9163   (when unmark
9164     (setq unmark (prefix-numeric-value unmark)))
9165   (let ((articles (gnus-summary-articles-in-thread)))
9166     (save-excursion
9167       ;; Expand the thread.
9168       (gnus-summary-show-thread)
9169       ;; Mark all the articles.
9170       (while articles
9171         (gnus-summary-goto-subject (car articles))
9172         (cond ((null unmark)
9173                (gnus-summary-mark-article-as-read gnus-killed-mark))
9174               ((> unmark 0)
9175                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9176               (t
9177                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9178         (setq articles (cdr articles))))
9179     ;; Hide killed subtrees.
9180     (and (null unmark)
9181          gnus-thread-hide-killed
9182          (gnus-summary-hide-thread))
9183     ;; If marked as read, go to next unread subject.
9184     (when (null unmark)
9185       ;; Go to next unread subject.
9186       (gnus-summary-next-subject 1 t)))
9187   (gnus-set-mode-line 'summary))
9188
9189 ;; Summary sorting commands
9190
9191 (defun gnus-summary-sort-by-number (&optional reverse)
9192   "Sort the summary buffer by article number.
9193 Argument REVERSE means reverse order."
9194   (interactive "P")
9195   (gnus-summary-sort 'number reverse))
9196
9197 (defun gnus-summary-sort-by-author (&optional reverse)
9198   "Sort the summary buffer by author name alphabetically.
9199 If `case-fold-search' is non-nil, case of letters is ignored.
9200 Argument REVERSE means reverse order."
9201   (interactive "P")
9202   (gnus-summary-sort 'author reverse))
9203
9204 (defun gnus-summary-sort-by-subject (&optional reverse)
9205   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9206 If `case-fold-search' is non-nil, case of letters is ignored.
9207 Argument REVERSE means reverse order."
9208   (interactive "P")
9209   (gnus-summary-sort 'subject reverse))
9210
9211 (defun gnus-summary-sort-by-date (&optional reverse)
9212   "Sort the summary buffer by date.
9213 Argument REVERSE means reverse order."
9214   (interactive "P")
9215   (gnus-summary-sort 'date reverse))
9216
9217 (defun gnus-summary-sort-by-score (&optional reverse)
9218   "Sort the summary buffer by score.
9219 Argument REVERSE means reverse order."
9220   (interactive "P")
9221   (gnus-summary-sort 'score reverse))
9222
9223 (defun gnus-summary-sort-by-lines (&optional reverse)
9224   "Sort the summary buffer by the number of lines.
9225 Argument REVERSE means reverse order."
9226   (interactive "P")
9227   (gnus-summary-sort 'lines reverse))
9228
9229 (defun gnus-summary-sort-by-chars (&optional reverse)
9230   "Sort the summary buffer by article length.
9231 Argument REVERSE means reverse order."
9232   (interactive "P")
9233   (gnus-summary-sort 'chars reverse))
9234
9235 (defun gnus-summary-sort (predicate reverse)
9236   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9237   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9238          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9239          (gnus-thread-sort-functions
9240           (if (not reverse)
9241               thread
9242             `(lambda (t1 t2)
9243                (,thread t2 t1))))
9244          (gnus-sort-gathered-threads-function
9245           gnus-thread-sort-functions)
9246          (gnus-article-sort-functions
9247           (if (not reverse)
9248               article
9249             `(lambda (t1 t2)
9250                (,article t2 t1))))
9251          (buffer-read-only)
9252          (gnus-summary-prepare-hook nil))
9253     ;; We do the sorting by regenerating the threads.
9254     (gnus-summary-prepare)
9255     ;; Hide subthreads if needed.
9256     (when (and gnus-show-threads gnus-thread-hide-subtree)
9257       (gnus-summary-hide-all-threads))))
9258
9259 ;; Summary saving commands.
9260
9261 (defun gnus-summary-save-article (&optional n not-saved)
9262   "Save the current article using the default saver function.
9263 If N is a positive number, save the N next articles.
9264 If N is a negative number, save the N previous articles.
9265 If N is nil and any articles have been marked with the process mark,
9266 save those articles instead.
9267 The variable `gnus-default-article-saver' specifies the saver function."
9268   (interactive "P")
9269   (let* ((articles (gnus-summary-work-articles n))
9270          (save-buffer (save-excursion
9271                         (nnheader-set-temp-buffer " *Gnus Save*")))
9272          (num (length articles))
9273          header file)
9274     (dolist (article articles)
9275       (setq header (gnus-summary-article-header article))
9276       (if (not (vectorp header))
9277           ;; This is a pseudo-article.
9278           (if (assq 'name header)
9279               (gnus-copy-file (cdr (assq 'name header)))
9280             (gnus-message 1 "Article %d is unsaveable" article))
9281         ;; This is a real article.
9282         (save-window-excursion
9283           (gnus-summary-select-article t nil nil article))
9284         (save-excursion
9285           (set-buffer save-buffer)
9286           (erase-buffer)
9287           (insert-buffer-substring gnus-original-article-buffer))
9288         (setq file (gnus-article-save save-buffer file num))
9289         (gnus-summary-remove-process-mark article)
9290         (unless not-saved
9291           (gnus-summary-set-saved-mark article))))
9292     (gnus-kill-buffer save-buffer)
9293     (gnus-summary-position-point)
9294     (gnus-set-mode-line 'summary)
9295     n))
9296
9297 (defun gnus-summary-pipe-output (&optional arg)
9298   "Pipe the current article to a subprocess.
9299 If N is a positive number, pipe the N next articles.
9300 If N is a negative number, pipe the N previous articles.
9301 If N is nil and any articles have been marked with the process mark,
9302 pipe those articles instead."
9303   (interactive "P")
9304   (require 'gnus-art)
9305   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9306     (gnus-summary-save-article arg t))
9307   (gnus-configure-windows 'pipe))
9308
9309 (defun gnus-summary-save-article-mail (&optional arg)
9310   "Append the current article to an mail file.
9311 If N is a positive number, save the N next articles.
9312 If N is a negative number, save the N previous articles.
9313 If N is nil and any articles have been marked with the process mark,
9314 save those articles instead."
9315   (interactive "P")
9316   (require 'gnus-art)
9317   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9318     (gnus-summary-save-article arg)))
9319
9320 (defun gnus-summary-save-article-rmail (&optional arg)
9321   "Append the current article to an rmail file.
9322 If N is a positive number, save the N next articles.
9323 If N is a negative number, save the N previous articles.
9324 If N is nil and any articles have been marked with the process mark,
9325 save those articles instead."
9326   (interactive "P")
9327   (require 'gnus-art)
9328   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9329     (gnus-summary-save-article arg)))
9330
9331 (defun gnus-summary-save-article-file (&optional arg)
9332   "Append the current article to a file.
9333 If N is a positive number, save the N next articles.
9334 If N is a negative number, save the N previous articles.
9335 If N is nil and any articles have been marked with the process mark,
9336 save those articles instead."
9337   (interactive "P")
9338   (require 'gnus-art)
9339   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9340     (gnus-summary-save-article arg)))
9341
9342 (defun gnus-summary-write-article-file (&optional arg)
9343   "Write the current article to a file, deleting the previous file.
9344 If N is a positive number, save the N next articles.
9345 If N is a negative number, save the N previous articles.
9346 If N is nil and any articles have been marked with the process mark,
9347 save those articles instead."
9348   (interactive "P")
9349   (require 'gnus-art)
9350   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9351     (gnus-summary-save-article arg)))
9352
9353 (defun gnus-summary-save-article-body-file (&optional arg)
9354   "Append the current article body to a file.
9355 If N is a positive number, save the N next articles.
9356 If N is a negative number, save the N previous articles.
9357 If N is nil and any articles have been marked with the process mark,
9358 save those articles instead."
9359   (interactive "P")
9360   (require 'gnus-art)
9361   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9362     (gnus-summary-save-article arg)))
9363
9364 (defun gnus-summary-pipe-message (program)
9365   "Pipe the current article through PROGRAM."
9366   (interactive "sProgram: ")
9367   (gnus-summary-select-article)
9368   (let ((mail-header-separator ""))
9369     (gnus-eval-in-buffer-window gnus-article-buffer
9370       (save-restriction
9371         (widen)
9372         (let ((start (window-start))
9373               buffer-read-only)
9374           (message-pipe-buffer-body program)
9375           (set-window-start (get-buffer-window (current-buffer)) start))))))
9376
9377 (defun gnus-get-split-value (methods)
9378   "Return a value based on the split METHODS."
9379   (let (split-name method result match)
9380     (when methods
9381       (save-excursion
9382         (set-buffer gnus-original-article-buffer)
9383         (save-restriction
9384           (nnheader-narrow-to-headers)
9385           (while (and methods (not split-name))
9386             (goto-char (point-min))
9387             (setq method (pop methods))
9388             (setq match (car method))
9389             (when (cond
9390                    ((stringp match)
9391                     ;; Regular expression.
9392                     (ignore-errors
9393                       (re-search-forward match nil t)))
9394                    ((gnus-functionp match)
9395                     ;; Function.
9396                     (save-restriction
9397                       (widen)
9398                       (setq result (funcall match gnus-newsgroup-name))))
9399                    ((consp match)
9400                     ;; Form.
9401                     (save-restriction
9402                       (widen)
9403                       (setq result (eval match)))))
9404               (setq split-name (cdr method))
9405               (cond ((stringp result)
9406                      (push (expand-file-name
9407                             result gnus-article-save-directory)
9408                            split-name))
9409                     ((consp result)
9410                      (setq split-name (append result split-name)))))))))
9411     (nreverse split-name)))
9412
9413 (defun gnus-valid-move-group-p (group)
9414   (and (boundp group)
9415        (symbol-name group)
9416        (symbol-value group)
9417        (gnus-get-function (gnus-find-method-for-group
9418                            (symbol-name group)) 'request-accept-article t)))
9419
9420 (defun gnus-read-move-group-name (prompt default articles prefix)
9421   "Read a group name."
9422   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9423          (minibuffer-confirm-incomplete nil) ; XEmacs
9424          (prom
9425           (format "%s %s to:"
9426                   prompt
9427                   (if (> (length articles) 1)
9428                       (format "these %d articles" (length articles))
9429                     "this article")))
9430          (to-newsgroup
9431           (cond
9432            ((null split-name)
9433             (gnus-completing-read default prom
9434                                   gnus-active-hashtb
9435                                   'gnus-valid-move-group-p
9436                                   nil prefix
9437                                   'gnus-group-history))
9438            ((= 1 (length split-name))
9439             (gnus-completing-read (car split-name) prom
9440                                   gnus-active-hashtb
9441                                   'gnus-valid-move-group-p
9442                                   nil nil
9443                                   'gnus-group-history))
9444            (t
9445             (gnus-completing-read nil prom
9446                                   (mapcar (lambda (el) (list el))
9447                                           (nreverse split-name))
9448                                   nil nil nil
9449                                   'gnus-group-history))))
9450          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9451     (when to-newsgroup
9452       (if (or (string= to-newsgroup "")
9453               (string= to-newsgroup prefix))
9454           (setq to-newsgroup default))
9455       (unless to-newsgroup
9456         (error "No group name entered"))
9457       (or (gnus-active to-newsgroup)
9458           (gnus-activate-group to-newsgroup nil nil to-method)
9459           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9460                                      to-newsgroup))
9461               (or (and (gnus-request-create-group to-newsgroup to-method)
9462                        (gnus-activate-group
9463                         to-newsgroup nil nil to-method)
9464                        (gnus-subscribe-group to-newsgroup))
9465                   (error "Couldn't create group %s" to-newsgroup)))
9466           (error "No such group: %s" to-newsgroup)))
9467     to-newsgroup))
9468
9469 (defun gnus-summary-save-parts (type dir n &optional reverse)
9470   "Save parts matching TYPE to DIR.
9471 If REVERSE, save parts that do not match TYPE."
9472   (interactive
9473    (list (read-string "Save parts of type: " 
9474                       (or (car gnus-summary-save-parts-type-history)
9475                           gnus-summary-save-parts-default-mime)
9476                       'gnus-summary-save-parts-type-history)
9477          (setq gnus-summary-save-parts-last-directory
9478                (read-file-name "Save to directory: " 
9479                                gnus-summary-save-parts-last-directory
9480                                nil t))
9481          current-prefix-arg))
9482   (gnus-summary-iterate n
9483     (let ((gnus-display-mime-function nil)
9484           (gnus-inhibit-treatment t))
9485       (gnus-summary-select-article))
9486     (save-excursion
9487       (set-buffer gnus-article-buffer)
9488       (let ((handles (or gnus-article-mime-handles
9489                          (mm-dissect-buffer) (mm-uu-dissect))))
9490         (when handles
9491           (gnus-summary-save-parts-1 type dir handles reverse)
9492           (unless gnus-article-mime-handles ;; Don't destroy this case.
9493             (mm-destroy-parts handles)))))))
9494
9495 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9496   (if (stringp (car handle))
9497       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9498               (cdr handle))
9499     (when (if reverse
9500               (not (string-match type (mm-handle-media-type handle)))
9501             (string-match type (mm-handle-media-type handle)))
9502       (let ((file (expand-file-name
9503                    (file-name-nondirectory
9504                     (or
9505                      (mail-content-type-get
9506                       (mm-handle-disposition handle) 'filename)
9507                      (concat gnus-newsgroup-name
9508                              "." (number-to-string
9509                                   (cdr gnus-article-current)))))
9510                    dir)))
9511         (unless (file-exists-p file)
9512           (mm-save-part-to-file handle file))))))
9513
9514 ;; Summary extract commands
9515
9516 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9517   (let ((buffer-read-only nil)
9518         (article (gnus-summary-article-number))
9519         after-article b e)
9520     (unless (gnus-summary-goto-subject article)
9521       (error "No such article: %d" article))
9522     (gnus-summary-position-point)
9523     ;; If all commands are to be bunched up on one line, we collect
9524     ;; them here.
9525     (unless gnus-view-pseudos-separately
9526       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9527             files action)
9528         (while ps
9529           (setq action (cdr (assq 'action (car ps))))
9530           (setq files (list (cdr (assq 'name (car ps)))))
9531           (while (and ps (cdr ps)
9532                       (string= (or action "1")
9533                                (or (cdr (assq 'action (cadr ps))) "2")))
9534             (push (cdr (assq 'name (cadr ps))) files)
9535             (setcdr ps (cddr ps)))
9536           (when files
9537             (when (not (string-match "%s" action))
9538               (push " " files))
9539             (push " " files)
9540             (when (assq 'execute (car ps))
9541               (setcdr (assq 'execute (car ps))
9542                       (funcall (if (string-match "%s" action)
9543                                    'format 'concat)
9544                                action
9545                                (mapconcat
9546                                 (lambda (f)
9547                                   (if (equal f " ")
9548                                       f
9549                                     (mm-quote-arg f)))
9550                                 files " ")))))
9551           (setq ps (cdr ps)))))
9552     (if (and gnus-view-pseudos (not not-view))
9553         (while pslist
9554           (when (assq 'execute (car pslist))
9555             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9556                                   (eq gnus-view-pseudos 'not-confirm)))
9557           (setq pslist (cdr pslist)))
9558       (save-excursion
9559         (while pslist
9560           (setq after-article (or (cdr (assq 'article (car pslist)))
9561                                   (gnus-summary-article-number)))
9562           (gnus-summary-goto-subject after-article)
9563           (forward-line 1)
9564           (setq b (point))
9565           (insert "    " (file-name-nondirectory
9566                           (cdr (assq 'name (car pslist))))
9567                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9568           (setq e (point))
9569           (forward-line -1)             ; back to `b'
9570           (gnus-add-text-properties
9571            b (1- e) (list 'gnus-number gnus-reffed-article-number
9572                           gnus-mouse-face-prop gnus-mouse-face))
9573           (gnus-data-enter
9574            after-article gnus-reffed-article-number
9575            gnus-unread-mark b (car pslist) 0 (- e b))
9576           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9577           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9578           (setq pslist (cdr pslist)))))))
9579
9580 (defun gnus-pseudos< (p1 p2)
9581   (let ((c1 (cdr (assq 'action p1)))
9582         (c2 (cdr (assq 'action p2))))
9583     (and c1 c2 (string< c1 c2))))
9584
9585 (defun gnus-request-pseudo-article (props)
9586   (cond ((assq 'execute props)
9587          (gnus-execute-command (cdr (assq 'execute props)))))
9588   (let ((gnus-current-article (gnus-summary-article-number)))
9589     (gnus-run-hooks 'gnus-mark-article-hook)))
9590
9591 (defun gnus-execute-command (command &optional automatic)
9592   (save-excursion
9593     (gnus-article-setup-buffer)
9594     (set-buffer gnus-article-buffer)
9595     (setq buffer-read-only nil)
9596     (let ((command (if automatic command
9597                      (read-string "Command: " (cons command 0)))))
9598       (erase-buffer)
9599       (insert "$ " command "\n\n")
9600       (if gnus-view-pseudo-asynchronously
9601           (start-process "gnus-execute" (current-buffer) shell-file-name
9602                          shell-command-switch command)
9603         (call-process shell-file-name nil t nil
9604                       shell-command-switch command)))))
9605
9606 ;; Summary kill commands.
9607
9608 (defun gnus-summary-edit-global-kill (article)
9609   "Edit the \"global\" kill file."
9610   (interactive (list (gnus-summary-article-number)))
9611   (gnus-group-edit-global-kill article))
9612
9613 (defun gnus-summary-edit-local-kill ()
9614   "Edit a local kill file applied to the current newsgroup."
9615   (interactive)
9616   (setq gnus-current-headers (gnus-summary-article-header))
9617   (gnus-group-edit-local-kill
9618    (gnus-summary-article-number) gnus-newsgroup-name))
9619
9620 ;;; Header reading.
9621
9622 (defun gnus-read-header (id &optional header)
9623   "Read the headers of article ID and enter them into the Gnus system."
9624   (let ((group gnus-newsgroup-name)
9625         (gnus-override-method
9626          (or
9627           gnus-override-method
9628           (and (gnus-news-group-p gnus-newsgroup-name)
9629                (car (gnus-refer-article-methods)))))
9630         where)
9631     ;; First we check to see whether the header in question is already
9632     ;; fetched.
9633     (if (stringp id)
9634         ;; This is a Message-ID.
9635         (setq header (or header (gnus-id-to-header id)))
9636       ;; This is an article number.
9637       (setq header (or header (gnus-summary-article-header id))))
9638     (if (and header
9639              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9640         ;; We have found the header.
9641         header
9642       ;; If this is a sparse article, we have to nix out its
9643       ;; previous entry in the thread hashtb.
9644       (when (and header
9645                  (gnus-summary-article-sparse-p (mail-header-number header)))
9646         (let* ((parent (gnus-parent-id (mail-header-references header)))
9647                (thread (and parent (gnus-id-to-thread parent))))
9648           (when thread
9649             (delq (assq header thread) thread))))
9650       ;; We have to really fetch the header to this article.
9651       (save-excursion
9652         (set-buffer nntp-server-buffer)
9653         (when (setq where (gnus-request-head id group))
9654           (nnheader-fold-continuation-lines)
9655           (goto-char (point-max))
9656           (insert ".\n")
9657           (goto-char (point-min))
9658           (insert "211 ")
9659           (princ (cond
9660                   ((numberp id) id)
9661                   ((cdr where) (cdr where))
9662                   (header (mail-header-number header))
9663                   (t gnus-reffed-article-number))
9664                  (current-buffer))
9665           (insert " Article retrieved.\n"))
9666         (if (or (not where)
9667                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9668             ()                          ; Malformed head.
9669           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9670             (when (and (stringp id)
9671                        (not (string= (gnus-group-real-name group)
9672                                      (car where))))
9673               ;; If we fetched by Message-ID and the article came
9674               ;; from a different group, we fudge some bogus article
9675               ;; numbers for this article.
9676               (mail-header-set-number header gnus-reffed-article-number))
9677             (save-excursion
9678               (set-buffer gnus-summary-buffer)
9679               (decf gnus-reffed-article-number)
9680               (gnus-remove-header (mail-header-number header))
9681               (push header gnus-newsgroup-headers)
9682               (setq gnus-current-headers header)
9683               (push (mail-header-number header) gnus-newsgroup-limit)))
9684           header)))))
9685
9686 (defun gnus-remove-header (number)
9687   "Remove header NUMBER from `gnus-newsgroup-headers'."
9688   (if (and gnus-newsgroup-headers
9689            (= number (mail-header-number (car gnus-newsgroup-headers))))
9690       (pop gnus-newsgroup-headers)
9691     (let ((headers gnus-newsgroup-headers))
9692       (while (and (cdr headers)
9693                   (not (= number (mail-header-number (cadr headers)))))
9694         (pop headers))
9695       (when (cdr headers)
9696         (setcdr headers (cddr headers))))))
9697
9698 ;;;
9699 ;;; summary highlights
9700 ;;;
9701
9702 (defun gnus-highlight-selected-summary ()
9703   "Highlight selected article in summary buffer."
9704   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9705   (when gnus-summary-selected-face
9706     (save-excursion
9707       (let* ((beg (progn (beginning-of-line) (point)))
9708              (end (progn (end-of-line) (point)))
9709              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9710              (from (if (get-text-property beg gnus-mouse-face-prop)
9711                        beg
9712                      (or (next-single-property-change
9713                           beg gnus-mouse-face-prop nil end)
9714                          beg)))
9715              (to
9716               (if (= from end)
9717                   (- from 2)
9718                 (or (next-single-property-change
9719                      from gnus-mouse-face-prop nil end)
9720                     end))))
9721         ;; If no mouse-face prop on line we will have to = from = end,
9722         ;; so we highlight the entire line instead.
9723         (when (= (+ to 2) from)
9724           (setq from beg)
9725           (setq to end))
9726         (if gnus-newsgroup-selected-overlay
9727             ;; Move old overlay.
9728             (gnus-move-overlay
9729              gnus-newsgroup-selected-overlay from to (current-buffer))
9730           ;; Create new overlay.
9731           (gnus-overlay-put
9732            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9733            'face gnus-summary-selected-face))))))
9734
9735 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9736 (defun gnus-summary-highlight-line ()
9737   "Highlight current line according to `gnus-summary-highlight'."
9738   (let* ((list gnus-summary-highlight)
9739          (p (point))
9740          (end (progn (end-of-line) (point)))
9741          ;; now find out where the line starts and leave point there.
9742          (beg (progn (beginning-of-line) (point)))
9743          (article (gnus-summary-article-number))
9744          (score (or (cdr (assq (or article gnus-current-article)
9745                                gnus-newsgroup-scored))
9746                     gnus-summary-default-score 0))
9747          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9748          (inhibit-read-only t))
9749     ;; Eval the cars of the lists until we find a match.
9750     (let ((default gnus-summary-default-score))
9751       (while (and list
9752                   (not (eval (caar list))))
9753         (setq list (cdr list))))
9754     (let ((face (cdar list)))
9755       (unless (eq face (get-text-property beg 'face))
9756         (gnus-put-text-property-excluding-characters-with-faces
9757          beg end 'face
9758          (setq face (if (boundp face) (symbol-value face) face)))
9759         (when gnus-summary-highlight-line-function
9760           (funcall gnus-summary-highlight-line-function article face))))
9761     (goto-char p)))
9762
9763 (defun gnus-update-read-articles (group unread &optional compute)
9764   "Update the list of read articles in GROUP."
9765   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9766          (entry (gnus-gethash group gnus-newsrc-hashtb))
9767          (info (nth 2 entry))
9768          (prev 1)
9769          (unread (sort (copy-sequence unread) '<))
9770          read)
9771     (if (or (not info) (not active))
9772         ;; There is no info on this group if it was, in fact,
9773         ;; killed.  Gnus stores no information on killed groups, so
9774         ;; there's nothing to be done.
9775         ;; One could store the information somewhere temporarily,
9776         ;; perhaps...  Hmmm...
9777         ()
9778       ;; Remove any negative articles numbers.
9779       (while (and unread (< (car unread) 0))
9780         (setq unread (cdr unread)))
9781       ;; Remove any expired article numbers
9782       (while (and unread (< (car unread) (car active)))
9783         (setq unread (cdr unread)))
9784       ;; Compute the ranges of read articles by looking at the list of
9785       ;; unread articles.
9786       (while unread
9787         (when (/= (car unread) prev)
9788           (push (if (= prev (1- (car unread))) prev
9789                   (cons prev (1- (car unread))))
9790                 read))
9791         (setq prev (1+ (car unread)))
9792         (setq unread (cdr unread)))
9793       (when (<= prev (cdr active))
9794         (push (cons prev (cdr active)) read))
9795       (setq read (if (> (length read) 1) (nreverse read) read))
9796       (if compute
9797           read
9798         (save-excursion
9799           (let (setmarkundo)
9800             ;; Propagate the read marks to the backend.
9801             (when (gnus-check-backend-function 'request-set-mark group)
9802               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9803                     (add (gnus-remove-from-range read (gnus-info-read info))))
9804                 (when (or add del)
9805                   (unless (gnus-check-group group)
9806                     (error "Can't open server for %s" group))
9807                   (gnus-request-set-mark
9808                    group (delq nil (list (if add (list add 'add '(read)))
9809                                          (if del (list del 'del '(read))))))
9810                   (setq setmarkundo
9811                         `(gnus-request-set-mark
9812                           ,group
9813                           ',(delq nil (list
9814                                        (if del (list del 'add '(read)))
9815                                        (if add (list add 'del '(read))))))))))
9816             (set-buffer gnus-group-buffer)
9817             (gnus-undo-register
9818               `(progn
9819                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9820                  (gnus-info-set-read ',info ',(gnus-info-read info))
9821                  (gnus-get-unread-articles-in-group ',info 
9822                                                     (gnus-active ,group))
9823                  (gnus-group-update-group ,group t)
9824                  ,setmarkundo))))
9825         ;; Enter this list into the group info.
9826         (gnus-info-set-read info read)
9827         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9828         (gnus-get-unread-articles-in-group info (gnus-active group))
9829         t))))
9830
9831 (defun gnus-offer-save-summaries ()
9832   "Offer to save all active summary buffers."
9833   (save-excursion
9834     (let ((buflist (buffer-list))
9835           buffers bufname)
9836       ;; Go through all buffers and find all summaries.
9837       (while buflist
9838         (and (setq bufname (buffer-name (car buflist)))
9839              (string-match "Summary" bufname)
9840              (save-excursion
9841                (set-buffer bufname)
9842                ;; We check that this is, indeed, a summary buffer.
9843                (and (eq major-mode 'gnus-summary-mode)
9844                     ;; Also make sure this isn't bogus.
9845                     gnus-newsgroup-prepared
9846                     ;; Also make sure that this isn't a dead summary buffer.
9847                     (not gnus-dead-summary-mode)))
9848              (push bufname buffers))
9849         (setq buflist (cdr buflist)))
9850       ;; Go through all these summary buffers and offer to save them.
9851       (when buffers
9852         (map-y-or-n-p
9853          "Update summary buffer %s? "
9854          (lambda (buf)
9855            (switch-to-buffer buf)
9856            (gnus-summary-exit))
9857          buffers)))))
9858
9859 (defun gnus-summary-setup-default-charset ()
9860   "Setup newsgroup default charset."
9861   (if (equal gnus-newsgroup-name "nndraft:drafts")
9862       (setq gnus-newsgroup-charset nil)
9863     (let* ((name (and gnus-newsgroup-name
9864                       (gnus-group-real-name gnus-newsgroup-name)))
9865            (ignored-charsets
9866             (or gnus-newsgroup-ephemeral-ignored-charsets
9867                 (append
9868                  (and gnus-newsgroup-name
9869                       (or (gnus-group-find-parameter gnus-newsgroup-name
9870                                                      'ignored-charsets t)
9871                           (let ((alist gnus-group-ignored-charsets-alist)
9872                                 elem (charsets nil))
9873                             (while (setq elem (pop alist))
9874                               (when (and name
9875                                          (string-match (car elem) name))
9876                                 (setq alist nil
9877                                       charsets (cdr elem))))
9878                             charsets)))
9879                  gnus-newsgroup-ignored-charsets))))
9880       (setq gnus-newsgroup-charset
9881             (or gnus-newsgroup-ephemeral-charset
9882                 (and gnus-newsgroup-name
9883                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9884                          (let ((alist gnus-group-charset-alist)
9885                                elem charset)
9886                            (while (setq elem (pop alist))
9887                              (when (and name
9888                                         (string-match (car elem) name))
9889                                (setq alist nil
9890                                      charset (cadr elem))))
9891                            charset)))
9892                 gnus-default-charset))
9893       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9894            ignored-charsets))))
9895
9896 ;;;
9897 ;;; Mime Commands
9898 ;;;
9899
9900 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9901   "Display the current article buffer fully MIME-buttonized.
9902 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9903 treated as multipart/mixed."
9904   (interactive "P")
9905   (require 'gnus-art)
9906   (let ((gnus-unbuttonized-mime-types nil)
9907         (gnus-mime-display-multipart-as-mixed show-all-parts))
9908     (gnus-summary-show-article)))
9909
9910 (defun gnus-summary-repair-multipart (article)
9911   "Add a Content-Type header to a multipart article without one."
9912   (interactive (list (gnus-summary-article-number)))
9913   (gnus-with-article article
9914     (message-narrow-to-head)
9915     (message-remove-header "Mime-Version")
9916     (goto-char (point-max))
9917     (insert "Mime-Version: 1.0\n")
9918     (widen)
9919     (when (search-forward "\n--" nil t)
9920       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9921         (message-narrow-to-head)
9922         (message-remove-header "Content-Type")
9923         (goto-char (point-max))
9924         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9925                         separator))
9926         (widen))))
9927   (let (gnus-mark-article-hook)
9928     (gnus-summary-select-article t t nil article)))
9929
9930 (defun gnus-summary-toggle-display-buttonized ()
9931   "Toggle the buttonizing of the article buffer."
9932   (interactive)
9933   (require 'gnus-art)
9934   (if (setq gnus-inhibit-mime-unbuttonizing
9935             (not gnus-inhibit-mime-unbuttonizing))
9936       (let ((gnus-unbuttonized-mime-types nil))
9937         (gnus-summary-show-article))
9938     (gnus-summary-show-article)))
9939
9940 ;;;
9941 ;;; Generic summary marking commands
9942 ;;;
9943
9944 (defvar gnus-summary-marking-alist
9945   '((read gnus-del-mark "d")
9946     (unread gnus-unread-mark "u")
9947     (ticked gnus-ticked-mark "!")
9948     (dormant gnus-dormant-mark "?")
9949     (expirable gnus-expirable-mark "e"))
9950   "An alist of names/marks/keystrokes.")
9951
9952 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9953 (defvar gnus-summary-mark-map)
9954
9955 (defun gnus-summary-make-all-marking-commands ()
9956   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9957   (dolist (elem gnus-summary-marking-alist)
9958     (apply 'gnus-summary-make-marking-command elem)))
9959
9960 (defun gnus-summary-make-marking-command (name mark keystroke)
9961   (let ((map (make-sparse-keymap)))
9962     (define-key gnus-summary-generic-mark-map keystroke map)
9963     (dolist (lway `((next "next" next nil "n")
9964                     (next-unread "next unread" next t "N")
9965                     (prev "previous" prev nil "p")
9966                     (prev-unread "previous unread" prev t "P")
9967                     (nomove "" nil nil ,keystroke)))
9968       (let ((func (gnus-summary-make-marking-command-1
9969                    mark (car lway) lway name)))
9970         (setq func (eval func))
9971         (define-key map (nth 4 lway) func)))))
9972       
9973 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9974   `(defun ,(intern
9975             (format "gnus-summary-put-mark-as-%s%s"
9976                     name (if (eq way 'nomove)
9977                              ""
9978                            (concat "-" (symbol-name way)))))
9979      (n)
9980      ,(format
9981        "Mark the current article as %s%s.
9982 If N, the prefix, then repeat N times.
9983 If N is negative, move in reverse order.
9984 The difference between N and the actual number of articles marked is
9985 returned."
9986        name (cadr lway))
9987      (interactive "p")
9988      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9989     
9990 (defun gnus-summary-generic-mark (n mark move unread)
9991   "Mark N articles with MARK."
9992   (unless (eq major-mode 'gnus-summary-mode)
9993     (error "This command can only be used in the summary buffer"))
9994   (gnus-summary-show-thread)
9995   (let ((nummove
9996          (cond
9997           ((eq move 'next) 1)
9998           ((eq move 'prev) -1)
9999           (t 0))))
10000     (if (zerop nummove)
10001         (setq n 1)
10002       (when (< n 0)
10003         (setq n (abs n)
10004               nummove (* -1 nummove))))
10005     (while (and (> n 0)
10006                 (gnus-summary-mark-article nil mark)
10007                 (zerop (gnus-summary-next-subject nummove unread t)))
10008       (setq n (1- n)))
10009     (when (/= 0 n)
10010       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10011     (gnus-summary-recenter)
10012     (gnus-summary-position-point)
10013     (gnus-set-mode-line 'summary)
10014     n))
10015
10016 (gnus-summary-make-all-marking-commands)
10017
10018 (gnus-ems-redefine)
10019
10020 (provide 'gnus-sum)
10021
10022 (run-hooks 'gnus-sum-load-hook)
10023
10024 ;;; gnus-sum.el ends here