2001-01-09 Michael Downes <mjd@ams.org>
[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       (let ((grpinfo (gnus-get-info group)))
2981         (if (null (gnus-info-read grpinfo))
2982             (gnus-message 3 "Group %s contains no messages" group)
2983           (gnus-message 3 "Can't select group")))
2984       nil)
2985      ;; The user did a `C-g' while prompting for number of articles,
2986      ;; so we exit this group.
2987      ((eq did-select 'quit)
2988       (and (eq major-mode 'gnus-summary-mode)
2989            (not (equal (current-buffer) kill-buffer))
2990            (kill-buffer (current-buffer)))
2991       (when kill-buffer
2992         (gnus-kill-or-deaden-summary kill-buffer))
2993       (if (not quit-config)
2994           (progn
2995             (set-buffer gnus-group-buffer)
2996             (gnus-group-jump-to-group group)
2997             (gnus-group-next-unread-group 1)
2998             (gnus-configure-windows 'group 'force))
2999         (gnus-handle-ephemeral-exit quit-config))
3000       ;; Finally signal the quit.
3001       (signal 'quit nil))
3002      ;; The group was successfully selected.
3003      (t
3004       (gnus-set-global-variables)
3005       ;; Save the active value in effect when the group was entered.
3006       (setq gnus-newsgroup-active
3007             (gnus-copy-sequence
3008              (gnus-active gnus-newsgroup-name)))
3009       ;; You can change the summary buffer in some way with this hook.
3010       (gnus-run-hooks 'gnus-select-group-hook)
3011       ;; Set any local variables in the group parameters.
3012       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3013       (gnus-update-format-specifications
3014        nil 'summary 'summary-mode 'summary-dummy)
3015       (gnus-update-summary-mark-positions)
3016       ;; Do score processing.
3017       (when gnus-use-scoring
3018         (gnus-possibly-score-headers))
3019       ;; Check whether to fill in the gaps in the threads.
3020       (when gnus-build-sparse-threads
3021         (gnus-build-sparse-threads))
3022       ;; Find the initial limit.
3023       (if gnus-show-threads
3024           (if show-all
3025               (let ((gnus-newsgroup-dormant nil))
3026                 (gnus-summary-initial-limit show-all))
3027             (gnus-summary-initial-limit show-all))
3028         ;; When untreaded, all articles are always shown.
3029         (setq gnus-newsgroup-limit
3030               (mapcar
3031                (lambda (header) (mail-header-number header))
3032                gnus-newsgroup-headers)))
3033       ;; Generate the summary buffer.
3034       (unless no-display
3035         (gnus-summary-prepare))
3036       (when gnus-use-trees
3037         (gnus-tree-open group)
3038         (setq gnus-summary-highlight-line-function
3039               'gnus-tree-highlight-article))
3040       ;; If the summary buffer is empty, but there are some low-scored
3041       ;; articles or some excluded dormants, we include these in the
3042       ;; buffer.
3043       (when (and (zerop (buffer-size))
3044                  (not no-display))
3045         (cond (gnus-newsgroup-dormant
3046                (gnus-summary-limit-include-dormant))
3047               ((and gnus-newsgroup-scored show-all)
3048                (gnus-summary-limit-include-expunged t))))
3049       ;; Function `gnus-apply-kill-file' must be called in this hook.
3050       (gnus-run-hooks 'gnus-apply-kill-hook)
3051       (if (and (zerop (buffer-size))
3052                (not no-display))
3053           (progn
3054             ;; This newsgroup is empty.
3055             (gnus-summary-catchup-and-exit nil t)
3056             (gnus-message 6 "No unread news")
3057             (when kill-buffer
3058               (gnus-kill-or-deaden-summary kill-buffer))
3059             ;; Return nil from this function.
3060             nil)
3061         ;; Hide conversation thread subtrees.  We cannot do this in
3062         ;; gnus-summary-prepare-hook since kill processing may not
3063         ;; work with hidden articles.
3064         (and gnus-show-threads
3065              gnus-thread-hide-subtree
3066              (gnus-summary-hide-all-threads))
3067         (when kill-buffer
3068           (gnus-kill-or-deaden-summary kill-buffer))
3069         ;; Show first unread article if requested.
3070         (if (and (not no-article)
3071                  (not no-display)
3072                  gnus-newsgroup-unreads
3073                  gnus-auto-select-first)
3074             (progn
3075               (gnus-configure-windows 'summary)
3076               (cond
3077                ((eq gnus-auto-select-first 'best)
3078                 (gnus-summary-best-unread-article))
3079                ((eq gnus-auto-select-first t)
3080                 (gnus-summary-first-unread-article))
3081                ((gnus-functionp gnus-auto-select-first)
3082                 (funcall gnus-auto-select-first))))
3083           ;; Don't select any articles, just move point to the first
3084           ;; article in the group.
3085           (goto-char (point-min))
3086           (gnus-summary-position-point)
3087           (gnus-configure-windows 'summary 'force)
3088           (gnus-set-mode-line 'summary))
3089         (when (get-buffer-window gnus-group-buffer t)
3090           ;; Gotta use windows, because recenter does weird stuff if
3091           ;; the current buffer ain't the displayed window.
3092           (let ((owin (selected-window)))
3093             (select-window (get-buffer-window gnus-group-buffer t))
3094             (when (gnus-group-goto-group group)
3095               (recenter))
3096             (select-window owin)))
3097         ;; Mark this buffer as "prepared".
3098         (setq gnus-newsgroup-prepared t)
3099         (gnus-run-hooks 'gnus-summary-prepared-hook)
3100         t)))))
3101
3102 (defun gnus-summary-prepare ()
3103   "Generate the summary buffer."
3104   (interactive)
3105   (let ((buffer-read-only nil))
3106     (erase-buffer)
3107     (setq gnus-newsgroup-data nil
3108           gnus-newsgroup-data-reverse nil)
3109     (gnus-run-hooks 'gnus-summary-generate-hook)
3110     ;; Generate the buffer, either with threads or without.
3111     (when gnus-newsgroup-headers
3112       (gnus-summary-prepare-threads
3113        (if gnus-show-threads
3114            (gnus-sort-gathered-threads
3115             (funcall gnus-summary-thread-gathering-function
3116                      (gnus-sort-threads
3117                       (gnus-cut-threads (gnus-make-threads)))))
3118          ;; Unthreaded display.
3119          (gnus-sort-articles gnus-newsgroup-headers))))
3120     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3121     ;; Call hooks for modifying summary buffer.
3122     (goto-char (point-min))
3123     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3124
3125 (defsubst gnus-general-simplify-subject (subject)
3126   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3127   (setq subject
3128         (cond
3129          ;; Truncate the subject.
3130          (gnus-simplify-subject-functions
3131           (gnus-map-function gnus-simplify-subject-functions subject))
3132          ((numberp gnus-summary-gather-subject-limit)
3133           (setq subject (gnus-simplify-subject-re subject))
3134           (if (> (length subject) gnus-summary-gather-subject-limit)
3135               (substring subject 0 gnus-summary-gather-subject-limit)
3136             subject))
3137          ;; Fuzzily simplify it.
3138          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3139           (gnus-simplify-subject-fuzzy subject))
3140          ;; Just remove the leading "Re:".
3141          (t
3142           (gnus-simplify-subject-re subject))))
3143
3144   (if (and gnus-summary-gather-exclude-subject
3145            (string-match gnus-summary-gather-exclude-subject subject))
3146       nil                               ; This article shouldn't be gathered
3147     subject))
3148
3149 (defun gnus-summary-simplify-subject-query ()
3150   "Query where the respool algorithm would put this article."
3151   (interactive)
3152   (gnus-summary-select-article)
3153   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3154
3155 (defun gnus-gather-threads-by-subject (threads)
3156   "Gather threads by looking at Subject headers."
3157   (if (not gnus-summary-make-false-root)
3158       threads
3159     (let ((hashtb (gnus-make-hashtable 1024))
3160           (prev threads)
3161           (result threads)
3162           subject hthread whole-subject)
3163       (while threads
3164         (setq subject (gnus-general-simplify-subject
3165                        (setq whole-subject (mail-header-subject
3166                                             (caar threads)))))
3167         (when subject
3168           (if (setq hthread (gnus-gethash subject hashtb))
3169               (progn
3170                 ;; We enter a dummy root into the thread, if we
3171                 ;; haven't done that already.
3172                 (unless (stringp (caar hthread))
3173                   (setcar hthread (list whole-subject (car hthread))))
3174                 ;; We add this new gathered thread to this gathered
3175                 ;; thread.
3176                 (setcdr (car hthread)
3177                         (nconc (cdar hthread) (list (car threads))))
3178                 ;; Remove it from the list of threads.
3179                 (setcdr prev (cdr threads))
3180                 (setq threads prev))
3181             ;; Enter this thread into the hash table.
3182             (gnus-sethash subject threads hashtb)))
3183         (setq prev threads)
3184         (setq threads (cdr threads)))
3185       result)))
3186
3187 (defun gnus-gather-threads-by-references (threads)
3188   "Gather threads by looking at References headers."
3189   (let ((idhashtb (gnus-make-hashtable 1024))
3190         (thhashtb (gnus-make-hashtable 1024))
3191         (prev threads)
3192         (result threads)
3193         ids references id gthread gid entered ref)
3194     (while threads
3195       (when (setq references (mail-header-references (caar threads)))
3196         (setq id (mail-header-id (caar threads))
3197               ids (gnus-split-references references)
3198               entered nil)
3199         (while (setq ref (pop ids))
3200           (setq ids (delete ref ids))
3201           (if (not (setq gid (gnus-gethash ref idhashtb)))
3202               (progn
3203                 (gnus-sethash ref id idhashtb)
3204                 (gnus-sethash id threads thhashtb))
3205             (setq gthread (gnus-gethash gid thhashtb))
3206             (unless entered
3207               ;; We enter a dummy root into the thread, if we
3208               ;; haven't done that already.
3209               (unless (stringp (caar gthread))
3210                 (setcar gthread (list (mail-header-subject (caar gthread))
3211                                       (car gthread))))
3212               ;; We add this new gathered thread to this gathered
3213               ;; thread.
3214               (setcdr (car gthread)
3215                       (nconc (cdar gthread) (list (car threads)))))
3216             ;; Add it into the thread hash table.
3217             (gnus-sethash id gthread thhashtb)
3218             (setq entered t)
3219             ;; Remove it from the list of threads.
3220             (setcdr prev (cdr threads))
3221             (setq threads prev))))
3222       (setq prev threads)
3223       (setq threads (cdr threads)))
3224     result))
3225
3226 (defun gnus-sort-gathered-threads (threads)
3227   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3228   (let ((result threads))
3229     (while threads
3230       (when (stringp (caar threads))
3231         (setcdr (car threads)
3232                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3233       (setq threads (cdr threads)))
3234     result))
3235
3236 (defun gnus-thread-loop-p (root thread)
3237   "Say whether ROOT is in THREAD."
3238   (let ((stack (list thread))
3239         (infloop 0)
3240         th)
3241     (while (setq thread (pop stack))
3242       (setq th (cdr thread))
3243       (while (and th
3244                   (not (eq (caar th) root)))
3245         (pop th))
3246       (if th
3247           ;; We have found a loop.
3248           (let (ref-dep)
3249             (setcdr thread (delq (car th) (cdr thread)))
3250             (if (boundp (setq ref-dep (intern "none"
3251                                               gnus-newsgroup-dependencies)))
3252                 (setcdr (symbol-value ref-dep)
3253                         (nconc (cdr (symbol-value ref-dep))
3254                                (list (car th))))
3255               (set ref-dep (list nil (car th))))
3256             (setq infloop 1
3257                   stack nil))
3258         ;; Push all the subthreads onto the stack.
3259         (push (cdr thread) stack)))
3260     infloop))
3261
3262 (defun gnus-make-threads ()
3263   "Go through the dependency hashtb and find the roots.  Return all threads."
3264   (let (threads)
3265     (while (catch 'infloop
3266              (mapatoms
3267               (lambda (refs)
3268                 ;; Deal with self-referencing References loops.
3269                 (when (and (car (symbol-value refs))
3270                            (not (zerop
3271                                  (apply
3272                                   '+
3273                                   (mapcar
3274                                    (lambda (thread)
3275                                      (gnus-thread-loop-p
3276                                       (car (symbol-value refs)) thread))
3277                                    (cdr (symbol-value refs)))))))
3278                   (setq threads nil)
3279                   (throw 'infloop t))
3280                 (unless (car (symbol-value refs))
3281                   ;; These threads do not refer back to any other articles,
3282                   ;; so they're roots.
3283                   (setq threads (append (cdr (symbol-value refs)) threads))))
3284               gnus-newsgroup-dependencies)))
3285     threads))
3286
3287 ;; Build the thread tree.
3288 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3289   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3290
3291 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3292 if it was already present.
3293
3294 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3295 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3296 Message-IDs will be renamed be renamed to a unique Message-ID before
3297 being entered.
3298
3299 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3300   (let* ((id (mail-header-id header))
3301          (id-dep (and id (intern id dependencies)))
3302          ref ref-dep ref-header)
3303     ;; Enter this `header' in the `dependencies' table.
3304     (cond
3305      ((not id-dep)
3306       (setq header nil))
3307      ;; The first two cases do the normal part: enter a new `header'
3308      ;; in the `dependencies' table.
3309      ((not (boundp id-dep))
3310       (set id-dep (list header)))
3311      ((null (car (symbol-value id-dep)))
3312       (setcar (symbol-value id-dep) header))
3313
3314      ;; From here the `header' was already present in the
3315      ;; `dependencies' table.
3316      (force-new
3317       ;; Overrides an existing entry;
3318       ;; just set the header part of the entry.
3319       (setcar (symbol-value id-dep) header))
3320
3321      ;; Renames the existing `header' to a unique Message-ID.
3322      ((not gnus-summary-ignore-duplicates)
3323       ;; An article with this Message-ID has already been seen.
3324       ;; We rename the Message-ID.
3325       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3326            (list header))
3327       (mail-header-set-id header id))
3328
3329      ;; The last case ignores an existing entry, except it adds any
3330      ;; additional Xrefs (in case the two articles came from different
3331      ;; servers.
3332      ;; Also sets `header' to `nil' meaning that the `dependencies'
3333      ;; table was *not* modified.
3334      (t
3335       (mail-header-set-xref
3336        (car (symbol-value id-dep))
3337        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3338                    "")
3339                (or (mail-header-xref header) "")))
3340       (setq header nil)))
3341
3342     (when header
3343       ;; First check if that we are not creating a References loop.
3344       (setq ref (gnus-parent-id (mail-header-references header)))
3345       (while (and ref
3346                   (setq ref-dep (intern-soft ref dependencies))
3347                   (boundp ref-dep)
3348                   (setq ref-header (car (symbol-value ref-dep))))
3349         (if (string= id ref)
3350             ;; Yuk!  This is a reference loop.  Make the article be a
3351             ;; root article.
3352             (progn
3353               (mail-header-set-references (car (symbol-value id-dep)) "none")
3354               (setq ref nil))
3355           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3356       (setq ref (gnus-parent-id (mail-header-references header)))
3357       (setq ref-dep (intern (or ref "none") dependencies))
3358       (if (boundp ref-dep)
3359           (setcdr (symbol-value ref-dep)
3360                   (nconc (cdr (symbol-value ref-dep))
3361                          (list (symbol-value id-dep))))
3362         (set ref-dep (list nil (symbol-value id-dep)))))
3363     header))
3364
3365 (defun gnus-build-sparse-threads ()
3366   (let ((headers gnus-newsgroup-headers)
3367         (mail-parse-charset gnus-newsgroup-charset)
3368         (gnus-summary-ignore-duplicates t)
3369         header references generation relations
3370         subject child end new-child date)
3371     ;; First we create an alist of generations/relations, where
3372     ;; generations is how much we trust the relation, and the relation
3373     ;; is parent/child.
3374     (gnus-message 7 "Making sparse threads...")
3375     (save-excursion
3376       (nnheader-set-temp-buffer " *gnus sparse threads*")
3377       (while (setq header (pop headers))
3378         (when (and (setq references (mail-header-references header))
3379                    (not (string= references "")))
3380           (insert references)
3381           (setq child (mail-header-id header)
3382                 subject (mail-header-subject header)
3383                 date (mail-header-date header)
3384                 generation 0)
3385           (while (search-backward ">" nil t)
3386             (setq end (1+ (point)))
3387             (when (search-backward "<" nil t)
3388               (setq new-child (buffer-substring (point) end))
3389               (push (list (incf generation)
3390                           child (setq child new-child)
3391                           subject date)
3392                     relations)))
3393           (when child
3394             (push (list (1+ generation) child nil subject) relations))
3395           (erase-buffer)))
3396       (kill-buffer (current-buffer)))
3397     ;; Sort over trustworthiness.
3398     (mapcar
3399      (lambda (relation)
3400        (when (gnus-dependencies-add-header
3401               (make-full-mail-header
3402                gnus-reffed-article-number
3403                (nth 3 relation) "" (or (nth 4 relation) "")
3404                (nth 1 relation)
3405                (or (nth 2 relation) "") 0 0 "")
3406               gnus-newsgroup-dependencies nil)
3407          (push gnus-reffed-article-number gnus-newsgroup-limit)
3408          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3409          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3410                gnus-newsgroup-reads)
3411          (decf gnus-reffed-article-number)))
3412      (sort relations 'car-less-than-car))
3413     (gnus-message 7 "Making sparse threads...done")))
3414
3415 (defun gnus-build-old-threads ()
3416   ;; Look at all the articles that refer back to old articles, and
3417   ;; fetch the headers for the articles that aren't there.  This will
3418   ;; build complete threads - if the roots haven't been expired by the
3419   ;; server, that is.
3420   (let ((mail-parse-charset gnus-newsgroup-charset)
3421         id heads)
3422     (mapatoms
3423      (lambda (refs)
3424        (when (not (car (symbol-value refs)))
3425          (setq heads (cdr (symbol-value refs)))
3426          (while heads
3427            (if (memq (mail-header-number (caar heads))
3428                      gnus-newsgroup-dormant)
3429                (setq heads (cdr heads))
3430              (setq id (symbol-name refs))
3431              (while (and (setq id (gnus-build-get-header id))
3432                          (not (car (gnus-id-to-thread id)))))
3433              (setq heads nil)))))
3434      gnus-newsgroup-dependencies)))
3435
3436 ;; This function has to be called with point after the article number
3437 ;; on the beginning of the line.
3438 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3439   (let ((eol (gnus-point-at-eol))
3440         (buffer (current-buffer))
3441         header)
3442
3443     ;; overview: [num subject from date id refs chars lines misc]
3444     (unwind-protect
3445         (progn
3446           (narrow-to-region (point) eol)
3447           (unless (eobp)
3448             (forward-char))
3449
3450           (setq header
3451                 (make-full-mail-header
3452                  number                 ; number
3453                  (funcall gnus-decode-encoded-word-function
3454                           (nnheader-nov-field)) ; subject
3455                  (funcall gnus-decode-encoded-word-function
3456                           (nnheader-nov-field)) ; from
3457                  (nnheader-nov-field)   ; date
3458                  (nnheader-nov-read-message-id) ; id
3459                  (nnheader-nov-field)   ; refs
3460                  (nnheader-nov-read-integer) ; chars
3461                  (nnheader-nov-read-integer) ; lines
3462                  (unless (eobp)
3463                    (if (looking-at "Xref: ")
3464                        (goto-char (match-end 0)))
3465                    (nnheader-nov-field)) ; Xref
3466                  (nnheader-nov-parse-extra)))) ; extra
3467
3468       (widen))
3469
3470     (when gnus-alter-header-function
3471       (funcall gnus-alter-header-function header))
3472     (gnus-dependencies-add-header header dependencies force-new)))
3473
3474 (defun gnus-build-get-header (id)
3475   "Look through the buffer of NOV lines and find the header to ID.
3476 Enter this line into the dependencies hash table, and return
3477 the id of the parent article (if any)."
3478   (let ((deps gnus-newsgroup-dependencies)
3479         found header)
3480     (prog1
3481         (save-excursion
3482           (set-buffer nntp-server-buffer)
3483           (let ((case-fold-search nil))
3484             (goto-char (point-min))
3485             (while (and (not found)
3486                         (search-forward id nil t))
3487               (beginning-of-line)
3488               (setq found (looking-at
3489                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3490                                    (regexp-quote id))))
3491               (or found (beginning-of-line 2)))
3492             (when found
3493               (beginning-of-line)
3494               (and
3495                (setq header (gnus-nov-parse-line
3496                              (read (current-buffer)) deps))
3497                (gnus-parent-id (mail-header-references header))))))
3498       (when header
3499         (let ((number (mail-header-number header)))
3500           (push number gnus-newsgroup-limit)
3501           (push header gnus-newsgroup-headers)
3502           (if (memq number gnus-newsgroup-unselected)
3503               (progn
3504                 (push number gnus-newsgroup-unreads)
3505                 (setq gnus-newsgroup-unselected
3506                       (delq number gnus-newsgroup-unselected)))
3507             (push number gnus-newsgroup-ancient)))))))
3508
3509 (defun gnus-build-all-threads ()
3510   "Read all the headers."
3511   (let ((gnus-summary-ignore-duplicates t)
3512         (mail-parse-charset gnus-newsgroup-charset)
3513         (dependencies gnus-newsgroup-dependencies)
3514         header article)
3515     (save-excursion
3516       (set-buffer nntp-server-buffer)
3517       (let ((case-fold-search nil))
3518         (goto-char (point-min))
3519         (while (not (eobp))
3520           (ignore-errors
3521             (setq article (read (current-buffer))
3522                   header (gnus-nov-parse-line article dependencies)))
3523           (when header
3524             (save-excursion
3525               (set-buffer gnus-summary-buffer)
3526               (push header gnus-newsgroup-headers)
3527               (if (memq (setq article (mail-header-number header))
3528                         gnus-newsgroup-unselected)
3529                   (progn
3530                     (push article gnus-newsgroup-unreads)
3531                     (setq gnus-newsgroup-unselected
3532                           (delq article gnus-newsgroup-unselected)))
3533                 (push article gnus-newsgroup-ancient)))
3534             (forward-line 1)))))))
3535
3536 (defun gnus-summary-update-article-line (article header)
3537   "Update the line for ARTICLE using HEADERS."
3538   (let* ((id (mail-header-id header))
3539          (thread (gnus-id-to-thread id)))
3540     (unless thread
3541       (error "Article in no thread"))
3542     ;; Update the thread.
3543     (setcar thread header)
3544     (gnus-summary-goto-subject article)
3545     (let* ((datal (gnus-data-find-list article))
3546            (data (car datal))
3547            (length (when (cdr datal)
3548                      (- (gnus-data-pos data)
3549                         (gnus-data-pos (cadr datal)))))
3550            (buffer-read-only nil)
3551            (level (gnus-summary-thread-level)))
3552       (gnus-delete-line)
3553       (gnus-summary-insert-line
3554        header level nil (gnus-article-mark article)
3555        (memq article gnus-newsgroup-replied)
3556        (memq article gnus-newsgroup-expirable)
3557        ;; Only insert the Subject string when it's different
3558        ;; from the previous Subject string.
3559        (if (and
3560             gnus-show-threads
3561             (gnus-subject-equal
3562              (condition-case ()
3563                  (mail-header-subject
3564                   (gnus-data-header
3565                    (cadr
3566                     (gnus-data-find-list
3567                      article
3568                      (gnus-data-list t)))))
3569                ;; Error on the side of excessive subjects.
3570                (error ""))
3571              (mail-header-subject header)))
3572            ""
3573          (mail-header-subject header))
3574        nil (cdr (assq article gnus-newsgroup-scored))
3575        (memq article gnus-newsgroup-processable))
3576       (when length
3577         (gnus-data-update-list
3578          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3579
3580 (defun gnus-summary-update-article (article &optional iheader)
3581   "Update ARTICLE in the summary buffer."
3582   (set-buffer gnus-summary-buffer)
3583   (let* ((header (gnus-summary-article-header article))
3584          (id (mail-header-id header))
3585          (data (gnus-data-find article))
3586          (thread (gnus-id-to-thread id))
3587          (references (mail-header-references header))
3588          (parent
3589           (gnus-id-to-thread
3590            (or (gnus-parent-id
3591                 (when (and references
3592                            (not (equal "" references)))
3593                   references))
3594                "none")))
3595          (buffer-read-only nil)
3596          (old (car thread)))
3597     (when thread
3598       (unless iheader
3599         (setcar thread nil)
3600         (when parent
3601           (delq thread parent)))
3602       (if (gnus-summary-insert-subject id header)
3603           ;; Set the (possibly) new article number in the data structure.
3604           (gnus-data-set-number data (gnus-id-to-article id))
3605         (setcar thread old)
3606         nil))))
3607
3608 (defun gnus-rebuild-thread (id &optional line)
3609   "Rebuild the thread containing ID.
3610 If LINE, insert the rebuilt thread starting on line LINE."
3611   (let ((buffer-read-only nil)
3612         old-pos current thread data)
3613     (if (not gnus-show-threads)
3614         (setq thread (list (car (gnus-id-to-thread id))))
3615       ;; Get the thread this article is part of.
3616       (setq thread (gnus-remove-thread id)))
3617     (setq old-pos (gnus-point-at-bol))
3618     (setq current (save-excursion
3619                     (and (zerop (forward-line -1))
3620                          (gnus-summary-article-number))))
3621     ;; If this is a gathered thread, we have to go some re-gathering.
3622     (when (stringp (car thread))
3623       (let ((subject (car thread))
3624             roots thr)
3625         (setq thread (cdr thread))
3626         (while thread
3627           (unless (memq (setq thr (gnus-id-to-thread
3628                                    (gnus-root-id
3629                                     (mail-header-id (caar thread)))))
3630                         roots)
3631             (push thr roots))
3632           (setq thread (cdr thread)))
3633         ;; We now have all (unique) roots.
3634         (if (= (length roots) 1)
3635             ;; All the loose roots are now one solid root.
3636             (setq thread (car roots))
3637           (setq thread (cons subject (gnus-sort-threads roots))))))
3638     (let (threads)
3639       ;; We then insert this thread into the summary buffer.
3640       (when line
3641         (goto-char (point-min))
3642         (forward-line (1- line)))
3643       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3644         (if gnus-show-threads
3645             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3646           (gnus-summary-prepare-unthreaded thread))
3647         (setq data (nreverse gnus-newsgroup-data))
3648         (setq threads gnus-newsgroup-threads))
3649       ;; We splice the new data into the data structure.
3650       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3651       ;;!!! then we want to insert at the beginning of the buffer.
3652       ;;!!! That happens to be true with Gnus now, but that may
3653       ;;!!! change in the future.  Perhaps.
3654       (gnus-data-enter-list
3655        (if line nil current) data (- (point) old-pos))
3656       (setq gnus-newsgroup-threads
3657             (nconc threads gnus-newsgroup-threads))
3658       (gnus-data-compute-positions))))
3659
3660 (defun gnus-number-to-header (number)
3661   "Return the header for article NUMBER."
3662   (let ((headers gnus-newsgroup-headers))
3663     (while (and headers
3664                 (not (= number (mail-header-number (car headers)))))
3665       (pop headers))
3666     (when headers
3667       (car headers))))
3668
3669 (defun gnus-parent-headers (in-headers &optional generation)
3670   "Return the headers of the GENERATIONeth parent of HEADERS."
3671   (unless generation
3672     (setq generation 1))
3673   (let ((parent t)
3674         (headers in-headers)
3675         references)
3676     (while (and parent
3677                 (not (zerop generation))
3678                 (setq references (mail-header-references headers)))
3679       (setq headers (if (and references
3680                              (setq parent (gnus-parent-id references)))
3681                         (car (gnus-id-to-thread parent))
3682                       nil))
3683       (decf generation))
3684     (and (not (eq headers in-headers))
3685          headers)))
3686
3687 (defun gnus-id-to-thread (id)
3688   "Return the (sub-)thread where ID appears."
3689   (gnus-gethash id gnus-newsgroup-dependencies))
3690
3691 (defun gnus-id-to-article (id)
3692   "Return the article number of ID."
3693   (let ((thread (gnus-id-to-thread id)))
3694     (when (and thread
3695                (car thread))
3696       (mail-header-number (car thread)))))
3697
3698 (defun gnus-id-to-header (id)
3699   "Return the article headers of ID."
3700   (car (gnus-id-to-thread id)))
3701
3702 (defun gnus-article-displayed-root-p (article)
3703   "Say whether ARTICLE is a root(ish) article."
3704   (let ((level (gnus-summary-thread-level article))
3705         (refs (mail-header-references  (gnus-summary-article-header article)))
3706         particle)
3707     (cond
3708      ((null level) nil)
3709      ((zerop level) t)
3710      ((null refs) t)
3711      ((null (gnus-parent-id refs)) t)
3712      ((and (= 1 level)
3713            (null (setq particle (gnus-id-to-article
3714                                  (gnus-parent-id refs))))
3715            (null (gnus-summary-thread-level particle)))))))
3716
3717 (defun gnus-root-id (id)
3718   "Return the id of the root of the thread where ID appears."
3719   (let (last-id prev)
3720     (while (and id (setq prev (car (gnus-id-to-thread id))))
3721       (setq last-id id
3722             id (gnus-parent-id (mail-header-references prev))))
3723     last-id))
3724
3725 (defun gnus-articles-in-thread (thread)
3726   "Return the list of articles in THREAD."
3727   (cons (mail-header-number (car thread))
3728         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3729
3730 (defun gnus-remove-thread (id &optional dont-remove)
3731   "Remove the thread that has ID in it."
3732   (let (headers thread last-id)
3733     ;; First go up in this thread until we find the root.
3734     (setq last-id (gnus-root-id id)
3735           headers (message-flatten-list (gnus-id-to-thread last-id)))
3736     ;; We have now found the real root of this thread.  It might have
3737     ;; been gathered into some loose thread, so we have to search
3738     ;; through the threads to find the thread we wanted.
3739     (let ((threads gnus-newsgroup-threads)
3740           sub)
3741       (while threads
3742         (setq sub (car threads))
3743         (if (stringp (car sub))
3744             ;; This is a gathered thread, so we look at the roots
3745             ;; below it to find whether this article is in this
3746             ;; gathered root.
3747             (progn
3748               (setq sub (cdr sub))
3749               (while sub
3750                 (when (member (caar sub) headers)
3751                   (setq thread (car threads)
3752                         threads nil
3753                         sub nil))
3754                 (setq sub (cdr sub))))
3755           ;; It's an ordinary thread, so we check it.
3756           (when (eq (car sub) (car headers))
3757             (setq thread sub
3758                   threads nil)))
3759         (setq threads (cdr threads)))
3760       ;; If this article is in no thread, then it's a root.
3761       (if thread
3762           (unless dont-remove
3763             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3764         (setq thread (gnus-id-to-thread last-id)))
3765       (when thread
3766         (prog1
3767             thread                      ; We return this thread.
3768           (unless dont-remove
3769             (if (stringp (car thread))
3770                 (progn
3771                   ;; If we use dummy roots, then we have to remove the
3772                   ;; dummy root as well.
3773                   (when (eq gnus-summary-make-false-root 'dummy)
3774                     ;; We go to the dummy root by going to
3775                     ;; the first sub-"thread", and then one line up.
3776                     (gnus-summary-goto-article
3777                      (mail-header-number (caadr thread)))
3778                     (forward-line -1)
3779                     (gnus-delete-line)
3780                     (gnus-data-compute-positions))
3781                   (setq thread (cdr thread))
3782                   (while thread
3783                     (gnus-remove-thread-1 (car thread))
3784                     (setq thread (cdr thread))))
3785               (gnus-remove-thread-1 thread))))))))
3786
3787 (defun gnus-remove-thread-1 (thread)
3788   "Remove the thread THREAD recursively."
3789   (let ((number (mail-header-number (pop thread)))
3790         d)
3791     (setq thread (reverse thread))
3792     (while thread
3793       (gnus-remove-thread-1 (pop thread)))
3794     (when (setq d (gnus-data-find number))
3795       (goto-char (gnus-data-pos d))
3796       (gnus-summary-show-thread)
3797       (gnus-data-remove
3798        number
3799        (- (gnus-point-at-bol)
3800           (prog1
3801               (1+ (gnus-point-at-eol))
3802             (gnus-delete-line)))))))
3803
3804 (defun gnus-sort-threads-1 (threads func)
3805   (sort (mapcar (lambda (thread)
3806                   (cons (car thread)
3807                         (and (cdr thread)
3808                              (gnus-sort-threads-1 (cdr thread) func))))
3809                 threads) func))
3810
3811 (defun gnus-sort-threads (threads)
3812   "Sort THREADS."
3813   (if (not gnus-thread-sort-functions)
3814       threads
3815     (gnus-message 8 "Sorting threads...")
3816     (prog1
3817         (gnus-sort-threads-1 
3818          threads 
3819          (gnus-make-sort-function gnus-thread-sort-functions))
3820       (gnus-message 8 "Sorting threads...done"))))
3821
3822 (defun gnus-sort-articles (articles)
3823   "Sort ARTICLES."
3824   (when gnus-article-sort-functions
3825     (gnus-message 7 "Sorting articles...")
3826     (prog1
3827         (setq gnus-newsgroup-headers
3828               (sort articles (gnus-make-sort-function
3829                               gnus-article-sort-functions)))
3830       (gnus-message 7 "Sorting articles...done"))))
3831
3832 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3833 (defmacro gnus-thread-header (thread)
3834   "Return header of first article in THREAD.
3835 Note that THREAD must never, ever be anything else than a variable -
3836 using some other form will lead to serious barfage."
3837   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3838   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3839   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3840         (vector thread) 2))
3841
3842 (defsubst gnus-article-sort-by-number (h1 h2)
3843   "Sort articles by article number."
3844   (< (mail-header-number h1)
3845      (mail-header-number h2)))
3846
3847 (defun gnus-thread-sort-by-number (h1 h2)
3848   "Sort threads by root article number."
3849   (gnus-article-sort-by-number
3850    (gnus-thread-header h1) (gnus-thread-header h2)))
3851
3852 (defsubst gnus-article-sort-by-lines (h1 h2)
3853   "Sort articles by article Lines header."
3854   (< (mail-header-lines h1)
3855      (mail-header-lines h2)))
3856
3857 (defun gnus-thread-sort-by-lines (h1 h2)
3858   "Sort threads by root article Lines header."
3859   (gnus-article-sort-by-lines
3860    (gnus-thread-header h1) (gnus-thread-header h2)))
3861
3862 (defsubst gnus-article-sort-by-chars (h1 h2)
3863   "Sort articles by octet length."
3864   (< (mail-header-chars h1)
3865      (mail-header-chars h2)))
3866
3867 (defun gnus-thread-sort-by-chars (h1 h2)
3868   "Sort threads by root article octet length."
3869   (gnus-article-sort-by-chars
3870    (gnus-thread-header h1) (gnus-thread-header h2)))
3871
3872 (defsubst gnus-article-sort-by-author (h1 h2)
3873   "Sort articles by root author."
3874   (string-lessp
3875    (let ((extract (funcall
3876                    gnus-extract-address-components
3877                    (mail-header-from h1))))
3878      (or (car extract) (cadr extract) ""))
3879    (let ((extract (funcall
3880                    gnus-extract-address-components
3881                    (mail-header-from h2))))
3882      (or (car extract) (cadr extract) ""))))
3883
3884 (defun gnus-thread-sort-by-author (h1 h2)
3885   "Sort threads by root author."
3886   (gnus-article-sort-by-author
3887    (gnus-thread-header h1)  (gnus-thread-header h2)))
3888
3889 (defsubst gnus-article-sort-by-subject (h1 h2)
3890   "Sort articles by root subject."
3891   (string-lessp
3892    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3893    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3894
3895 (defun gnus-thread-sort-by-subject (h1 h2)
3896   "Sort threads by root subject."
3897   (gnus-article-sort-by-subject
3898    (gnus-thread-header h1) (gnus-thread-header h2)))
3899
3900 (defsubst gnus-article-sort-by-date (h1 h2)
3901   "Sort articles by root article date."
3902   (time-less-p
3903    (gnus-date-get-time (mail-header-date h1))
3904    (gnus-date-get-time (mail-header-date h2))))
3905
3906 (defun gnus-thread-sort-by-date (h1 h2)
3907   "Sort threads by root article date."
3908   (gnus-article-sort-by-date
3909    (gnus-thread-header h1) (gnus-thread-header h2)))
3910
3911 (defsubst gnus-article-sort-by-score (h1 h2)
3912   "Sort articles by root article score.
3913 Unscored articles will be counted as having a score of zero."
3914   (> (or (cdr (assq (mail-header-number h1)
3915                     gnus-newsgroup-scored))
3916          gnus-summary-default-score 0)
3917      (or (cdr (assq (mail-header-number h2)
3918                     gnus-newsgroup-scored))
3919          gnus-summary-default-score 0)))
3920
3921 (defun gnus-thread-sort-by-score (h1 h2)
3922   "Sort threads by root article score."
3923   (gnus-article-sort-by-score
3924    (gnus-thread-header h1) (gnus-thread-header h2)))
3925
3926 (defun gnus-thread-sort-by-total-score (h1 h2)
3927   "Sort threads by the sum of all scores in the thread.
3928 Unscored articles will be counted as having a score of zero."
3929   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3930
3931 (defun gnus-thread-total-score (thread)
3932   ;; This function find the total score of THREAD.
3933   (cond ((null thread)
3934          0)
3935         ((consp thread)
3936          (if (stringp (car thread))
3937              (apply gnus-thread-score-function 0
3938                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3939            (gnus-thread-total-score-1 thread)))
3940         (t
3941          (gnus-thread-total-score-1 (list thread)))))
3942
3943 (defun gnus-thread-total-score-1 (root)
3944   ;; This function find the total score of the thread below ROOT.
3945   (setq root (car root))
3946   (apply gnus-thread-score-function
3947          (or (append
3948               (mapcar 'gnus-thread-total-score
3949                       (cdr (gnus-id-to-thread (mail-header-id root))))
3950               (when (> (mail-header-number root) 0)
3951                 (list (or (cdr (assq (mail-header-number root)
3952                                      gnus-newsgroup-scored))
3953                           gnus-summary-default-score 0))))
3954              (list gnus-summary-default-score)
3955              '(0))))
3956
3957 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3958 (defvar gnus-tmp-prev-subject nil)
3959 (defvar gnus-tmp-false-parent nil)
3960 (defvar gnus-tmp-root-expunged nil)
3961 (defvar gnus-tmp-dummy-line nil)
3962
3963 (eval-when-compile (defvar gnus-tmp-header))
3964 (defun gnus-extra-header (type &optional header)
3965   "Return the extra header of TYPE."
3966   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3967       ""))
3968
3969 (defun gnus-summary-prepare-threads (threads)
3970   "Prepare summary buffer from THREADS and indentation LEVEL.
3971 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3972 or a straight list of headers."
3973   (gnus-message 7 "Generating summary...")
3974
3975   (setq gnus-newsgroup-threads threads)
3976   (beginning-of-line)
3977
3978   (let ((gnus-tmp-level 0)
3979         (default-score (or gnus-summary-default-score 0))
3980         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3981         thread number subject stack state gnus-tmp-gathered beg-match
3982         new-roots gnus-tmp-new-adopts thread-end
3983         gnus-tmp-header gnus-tmp-unread
3984         gnus-tmp-replied gnus-tmp-subject-or-nil
3985         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3986         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3987         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3988
3989     (setq gnus-tmp-prev-subject nil)
3990
3991     (if (vectorp (car threads))
3992         ;; If this is a straight (sic) list of headers, then a
3993         ;; threaded summary display isn't required, so we just create
3994         ;; an unthreaded one.
3995         (gnus-summary-prepare-unthreaded threads)
3996
3997       ;; Do the threaded display.
3998
3999       (while (or threads stack gnus-tmp-new-adopts new-roots)
4000
4001         (if (and (= gnus-tmp-level 0)
4002                  (or (not stack)
4003                      (= (caar stack) 0))
4004                  (not gnus-tmp-false-parent)
4005                  (or gnus-tmp-new-adopts new-roots))
4006             (if gnus-tmp-new-adopts
4007                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4008                       thread (list (car gnus-tmp-new-adopts))
4009                       gnus-tmp-header (caar thread)
4010                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4011               (when new-roots
4012                 (setq thread (list (car new-roots))
4013                       gnus-tmp-header (caar thread)
4014                       new-roots (cdr new-roots))))
4015
4016           (if threads
4017               ;; If there are some threads, we do them before the
4018               ;; threads on the stack.
4019               (setq thread threads
4020                     gnus-tmp-header (caar thread))
4021             ;; There were no current threads, so we pop something off
4022             ;; the stack.
4023             (setq state (car stack)
4024                   gnus-tmp-level (car state)
4025                   thread (cdr state)
4026                   stack (cdr stack)
4027                   gnus-tmp-header (caar thread))))
4028
4029         (setq gnus-tmp-false-parent nil)
4030         (setq gnus-tmp-root-expunged nil)
4031         (setq thread-end nil)
4032
4033         (if (stringp gnus-tmp-header)
4034             ;; The header is a dummy root.
4035             (cond
4036              ((eq gnus-summary-make-false-root 'adopt)
4037               ;; We let the first article adopt the rest.
4038               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4039                                                (cddar thread)))
4040               (setq gnus-tmp-gathered
4041                     (nconc (mapcar
4042                             (lambda (h) (mail-header-number (car h)))
4043                             (cddar thread))
4044                            gnus-tmp-gathered))
4045               (setq thread (cons (list (caar thread)
4046                                        (cadar thread))
4047                                  (cdr thread)))
4048               (setq gnus-tmp-level -1
4049                     gnus-tmp-false-parent t))
4050              ((eq gnus-summary-make-false-root 'empty)
4051               ;; We print adopted articles with empty subject fields.
4052               (setq gnus-tmp-gathered
4053                     (nconc (mapcar
4054                             (lambda (h) (mail-header-number (car h)))
4055                             (cddar thread))
4056                            gnus-tmp-gathered))
4057               (setq gnus-tmp-level -1))
4058              ((eq gnus-summary-make-false-root 'dummy)
4059               ;; We remember that we probably want to output a dummy
4060               ;; root.
4061               (setq gnus-tmp-dummy-line gnus-tmp-header)
4062               (setq gnus-tmp-prev-subject gnus-tmp-header))
4063              (t
4064               ;; We do not make a root for the gathered
4065               ;; sub-threads at all.
4066               (setq gnus-tmp-level -1)))
4067
4068           (setq number (mail-header-number gnus-tmp-header)
4069                 subject (mail-header-subject gnus-tmp-header))
4070
4071           (cond
4072            ;; If the thread has changed subject, we might want to make
4073            ;; this subthread into a root.
4074            ((and (null gnus-thread-ignore-subject)
4075                  (not (zerop gnus-tmp-level))
4076                  gnus-tmp-prev-subject
4077                  (not (inline
4078                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4079             (setq new-roots (nconc new-roots (list (car thread)))
4080                   thread-end t
4081                   gnus-tmp-header nil))
4082            ;; If the article lies outside the current limit,
4083            ;; then we do not display it.
4084            ((not (memq number gnus-newsgroup-limit))
4085             (setq gnus-tmp-gathered
4086                   (nconc (mapcar
4087                           (lambda (h) (mail-header-number (car h)))
4088                           (cdar thread))
4089                          gnus-tmp-gathered))
4090             (setq gnus-tmp-new-adopts (if (cdar thread)
4091                                           (append gnus-tmp-new-adopts
4092                                                   (cdar thread))
4093                                         gnus-tmp-new-adopts)
4094                   thread-end t
4095                   gnus-tmp-header nil)
4096             (when (zerop gnus-tmp-level)
4097               (setq gnus-tmp-root-expunged t)))
4098            ;; Perhaps this article is to be marked as read?
4099            ((and gnus-summary-mark-below
4100                  (< (or (cdr (assq number gnus-newsgroup-scored))
4101                         default-score)
4102                     gnus-summary-mark-below)
4103                  ;; Don't touch sparse articles.
4104                  (not (gnus-summary-article-sparse-p number))
4105                  (not (gnus-summary-article-ancient-p number)))
4106             (setq gnus-newsgroup-unreads
4107                   (delq number gnus-newsgroup-unreads))
4108             (if gnus-newsgroup-auto-expire
4109                 (push number gnus-newsgroup-expirable)
4110               (push (cons number gnus-low-score-mark)
4111                     gnus-newsgroup-reads))))
4112
4113           (when gnus-tmp-header
4114             ;; We may have an old dummy line to output before this
4115             ;; article.
4116             (when (and gnus-tmp-dummy-line
4117                        (gnus-subject-equal
4118                         gnus-tmp-dummy-line
4119                         (mail-header-subject gnus-tmp-header)))
4120               (gnus-summary-insert-dummy-line
4121                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4122               (setq gnus-tmp-dummy-line nil))
4123
4124             ;; Compute the mark.
4125             (setq gnus-tmp-unread (gnus-article-mark number))
4126
4127             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4128                                   gnus-tmp-header gnus-tmp-level)
4129                   gnus-newsgroup-data)
4130
4131             ;; Actually insert the line.
4132             (setq
4133              gnus-tmp-subject-or-nil
4134              (cond
4135               ((and gnus-thread-ignore-subject
4136                     gnus-tmp-prev-subject
4137                     (not (inline (gnus-subject-equal
4138                                   gnus-tmp-prev-subject subject))))
4139                subject)
4140               ((zerop gnus-tmp-level)
4141                (if (and (eq gnus-summary-make-false-root 'empty)
4142                         (memq number gnus-tmp-gathered)
4143                         gnus-tmp-prev-subject
4144                         (inline (gnus-subject-equal
4145                                  gnus-tmp-prev-subject subject)))
4146                    gnus-summary-same-subject
4147                  subject))
4148               (t gnus-summary-same-subject)))
4149             (if (and (eq gnus-summary-make-false-root 'adopt)
4150                      (= gnus-tmp-level 1)
4151                      (memq number gnus-tmp-gathered))
4152                 (setq gnus-tmp-opening-bracket ?\<
4153                       gnus-tmp-closing-bracket ?\>)
4154               (setq gnus-tmp-opening-bracket ?\[
4155                     gnus-tmp-closing-bracket ?\]))
4156             (setq
4157              gnus-tmp-indentation
4158              (aref gnus-thread-indent-array gnus-tmp-level)
4159              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4160              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4161                                 gnus-summary-default-score 0)
4162              gnus-tmp-score-char
4163              (if (or (null gnus-summary-default-score)
4164                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4165                          gnus-summary-zcore-fuzz))
4166                  ?  ;Whitespace
4167                (if (< gnus-tmp-score gnus-summary-default-score)
4168                    gnus-score-below-mark gnus-score-over-mark))
4169              gnus-tmp-replied
4170              (cond ((memq number gnus-newsgroup-processable)
4171                     gnus-process-mark)
4172                    ((memq number gnus-newsgroup-cached)
4173                     gnus-cached-mark)
4174                    ((memq number gnus-newsgroup-replied)
4175                     gnus-replied-mark)
4176                    ((memq number gnus-newsgroup-saved)
4177                     gnus-saved-mark)
4178                    (t gnus-no-mark))
4179              gnus-tmp-from (mail-header-from gnus-tmp-header)
4180              gnus-tmp-name
4181              (cond
4182               ((string-match "<[^>]+> *$" gnus-tmp-from)
4183                (setq beg-match (match-beginning 0))
4184                (or (and (string-match "^\".+\"" gnus-tmp-from)
4185                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4186                    (substring gnus-tmp-from 0 beg-match)))
4187               ((string-match "(.+)" gnus-tmp-from)
4188                (substring gnus-tmp-from
4189                           (1+ (match-beginning 0)) (1- (match-end 0))))
4190               (t gnus-tmp-from)))
4191             (when (string= gnus-tmp-name "")
4192               (setq gnus-tmp-name gnus-tmp-from))
4193             (unless (numberp gnus-tmp-lines)
4194               (setq gnus-tmp-lines 0))
4195             (gnus-put-text-property
4196              (point)
4197              (progn (eval gnus-summary-line-format-spec) (point))
4198              'gnus-number number)
4199             (when gnus-visual-p
4200               (forward-line -1)
4201               (gnus-run-hooks 'gnus-summary-update-hook)
4202               (forward-line 1))
4203
4204             (setq gnus-tmp-prev-subject subject)))
4205
4206         (when (nth 1 thread)
4207           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4208         (incf gnus-tmp-level)
4209         (setq threads (if thread-end nil (cdar thread)))
4210         (unless threads
4211           (setq gnus-tmp-level 0)))))
4212   (gnus-message 7 "Generating summary...done"))
4213
4214 (defun gnus-summary-prepare-unthreaded (headers)
4215   "Generate an unthreaded summary buffer based on HEADERS."
4216   (let (header number mark)
4217
4218     (beginning-of-line)
4219
4220     (while headers
4221       ;; We may have to root out some bad articles...
4222       (when (memq (setq number (mail-header-number
4223                                 (setq header (pop headers))))
4224                   gnus-newsgroup-limit)
4225         ;; Mark article as read when it has a low score.
4226         (when (and gnus-summary-mark-below
4227                    (< (or (cdr (assq number gnus-newsgroup-scored))
4228                           gnus-summary-default-score 0)
4229                       gnus-summary-mark-below)
4230                    (not (gnus-summary-article-ancient-p number)))
4231           (setq gnus-newsgroup-unreads
4232                 (delq number gnus-newsgroup-unreads))
4233           (if gnus-newsgroup-auto-expire
4234               (push number gnus-newsgroup-expirable)
4235             (push (cons number gnus-low-score-mark)
4236                   gnus-newsgroup-reads)))
4237
4238         (setq mark (gnus-article-mark number))
4239         (push (gnus-data-make number mark (1+ (point)) header 0)
4240               gnus-newsgroup-data)
4241         (gnus-summary-insert-line
4242          header 0 number
4243          mark (memq number gnus-newsgroup-replied)
4244          (memq number gnus-newsgroup-expirable)
4245          (mail-header-subject header) nil
4246          (cdr (assq number gnus-newsgroup-scored))
4247          (memq number gnus-newsgroup-processable))))))
4248
4249 (defun gnus-summary-remove-list-identifiers ()
4250   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4251   (let ((regexp (if (stringp gnus-list-identifiers)
4252                     gnus-list-identifiers
4253                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4254     (dolist (header gnus-newsgroup-headers)
4255       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4256                                   " *\\)\\)+\\(Re: +\\)?\\)")
4257                           (mail-header-subject header))
4258         (mail-header-set-subject
4259          header (concat (substring (mail-header-subject header)
4260                                    0 (match-beginning 1))
4261                         (or
4262                          (match-string 3 (mail-header-subject header))
4263                          (match-string 5 (mail-header-subject header)))
4264                         (substring (mail-header-subject header)
4265                                    (match-end 1))))))))
4266
4267 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4268   "Select newsgroup GROUP.
4269 If READ-ALL is non-nil, all articles in the group are selected.
4270 If SELECT-ARTICLES, only select those articles from GROUP."
4271   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4272          ;;!!! Dirty hack; should be removed.
4273          (gnus-summary-ignore-duplicates
4274           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4275               t
4276             gnus-summary-ignore-duplicates))
4277          (info (nth 2 entry))
4278          articles fetched-articles cached)
4279
4280     (unless (gnus-check-server
4281              (setq gnus-current-select-method
4282                    (gnus-find-method-for-group group)))
4283       (error "Couldn't open server"))
4284
4285     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4286         (gnus-activate-group group)     ; Or we can activate it...
4287         (progn                          ; Or we bug out.
4288           (when (equal major-mode 'gnus-summary-mode)
4289             (kill-buffer (current-buffer)))
4290           (error "Couldn't activate group %s: %s"
4291                  group (gnus-status-message group))))
4292
4293     (unless (gnus-request-group group t)
4294       (when (equal major-mode 'gnus-summary-mode)
4295         (kill-buffer (current-buffer)))
4296       (error "Couldn't request group %s: %s"
4297              group (gnus-status-message group)))
4298
4299     (setq gnus-newsgroup-name group)
4300     (setq gnus-newsgroup-unselected nil)
4301     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4302     (gnus-summary-setup-default-charset)
4303
4304     ;; Adjust and set lists of article marks.
4305     (when info
4306       (gnus-adjust-marked-articles info))
4307
4308     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4309     (when (gnus-virtual-group-p group)
4310       (setq cached gnus-newsgroup-cached))
4311
4312     (setq gnus-newsgroup-unreads
4313           (gnus-set-difference
4314            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4315            gnus-newsgroup-dormant))
4316
4317     (setq gnus-newsgroup-processable nil)
4318
4319     (gnus-update-read-articles group gnus-newsgroup-unreads)
4320
4321     (if (setq articles select-articles)
4322         (setq gnus-newsgroup-unselected
4323               (gnus-sorted-intersection
4324                gnus-newsgroup-unreads
4325                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4326       (setq articles (gnus-articles-to-read group read-all)))
4327
4328     (cond
4329      ((null articles)
4330       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4331       'quit)
4332      ((eq articles 0) nil)
4333      (t
4334       ;; Init the dependencies hash table.
4335       (setq gnus-newsgroup-dependencies
4336             (gnus-make-hashtable (length articles)))
4337       (gnus-set-global-variables)
4338       ;; Retrieve the headers and read them in.
4339       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4340       (setq gnus-newsgroup-headers
4341             (if (eq 'nov
4342                     (setq gnus-headers-retrieved-by
4343                           (gnus-retrieve-headers
4344                            articles gnus-newsgroup-name
4345                            ;; We might want to fetch old headers, but
4346                            ;; not if there is only 1 article.
4347                            (and (or (and
4348                                      (not (eq gnus-fetch-old-headers 'some))
4349                                      (not (numberp gnus-fetch-old-headers)))
4350                                     (> (length articles) 1))
4351                                 gnus-fetch-old-headers))))
4352                 (gnus-get-newsgroup-headers-xover
4353                  articles nil nil gnus-newsgroup-name t)
4354               (gnus-get-newsgroup-headers)))
4355       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4356
4357       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4358       (when cached
4359         (setq gnus-newsgroup-cached cached))
4360
4361       ;; Suppress duplicates?
4362       (when gnus-suppress-duplicates
4363         (gnus-dup-suppress-articles))
4364
4365       ;; Set the initial limit.
4366       (setq gnus-newsgroup-limit (copy-sequence articles))
4367       ;; Remove canceled articles from the list of unread articles.
4368       (setq gnus-newsgroup-unreads
4369             (gnus-set-sorted-intersection
4370              gnus-newsgroup-unreads
4371              (setq fetched-articles
4372                    (mapcar (lambda (headers) (mail-header-number headers))
4373                            gnus-newsgroup-headers))))
4374       ;; Removed marked articles that do not exist.
4375       (gnus-update-missing-marks
4376        (gnus-sorted-complement fetched-articles articles))
4377       ;; We might want to build some more threads first.
4378       (when (and gnus-fetch-old-headers
4379                  (eq gnus-headers-retrieved-by 'nov))
4380         (if (eq gnus-fetch-old-headers 'invisible)
4381             (gnus-build-all-threads)
4382           (gnus-build-old-threads)))
4383       ;; Let the Gnus agent mark articles as read.
4384       (when gnus-agent
4385         (gnus-agent-get-undownloaded-list))
4386       ;; Remove list identifiers from subject
4387       (when gnus-list-identifiers
4388         (gnus-summary-remove-list-identifiers))
4389       ;; Check whether auto-expire is to be done in this group.
4390       (setq gnus-newsgroup-auto-expire
4391             (gnus-group-auto-expirable-p group))
4392       ;; Set up the article buffer now, if necessary.
4393       (unless gnus-single-article-buffer
4394         (gnus-article-setup-buffer))
4395       ;; First and last article in this newsgroup.
4396       (when gnus-newsgroup-headers
4397         (setq gnus-newsgroup-begin
4398               (mail-header-number (car gnus-newsgroup-headers))
4399               gnus-newsgroup-end
4400               (mail-header-number
4401                (gnus-last-element gnus-newsgroup-headers))))
4402       ;; GROUP is successfully selected.
4403       (or gnus-newsgroup-headers t)))))
4404
4405 (defun gnus-articles-to-read (group &optional read-all)
4406   "Find out what articles the user wants to read."
4407   (let* ((articles
4408           ;; Select all articles if `read-all' is non-nil, or if there
4409           ;; are no unread articles.
4410           (if (or read-all
4411                   (and (zerop (length gnus-newsgroup-marked))
4412                        (zerop (length gnus-newsgroup-unreads)))
4413                   (eq (gnus-group-find-parameter group 'display)
4414                       'all))
4415               (or
4416                (gnus-uncompress-range (gnus-active group))
4417                (gnus-cache-articles-in-group group))
4418             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4419                           (copy-sequence gnus-newsgroup-unreads))
4420                   '<)))
4421          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4422          (scored (length scored-list))
4423          (number (length articles))
4424          (marked (+ (length gnus-newsgroup-marked)
4425                     (length gnus-newsgroup-dormant)))
4426          (select
4427           (cond
4428            ((numberp read-all)
4429             read-all)
4430            (t
4431             (condition-case ()
4432                 (cond
4433                  ((and (or (<= scored marked) (= scored number))
4434                        (numberp gnus-large-newsgroup)
4435                        (> number gnus-large-newsgroup))
4436                   (let ((input
4437                          (read-string
4438                           (format
4439                            "How many articles from %s (default %d): "
4440                            (gnus-limit-string gnus-newsgroup-name 35)
4441                            number))))
4442                     (if (string-match "^[ \t]*$" input) number input)))
4443                  ((and (> scored marked) (< scored number)
4444                        (> (- scored number) 20))
4445                   (let ((input
4446                          (read-string
4447                           (format "%s %s (%d scored, %d total): "
4448                                   "How many articles from"
4449                                   group scored number))))
4450                     (if (string-match "^[ \t]*$" input)
4451                         number input)))
4452                  (t number))
4453               (quit
4454                (message "Quit getting the articles to read")
4455                nil))))))
4456     (setq select (if (stringp select) (string-to-number select) select))
4457     (if (or (null select) (zerop select))
4458         select
4459       (if (and (not (zerop scored)) (<= (abs select) scored))
4460           (progn
4461             (setq articles (sort scored-list '<))
4462             (setq number (length articles)))
4463         (setq articles (copy-sequence articles)))
4464
4465       (when (< (abs select) number)
4466         (if (< select 0)
4467             ;; Select the N oldest articles.
4468             (setcdr (nthcdr (1- (abs select)) articles) nil)
4469           ;; Select the N most recent articles.
4470           (setq articles (nthcdr (- number select) articles))))
4471       (setq gnus-newsgroup-unselected
4472             (gnus-sorted-intersection
4473              gnus-newsgroup-unreads
4474              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4475       (when gnus-alter-articles-to-read-function
4476         (setq gnus-newsgroup-unreads
4477               (sort 
4478                (funcall gnus-alter-articles-to-read-function
4479                         gnus-newsgroup-name gnus-newsgroup-unreads)
4480                '<)))
4481       articles)))
4482
4483 (defun gnus-killed-articles (killed articles)
4484   (let (out)
4485     (while articles
4486       (when (inline (gnus-member-of-range (car articles) killed))
4487         (push (car articles) out))
4488       (setq articles (cdr articles)))
4489     out))
4490
4491 (defun gnus-uncompress-marks (marks)
4492   "Uncompress the mark ranges in MARKS."
4493   (let ((uncompressed '(score bookmark))
4494         out)
4495     (while marks
4496       (if (memq (caar marks) uncompressed)
4497           (push (car marks) out)
4498         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4499       (setq marks (cdr marks)))
4500     out))
4501
4502 (defun gnus-adjust-marked-articles (info)
4503   "Set all article lists and remove all marks that are no longer valid."
4504   (let* ((marked-lists (gnus-info-marks info))
4505          (active (gnus-active (gnus-info-group info)))
4506          (min (car active))
4507          (max (cdr active))
4508          (types gnus-article-mark-lists)
4509          (uncompressed '(score bookmark killed))
4510          marks var articles article mark)
4511
4512     (while marked-lists
4513       (setq marks (pop marked-lists))
4514       (set (setq var (intern (format "gnus-newsgroup-%s"
4515                                      (car (rassq (setq mark (car marks))
4516                                                  types)))))
4517            (if (memq (car marks) uncompressed) (cdr marks)
4518              (gnus-uncompress-range (cdr marks))))
4519
4520       (setq articles (symbol-value var))
4521
4522       ;; All articles have to be subsets of the active articles.
4523       (cond
4524        ;; Adjust "simple" lists.
4525        ((memq mark '(tick dormant expire reply save))
4526         (while articles
4527           (when (or (< (setq article (pop articles)) min) (> article max))
4528             (set var (delq article (symbol-value var))))))
4529        ;; Adjust assocs.
4530        ((memq mark uncompressed)
4531         (when (not (listp (cdr (symbol-value var))))
4532           (set var (list (symbol-value var))))
4533         (when (not (listp (cdr articles)))
4534           (setq articles (list articles)))
4535         (while articles
4536           (when (or (not (consp (setq article (pop articles))))
4537                     (< (car article) min)
4538                     (> (car article) max))
4539             (set var (delq article (symbol-value var))))))))))
4540
4541 (defun gnus-update-missing-marks (missing)
4542   "Go through the list of MISSING articles and remove them from the mark lists."
4543   (when missing
4544     (let ((types gnus-article-mark-lists)
4545           var m)
4546       ;; Go through all types.
4547       (while types
4548         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4549         (when (symbol-value var)
4550           ;; This list has articles.  So we delete all missing articles
4551           ;; from it.
4552           (setq m missing)
4553           (while m
4554             (set var (delq (pop m) (symbol-value var)))))))))
4555
4556 (defun gnus-update-marks ()
4557   "Enter the various lists of marked articles into the newsgroup info list."
4558   (let ((types gnus-article-mark-lists)
4559         (info (gnus-get-info gnus-newsgroup-name))
4560         (uncompressed '(score bookmark killed))
4561         type list newmarked symbol delta-marks)
4562     (when info
4563       ;; Add all marks lists to the list of marks lists.
4564       (while (setq type (pop types))
4565         (setq list (symbol-value
4566                     (setq symbol
4567                           (intern (format "gnus-newsgroup-%s"
4568                                           (car type))))))
4569
4570         (when list
4571           ;; Get rid of the entries of the articles that have the
4572           ;; default score.
4573           (when (and (eq (cdr type) 'score)
4574                      gnus-save-score
4575                      list)
4576             (let* ((arts list)
4577                    (prev (cons nil list))
4578                    (all prev))
4579               (while arts
4580                 (if (or (not (consp (car arts)))
4581                         (= (cdar arts) gnus-summary-default-score))
4582                     (setcdr prev (cdr arts))
4583                   (setq prev arts))
4584                 (setq arts (cdr arts)))
4585               (setq list (cdr all)))))
4586
4587         (unless (memq (cdr type) uncompressed)
4588           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4589        
4590         (when (gnus-check-backend-function
4591                'request-set-mark gnus-newsgroup-name)
4592           ;; propagate flags to server, with the following exceptions:
4593           ;; uncompressed:s are not proper flags (they are cons cells)
4594           ;; cache is a internal gnus flag
4595           ;; download are local to one gnus installation (well)
4596           ;; unsend are for nndraft groups only
4597           ;; xxx: generality of this?  this suits nnimap anyway
4598           (unless (memq (cdr type) (append '(cache download unsend)
4599                                            uncompressed))
4600             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4601                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4602                    (add (gnus-remove-from-range
4603                          (gnus-copy-sequence list) old)))
4604               (when add
4605                 (push (list add 'add (list (cdr type))) delta-marks))
4606               (when del
4607                 (push (list del 'del (list (cdr type))) delta-marks)))))
4608           
4609         (when list
4610           (push (cons (cdr type) list) newmarked)))
4611
4612       (when delta-marks
4613         (unless (gnus-check-group gnus-newsgroup-name)
4614           (error "Can't open server for %s" gnus-newsgroup-name))
4615         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4616           
4617       ;; Enter these new marks into the info of the group.
4618       (if (nthcdr 3 info)
4619           (setcar (nthcdr 3 info) newmarked)
4620         ;; Add the marks lists to the end of the info.
4621         (when newmarked
4622           (setcdr (nthcdr 2 info) (list newmarked))))
4623
4624       ;; Cut off the end of the info if there's nothing else there.
4625       (let ((i 5))
4626         (while (and (> i 2)
4627                     (not (nth i info)))
4628           (when (nthcdr (decf i) info)
4629             (setcdr (nthcdr i info) nil)))))))
4630
4631 (defun gnus-set-mode-line (where)
4632   "Set the mode line of the article or summary buffers.
4633 If WHERE is `summary', the summary mode line format will be used."
4634   ;; Is this mode line one we keep updated?
4635   (when (and (memq where gnus-updated-mode-lines)
4636              (symbol-value
4637               (intern (format "gnus-%s-mode-line-format-spec" where))))
4638     (let (mode-string)
4639       (save-excursion
4640         ;; We evaluate this in the summary buffer since these
4641         ;; variables are buffer-local to that buffer.
4642         (set-buffer gnus-summary-buffer)
4643         ;; We bind all these variables that are used in the `eval' form
4644         ;; below.
4645         (let* ((mformat (symbol-value
4646                          (intern
4647                           (format "gnus-%s-mode-line-format-spec" where))))
4648                (gnus-tmp-group-name (gnus-group-name-decode 
4649                                      gnus-newsgroup-name
4650                                      (gnus-group-name-charset 
4651                                       nil
4652                                       gnus-newsgroup-name)))
4653                (gnus-tmp-article-number (or gnus-current-article 0))
4654                (gnus-tmp-unread gnus-newsgroup-unreads)
4655                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4656                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4657                (gnus-tmp-unread-and-unselected
4658                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4659                             (zerop gnus-tmp-unselected))
4660                        "")
4661                       ((zerop gnus-tmp-unselected)
4662                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4663                       (t (format "{%d(+%d) more}"
4664                                  gnus-tmp-unread-and-unticked
4665                                  gnus-tmp-unselected))))
4666                (gnus-tmp-subject
4667                 (if (and gnus-current-headers
4668                          (vectorp gnus-current-headers))
4669                     (gnus-mode-string-quote
4670                      (mail-header-subject gnus-current-headers))
4671                   ""))
4672                bufname-length max-len
4673                gnus-tmp-header);; passed as argument to any user-format-funcs
4674           (setq mode-string (eval mformat))
4675           (setq bufname-length (if (string-match "%b" mode-string)
4676                                    (- (length
4677                                        (buffer-name
4678                                         (if (eq where 'summary)
4679                                             nil
4680                                           (get-buffer gnus-article-buffer))))
4681                                       2)
4682                                  0))
4683           (setq max-len (max 4 (if gnus-mode-non-string-length
4684                                    (- (window-width)
4685                                       gnus-mode-non-string-length
4686                                       bufname-length)
4687                                  (length mode-string))))
4688           ;; We might have to chop a bit of the string off...
4689           (when (> (length mode-string) max-len)
4690             (setq mode-string
4691                   (concat (truncate-string-to-width mode-string (- max-len 3))
4692                           "...")))
4693           ;; Pad the mode string a bit.
4694           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4695       ;; Update the mode line.
4696       (setq mode-line-buffer-identification
4697             (gnus-mode-line-buffer-identification (list mode-string)))
4698       (set-buffer-modified-p t))))
4699
4700 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4701   "Go through the HEADERS list and add all Xrefs to a hash table.
4702 The resulting hash table is returned, or nil if no Xrefs were found."
4703   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4704          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4705          (xref-hashtb (gnus-make-hashtable))
4706          start group entry number xrefs header)
4707     (while headers
4708       (setq header (pop headers))
4709       (when (and (setq xrefs (mail-header-xref header))
4710                  (not (memq (setq number (mail-header-number header))
4711                             unreads)))
4712         (setq start 0)
4713         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4714           (setq start (match-end 0))
4715           (setq group (if prefix
4716                           (concat prefix (substring xrefs (match-beginning 1)
4717                                                     (match-end 1)))
4718                         (substring xrefs (match-beginning 1) (match-end 1))))
4719           (setq number
4720                 (string-to-int (substring xrefs (match-beginning 2)
4721                                           (match-end 2))))
4722           (if (setq entry (gnus-gethash group xref-hashtb))
4723               (setcdr entry (cons number (cdr entry)))
4724             (gnus-sethash group (cons number nil) xref-hashtb)))))
4725     (and start xref-hashtb)))
4726
4727 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4728   "Look through all the headers and mark the Xrefs as read."
4729   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4730         name entry info xref-hashtb idlist method nth4)
4731     (save-excursion
4732       (set-buffer gnus-group-buffer)
4733       (when (setq xref-hashtb
4734                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4735         (mapatoms
4736          (lambda (group)
4737            (unless (string= from-newsgroup (setq name (symbol-name group)))
4738              (setq idlist (symbol-value group))
4739              ;; Dead groups are not updated.
4740              (and (prog1
4741                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4742                             info (nth 2 entry))
4743                     (when (stringp (setq nth4 (gnus-info-method info)))
4744                       (setq nth4 (gnus-server-to-method nth4))))
4745                   ;; Only do the xrefs if the group has the same
4746                   ;; select method as the group we have just read.
4747                   (or (gnus-methods-equal-p
4748                        nth4 (gnus-find-method-for-group from-newsgroup))
4749                       virtual
4750                       (equal nth4 (setq method (gnus-find-method-for-group
4751                                                 from-newsgroup)))
4752                       (and (equal (car nth4) (car method))
4753                            (equal (nth 1 nth4) (nth 1 method))))
4754                   gnus-use-cross-reference
4755                   (or (not (eq gnus-use-cross-reference t))
4756                       virtual
4757                       ;; Only do cross-references on subscribed
4758                       ;; groups, if that is what is wanted.
4759                       (<= (gnus-info-level info) gnus-level-subscribed))
4760                   (gnus-group-make-articles-read name idlist))))
4761          xref-hashtb)))))
4762
4763 (defun gnus-compute-read-articles (group articles)
4764   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4765          (info (nth 2 entry))
4766          (active (gnus-active group))
4767          ninfo)
4768     (when entry
4769       ;; First peel off all invalid article numbers.
4770       (when active
4771         (let ((ids articles)
4772               id first)
4773           (while (setq id (pop ids))
4774             (when (and first (> id (cdr active)))
4775               ;; We'll end up in this situation in one particular
4776               ;; obscure situation.  If you re-scan a group and get
4777               ;; a new article that is cross-posted to a different
4778               ;; group that has not been re-scanned, you might get
4779               ;; crossposted article that has a higher number than
4780               ;; Gnus believes possible.  So we re-activate this
4781               ;; group as well.  This might mean doing the
4782               ;; crossposting thingy will *increase* the number
4783               ;; of articles in some groups.  Tsk, tsk.
4784               (setq active (or (gnus-activate-group group) active)))
4785             (when (or (> id (cdr active))
4786                       (< id (car active)))
4787               (setq articles (delq id articles))))))
4788       ;; If the read list is nil, we init it.
4789       (if (and active
4790                (null (gnus-info-read info))
4791                (> (car active) 1))
4792           (setq ninfo (cons 1 (1- (car active))))
4793         (setq ninfo (gnus-info-read info)))
4794       ;; Then we add the read articles to the range.
4795       (gnus-add-to-range
4796        ninfo (setq articles (sort articles '<))))))
4797
4798 (defun gnus-group-make-articles-read (group articles)
4799   "Update the info of GROUP to say that ARTICLES are read."
4800   (let* ((num 0)
4801          (entry (gnus-gethash group gnus-newsrc-hashtb))
4802          (info (nth 2 entry))
4803          (active (gnus-active group))
4804          range)
4805     (when entry
4806       (setq range (gnus-compute-read-articles group articles))
4807       (save-excursion
4808         (set-buffer gnus-group-buffer)
4809         (gnus-undo-register
4810           `(progn
4811              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4812              (gnus-info-set-read ',info ',(gnus-info-read info))
4813              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4814              (gnus-group-update-group ,group t))))
4815       ;; Add the read articles to the range.
4816       (gnus-info-set-read info range)
4817       ;; Then we have to re-compute how many unread
4818       ;; articles there are in this group.
4819       (when active
4820         (cond
4821          ((not range)
4822           (setq num (- (1+ (cdr active)) (car active))))
4823          ((not (listp (cdr range)))
4824           (setq num (- (cdr active) (- (1+ (cdr range))
4825                                        (car range)))))
4826          (t
4827           (while range
4828             (if (numberp (car range))
4829                 (setq num (1+ num))
4830               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4831             (setq range (cdr range)))
4832           (setq num (- (cdr active) num))))
4833         ;; Update the number of unread articles.
4834         (setcar entry num)
4835         ;; Update the group buffer.
4836         (gnus-group-update-group group t)))))
4837
4838 (defvar gnus-newsgroup-none-id 0)
4839
4840 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4841   (let ((cur nntp-server-buffer)
4842         (dependencies
4843          (or dependencies
4844              (save-excursion (set-buffer gnus-summary-buffer)
4845                              gnus-newsgroup-dependencies)))
4846         headers id end ref
4847         (mail-parse-charset gnus-newsgroup-charset)
4848         (mail-parse-ignored-charsets
4849          (save-excursion (condition-case nil
4850                              (set-buffer gnus-summary-buffer)
4851                            (error))
4852                          gnus-newsgroup-ignored-charsets)))
4853     (save-excursion
4854       (set-buffer nntp-server-buffer)
4855       ;; Translate all TAB characters into SPACE characters.
4856       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4857       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4858       (gnus-run-hooks 'gnus-parse-headers-hook)
4859       (let ((case-fold-search t)
4860             in-reply-to header p lines chars)
4861         (goto-char (point-min))
4862         ;; Search to the beginning of the next header.  Error messages
4863         ;; do not begin with 2 or 3.
4864         (while (re-search-forward "^[23][0-9]+ " nil t)
4865           (setq id nil
4866                 ref nil)
4867           ;; This implementation of this function, with nine
4868           ;; search-forwards instead of the one re-search-forward and
4869           ;; a case (which basically was the old function) is actually
4870           ;; about twice as fast, even though it looks messier.  You
4871           ;; can't have everything, I guess.  Speed and elegance
4872           ;; doesn't always go hand in hand.
4873           (setq
4874            header
4875            (vector
4876             ;; Number.
4877             (prog1
4878                 (read cur)
4879               (end-of-line)
4880               (setq p (point))
4881               (narrow-to-region (point)
4882                                 (or (and (search-forward "\n.\n" nil t)
4883                                          (- (point) 2))
4884                                     (point))))
4885             ;; Subject.
4886             (progn
4887               (goto-char p)
4888               (if (search-forward "\nsubject: " nil t)
4889                   (funcall gnus-decode-encoded-word-function
4890                            (nnheader-header-value))
4891                 "(none)"))
4892             ;; From.
4893             (progn
4894               (goto-char p)
4895               (if (or (search-forward "\nfrom: " nil t)
4896                       (search-forward "\nfrom:" nil t))
4897                   (funcall gnus-decode-encoded-word-function
4898                            (nnheader-header-value))
4899                 "(nobody)"))
4900             ;; Date.
4901             (progn
4902               (goto-char p)
4903               (if (search-forward "\ndate: " nil t)
4904                   (nnheader-header-value) ""))
4905             ;; Message-ID.
4906             (progn
4907               (goto-char p)
4908               (setq id (if (re-search-forward
4909                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4910                            ;; We do it this way to make sure the Message-ID
4911                            ;; is (somewhat) syntactically valid.
4912                            (buffer-substring (match-beginning 1)
4913                                              (match-end 1))
4914                          ;; If there was no message-id, we just fake one
4915                          ;; to make subsequent routines simpler.
4916                          (nnheader-generate-fake-message-id))))
4917             ;; References.
4918             (progn
4919               (goto-char p)
4920               (if (search-forward "\nreferences: " nil t)
4921                   (progn
4922                     (setq end (point))
4923                     (prog1
4924                         (nnheader-header-value)
4925                       (setq ref
4926                             (buffer-substring
4927                              (progn
4928                                (end-of-line)
4929                                (search-backward ">" end t)
4930                                (1+ (point)))
4931                              (progn
4932                                (search-backward "<" end t)
4933                                (point))))))
4934                 ;; Get the references from the in-reply-to header if there
4935                 ;; were no references and the in-reply-to header looks
4936                 ;; promising.
4937                 (if (and (search-forward "\nin-reply-to: " nil t)
4938                          (setq in-reply-to (nnheader-header-value))
4939                          (string-match "<[^>]+>" in-reply-to))
4940                     (let (ref2)
4941                       (setq ref (substring in-reply-to (match-beginning 0)
4942                                            (match-end 0)))
4943                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4944                         (setq ref2 (substring in-reply-to (match-beginning 0)
4945                                               (match-end 0)))
4946                         (when (> (length ref2) (length ref))
4947                           (setq ref ref2)))
4948                       ref)
4949                   (setq ref nil))))
4950             ;; Chars.
4951             (progn
4952               (goto-char p)
4953               (if (search-forward "\nchars: " nil t)
4954                   (if (numberp (setq chars (ignore-errors (read cur))))
4955                       chars 0)
4956                 0))
4957             ;; Lines.
4958             (progn
4959               (goto-char p)
4960               (if (search-forward "\nlines: " nil t)
4961                   (if (numberp (setq lines (ignore-errors (read cur))))
4962                       lines 0)
4963                 0))
4964             ;; Xref.
4965             (progn
4966               (goto-char p)
4967               (and (search-forward "\nxref: " nil t)
4968                    (nnheader-header-value)))
4969             ;; Extra.
4970             (when gnus-extra-headers
4971               (let ((extra gnus-extra-headers)
4972                     out)
4973                 (while extra
4974                   (goto-char p)
4975                   (when (search-forward
4976                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4977                     (push (cons (car extra) (nnheader-header-value))
4978                           out))
4979                   (pop extra))
4980                 out))))
4981           (when (equal id ref)
4982             (setq ref nil))
4983
4984           (when gnus-alter-header-function
4985             (funcall gnus-alter-header-function header)
4986             (setq id (mail-header-id header)
4987                   ref (gnus-parent-id (mail-header-references header))))
4988
4989           (when (setq header
4990                       (gnus-dependencies-add-header
4991                        header dependencies force-new))
4992             (push header headers))
4993           (goto-char (point-max))
4994           (widen))
4995         (nreverse headers)))))
4996
4997 ;; Goes through the xover lines and returns a list of vectors
4998 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4999                                                   force-new dependencies
5000                                                   group also-fetch-heads)
5001   "Parse the news overview data in the server buffer.
5002 Return a list of headers that match SEQUENCE (see
5003 `nntp-retrieve-headers')."
5004   ;; Get the Xref when the users reads the articles since most/some
5005   ;; NNTP servers do not include Xrefs when using XOVER.
5006   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5007   (let ((mail-parse-charset gnus-newsgroup-charset)
5008         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5009         (cur nntp-server-buffer)
5010         (dependencies (or dependencies gnus-newsgroup-dependencies))
5011         number headers header)
5012     (save-excursion
5013       (set-buffer nntp-server-buffer)
5014       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5015       ;; Allow the user to mangle the headers before parsing them.
5016       (gnus-run-hooks 'gnus-parse-headers-hook)
5017       (goto-char (point-min))
5018       (while (not (eobp))
5019         (condition-case ()
5020             (while (and sequence (not (eobp)))
5021               (setq number (read cur))
5022               (while (and sequence
5023                           (< (car sequence) number))
5024                 (setq sequence (cdr sequence)))
5025               (and sequence
5026                    (eq number (car sequence))
5027                    (progn
5028                      (setq sequence (cdr sequence))
5029                      (setq header (inline
5030                                     (gnus-nov-parse-line
5031                                      number dependencies force-new))))
5032                    (push header headers))
5033               (forward-line 1))
5034           (error
5035            (gnus-error 4 "Strange nov line (%d)"
5036                        (count-lines (point-min) (point)))))
5037         (forward-line 1))
5038       ;; A common bug in inn is that if you have posted an article and
5039       ;; then retrieves the active file, it will answer correctly --
5040       ;; the new article is included.  However, a NOV entry for the
5041       ;; article may not have been generated yet, so this may fail.
5042       ;; We work around this problem by retrieving the last few
5043       ;; headers using HEAD.
5044       (if (or (not also-fetch-heads)
5045               (not sequence))
5046           ;; We (probably) got all the headers.
5047           (nreverse headers)
5048         (let ((gnus-nov-is-evil t))
5049           (nconc
5050            (nreverse headers)
5051            (when (gnus-retrieve-headers sequence group)
5052              (gnus-get-newsgroup-headers))))))))
5053
5054 (defun gnus-article-get-xrefs ()
5055   "Fill in the Xref value in `gnus-current-headers', if necessary.
5056 This is meant to be called in `gnus-article-internal-prepare-hook'."
5057   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5058                                  gnus-current-headers)))
5059     (or (not gnus-use-cross-reference)
5060         (not headers)
5061         (and (mail-header-xref headers)
5062              (not (string= (mail-header-xref headers) "")))
5063         (let ((case-fold-search t)
5064               xref)
5065           (save-restriction
5066             (nnheader-narrow-to-headers)
5067             (goto-char (point-min))
5068             (when (or (and (not (eobp))
5069                            (eq (downcase (char-after)) ?x)
5070                            (looking-at "Xref:"))
5071                       (search-forward "\nXref:" nil t))
5072               (goto-char (1+ (match-end 0)))
5073               (setq xref (buffer-substring (point)
5074                                            (progn (end-of-line) (point))))
5075               (mail-header-set-xref headers xref)))))))
5076
5077 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5078   "Find article ID and insert the summary line for that article.
5079 OLD-HEADER can either be a header or a line number to insert
5080 the subject line on."
5081   (let* ((line (and (numberp old-header) old-header))
5082          (old-header (and (vectorp old-header) old-header))
5083          (header (cond ((and old-header use-old-header)
5084                         old-header)
5085                        ((and (numberp id)
5086                              (gnus-number-to-header id))
5087                         (gnus-number-to-header id))
5088                        (t
5089                         (gnus-read-header id))))
5090          (number (and (numberp id) id))
5091          d)
5092     (when header
5093       ;; Rebuild the thread that this article is part of and go to the
5094       ;; article we have fetched.
5095       (when (and (not gnus-show-threads)
5096                  old-header)
5097         (when (and number
5098                    (setq d (gnus-data-find (mail-header-number old-header))))
5099           (goto-char (gnus-data-pos d))
5100           (gnus-data-remove
5101            number
5102            (- (gnus-point-at-bol)
5103               (prog1
5104                   (1+ (gnus-point-at-eol))
5105                 (gnus-delete-line))))))
5106       (when old-header
5107         (mail-header-set-number header (mail-header-number old-header)))
5108       (setq gnus-newsgroup-sparse
5109             (delq (setq number (mail-header-number header))
5110                   gnus-newsgroup-sparse))
5111       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5112       (push number gnus-newsgroup-limit)
5113       (gnus-rebuild-thread (mail-header-id header) line)
5114       (gnus-summary-goto-subject number nil t))
5115     (when (and (numberp number)
5116                (> number 0))
5117       ;; We have to update the boundaries even if we can't fetch the
5118       ;; article if ID is a number -- so that the next `P' or `N'
5119       ;; command will fetch the previous (or next) article even
5120       ;; if the one we tried to fetch this time has been canceled.
5121       (when (> number gnus-newsgroup-end)
5122         (setq gnus-newsgroup-end number))
5123       (when (< number gnus-newsgroup-begin)
5124         (setq gnus-newsgroup-begin number))
5125       (setq gnus-newsgroup-unselected
5126             (delq number gnus-newsgroup-unselected)))
5127     ;; Report back a success?
5128     (and header (mail-header-number header))))
5129
5130 ;;; Process/prefix in the summary buffer
5131
5132 (defun gnus-summary-work-articles (n)
5133   "Return a list of articles to be worked upon.
5134 The prefix argument, the list of process marked articles, and the
5135 current article will be taken into consideration."
5136   (save-excursion
5137     (set-buffer gnus-summary-buffer)
5138     (cond
5139      (n
5140       ;; A numerical prefix has been given.
5141       (setq n (prefix-numeric-value n))
5142       (let ((backward (< n 0))
5143             (n (abs (prefix-numeric-value n)))
5144             articles article)
5145         (save-excursion
5146           (while
5147               (and (> n 0)
5148                    (push (setq article (gnus-summary-article-number))
5149                          articles)
5150                    (if backward
5151                        (gnus-summary-find-prev nil article)
5152                      (gnus-summary-find-next nil article)))
5153             (decf n)))
5154         (nreverse articles)))
5155      ((and (gnus-region-active-p) (mark))
5156       (message "region active")
5157       ;; Work on the region between point and mark.
5158       (let ((max (max (point) (mark)))
5159             articles article)
5160         (save-excursion
5161           (goto-char (min (min (point) (mark))))
5162           (while
5163               (and
5164                (push (setq article (gnus-summary-article-number)) articles)
5165                (gnus-summary-find-next nil article)
5166                (< (point) max)))
5167           (nreverse articles))))
5168      (gnus-newsgroup-processable
5169       ;; There are process-marked articles present.
5170       ;; Save current state.
5171       (gnus-summary-save-process-mark)
5172       ;; Return the list.
5173       (reverse gnus-newsgroup-processable))
5174      (t
5175       ;; Just return the current article.
5176       (list (gnus-summary-article-number))))))
5177
5178 (defmacro gnus-summary-iterate (arg &rest forms)
5179   "Iterate over the process/prefixed articles and do FORMS.
5180 ARG is the interactive prefix given to the command.  FORMS will be
5181 executed with point over the summary line of the articles."
5182   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5183     `(let ((,articles (gnus-summary-work-articles ,arg)))
5184        (while ,articles
5185          (gnus-summary-goto-subject (car ,articles))
5186          ,@forms
5187          (pop ,articles)))))
5188
5189 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5190 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5191
5192 (defun gnus-summary-save-process-mark ()
5193   "Push the current set of process marked articles on the stack."
5194   (interactive)
5195   (push (copy-sequence gnus-newsgroup-processable)
5196         gnus-newsgroup-process-stack))
5197
5198 (defun gnus-summary-kill-process-mark ()
5199   "Push the current set of process marked articles on the stack and unmark."
5200   (interactive)
5201   (gnus-summary-save-process-mark)
5202   (gnus-summary-unmark-all-processable))
5203
5204 (defun gnus-summary-yank-process-mark ()
5205   "Pop the last process mark state off the stack and restore it."
5206   (interactive)
5207   (unless gnus-newsgroup-process-stack
5208     (error "Empty mark stack"))
5209   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5210
5211 (defun gnus-summary-process-mark-set (set)
5212   "Make SET into the current process marked articles."
5213   (gnus-summary-unmark-all-processable)
5214   (while set
5215     (gnus-summary-set-process-mark (pop set))))
5216
5217 ;;; Searching and stuff
5218
5219 (defun gnus-summary-search-group (&optional backward use-level)
5220   "Search for next unread newsgroup.
5221 If optional argument BACKWARD is non-nil, search backward instead."
5222   (save-excursion
5223     (set-buffer gnus-group-buffer)
5224     (when (gnus-group-search-forward
5225            backward nil (if use-level (gnus-group-group-level) nil))
5226       (gnus-group-group-name))))
5227
5228 (defun gnus-summary-best-group (&optional exclude-group)
5229   "Find the name of the best unread group.
5230 If EXCLUDE-GROUP, do not go to this group."
5231   (save-excursion
5232     (set-buffer gnus-group-buffer)
5233     (save-excursion
5234       (gnus-group-best-unread-group exclude-group))))
5235
5236 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5237   (if backward (gnus-summary-find-prev)
5238     (let* ((dummy (gnus-summary-article-intangible-p))
5239            (article (or article (gnus-summary-article-number)))
5240            (arts (gnus-data-find-list article))
5241            result)
5242       (when (and (not dummy)
5243                  (or (not gnus-summary-check-current)
5244                      (not unread)
5245                      (not (gnus-data-unread-p (car arts)))))
5246         (setq arts (cdr arts)))
5247       (when (setq result
5248                   (if unread
5249                       (progn
5250                         (while arts
5251                           (when (or (and undownloaded
5252                                          (eq gnus-undownloaded-mark
5253                                              (gnus-data-mark (car arts))))
5254                                     (gnus-data-unread-p (car arts)))
5255                             (setq result (car arts)
5256                                   arts nil))
5257                           (setq arts (cdr arts)))
5258                         result)
5259                     (car arts)))
5260         (goto-char (gnus-data-pos result))
5261         (gnus-data-number result)))))
5262
5263 (defun gnus-summary-find-prev (&optional unread article)
5264   (let* ((eobp (eobp))
5265          (article (or article (gnus-summary-article-number)))
5266          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5267          result)
5268     (when (and (not eobp)
5269                (or (not gnus-summary-check-current)
5270                    (not unread)
5271                    (not (gnus-data-unread-p (car arts)))))
5272       (setq arts (cdr arts)))
5273     (when (setq result
5274                 (if unread
5275                     (progn
5276                       (while arts
5277                         (when (gnus-data-unread-p (car arts))
5278                           (setq result (car arts)
5279                                 arts nil))
5280                         (setq arts (cdr arts)))
5281                       result)
5282                   (car arts)))
5283       (goto-char (gnus-data-pos result))
5284       (gnus-data-number result))))
5285
5286 (defun gnus-summary-find-subject (subject &optional unread backward article)
5287   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5288          (article (or article (gnus-summary-article-number)))
5289          (articles (gnus-data-list backward))
5290          (arts (gnus-data-find-list article articles))
5291          result)
5292     (when (or (not gnus-summary-check-current)
5293               (not unread)
5294               (not (gnus-data-unread-p (car arts))))
5295       (setq arts (cdr arts)))
5296     (while arts
5297       (and (or (not unread)
5298                (gnus-data-unread-p (car arts)))
5299            (vectorp (gnus-data-header (car arts)))
5300            (gnus-subject-equal
5301             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5302            (setq result (car arts)
5303                  arts nil))
5304       (setq arts (cdr arts)))
5305     (and result
5306          (goto-char (gnus-data-pos result))
5307          (gnus-data-number result))))
5308
5309 (defun gnus-summary-search-forward (&optional unread subject backward)
5310   "Search forward for an article.
5311 If UNREAD, look for unread articles.  If SUBJECT, look for
5312 articles with that subject.  If BACKWARD, search backward instead."
5313   (cond (subject (gnus-summary-find-subject subject unread backward))
5314         (backward (gnus-summary-find-prev unread))
5315         (t (gnus-summary-find-next unread))))
5316
5317 (defun gnus-recenter (&optional n)
5318   "Center point in window and redisplay frame.
5319 Also do horizontal recentering."
5320   (interactive "P")
5321   (when (and gnus-auto-center-summary
5322              (not (eq gnus-auto-center-summary 'vertical)))
5323     (gnus-horizontal-recenter))
5324   (recenter n))
5325
5326 (defun gnus-summary-recenter ()
5327   "Center point in the summary window.
5328 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5329 displayed, no centering will be performed."
5330   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5331   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5332   (interactive)
5333   (let* ((top (cond ((< (window-height) 4) 0)
5334                     ((< (window-height) 7) 1)
5335                     (t (if (numberp gnus-auto-center-summary)
5336                            gnus-auto-center-summary
5337                          2))))
5338          (height (1- (window-height)))
5339          (bottom (save-excursion (goto-char (point-max))
5340                                  (forward-line (- height))
5341                                  (point)))
5342          (window (get-buffer-window (current-buffer))))
5343     ;; The user has to want it.
5344     (when gnus-auto-center-summary
5345       (when (get-buffer-window gnus-article-buffer)
5346         ;; Only do recentering when the article buffer is displayed,
5347         ;; Set the window start to either `bottom', which is the biggest
5348         ;; possible valid number, or the second line from the top,
5349         ;; whichever is the least.
5350         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5351           (if (> bottom top-pos)
5352               ;; Keep the second line from the top visible
5353               (set-window-start window top-pos t)
5354             ;; Try to keep the bottom line visible; if it's partially
5355             ;; obscured, either scroll one more line to make it fully
5356             ;; visible, or revert to using TOP-POS.
5357             (save-excursion
5358               (goto-char (point-max))
5359               (forward-line -1)
5360               (let ((last-line-start (point)))
5361                 (goto-char bottom)
5362                 (set-window-start window (point) t)
5363                 (when (not (pos-visible-in-window-p last-line-start window))
5364                   (forward-line 1)
5365                   (set-window-start window (min (point) top-pos) t)))))))
5366       ;; Do horizontal recentering while we're at it.
5367       (when (and (get-buffer-window (current-buffer) t)
5368                  (not (eq gnus-auto-center-summary 'vertical)))
5369         (let ((selected (selected-window)))
5370           (select-window (get-buffer-window (current-buffer) t))
5371           (gnus-summary-position-point)
5372           (gnus-horizontal-recenter)
5373           (select-window selected))))))
5374
5375 (defun gnus-summary-jump-to-group (newsgroup)
5376   "Move point to NEWSGROUP in group mode buffer."
5377   ;; Keep update point of group mode buffer if visible.
5378   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5379       (save-window-excursion
5380         ;; Take care of tree window mode.
5381         (when (get-buffer-window gnus-group-buffer)
5382           (pop-to-buffer gnus-group-buffer))
5383         (gnus-group-jump-to-group newsgroup))
5384     (save-excursion
5385       ;; Take care of tree window mode.
5386       (if (get-buffer-window gnus-group-buffer)
5387           (pop-to-buffer gnus-group-buffer)
5388         (set-buffer gnus-group-buffer))
5389       (gnus-group-jump-to-group newsgroup))))
5390
5391 ;; This function returns a list of article numbers based on the
5392 ;; difference between the ranges of read articles in this group and
5393 ;; the range of active articles.
5394 (defun gnus-list-of-unread-articles (group)
5395   (let* ((read (gnus-info-read (gnus-get-info group)))
5396          (active (or (gnus-active group) (gnus-activate-group group)))
5397          (last (cdr active))
5398          first nlast unread)
5399     ;; If none are read, then all are unread.
5400     (if (not read)
5401         (setq first (car active))
5402       ;; If the range of read articles is a single range, then the
5403       ;; first unread article is the article after the last read
5404       ;; article.  Sounds logical, doesn't it?
5405       (if (and (not (listp (cdr read)))
5406                (or (< (car read) (car active))
5407                    (progn (setq read (list read))
5408                           nil)))
5409           (setq first (max (car active) (1+ (cdr read))))
5410         ;; `read' is a list of ranges.
5411         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5412                                   (caar read)))
5413                   1)
5414           (setq first (car active)))
5415         (while read
5416           (when first
5417             (while (< first nlast)
5418               (push first unread)
5419               (setq first (1+ first))))
5420           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5421           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5422           (setq read (cdr read)))))
5423     ;; And add the last unread articles.
5424     (while (<= first last)
5425       (push first unread)
5426       (setq first (1+ first)))
5427     ;; Return the list of unread articles.
5428     (delq 0 (nreverse unread))))
5429
5430 (defun gnus-list-of-read-articles (group)
5431   "Return a list of unread, unticked and non-dormant articles."
5432   (let* ((info (gnus-get-info group))
5433          (marked (gnus-info-marks info))
5434          (active (gnus-active group)))
5435     (and info active
5436          (gnus-set-difference
5437           (gnus-sorted-complement
5438            (gnus-uncompress-range active)
5439            (gnus-list-of-unread-articles group))
5440           (append
5441            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5442            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5443
5444 ;; Various summary commands
5445
5446 (defun gnus-summary-select-article-buffer ()
5447   "Reconfigure windows to show article buffer."
5448   (interactive)
5449   (if (not (gnus-buffer-live-p gnus-article-buffer))
5450       (error "There is no article buffer for this summary buffer")
5451     (gnus-configure-windows 'article)
5452     (select-window (get-buffer-window gnus-article-buffer))))
5453
5454 (defun gnus-summary-universal-argument (arg)
5455   "Perform any operation on all articles that are process/prefixed."
5456   (interactive "P")
5457   (let ((articles (gnus-summary-work-articles arg))
5458         func article)
5459     (if (eq
5460          (setq
5461           func
5462           (key-binding
5463            (read-key-sequence
5464             (substitute-command-keys
5465              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5466          'undefined)
5467         (gnus-error 1 "Undefined key")
5468       (save-excursion
5469         (while articles
5470           (gnus-summary-goto-subject (setq article (pop articles)))
5471           (let (gnus-newsgroup-processable)
5472             (command-execute func))
5473           (gnus-summary-remove-process-mark article)))))
5474   (gnus-summary-position-point))
5475
5476 (defun gnus-summary-toggle-truncation (&optional arg)
5477   "Toggle truncation of summary lines.
5478 With arg, turn line truncation on iff arg is positive."
5479   (interactive "P")
5480   (setq truncate-lines
5481         (if (null arg) (not truncate-lines)
5482           (> (prefix-numeric-value arg) 0)))
5483   (redraw-display))
5484
5485 (defun gnus-summary-reselect-current-group (&optional all rescan)
5486   "Exit and then reselect the current newsgroup.
5487 The prefix argument ALL means to select all articles."
5488   (interactive "P")
5489   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5490     (error "Ephemeral groups can't be reselected"))
5491   (let ((current-subject (gnus-summary-article-number))
5492         (group gnus-newsgroup-name))
5493     (setq gnus-newsgroup-begin nil)
5494     (gnus-summary-exit)
5495     ;; We have to adjust the point of group mode buffer because
5496     ;; point was moved to the next unread newsgroup by exiting.
5497     (gnus-summary-jump-to-group group)
5498     (when rescan
5499       (save-excursion
5500         (gnus-group-get-new-news-this-group 1)))
5501     (gnus-group-read-group all t)
5502     (gnus-summary-goto-subject current-subject nil t)))
5503
5504 (defun gnus-summary-rescan-group (&optional all)
5505   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5506   (interactive "P")
5507   (gnus-summary-reselect-current-group all t))
5508
5509 (defun gnus-summary-update-info (&optional non-destructive)
5510   (save-excursion
5511     (let ((group gnus-newsgroup-name))
5512       (when group
5513         (when gnus-newsgroup-kill-headers
5514           (setq gnus-newsgroup-killed
5515                 (gnus-compress-sequence
5516                  (nconc
5517                   (gnus-set-sorted-intersection
5518                    (gnus-uncompress-range gnus-newsgroup-killed)
5519                    (setq gnus-newsgroup-unselected
5520                          (sort gnus-newsgroup-unselected '<)))
5521                   (setq gnus-newsgroup-unreads
5522                         (sort gnus-newsgroup-unreads '<)))
5523                  t)))
5524         (unless (listp (cdr gnus-newsgroup-killed))
5525           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5526         (let ((headers gnus-newsgroup-headers))
5527           ;; Set the new ranges of read articles.
5528           (save-excursion
5529             (set-buffer gnus-group-buffer)
5530             (gnus-undo-force-boundary))
5531           (gnus-update-read-articles
5532            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5533           ;; Set the current article marks.
5534           (let ((gnus-newsgroup-scored
5535                  (if (and (not gnus-save-score)
5536                           (not non-destructive))
5537                      nil
5538                    gnus-newsgroup-scored)))
5539             (save-excursion
5540               (gnus-update-marks)))
5541           ;; Do the cross-ref thing.
5542           (when gnus-use-cross-reference
5543             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5544           ;; Do not switch windows but change the buffer to work.
5545           (set-buffer gnus-group-buffer)
5546           (unless (gnus-ephemeral-group-p group)
5547             (gnus-group-update-group group)))))))
5548
5549 (defun gnus-summary-save-newsrc (&optional force)
5550   "Save the current number of read/marked articles in the dribble buffer.
5551 The dribble buffer will then be saved.
5552 If FORCE (the prefix), also save the .newsrc file(s)."
5553   (interactive "P")
5554   (gnus-summary-update-info t)
5555   (if force
5556       (gnus-save-newsrc-file)
5557     (gnus-dribble-save)))
5558
5559 (defun gnus-summary-exit (&optional temporary)
5560   "Exit reading current newsgroup, and then return to group selection mode.
5561 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5562   (interactive)
5563   (gnus-set-global-variables)
5564   (when (gnus-buffer-live-p gnus-article-buffer)
5565     (save-excursion
5566       (set-buffer gnus-article-buffer)
5567       (mm-destroy-parts gnus-article-mime-handles)
5568       ;; Set it to nil for safety reason.
5569       (setq gnus-article-mime-handle-alist nil)
5570       (setq gnus-article-mime-handles nil)))
5571   (gnus-kill-save-kill-buffer)
5572   (gnus-async-halt-prefetch)
5573   (let* ((group gnus-newsgroup-name)
5574          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5575          (mode major-mode)
5576          (group-point nil)
5577          (buf (current-buffer)))
5578     (unless quit-config
5579       ;; Do adaptive scoring, and possibly save score files.
5580       (when gnus-newsgroup-adaptive
5581         (gnus-score-adaptive))
5582       (when gnus-use-scoring
5583         (gnus-score-save)))
5584     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5585     ;; If we have several article buffers, we kill them at exit.
5586     (unless gnus-single-article-buffer
5587       (gnus-kill-buffer gnus-original-article-buffer)
5588       (setq gnus-article-current nil))
5589     (when gnus-use-cache
5590       (gnus-cache-possibly-remove-articles)
5591       (gnus-cache-save-buffers))
5592     (gnus-async-prefetch-remove-group group)
5593     (when gnus-suppress-duplicates
5594       (gnus-dup-enter-articles))
5595     (when gnus-use-trees
5596       (gnus-tree-close group))
5597     (when gnus-use-cache
5598       (gnus-cache-write-active))
5599     ;; Remove entries for this group.
5600     (nnmail-purge-split-history (gnus-group-real-name group))
5601     ;; Make all changes in this group permanent.
5602     (unless quit-config
5603       (gnus-run-hooks 'gnus-exit-group-hook)
5604       (gnus-summary-update-info))
5605     (gnus-close-group group)
5606     ;; Make sure where we were, and go to next newsgroup.
5607     (set-buffer gnus-group-buffer)
5608     (unless quit-config
5609       (gnus-group-jump-to-group group))
5610     (gnus-run-hooks 'gnus-summary-exit-hook)
5611     (unless (or quit-config
5612                 ;; If this group has disappeared from the summary
5613                 ;; buffer, don't skip forwards.
5614                 (not (string= group (gnus-group-group-name))))
5615       (gnus-group-next-unread-group 1))
5616     (setq group-point (point))
5617     (if temporary
5618         nil                             ;Nothing to do.
5619       ;; If we have several article buffers, we kill them at exit.
5620       (unless gnus-single-article-buffer
5621         (gnus-kill-buffer gnus-article-buffer)
5622         (gnus-kill-buffer gnus-original-article-buffer)
5623         (setq gnus-article-current nil))
5624       (set-buffer buf)
5625       (if (not gnus-kill-summary-on-exit)
5626           (gnus-deaden-summary)
5627         ;; We set all buffer-local variables to nil.  It is unclear why
5628         ;; this is needed, but if we don't, buffer-local variables are
5629         ;; not garbage-collected, it seems.  This would the lead to en
5630         ;; ever-growing Emacs.
5631         (gnus-summary-clear-local-variables)
5632         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5633           (gnus-summary-clear-local-variables))
5634         (when (get-buffer gnus-article-buffer)
5635           (bury-buffer gnus-article-buffer))
5636         ;; We clear the global counterparts of the buffer-local
5637         ;; variables as well, just to be on the safe side.
5638         (set-buffer gnus-group-buffer)
5639         (gnus-summary-clear-local-variables)
5640         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5641           (gnus-summary-clear-local-variables))
5642         ;; Return to group mode buffer.
5643         (when (eq mode 'gnus-summary-mode)
5644           (gnus-kill-buffer buf)))
5645       (setq gnus-current-select-method gnus-select-method)
5646       (pop-to-buffer gnus-group-buffer)
5647       (if (not quit-config)
5648           (progn
5649             (goto-char group-point)
5650             (gnus-configure-windows 'group 'force))
5651         (gnus-handle-ephemeral-exit quit-config))
5652       ;; Clear the current group name.
5653       (unless quit-config
5654         (setq gnus-newsgroup-name nil)))))
5655
5656 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5657 (defun gnus-summary-exit-no-update (&optional no-questions)
5658   "Quit reading current newsgroup without updating read article info."
5659   (interactive)
5660   (let* ((group gnus-newsgroup-name)
5661          (quit-config (gnus-group-quit-config group)))
5662     (when (or no-questions
5663               gnus-expert-user
5664               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5665       (gnus-async-halt-prefetch)
5666       (mapcar 'funcall
5667               (delq 'gnus-summary-expire-articles
5668                     (copy-sequence gnus-summary-prepare-exit-hook)))
5669       (when (gnus-buffer-live-p gnus-article-buffer)
5670         (save-excursion
5671           (set-buffer gnus-article-buffer)
5672           (mm-destroy-parts gnus-article-mime-handles)
5673           ;; Set it to nil for safety reason.
5674           (setq gnus-article-mime-handle-alist nil)
5675           (setq gnus-article-mime-handles nil)))
5676       ;; If we have several article buffers, we kill them at exit.
5677       (unless gnus-single-article-buffer
5678         (gnus-kill-buffer gnus-article-buffer)
5679         (gnus-kill-buffer gnus-original-article-buffer)
5680         (setq gnus-article-current nil))
5681       (if (not gnus-kill-summary-on-exit)
5682           (gnus-deaden-summary)
5683         (gnus-close-group group)
5684         (gnus-summary-clear-local-variables)
5685         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5686           (gnus-summary-clear-local-variables))
5687         (set-buffer gnus-group-buffer)
5688         (gnus-summary-clear-local-variables)
5689         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5690           (gnus-summary-clear-local-variables))
5691         (when (get-buffer gnus-summary-buffer)
5692           (kill-buffer gnus-summary-buffer)))
5693       (unless gnus-single-article-buffer
5694         (setq gnus-article-current nil))
5695       (when gnus-use-trees
5696         (gnus-tree-close group))
5697       (gnus-async-prefetch-remove-group group)
5698       (when (get-buffer gnus-article-buffer)
5699         (bury-buffer gnus-article-buffer))
5700       ;; Return to the group buffer.
5701       (gnus-configure-windows 'group 'force)
5702       ;; Clear the current group name.
5703       (setq gnus-newsgroup-name nil)
5704       (when (equal (gnus-group-group-name) group)
5705         (gnus-group-next-unread-group 1))
5706       (when quit-config
5707         (gnus-handle-ephemeral-exit quit-config)))))
5708
5709 (defun gnus-handle-ephemeral-exit (quit-config)
5710   "Handle movement when leaving an ephemeral group.
5711 The state which existed when entering the ephemeral is reset."
5712   (if (not (buffer-name (car quit-config)))
5713       (gnus-configure-windows 'group 'force)
5714     (set-buffer (car quit-config))
5715     (cond ((eq major-mode 'gnus-summary-mode)
5716            (gnus-set-global-variables))
5717           ((eq major-mode 'gnus-article-mode)
5718            (save-excursion
5719              ;; The `gnus-summary-buffer' variable may point
5720              ;; to the old summary buffer when using a single
5721              ;; article buffer.
5722              (unless (gnus-buffer-live-p gnus-summary-buffer)
5723                (set-buffer gnus-group-buffer))
5724              (set-buffer gnus-summary-buffer)
5725              (gnus-set-global-variables))))
5726     (if (or (eq (cdr quit-config) 'article)
5727             (eq (cdr quit-config) 'pick))
5728         (progn
5729           ;; The current article may be from the ephemeral group
5730           ;; thus it is best that we reload this article
5731           (gnus-summary-show-article)
5732           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5733               (gnus-configure-windows 'pick 'force)
5734             (gnus-configure-windows (cdr quit-config) 'force)))
5735       (gnus-configure-windows (cdr quit-config) 'force))
5736     (when (eq major-mode 'gnus-summary-mode)
5737       (gnus-summary-next-subject 1 nil t)
5738       (gnus-summary-recenter)
5739       (gnus-summary-position-point))))
5740
5741 ;;; Dead summaries.
5742
5743 (defvar gnus-dead-summary-mode-map nil)
5744
5745 (unless gnus-dead-summary-mode-map
5746   (setq gnus-dead-summary-mode-map (make-keymap))
5747   (suppress-keymap gnus-dead-summary-mode-map)
5748   (substitute-key-definition
5749    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5750   (let ((keys '("\C-d" "\r" "\177" [delete])))
5751     (while keys
5752       (define-key gnus-dead-summary-mode-map
5753         (pop keys) 'gnus-summary-wake-up-the-dead))))
5754
5755 (defvar gnus-dead-summary-mode nil
5756   "Minor mode for Gnus summary buffers.")
5757
5758 (defun gnus-dead-summary-mode (&optional arg)
5759   "Minor mode for Gnus summary buffers."
5760   (interactive "P")
5761   (when (eq major-mode 'gnus-summary-mode)
5762     (make-local-variable 'gnus-dead-summary-mode)
5763     (setq gnus-dead-summary-mode
5764           (if (null arg) (not gnus-dead-summary-mode)
5765             (> (prefix-numeric-value arg) 0)))
5766     (when gnus-dead-summary-mode
5767       (gnus-add-minor-mode
5768        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5769
5770 (defun gnus-deaden-summary ()
5771   "Make the current summary buffer into a dead summary buffer."
5772   ;; Kill any previous dead summary buffer.
5773   (when (and gnus-dead-summary
5774              (buffer-name gnus-dead-summary))
5775     (save-excursion
5776       (set-buffer gnus-dead-summary)
5777       (when gnus-dead-summary-mode
5778         (kill-buffer (current-buffer)))))
5779   ;; Make this the current dead summary.
5780   (setq gnus-dead-summary (current-buffer))
5781   (gnus-dead-summary-mode 1)
5782   (let ((name (buffer-name)))
5783     (when (string-match "Summary" name)
5784       (rename-buffer
5785        (concat (substring name 0 (match-beginning 0)) "Dead "
5786                (substring name (match-beginning 0)))
5787        t)
5788       (bury-buffer))))
5789
5790 (defun gnus-kill-or-deaden-summary (buffer)
5791   "Kill or deaden the summary BUFFER."
5792   (save-excursion
5793     (when (and (buffer-name buffer)
5794                (not gnus-single-article-buffer))
5795       (save-excursion
5796         (set-buffer buffer)
5797         (gnus-kill-buffer gnus-article-buffer)
5798         (gnus-kill-buffer gnus-original-article-buffer)))
5799     (cond (gnus-kill-summary-on-exit
5800            (when (and gnus-use-trees
5801                       (gnus-buffer-exists-p buffer))
5802              (save-excursion
5803                (set-buffer buffer)
5804                (gnus-tree-close gnus-newsgroup-name)))
5805            (gnus-kill-buffer buffer))
5806           ((gnus-buffer-exists-p buffer)
5807            (save-excursion
5808              (set-buffer buffer)
5809              (gnus-deaden-summary))))))
5810
5811 (defun gnus-summary-wake-up-the-dead (&rest args)
5812   "Wake up the dead summary buffer."
5813   (interactive)
5814   (gnus-dead-summary-mode -1)
5815   (let ((name (buffer-name)))
5816     (when (string-match "Dead " name)
5817       (rename-buffer
5818        (concat (substring name 0 (match-beginning 0))
5819                (substring name (match-end 0)))
5820        t)))
5821   (gnus-message 3 "This dead summary is now alive again"))
5822
5823 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5824 (defun gnus-summary-fetch-faq (&optional faq-dir)
5825   "Fetch the FAQ for the current group.
5826 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5827 in."
5828   (interactive
5829    (list
5830     (when current-prefix-arg
5831       (completing-read
5832        "Faq dir: " (and (listp gnus-group-faq-directory)
5833                         (mapcar (lambda (file) (list file))
5834                                 gnus-group-faq-directory))))))
5835   (let (gnus-faq-buffer)
5836     (when (setq gnus-faq-buffer
5837                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5838       (gnus-configure-windows 'summary-faq))))
5839
5840 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5841 (defun gnus-summary-describe-group (&optional force)
5842   "Describe the current newsgroup."
5843   (interactive "P")
5844   (gnus-group-describe-group force gnus-newsgroup-name))
5845
5846 (defun gnus-summary-describe-briefly ()
5847   "Describe summary mode commands briefly."
5848   (interactive)
5849   (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")))
5850
5851 ;; Walking around group mode buffer from summary mode.
5852
5853 (defun gnus-summary-next-group (&optional no-article target-group backward)
5854   "Exit current newsgroup and then select next unread newsgroup.
5855 If prefix argument NO-ARTICLE is non-nil, no article is selected
5856 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5857 previous group instead."
5858   (interactive "P")
5859   ;; Stop pre-fetching.
5860   (gnus-async-halt-prefetch)
5861   (let ((current-group gnus-newsgroup-name)
5862         (current-buffer (current-buffer))
5863         entered)
5864     ;; First we semi-exit this group to update Xrefs and all variables.
5865     ;; We can't do a real exit, because the window conf must remain
5866     ;; the same in case the user is prompted for info, and we don't
5867     ;; want the window conf to change before that...
5868     (gnus-summary-exit t)
5869     (while (not entered)
5870       ;; Then we find what group we are supposed to enter.
5871       (set-buffer gnus-group-buffer)
5872       (gnus-group-jump-to-group current-group)
5873       (setq target-group
5874             (or target-group
5875                 (if (eq gnus-keep-same-level 'best)
5876                     (gnus-summary-best-group gnus-newsgroup-name)
5877                   (gnus-summary-search-group backward gnus-keep-same-level))))
5878       (if (not target-group)
5879           ;; There are no further groups, so we return to the group
5880           ;; buffer.
5881           (progn
5882             (gnus-message 5 "Returning to the group buffer")
5883             (setq entered t)
5884             (when (gnus-buffer-live-p current-buffer)
5885               (set-buffer current-buffer)
5886               (gnus-summary-exit))
5887             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5888         ;; We try to enter the target group.
5889         (gnus-group-jump-to-group target-group)
5890         (let ((unreads (gnus-group-group-unread)))
5891           (if (and (or (eq t unreads)
5892                        (and unreads (not (zerop unreads))))
5893                    (gnus-summary-read-group
5894                     target-group nil no-article
5895                     (and (buffer-name current-buffer) current-buffer)
5896                     nil backward))
5897               (setq entered t)
5898             (setq current-group target-group
5899                   target-group nil)))))))
5900
5901 (defun gnus-summary-prev-group (&optional no-article)
5902   "Exit current newsgroup and then select previous unread newsgroup.
5903 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5904   (interactive "P")
5905   (gnus-summary-next-group no-article nil t))
5906
5907 ;; Walking around summary lines.
5908
5909 (defun gnus-summary-first-subject (&optional unread undownloaded)
5910   "Go to the first unread subject.
5911 If UNREAD is non-nil, go to the first unread article.
5912 Returns the article selected or nil if there are no unread articles."
5913   (interactive "P")
5914   (prog1
5915       (cond
5916        ;; Empty summary.
5917        ((null gnus-newsgroup-data)
5918         (gnus-message 3 "No articles in the group")
5919         nil)
5920        ;; Pick the first article.
5921        ((not unread)
5922         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5923         (gnus-data-number (car gnus-newsgroup-data)))
5924        ;; No unread articles.
5925        ((null gnus-newsgroup-unreads)
5926         (gnus-message 3 "No more unread articles")
5927         nil)
5928        ;; Find the first unread article.
5929        (t
5930         (let ((data gnus-newsgroup-data))
5931           (while (and data
5932                       (and (not (and undownloaded
5933                                      (eq gnus-undownloaded-mark
5934                                          (gnus-data-mark (car data)))))
5935                            (not (gnus-data-unread-p (car data)))))
5936             (setq data (cdr data)))
5937           (when data
5938             (goto-char (gnus-data-pos (car data)))
5939             (gnus-data-number (car data))))))
5940     (gnus-summary-position-point)))
5941
5942 (defun gnus-summary-next-subject (n &optional unread dont-display)
5943   "Go to next N'th summary line.
5944 If N is negative, go to the previous N'th subject line.
5945 If UNREAD is non-nil, only unread articles are selected.
5946 The difference between N and the actual number of steps taken is
5947 returned."
5948   (interactive "p")
5949   (let ((backward (< n 0))
5950         (n (abs n)))
5951     (while (and (> n 0)
5952                 (if backward
5953                     (gnus-summary-find-prev unread)
5954                   (gnus-summary-find-next unread)))
5955       (unless (zerop (setq n (1- n)))
5956         (gnus-summary-show-thread)))
5957     (when (/= 0 n)
5958       (gnus-message 7 "No more%s articles"
5959                     (if unread " unread" "")))
5960     (unless dont-display
5961       (gnus-summary-recenter)
5962       (gnus-summary-position-point))
5963     n))
5964
5965 (defun gnus-summary-next-unread-subject (n)
5966   "Go to next N'th unread summary line."
5967   (interactive "p")
5968   (gnus-summary-next-subject n t))
5969
5970 (defun gnus-summary-prev-subject (n &optional unread)
5971   "Go to previous N'th summary line.
5972 If optional argument UNREAD is non-nil, only unread article is selected."
5973   (interactive "p")
5974   (gnus-summary-next-subject (- n) unread))
5975
5976 (defun gnus-summary-prev-unread-subject (n)
5977   "Go to previous N'th unread summary line."
5978   (interactive "p")
5979   (gnus-summary-next-subject (- n) t))
5980
5981 (defun gnus-summary-goto-subject (article &optional force silent)
5982   "Go the subject line of ARTICLE.
5983 If FORCE, also allow jumping to articles not currently shown."
5984   (interactive "nArticle number: ")
5985   (let ((b (point))
5986         (data (gnus-data-find article)))
5987     ;; We read in the article if we have to.
5988     (and (not data)
5989          force
5990          (gnus-summary-insert-subject
5991           article
5992           (if (or (numberp force) (vectorp force)) force)
5993           t)
5994          (setq data (gnus-data-find article)))
5995     (goto-char b)
5996     (if (not data)
5997         (progn
5998           (unless silent
5999             (gnus-message 3 "Can't find article %d" article))
6000           nil)
6001       (goto-char (gnus-data-pos data))
6002       (gnus-summary-position-point)
6003       article)))
6004
6005 ;; Walking around summary lines with displaying articles.
6006
6007 (defun gnus-summary-expand-window (&optional arg)
6008   "Make the summary buffer take up the entire Emacs frame.
6009 Given a prefix, will force an `article' buffer configuration."
6010   (interactive "P")
6011   (if arg
6012       (gnus-configure-windows 'article 'force)
6013     (gnus-configure-windows 'summary 'force)))
6014
6015 (defun gnus-summary-display-article (article &optional all-header)
6016   "Display ARTICLE in article buffer."
6017   (when (gnus-buffer-live-p gnus-article-buffer)
6018     (with-current-buffer gnus-article-buffer
6019       (mm-enable-multibyte-mule4)))
6020   (gnus-set-global-variables)
6021   (when (gnus-buffer-live-p gnus-article-buffer)
6022     (with-current-buffer gnus-article-buffer
6023       (setq gnus-article-charset gnus-newsgroup-charset)
6024       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6025       (mm-enable-multibyte-mule4)))
6026   (if (null article)
6027       nil
6028     (prog1
6029         (if gnus-summary-display-article-function
6030             (funcall gnus-summary-display-article-function article all-header)
6031           (gnus-article-prepare article all-header))
6032       (gnus-run-hooks 'gnus-select-article-hook)
6033       (when (and gnus-current-article
6034                  (not (zerop gnus-current-article)))
6035         (gnus-summary-goto-subject gnus-current-article))
6036       (gnus-summary-recenter)
6037       (when (and gnus-use-trees gnus-show-threads)
6038         (gnus-possibly-generate-tree article)
6039         (gnus-highlight-selected-tree article))
6040       ;; Successfully display article.
6041       (gnus-article-set-window-start
6042        (cdr (assq article gnus-newsgroup-bookmarks))))))
6043
6044 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6045   "Select the current article.
6046 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6047 non-nil, the article will be re-fetched even if it already present in
6048 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6049 be displayed."
6050   ;; Make sure we are in the summary buffer to work around bbdb bug.
6051   (unless (eq major-mode 'gnus-summary-mode)
6052     (set-buffer gnus-summary-buffer))
6053   (let ((article (or article (gnus-summary-article-number)))
6054         (all-headers (not (not all-headers))) ;Must be T or NIL.
6055         gnus-summary-display-article-function)
6056     (and (not pseudo)
6057          (gnus-summary-article-pseudo-p article)
6058          (error "This is a pseudo-article"))
6059     (save-excursion
6060       (set-buffer gnus-summary-buffer)
6061       (if (or (and gnus-single-article-buffer
6062                    (or (null gnus-current-article)
6063                        (null gnus-article-current)
6064                        (null (get-buffer gnus-article-buffer))
6065                        (not (eq article (cdr gnus-article-current)))
6066                        (not (equal (car gnus-article-current)
6067                                    gnus-newsgroup-name))))
6068               (and (not gnus-single-article-buffer)
6069                    (or (null gnus-current-article)
6070                        (not (eq gnus-current-article article))))
6071               force)
6072           ;; The requested article is different from the current article.
6073           (progn
6074             (gnus-summary-display-article article all-headers)
6075             (when (gnus-buffer-live-p gnus-article-buffer)
6076                (with-current-buffer gnus-article-buffer
6077                 (if (not gnus-article-decoded-p) ;; a local variable
6078                     (mm-disable-multibyte-mule4))))
6079             (when (or all-headers gnus-show-all-headers)
6080               (gnus-article-show-all-headers))
6081             (gnus-article-set-window-start
6082              (cdr (assq article gnus-newsgroup-bookmarks)))
6083             article)
6084         (when (or all-headers gnus-show-all-headers)
6085           (gnus-article-show-all-headers))
6086         'old))))
6087
6088 (defun gnus-summary-force-verify-and-decrypt ()
6089   (interactive)
6090   (let ((mm-verify-option 'known)
6091         (mm-decrypt-option 'known))
6092     (gnus-summary-select-article nil 'force)))
6093
6094 (defun gnus-summary-set-current-mark (&optional current-mark)
6095   "Obsolete function."
6096   nil)
6097
6098 (defun gnus-summary-next-article (&optional unread subject backward push)
6099   "Select the next article.
6100 If UNREAD, only unread articles are selected.
6101 If SUBJECT, only articles with SUBJECT are selected.
6102 If BACKWARD, the previous article is selected instead of the next."
6103   (interactive "P")
6104   (cond
6105    ;; Is there such an article?
6106    ((and (gnus-summary-search-forward unread subject backward)
6107          (or (gnus-summary-display-article (gnus-summary-article-number))
6108              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6109     (gnus-summary-position-point))
6110    ;; If not, we try the first unread, if that is wanted.
6111    ((and subject
6112          gnus-auto-select-same
6113          (gnus-summary-first-unread-article))
6114     (gnus-summary-position-point)
6115     (gnus-message 6 "Wrapped"))
6116    ;; Try to get next/previous article not displayed in this group.
6117    ((and gnus-auto-extend-newsgroup
6118          (not unread) (not subject))
6119     (gnus-summary-goto-article
6120      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6121      nil (count-lines (point-min) (point))))
6122    ;; Go to next/previous group.
6123    (t
6124     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6125       (gnus-summary-jump-to-group gnus-newsgroup-name))
6126     (let ((cmd last-command-char)
6127           (point
6128            (save-excursion
6129              (set-buffer gnus-group-buffer)
6130              (point)))
6131           (group
6132            (if (eq gnus-keep-same-level 'best)
6133                (gnus-summary-best-group gnus-newsgroup-name)
6134              (gnus-summary-search-group backward gnus-keep-same-level))))
6135       ;; For some reason, the group window gets selected.  We change
6136       ;; it back.
6137       (select-window (get-buffer-window (current-buffer)))
6138       ;; Select next unread newsgroup automagically.
6139       (cond
6140        ((or (not gnus-auto-select-next)
6141             (not cmd))
6142         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6143        ((or (eq gnus-auto-select-next 'quietly)
6144             (and (eq gnus-auto-select-next 'slightly-quietly)
6145                  push)
6146             (and (eq gnus-auto-select-next 'almost-quietly)
6147                  (gnus-summary-last-article-p)))
6148         ;; Select quietly.
6149         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6150             (gnus-summary-exit)
6151           (gnus-message 7 "No more%s articles (%s)..."
6152                         (if unread " unread" "")
6153                         (if group (concat "selecting " group)
6154                           "exiting"))
6155           (gnus-summary-next-group nil group backward)))
6156        (t
6157         (when (gnus-key-press-event-p last-input-event)
6158           (gnus-summary-walk-group-buffer
6159            gnus-newsgroup-name cmd unread backward point))))))))
6160
6161 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6162   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6163                       (?\C-p (gnus-group-prev-unread-group 1))))
6164         (cursor-in-echo-area t)
6165         keve key group ended)
6166     (save-excursion
6167       (set-buffer gnus-group-buffer)
6168       (goto-char start)
6169       (setq group
6170             (if (eq gnus-keep-same-level 'best)
6171                 (gnus-summary-best-group gnus-newsgroup-name)
6172               (gnus-summary-search-group backward gnus-keep-same-level))))
6173     (while (not ended)
6174       (gnus-message
6175        5 "No more%s articles%s" (if unread " unread" "")
6176        (if (and group
6177                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6178            (format " (Type %s for %s [%s])"
6179                    (single-key-description cmd) group
6180                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6181          (format " (Type %s to exit %s)"
6182                  (single-key-description cmd)
6183                  gnus-newsgroup-name)))
6184       ;; Confirm auto selection.
6185       (setq key (car (setq keve (gnus-read-event-char))))
6186       (setq ended t)
6187       (cond
6188        ((assq key keystrokes)
6189         (let ((obuf (current-buffer)))
6190           (switch-to-buffer gnus-group-buffer)
6191           (when group
6192             (gnus-group-jump-to-group group))
6193           (eval (cadr (assq key keystrokes)))
6194           (setq group (gnus-group-group-name))
6195           (switch-to-buffer obuf))
6196         (setq ended nil))
6197        ((equal key cmd)
6198         (if (or (not group)
6199                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6200             (gnus-summary-exit)
6201           (gnus-summary-next-group nil group backward)))
6202        (t
6203         (push (cdr keve) unread-command-events))))))
6204
6205 (defun gnus-summary-next-unread-article ()
6206   "Select unread article after current one."
6207   (interactive)
6208   (gnus-summary-next-article
6209    (or (not (eq gnus-summary-goto-unread 'never))
6210        (gnus-summary-last-article-p (gnus-summary-article-number)))
6211    (and gnus-auto-select-same
6212         (gnus-summary-article-subject))))
6213
6214 (defun gnus-summary-prev-article (&optional unread subject)
6215   "Select the article after the current one.
6216 If UNREAD is non-nil, only unread articles are selected."
6217   (interactive "P")
6218   (gnus-summary-next-article unread subject t))
6219
6220 (defun gnus-summary-prev-unread-article ()
6221   "Select unread article before current one."
6222   (interactive)
6223   (gnus-summary-prev-article
6224    (or (not (eq gnus-summary-goto-unread 'never))
6225        (gnus-summary-first-article-p (gnus-summary-article-number)))
6226    (and gnus-auto-select-same
6227         (gnus-summary-article-subject))))
6228
6229 (defun gnus-summary-next-page (&optional lines circular)
6230   "Show next page of the selected article.
6231 If at the end of the current article, select the next article.
6232 LINES says how many lines should be scrolled up.
6233
6234 If CIRCULAR is non-nil, go to the start of the article instead of
6235 selecting the next article when reaching the end of the current
6236 article."
6237   (interactive "P")
6238   (setq gnus-summary-buffer (current-buffer))
6239   (gnus-set-global-variables)
6240   (let ((article (gnus-summary-article-number))
6241         (article-window (get-buffer-window gnus-article-buffer t))
6242         endp)
6243     ;; If the buffer is empty, we have no article.
6244     (unless article
6245       (error "No article to select"))
6246     (gnus-configure-windows 'article)
6247     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6248         (if (and (eq gnus-summary-goto-unread 'never)
6249                  (not (gnus-summary-last-article-p article)))
6250             (gnus-summary-next-article)
6251           (gnus-summary-next-unread-article))
6252       (if (or (null gnus-current-article)
6253               (null gnus-article-current)
6254               (/= article (cdr gnus-article-current))
6255               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6256           ;; Selected subject is different from current article's.
6257           (gnus-summary-display-article article)
6258         (when article-window
6259           (gnus-eval-in-buffer-window gnus-article-buffer
6260             (setq endp (gnus-article-next-page lines)))
6261           (when endp
6262             (cond (circular
6263                    (gnus-summary-beginning-of-article))
6264                   (lines
6265                    (gnus-message 3 "End of message"))
6266                   ((null lines)
6267                    (if (and (eq gnus-summary-goto-unread 'never)
6268                             (not (gnus-summary-last-article-p article)))
6269                        (gnus-summary-next-article)
6270                      (gnus-summary-next-unread-article))))))))
6271     (gnus-summary-recenter)
6272     (gnus-summary-position-point)))
6273
6274 (defun gnus-summary-prev-page (&optional lines move)
6275   "Show previous page of selected article.
6276 Argument LINES specifies lines to be scrolled down.
6277 If MOVE, move to the previous unread article if point is at
6278 the beginning of the buffer."
6279   (interactive "P")
6280   (let ((article (gnus-summary-article-number))
6281         (article-window (get-buffer-window gnus-article-buffer t))
6282         endp)
6283     (gnus-configure-windows 'article)
6284     (if (or (null gnus-current-article)
6285             (null gnus-article-current)
6286             (/= article (cdr gnus-article-current))
6287             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6288         ;; Selected subject is different from current article's.
6289         (gnus-summary-display-article article)
6290       (gnus-summary-recenter)
6291       (when article-window
6292         (gnus-eval-in-buffer-window gnus-article-buffer
6293           (setq endp (gnus-article-prev-page lines)))
6294         (when (and move endp)
6295           (cond (lines
6296                  (gnus-message 3 "Beginning of message"))
6297                 ((null lines)
6298                  (if (and (eq gnus-summary-goto-unread 'never)
6299                           (not (gnus-summary-first-article-p article)))
6300                      (gnus-summary-prev-article)
6301                    (gnus-summary-prev-unread-article))))))))
6302   (gnus-summary-position-point))
6303
6304 (defun gnus-summary-prev-page-or-article (&optional lines)
6305   "Show previous page of selected article.
6306 Argument LINES specifies lines to be scrolled down.
6307 If at the beginning of the article, go to the next article."
6308   (interactive "P")
6309   (gnus-summary-prev-page lines t))
6310
6311 (defun gnus-summary-scroll-up (lines)
6312   "Scroll up (or down) one line current article.
6313 Argument LINES specifies lines to be scrolled up (or down if negative)."
6314   (interactive "p")
6315   (gnus-configure-windows 'article)
6316   (gnus-summary-show-thread)
6317   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6318     (gnus-eval-in-buffer-window gnus-article-buffer
6319       (cond ((> lines 0)
6320              (when (gnus-article-next-page lines)
6321                (gnus-message 3 "End of message")))
6322             ((< lines 0)
6323              (gnus-article-prev-page (- lines))))))
6324   (gnus-summary-recenter)
6325   (gnus-summary-position-point))
6326
6327 (defun gnus-summary-scroll-down (lines)
6328   "Scroll down (or up) one line current article.
6329 Argument LINES specifies lines to be scrolled down (or up if negative)."
6330   (interactive "p")
6331   (gnus-summary-scroll-up (- lines)))
6332
6333 (defun gnus-summary-next-same-subject ()
6334   "Select next article which has the same subject as current one."
6335   (interactive)
6336   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6337
6338 (defun gnus-summary-prev-same-subject ()
6339   "Select previous article which has the same subject as current one."
6340   (interactive)
6341   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6342
6343 (defun gnus-summary-next-unread-same-subject ()
6344   "Select next unread article which has the same subject as current one."
6345   (interactive)
6346   (gnus-summary-next-article t (gnus-summary-article-subject)))
6347
6348 (defun gnus-summary-prev-unread-same-subject ()
6349   "Select previous unread article which has the same subject as current one."
6350   (interactive)
6351   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6352
6353 (defun gnus-summary-first-unread-article ()
6354   "Select the first unread article.
6355 Return nil if there are no unread articles."
6356   (interactive)
6357   (prog1
6358       (when (gnus-summary-first-subject t)
6359         (gnus-summary-show-thread)
6360         (gnus-summary-first-subject t)
6361         (gnus-summary-display-article (gnus-summary-article-number)))
6362     (gnus-summary-position-point)))
6363
6364 (defun gnus-summary-first-unread-subject ()
6365   "Place the point on the subject line of the first unread article.
6366 Return nil if there are no unread articles."
6367   (interactive)
6368   (prog1
6369       (when (gnus-summary-first-subject t)
6370         (gnus-summary-show-thread)
6371         (gnus-summary-first-subject t))
6372     (gnus-summary-position-point)))
6373
6374 (defun gnus-summary-first-article ()
6375   "Select the first article.
6376 Return nil if there are no articles."
6377   (interactive)
6378   (prog1
6379       (when (gnus-summary-first-subject)
6380         (gnus-summary-show-thread)
6381         (gnus-summary-first-subject)
6382         (gnus-summary-display-article (gnus-summary-article-number)))
6383     (gnus-summary-position-point)))
6384
6385 (defun gnus-summary-best-unread-article ()
6386   "Select the unread article with the highest score."
6387   (interactive)
6388   (let ((best -1000000)
6389         (data gnus-newsgroup-data)
6390         article score)
6391     (while data
6392       (and (gnus-data-unread-p (car data))
6393            (> (setq score
6394                     (gnus-summary-article-score (gnus-data-number (car data))))
6395               best)
6396            (setq best score
6397                  article (gnus-data-number (car data))))
6398       (setq data (cdr data)))
6399     (prog1
6400         (if article
6401             (gnus-summary-goto-article article)
6402           (error "No unread articles"))
6403       (gnus-summary-position-point))))
6404
6405 (defun gnus-summary-last-subject ()
6406   "Go to the last displayed subject line in the group."
6407   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6408     (when article
6409       (gnus-summary-goto-subject article))))
6410
6411 (defun gnus-summary-goto-article (article &optional all-headers force)
6412   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6413 If ALL-HEADERS is non-nil, no header lines are hidden.
6414 If FORCE, go to the article even if it isn't displayed.  If FORCE
6415 is a number, it is the line the article is to be displayed on."
6416   (interactive
6417    (list
6418     (completing-read
6419      "Article number or Message-ID: "
6420      (mapcar (lambda (number) (list (int-to-string number)))
6421              gnus-newsgroup-limit))
6422     current-prefix-arg
6423     t))
6424   (prog1
6425       (if (and (stringp article)
6426                (string-match "@" article))
6427           (gnus-summary-refer-article article)
6428         (when (stringp article)
6429           (setq article (string-to-number article)))
6430         (if (gnus-summary-goto-subject article force)
6431             (gnus-summary-display-article article all-headers)
6432           (gnus-message 4 "Couldn't go to article %s" article) nil))
6433     (gnus-summary-position-point)))
6434
6435 (defun gnus-summary-goto-last-article ()
6436   "Go to the previously read article."
6437   (interactive)
6438   (prog1
6439       (when gnus-last-article
6440         (gnus-summary-goto-article gnus-last-article nil t))
6441     (gnus-summary-position-point)))
6442
6443 (defun gnus-summary-pop-article (number)
6444   "Pop one article off the history and go to the previous.
6445 NUMBER articles will be popped off."
6446   (interactive "p")
6447   (let (to)
6448     (setq gnus-newsgroup-history
6449           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6450     (if to
6451         (gnus-summary-goto-article (car to) nil t)
6452       (error "Article history empty")))
6453   (gnus-summary-position-point))
6454
6455 ;; Summary commands and functions for limiting the summary buffer.
6456
6457 (defun gnus-summary-limit-to-articles (n)
6458   "Limit the summary buffer to the next N articles.
6459 If not given a prefix, use the process marked articles instead."
6460   (interactive "P")
6461   (prog1
6462       (let ((articles (gnus-summary-work-articles n)))
6463         (setq gnus-newsgroup-processable nil)
6464         (gnus-summary-limit articles))
6465     (gnus-summary-position-point)))
6466
6467 (defun gnus-summary-pop-limit (&optional total)
6468   "Restore the previous limit.
6469 If given a prefix, remove all limits."
6470   (interactive "P")
6471   (when total
6472     (setq gnus-newsgroup-limits
6473           (list (mapcar (lambda (h) (mail-header-number h))
6474                         gnus-newsgroup-headers))))
6475   (unless gnus-newsgroup-limits
6476     (error "No limit to pop"))
6477   (prog1
6478       (gnus-summary-limit nil 'pop)
6479     (gnus-summary-position-point)))
6480
6481 (defun gnus-summary-limit-to-subject (subject &optional header)
6482   "Limit the summary buffer to articles that have subjects that match a regexp."
6483   (interactive "sLimit to subject (regexp): ")
6484   (unless header
6485     (setq header "subject"))
6486   (when (not (equal "" subject))
6487     (prog1
6488         (let ((articles (gnus-summary-find-matching
6489                          (or header "subject") subject 'all)))
6490           (unless articles
6491             (error "Found no matches for \"%s\"" subject))
6492           (gnus-summary-limit articles))
6493       (gnus-summary-position-point))))
6494
6495 (defun gnus-summary-limit-to-author (from)
6496   "Limit the summary buffer to articles that have authors that match a regexp."
6497   (interactive "sLimit to author (regexp): ")
6498   (gnus-summary-limit-to-subject from "from"))
6499
6500 (defun gnus-summary-limit-to-age (age &optional younger-p)
6501   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6502 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6503 articles that are younger than AGE days."
6504   (interactive
6505    (let ((younger current-prefix-arg)
6506          (days-got nil)
6507          days)
6508      (while (not days-got)
6509        (setq days (if younger
6510                       (read-string "Limit to articles within (in days): ")
6511                     (read-string "Limit to articles older than (in days): ")))
6512        (when (> (length days) 0)
6513          (setq days (read days)))
6514        (if (numberp days)
6515            (setq days-got t)
6516          (message "Please enter a number.")
6517          (sleep-for 1)))
6518      (list days younger)))
6519   (prog1
6520       (let ((data gnus-newsgroup-data)
6521             (cutoff (days-to-time age))
6522             articles d date is-younger)
6523         (while (setq d (pop data))
6524           (when (and (vectorp (gnus-data-header d))
6525                      (setq date (mail-header-date (gnus-data-header d))))
6526             (setq is-younger (time-less-p
6527                               (time-since (condition-case ()
6528                                               (date-to-time date)
6529                                             (error '(0 0))))
6530                               cutoff))
6531             (when (if younger-p
6532                       is-younger
6533                     (not is-younger))
6534               (push (gnus-data-number d) articles))))
6535         (gnus-summary-limit (nreverse articles)))
6536     (gnus-summary-position-point)))
6537
6538 (defun gnus-summary-limit-to-extra (header regexp)
6539   "Limit the summary buffer to articles that match an 'extra' header."
6540   (interactive
6541    (let ((header
6542           (intern
6543            (gnus-completing-read
6544             (symbol-name (car gnus-extra-headers))
6545             "Limit extra header:"
6546             (mapcar (lambda (x)
6547                       (cons (symbol-name x) x))
6548                     gnus-extra-headers)
6549             nil
6550             t))))
6551      (list header
6552            (read-string (format "Limit to header %s (regexp): " header)))))
6553   (when (not (equal "" regexp))
6554     (prog1
6555         (let ((articles (gnus-summary-find-matching
6556                          (cons 'extra header) regexp 'all)))
6557           (unless articles
6558             (error "Found no matches for \"%s\"" regexp))
6559           (gnus-summary-limit articles))
6560       (gnus-summary-position-point))))
6561
6562 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6563 (make-obsolete
6564  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6565
6566 (defun gnus-summary-limit-to-unread (&optional all)
6567   "Limit the summary buffer to articles that are not marked as read.
6568 If ALL is non-nil, limit strictly to unread articles."
6569   (interactive "P")
6570   (if all
6571       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6572     (gnus-summary-limit-to-marks
6573      ;; Concat all the marks that say that an article is read and have
6574      ;; those removed.
6575      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6576            gnus-killed-mark gnus-kill-file-mark
6577            gnus-low-score-mark gnus-expirable-mark
6578            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6579            gnus-duplicate-mark gnus-souped-mark)
6580      'reverse)))
6581
6582 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6583 (make-obsolete 'gnus-summary-delete-marked-with
6584                'gnus-summary-limit-exlude-marks)
6585
6586 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6587   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6588 If REVERSE, limit the summary buffer to articles that are marked
6589 with MARKS.  MARKS can either be a string of marks or a list of marks.
6590 Returns how many articles were removed."
6591   (interactive "sMarks: ")
6592   (gnus-summary-limit-to-marks marks t))
6593
6594 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6595   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6596 If REVERSE (the prefix), limit the summary buffer to articles that are
6597 not marked with MARKS.  MARKS can either be a string of marks or a
6598 list of marks.
6599 Returns how many articles were removed."
6600   (interactive "sMarks: \nP")
6601   (prog1
6602       (let ((data gnus-newsgroup-data)
6603             (marks (if (listp marks) marks
6604                      (append marks nil))) ; Transform to list.
6605             articles)
6606         (while data
6607           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6608                   (memq (gnus-data-mark (car data)) marks))
6609             (push (gnus-data-number (car data)) articles))
6610           (setq data (cdr data)))
6611         (gnus-summary-limit articles))
6612     (gnus-summary-position-point)))
6613
6614 (defun gnus-summary-limit-to-score (&optional score)
6615   "Limit to articles with score at or above SCORE."
6616   (interactive "P")
6617   (setq score (if score
6618                   (prefix-numeric-value score)
6619                 (or gnus-summary-default-score 0)))
6620   (let ((data gnus-newsgroup-data)
6621         articles)
6622     (while data
6623       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6624                 score)
6625         (push (gnus-data-number (car data)) articles))
6626       (setq data (cdr data)))
6627     (prog1
6628         (gnus-summary-limit articles)
6629       (gnus-summary-position-point))))
6630
6631 (defun gnus-summary-limit-include-thread (id)
6632   "Display all the hidden articles that in the current thread."
6633   (interactive (list (mail-header-id (gnus-summary-article-header))))
6634   (let ((articles (gnus-articles-in-thread
6635                    (gnus-id-to-thread (gnus-root-id id)))))
6636     (prog1
6637         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6638       (gnus-summary-position-point))))
6639
6640 (defun gnus-summary-limit-include-dormant ()
6641   "Display all the hidden articles that are marked as dormant.
6642 Note that this command only works on a subset of the articles currently
6643 fetched for this group."
6644   (interactive)
6645   (unless gnus-newsgroup-dormant
6646     (error "There are no dormant articles in this group"))
6647   (prog1
6648       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6649     (gnus-summary-position-point)))
6650
6651 (defun gnus-summary-limit-exclude-dormant ()
6652   "Hide all dormant articles."
6653   (interactive)
6654   (prog1
6655       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6656     (gnus-summary-position-point)))
6657
6658 (defun gnus-summary-limit-exclude-childless-dormant ()
6659   "Hide all dormant articles that have no children."
6660   (interactive)
6661   (let ((data (gnus-data-list t))
6662         articles d children)
6663     ;; Find all articles that are either not dormant or have
6664     ;; children.
6665     (while (setq d (pop data))
6666       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6667                 (and (setq children
6668                            (gnus-article-children (gnus-data-number d)))
6669                      (let (found)
6670                        (while children
6671                          (when (memq (car children) articles)
6672                            (setq children nil
6673                                  found t))
6674                          (pop children))
6675                        found)))
6676         (push (gnus-data-number d) articles)))
6677     ;; Do the limiting.
6678     (prog1
6679         (gnus-summary-limit articles)
6680       (gnus-summary-position-point))))
6681
6682 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6683   "Mark all unread excluded articles as read.
6684 If ALL, mark even excluded ticked and dormants as read."
6685   (interactive "P")
6686   (let ((articles (gnus-sorted-complement
6687                    (sort
6688                     (mapcar (lambda (h) (mail-header-number h))
6689                             gnus-newsgroup-headers)
6690                     '<)
6691                    (sort gnus-newsgroup-limit '<)))
6692         article)
6693     (setq gnus-newsgroup-unreads
6694           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6695     (if all
6696         (setq gnus-newsgroup-dormant nil
6697               gnus-newsgroup-marked nil
6698               gnus-newsgroup-reads
6699               (nconc
6700                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6701                gnus-newsgroup-reads))
6702       (while (setq article (pop articles))
6703         (unless (or (memq article gnus-newsgroup-dormant)
6704                     (memq article gnus-newsgroup-marked))
6705           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6706
6707 (defun gnus-summary-limit (articles &optional pop)
6708   (if pop
6709       ;; We pop the previous limit off the stack and use that.
6710       (setq articles (car gnus-newsgroup-limits)
6711             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6712     ;; We use the new limit, so we push the old limit on the stack.
6713     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6714   ;; Set the limit.
6715   (setq gnus-newsgroup-limit articles)
6716   (let ((total (length gnus-newsgroup-data))
6717         (data (gnus-data-find-list (gnus-summary-article-number)))
6718         (gnus-summary-mark-below nil)   ; Inhibit this.
6719         found)
6720     ;; This will do all the work of generating the new summary buffer
6721     ;; according to the new limit.
6722     (gnus-summary-prepare)
6723     ;; Hide any threads, possibly.
6724     (and gnus-show-threads
6725          gnus-thread-hide-subtree
6726          (gnus-summary-hide-all-threads))
6727     ;; Try to return to the article you were at, or one in the
6728     ;; neighborhood.
6729     (when data
6730       ;; We try to find some article after the current one.
6731       (while data
6732         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6733           (setq data nil
6734                 found t))
6735         (setq data (cdr data))))
6736     (unless found
6737       ;; If there is no data, that means that we were after the last
6738       ;; article.  The same goes when we can't find any articles
6739       ;; after the current one.
6740       (goto-char (point-max))
6741       (gnus-summary-find-prev))
6742     (gnus-set-mode-line 'summary)
6743     ;; We return how many articles were removed from the summary
6744     ;; buffer as a result of the new limit.
6745     (- total (length gnus-newsgroup-data))))
6746
6747 (defsubst gnus-invisible-cut-children (threads)
6748   (let ((num 0))
6749     (while threads
6750       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6751         (incf num))
6752       (pop threads))
6753     (< num 2)))
6754
6755 (defsubst gnus-cut-thread (thread)
6756   "Go forwards in the thread until we find an article that we want to display."
6757   (when (or (eq gnus-fetch-old-headers 'some)
6758             (eq gnus-fetch-old-headers 'invisible)
6759             (numberp gnus-fetch-old-headers)
6760             (eq gnus-build-sparse-threads 'some)
6761             (eq gnus-build-sparse-threads 'more))
6762     ;; Deal with old-fetched headers and sparse threads.
6763     (while (and
6764             thread
6765             (or
6766              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6767              (gnus-summary-article-ancient-p
6768               (mail-header-number (car thread))))
6769             (if (or (<= (length (cdr thread)) 1)
6770                     (eq gnus-fetch-old-headers 'invisible))
6771                 (setq gnus-newsgroup-limit
6772                       (delq (mail-header-number (car thread))
6773                             gnus-newsgroup-limit)
6774                       thread (cadr thread))
6775               (when (gnus-invisible-cut-children (cdr thread))
6776                 (let ((th (cdr thread)))
6777                   (while th
6778                     (if (memq (mail-header-number (caar th))
6779                               gnus-newsgroup-limit)
6780                         (setq thread (car th)
6781                               th nil)
6782                       (setq th (cdr th))))))))))
6783   thread)
6784
6785 (defun gnus-cut-threads (threads)
6786   "Cut off all uninteresting articles from the beginning of threads."
6787   (when (or (eq gnus-fetch-old-headers 'some)
6788             (eq gnus-fetch-old-headers 'invisible)
6789             (numberp gnus-fetch-old-headers)
6790             (eq gnus-build-sparse-threads 'some)
6791             (eq gnus-build-sparse-threads 'more))
6792     (let ((th threads))
6793       (while th
6794         (setcar th (gnus-cut-thread (car th)))
6795         (setq th (cdr th)))))
6796   ;; Remove nixed out threads.
6797   (delq nil threads))
6798
6799 (defun gnus-summary-initial-limit (&optional show-if-empty)
6800   "Figure out what the initial limit is supposed to be on group entry.
6801 This entails weeding out unwanted dormants, low-scored articles,
6802 fetch-old-headers verbiage, and so on."
6803   ;; Most groups have nothing to remove.
6804   (if (or gnus-inhibit-limiting
6805           (and (null gnus-newsgroup-dormant)
6806                (not (eq gnus-fetch-old-headers 'some))
6807                (not (numberp gnus-fetch-old-headers))
6808                (not (eq gnus-fetch-old-headers 'invisible))
6809                (null gnus-summary-expunge-below)
6810                (not (eq gnus-build-sparse-threads 'some))
6811                (not (eq gnus-build-sparse-threads 'more))
6812                (null gnus-thread-expunge-below)
6813                (not gnus-use-nocem)))
6814       ()                                ; Do nothing.
6815     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6816     (setq gnus-newsgroup-limit nil)
6817     (mapatoms
6818      (lambda (node)
6819        (unless (car (symbol-value node))
6820          ;; These threads have no parents -- they are roots.
6821          (let ((nodes (cdr (symbol-value node)))
6822                thread)
6823            (while nodes
6824              (if (and gnus-thread-expunge-below
6825                       (< (gnus-thread-total-score (car nodes))
6826                          gnus-thread-expunge-below))
6827                  (gnus-expunge-thread (pop nodes))
6828                (setq thread (pop nodes))
6829                (gnus-summary-limit-children thread))))))
6830      gnus-newsgroup-dependencies)
6831     ;; If this limitation resulted in an empty group, we might
6832     ;; pop the previous limit and use it instead.
6833     (when (and (not gnus-newsgroup-limit)
6834                show-if-empty)
6835       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6836     gnus-newsgroup-limit))
6837
6838 (defun gnus-summary-limit-children (thread)
6839   "Return 1 if this subthread is visible and 0 if it is not."
6840   ;; First we get the number of visible children to this thread.  This
6841   ;; is done by recursing down the thread using this function, so this
6842   ;; will really go down to a leaf article first, before slowly
6843   ;; working its way up towards the root.
6844   (when thread
6845     (let ((children
6846            (if (cdr thread)
6847                (apply '+ (mapcar 'gnus-summary-limit-children
6848                                  (cdr thread)))
6849              0))
6850           (number (mail-header-number (car thread)))
6851           score)
6852       (if (and
6853            (not (memq number gnus-newsgroup-marked))
6854            (or
6855             ;; If this article is dormant and has absolutely no visible
6856             ;; children, then this article isn't visible.
6857             (and (memq number gnus-newsgroup-dormant)
6858                  (zerop children))
6859             ;; If this is "fetch-old-headered" and there is no
6860             ;; visible children, then we don't want this article.
6861             (and (or (eq gnus-fetch-old-headers 'some)
6862                      (numberp gnus-fetch-old-headers))
6863                  (gnus-summary-article-ancient-p number)
6864                  (zerop children))
6865             ;; If this is "fetch-old-headered" and `invisible', then
6866             ;; we don't want this article.
6867             (and (eq gnus-fetch-old-headers 'invisible)
6868                  (gnus-summary-article-ancient-p number))
6869             ;; If this is a sparsely inserted article with no children,
6870             ;; we don't want it.
6871             (and (eq gnus-build-sparse-threads 'some)
6872                  (gnus-summary-article-sparse-p number)
6873                  (zerop children))
6874             ;; If we use expunging, and this article is really
6875             ;; low-scored, then we don't want this article.
6876             (when (and gnus-summary-expunge-below
6877                        (< (setq score
6878                                 (or (cdr (assq number gnus-newsgroup-scored))
6879                                     gnus-summary-default-score))
6880                           gnus-summary-expunge-below))
6881               ;; We increase the expunge-tally here, but that has
6882               ;; nothing to do with the limits, really.
6883               (incf gnus-newsgroup-expunged-tally)
6884               ;; We also mark as read here, if that's wanted.
6885               (when (and gnus-summary-mark-below
6886                          (< score gnus-summary-mark-below))
6887                 (setq gnus-newsgroup-unreads
6888                       (delq number gnus-newsgroup-unreads))
6889                 (if gnus-newsgroup-auto-expire
6890                     (push number gnus-newsgroup-expirable)
6891                   (push (cons number gnus-low-score-mark)
6892                         gnus-newsgroup-reads)))
6893               t)
6894             ;; Check NoCeM things.
6895             (if (and gnus-use-nocem
6896                      (gnus-nocem-unwanted-article-p
6897                       (mail-header-id (car thread))))
6898                 (progn
6899                   (setq gnus-newsgroup-unreads
6900                         (delq number gnus-newsgroup-unreads))
6901                   t))))
6902           ;; Nope, invisible article.
6903           0
6904         ;; Ok, this article is to be visible, so we add it to the limit
6905         ;; and return 1.
6906         (push number gnus-newsgroup-limit)
6907         1))))
6908
6909 (defun gnus-expunge-thread (thread)
6910   "Mark all articles in THREAD as read."
6911   (let* ((number (mail-header-number (car thread))))
6912     (incf gnus-newsgroup-expunged-tally)
6913     ;; We also mark as read here, if that's wanted.
6914     (setq gnus-newsgroup-unreads
6915           (delq number gnus-newsgroup-unreads))
6916     (if gnus-newsgroup-auto-expire
6917         (push number gnus-newsgroup-expirable)
6918       (push (cons number gnus-low-score-mark)
6919             gnus-newsgroup-reads)))
6920   ;; Go recursively through all subthreads.
6921   (mapcar 'gnus-expunge-thread (cdr thread)))
6922
6923 ;; Summary article oriented commands
6924
6925 (defun gnus-summary-refer-parent-article (n)
6926   "Refer parent article N times.
6927 If N is negative, go to ancestor -N instead.
6928 The difference between N and the number of articles fetched is returned."
6929   (interactive "p")
6930   (let ((skip 1)
6931         error header ref)
6932     (when (not (natnump n))
6933       (setq skip (abs n)
6934             n 1))
6935     (while (and (> n 0)
6936                 (not error))
6937       (setq header (gnus-summary-article-header))
6938       (if (and (eq (mail-header-number header)
6939                    (cdr gnus-article-current))
6940                (equal gnus-newsgroup-name
6941                       (car gnus-article-current)))
6942           ;; If we try to find the parent of the currently
6943           ;; displayed article, then we take a look at the actual
6944           ;; References header, since this is slightly more
6945           ;; reliable than the References field we got from the
6946           ;; server.
6947           (save-excursion
6948             (set-buffer gnus-original-article-buffer)
6949             (nnheader-narrow-to-headers)
6950             (unless (setq ref (message-fetch-field "references"))
6951               (setq ref (message-fetch-field "in-reply-to")))
6952             (widen))
6953         (setq ref
6954               ;; It's not the current article, so we take a bet on
6955               ;; the value we got from the server.
6956               (mail-header-references header)))
6957       (if (and ref
6958                (not (equal ref "")))
6959           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6960             (gnus-message 1 "Couldn't find parent"))
6961         (gnus-message 1 "No references in article %d"
6962                       (gnus-summary-article-number))
6963         (setq error t))
6964       (decf n))
6965     (gnus-summary-position-point)
6966     n))
6967
6968 (defun gnus-summary-refer-references ()
6969   "Fetch all articles mentioned in the References header.
6970 Return the number of articles fetched."
6971   (interactive)
6972   (let ((ref (mail-header-references (gnus-summary-article-header)))
6973         (current (gnus-summary-article-number))
6974         (n 0))
6975     (if (or (not ref)
6976             (equal ref ""))
6977         (error "No References in the current article")
6978       ;; For each Message-ID in the References header...
6979       (while (string-match "<[^>]*>" ref)
6980         (incf n)
6981         ;; ... fetch that article.
6982         (gnus-summary-refer-article
6983          (prog1 (match-string 0 ref)
6984            (setq ref (substring ref (match-end 0))))))
6985       (gnus-summary-goto-subject current)
6986       (gnus-summary-position-point)
6987       n)))
6988
6989 (defun gnus-summary-refer-thread (&optional limit)
6990   "Fetch all articles in the current thread.
6991 If LIMIT (the numerical prefix), fetch that many old headers instead
6992 of what's specified by the `gnus-refer-thread-limit' variable."
6993   (interactive "P")
6994   (let ((id (mail-header-id (gnus-summary-article-header)))
6995         (limit (if limit (prefix-numeric-value limit)
6996                  gnus-refer-thread-limit)))
6997     ;; We want to fetch LIMIT *old* headers, but we also have to
6998     ;; re-fetch all the headers in the current buffer, because many of
6999     ;; them may be undisplayed.  So we adjust LIMIT.
7000     (when (numberp limit)
7001       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7002     (unless (eq gnus-fetch-old-headers 'invisible)
7003       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7004       ;; Retrieve the headers and read them in.
7005       (if (eq (gnus-retrieve-headers
7006                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7007               'nov)
7008           (gnus-build-all-threads)
7009         (error "Can't fetch thread from backends that don't support NOV"))
7010       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7011     (gnus-summary-limit-include-thread id)))
7012
7013 (defun gnus-summary-refer-article (message-id)
7014   "Fetch an article specified by MESSAGE-ID."
7015   (interactive "sMessage-ID: ")
7016   (when (and (stringp message-id)
7017              (not (zerop (length message-id))))
7018     ;; Construct the correct Message-ID if necessary.
7019     ;; Suggested by tale@pawl.rpi.edu.
7020     (unless (string-match "^<" message-id)
7021       (setq message-id (concat "<" message-id)))
7022     (unless (string-match ">$" message-id)
7023       (setq message-id (concat message-id ">")))
7024     (let* ((header (gnus-id-to-header message-id))
7025            (sparse (and header
7026                         (gnus-summary-article-sparse-p
7027                          (mail-header-number header))
7028                         (memq (mail-header-number header)
7029                               gnus-newsgroup-limit)))
7030            number)
7031       (cond
7032        ;; If the article is present in the buffer we just go to it.
7033        ((and header
7034              (or (not (gnus-summary-article-sparse-p
7035                        (mail-header-number header)))
7036                  sparse))
7037         (prog1
7038             (gnus-summary-goto-article
7039              (mail-header-number header) nil t)
7040           (when sparse
7041             (gnus-summary-update-article (mail-header-number header)))))
7042        (t
7043         ;; We fetch the article.
7044         (catch 'found
7045           (dolist (gnus-override-method (gnus-refer-article-methods))
7046             (gnus-check-server gnus-override-method)
7047             ;; Fetch the header, and display the article.
7048             (when (setq number (gnus-summary-insert-subject message-id))
7049               (gnus-summary-select-article nil nil nil number)
7050               (throw 'found t)))
7051           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7052
7053 (defun gnus-refer-article-methods ()
7054   "Return a list of referrable methods."
7055   (cond
7056    ;; No method, so we default to current and native.
7057    ((null gnus-refer-article-method)
7058     (list gnus-current-select-method gnus-select-method))
7059    ;; Current.
7060    ((eq 'current gnus-refer-article-method)
7061     (list gnus-current-select-method))
7062    ;; List of select methods.
7063    ((not (and (symbolp (car gnus-refer-article-method))
7064               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7065     (let (out)
7066       (dolist (method gnus-refer-article-method)
7067         (push (if (eq 'current method)
7068                   gnus-current-select-method
7069                 method)
7070               out))
7071       (nreverse out)))
7072    ;; One single select method.
7073    (t
7074     (list gnus-refer-article-method))))
7075
7076 (defun gnus-summary-edit-parameters ()
7077   "Edit the group parameters of the current group."
7078   (interactive)
7079   (gnus-group-edit-group gnus-newsgroup-name 'params))
7080
7081 (defun gnus-summary-customize-parameters ()
7082   "Customize the group parameters of the current group."
7083   (interactive)
7084   (gnus-group-customize gnus-newsgroup-name))
7085
7086 (defun gnus-summary-enter-digest-group (&optional force)
7087   "Enter an nndoc group based on the current article.
7088 If FORCE, force a digest interpretation.  If not, try
7089 to guess what the document format is."
7090   (interactive "P")
7091   (let ((conf gnus-current-window-configuration))
7092     (save-excursion
7093       (gnus-summary-select-article))
7094     (setq gnus-current-window-configuration conf)
7095     (let* ((name (format "%s-%d"
7096                          (gnus-group-prefixed-name
7097                           gnus-newsgroup-name (list 'nndoc ""))
7098                          (save-excursion
7099                            (set-buffer gnus-summary-buffer)
7100                            gnus-current-article)))
7101            (ogroup gnus-newsgroup-name)
7102            (params (append (gnus-info-params (gnus-get-info ogroup))
7103                            (list (cons 'to-group ogroup))
7104                            (list (cons 'save-article-group ogroup))))
7105            (case-fold-search t)
7106            (buf (current-buffer))
7107            dig to-address)
7108       (save-excursion
7109         (set-buffer gnus-original-article-buffer)
7110         ;; Have the digest group inherit the main mail address of
7111         ;; the parent article.
7112         (when (setq to-address (or (message-fetch-field "reply-to")
7113                                    (message-fetch-field "from")))
7114           (setq params (append 
7115                         (list (cons 'to-address 
7116                                     (funcall gnus-decode-encoded-word-function
7117                                              to-address))))))
7118         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7119         (insert-buffer-substring gnus-original-article-buffer)
7120         ;; Remove lines that may lead nndoc to misinterpret the
7121         ;; document type.
7122         (narrow-to-region
7123          (goto-char (point-min))
7124          (or (search-forward "\n\n" nil t) (point)))
7125         (goto-char (point-min))
7126         (delete-matching-lines "^Path:\\|^From ")
7127         (widen))
7128       (unwind-protect
7129           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7130                     (gnus-newsgroup-ephemeral-ignored-charsets
7131                      gnus-newsgroup-ignored-charsets))
7132                 (gnus-group-read-ephemeral-group
7133                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7134                               (nndoc-article-type
7135                                ,(if force 'mbox 'guess))) t))
7136               ;; Make all postings to this group go to the parent group.
7137               (nconc (gnus-info-params (gnus-get-info name))
7138                      params)
7139             ;; Couldn't select this doc group.
7140             (switch-to-buffer buf)
7141             (gnus-set-global-variables)
7142             (gnus-configure-windows 'summary)
7143             (gnus-message 3 "Article couldn't be entered?"))
7144         (kill-buffer dig)))))
7145
7146 (defun gnus-summary-read-document (n)
7147   "Open a new group based on the current article(s).
7148 This will allow you to read digests and other similar
7149 documents as newsgroups.
7150 Obeys the standard process/prefix convention."
7151   (interactive "P")
7152   (let* ((articles (gnus-summary-work-articles n))
7153          (ogroup gnus-newsgroup-name)
7154          (params (append (gnus-info-params (gnus-get-info ogroup))
7155                          (list (cons 'to-group ogroup))))
7156          article group egroup groups vgroup)
7157     (while (setq article (pop articles))
7158       (setq group (format "%s-%d" gnus-newsgroup-name article))
7159       (gnus-summary-remove-process-mark article)
7160       (when (gnus-summary-display-article article)
7161         (save-excursion
7162           (with-temp-buffer
7163             (insert-buffer-substring gnus-original-article-buffer)
7164             ;; Remove some headers that may lead nndoc to make
7165             ;; the wrong guess.
7166             (message-narrow-to-head)
7167             (goto-char (point-min))
7168             (delete-matching-lines "^\\(Path\\):\\|^From ")
7169             (widen)
7170             (if (setq egroup
7171                       (gnus-group-read-ephemeral-group
7172                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7173                                      (nndoc-article-type guess))
7174                        t nil t))
7175                 (progn
7176                   ;; Make all postings to this group go to the parent group.
7177                   (nconc (gnus-info-params (gnus-get-info egroup))
7178                          params)
7179                   (push egroup groups))
7180               ;; Couldn't select this doc group.
7181               (gnus-error 3 "Article couldn't be entered"))))))
7182     ;; Now we have selected all the documents.
7183     (cond
7184      ((not groups)
7185       (error "None of the articles could be interpreted as documents"))
7186      ((gnus-group-read-ephemeral-group
7187        (setq vgroup (format
7188                      "nnvirtual:%s-%s" gnus-newsgroup-name
7189                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7190        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7191        t
7192        (cons (current-buffer) 'summary)))
7193      (t
7194       (error "Couldn't select virtual nndoc group")))))
7195
7196 (defun gnus-summary-isearch-article (&optional regexp-p)
7197   "Do incremental search forward on the current article.
7198 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7199   (interactive "P")
7200   (gnus-summary-select-article)
7201   (gnus-configure-windows 'article)
7202   (gnus-eval-in-buffer-window gnus-article-buffer
7203     (save-restriction
7204       (widen)
7205       (isearch-forward regexp-p))))
7206
7207 (defun gnus-summary-search-article-forward (regexp &optional backward)
7208   "Search for an article containing REGEXP forward.
7209 If BACKWARD, search backward instead."
7210   (interactive
7211    (list (read-string
7212           (format "Search article %s (regexp%s): "
7213                   (if current-prefix-arg "backward" "forward")
7214                   (if gnus-last-search-regexp
7215                       (concat ", default " gnus-last-search-regexp)
7216                     "")))
7217          current-prefix-arg))
7218   (if (string-equal regexp "")
7219       (setq regexp (or gnus-last-search-regexp ""))
7220     (setq gnus-last-search-regexp regexp)
7221     (setq gnus-article-before-search gnus-current-article))
7222   ;; Intentionally set gnus-last-article.
7223   (setq gnus-last-article gnus-article-before-search)
7224   (let ((gnus-last-article gnus-last-article))
7225     (if (gnus-summary-search-article regexp backward)
7226         (gnus-summary-show-thread)
7227       (error "Search failed: \"%s\"" regexp))))
7228
7229 (defun gnus-summary-search-article-backward (regexp)
7230   "Search for an article containing REGEXP backward."
7231   (interactive
7232    (list (read-string
7233           (format "Search article backward (regexp%s): "
7234                   (if gnus-last-search-regexp
7235                       (concat ", default " gnus-last-search-regexp)
7236                     "")))))
7237   (gnus-summary-search-article-forward regexp 'backward))
7238
7239 (defun gnus-summary-search-article (regexp &optional backward)
7240   "Search for an article containing REGEXP.
7241 Optional argument BACKWARD means do search for backward.
7242 `gnus-select-article-hook' is not called during the search."
7243   ;; We have to require this here to make sure that the following
7244   ;; dynamic binding isn't shadowed by autoloading.
7245   (require 'gnus-async)
7246   (require 'gnus-art)
7247   (let ((gnus-select-article-hook nil)  ;Disable hook.
7248         (gnus-article-prepare-hook nil)
7249         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7250         (gnus-use-article-prefetch nil)
7251         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7252         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7253         (sum (current-buffer))
7254         (gnus-display-mime-function nil)
7255         (found nil)
7256         point)
7257     (gnus-save-hidden-threads
7258       (gnus-summary-select-article)
7259       (set-buffer gnus-article-buffer)
7260       (goto-char (window-point (get-buffer-window (current-buffer))))
7261       (when backward
7262         (forward-line -1))
7263       (while (not found)
7264         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7265         (if (if backward
7266                 (re-search-backward regexp nil t)
7267               (re-search-forward regexp nil t))
7268             ;; We found the regexp.
7269             (progn
7270               (setq found 'found)
7271               (beginning-of-line)
7272               (set-window-start
7273                (get-buffer-window (current-buffer))
7274                (point))
7275               (forward-line 1)
7276               (set-window-point
7277                (get-buffer-window (current-buffer))
7278                (point))
7279               (set-buffer sum)
7280               (setq point (point)))
7281           ;; We didn't find it, so we go to the next article.
7282           (set-buffer sum)
7283           (setq found 'not)
7284           (while (eq found 'not)
7285             (if (not (if backward (gnus-summary-find-prev)
7286                        (gnus-summary-find-next)))
7287                 ;; No more articles.
7288                 (setq found t)
7289               ;; Select the next article and adjust point.
7290               (unless (gnus-summary-article-sparse-p
7291                        (gnus-summary-article-number))
7292                 (setq found nil)
7293                 (gnus-summary-select-article)
7294                 (set-buffer gnus-article-buffer)
7295                 (widen)
7296                 (goto-char (if backward (point-max) (point-min))))))))
7297       (gnus-message 7 ""))
7298     ;; Return whether we found the regexp.
7299     (when (eq found 'found)
7300       (goto-char point)
7301       (gnus-summary-show-thread)
7302       (gnus-summary-goto-subject gnus-current-article)
7303       (gnus-summary-position-point)
7304       t)))
7305
7306 (defun gnus-summary-find-matching (header regexp &optional backward unread
7307                                           not-case-fold)
7308   "Return a list of all articles that match REGEXP on HEADER.
7309 The search stars on the current article and goes forwards unless
7310 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7311 If UNREAD is non-nil, only unread articles will
7312 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7313 in the comparisons."
7314   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7315                 (gnus-data-find-list
7316                  (gnus-summary-article-number) (gnus-data-list backward))))
7317         (case-fold-search (not not-case-fold))
7318         articles d func)
7319     (if (consp header)
7320         (if (eq (car header) 'extra)
7321             (setq func
7322                   `(lambda (h)
7323                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7324                          "")))
7325           (error "%s is an invalid header" header))
7326       (unless (fboundp (intern (concat "mail-header-" header)))
7327         (error "%s is not a valid header" header))
7328       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7329     (while data
7330       (setq d (car data))
7331       (and (or (not unread)             ; We want all articles...
7332                (gnus-data-unread-p d))  ; Or just unreads.
7333            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7334            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7335            (push (gnus-data-number d) articles)) ; Success!
7336       (setq data (cdr data)))
7337     (nreverse articles)))
7338
7339 (defun gnus-summary-execute-command (header regexp command &optional backward)
7340   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7341 If HEADER is an empty string (or nil), the match is done on the entire
7342 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7343   (interactive
7344    (list (let ((completion-ignore-case t))
7345            (completing-read
7346             "Header name: "
7347             (mapcar (lambda (string) (list string))
7348                     '("Number" "Subject" "From" "Lines" "Date"
7349                       "Message-ID" "Xref" "References" "Body"))
7350             nil 'require-match))
7351          (read-string "Regexp: ")
7352          (read-key-sequence "Command: ")
7353          current-prefix-arg))
7354   (when (equal header "Body")
7355     (setq header ""))
7356   ;; Hidden thread subtrees must be searched as well.
7357   (gnus-summary-show-all-threads)
7358   ;; We don't want to change current point nor window configuration.
7359   (save-excursion
7360     (save-window-excursion
7361       (gnus-message 6 "Executing %s..." (key-description command))
7362       ;; We'd like to execute COMMAND interactively so as to give arguments.
7363       (gnus-execute header regexp
7364                     `(call-interactively ',(key-binding command))
7365                     backward)
7366       (gnus-message 6 "Executing %s...done" (key-description command)))))
7367
7368 (defun gnus-summary-beginning-of-article ()
7369   "Scroll the article back to the beginning."
7370   (interactive)
7371   (gnus-summary-select-article)
7372   (gnus-configure-windows 'article)
7373   (gnus-eval-in-buffer-window gnus-article-buffer
7374     (widen)
7375     (goto-char (point-min))
7376     (when gnus-page-broken
7377       (gnus-narrow-to-page))))
7378
7379 (defun gnus-summary-end-of-article ()
7380   "Scroll to the end of the article."
7381   (interactive)
7382   (gnus-summary-select-article)
7383   (gnus-configure-windows 'article)
7384   (gnus-eval-in-buffer-window gnus-article-buffer
7385     (widen)
7386     (goto-char (point-max))
7387     (recenter -3)
7388     (when gnus-page-broken
7389       (gnus-narrow-to-page))))
7390
7391 (defun gnus-summary-print-article (&optional filename n)
7392   "Generate and print a PostScript image of the N next (mail) articles.
7393
7394 If N is negative, print the N previous articles.  If N is nil and articles
7395 have been marked with the process mark, print these instead.
7396
7397 If the optional first argument FILENAME is nil, send the image to the
7398 printer.  If FILENAME is a string, save the PostScript image in a file with
7399 that name.  If FILENAME is a number, prompt the user for the name of the file
7400 to save in."
7401   (interactive (list (ps-print-preprint current-prefix-arg)
7402                      current-prefix-arg))
7403   (dolist (article (gnus-summary-work-articles n))
7404     (gnus-summary-select-article nil nil 'pseudo article)
7405     (gnus-eval-in-buffer-window gnus-article-buffer
7406       (let ((buffer (generate-new-buffer " *print*")))
7407         (unwind-protect
7408             (progn
7409               (copy-to-buffer buffer (point-min) (point-max))
7410               (set-buffer buffer)
7411               (gnus-article-delete-invisible-text)
7412               (let ((ps-left-header
7413                      (list
7414                       (concat "("
7415                               (mail-header-subject gnus-current-headers) ")")
7416                       (concat "("
7417                               (mail-header-from gnus-current-headers) ")")))
7418                     (ps-right-header
7419                      (list
7420                       "/pagenumberstring load"
7421                       (concat "("
7422                               (mail-header-date gnus-current-headers) ")"))))
7423                 (gnus-run-hooks 'gnus-ps-print-hook)
7424                 (save-excursion
7425                   (ps-print-buffer-with-faces filename))))
7426           (kill-buffer buffer))))))
7427
7428 (defun gnus-summary-show-article (&optional arg)
7429   "Force re-fetching of the current article.
7430 If ARG (the prefix) is a number, show the article with the charset
7431 defined in `gnus-summary-show-article-charset-alist', or the charset
7432 inputed.
7433 If ARG (the prefix) is non-nil and not a number, show the raw article
7434 without any article massaging functions being run."
7435   (interactive "P")
7436   (cond
7437    ((numberp arg)
7438     (let ((gnus-newsgroup-charset
7439            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7440                (read-coding-system "Charset: ")))
7441           (gnus-newsgroup-ignored-charsets 'gnus-all))
7442       (gnus-summary-select-article nil 'force)
7443       (let ((deps gnus-newsgroup-dependencies)
7444             head header)
7445         (save-excursion
7446           (set-buffer gnus-original-article-buffer)
7447           (save-restriction
7448             (message-narrow-to-head)
7449             (setq head (buffer-string)))
7450           (with-temp-buffer
7451             (insert (format "211 %d Article retrieved.\n"
7452                             (cdr gnus-article-current)))
7453             (insert head)
7454             (insert ".\n")
7455             (let ((nntp-server-buffer (current-buffer)))
7456               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7457         (gnus-data-set-header
7458          (gnus-data-find (cdr gnus-article-current))
7459          header)
7460         (gnus-summary-update-article-line
7461          (cdr gnus-article-current) header))))
7462    ((not arg)
7463     ;; Select the article the normal way.
7464     (gnus-summary-select-article nil 'force))
7465    (t
7466     ;; We have to require this here to make sure that the following
7467     ;; dynamic binding isn't shadowed by autoloading.
7468     (require 'gnus-async)
7469     (require 'gnus-art)
7470     ;; Bind the article treatment functions to nil.
7471     (let ((gnus-have-all-headers t)
7472           gnus-article-prepare-hook
7473           gnus-article-decode-hook
7474           gnus-display-mime-function
7475           gnus-break-pages)
7476       ;; Destroy any MIME parts.
7477       (when (gnus-buffer-live-p gnus-article-buffer)
7478         (save-excursion
7479           (set-buffer gnus-article-buffer)
7480           (mm-destroy-parts gnus-article-mime-handles)
7481           ;; Set it to nil for safety reason.
7482           (setq gnus-article-mime-handle-alist nil)
7483           (setq gnus-article-mime-handles nil)))
7484       (gnus-summary-select-article nil 'force))))
7485   (gnus-summary-goto-subject gnus-current-article)
7486   (gnus-summary-position-point))
7487
7488 (defun gnus-summary-verbose-headers (&optional arg)
7489   "Toggle permanent full header display.
7490 If ARG is a positive number, turn header display on.
7491 If ARG is a negative number, turn header display off."
7492   (interactive "P")
7493   (setq gnus-show-all-headers
7494         (cond ((or (not (numberp arg))
7495                    (zerop arg))
7496                (not gnus-show-all-headers))
7497               ((natnump arg)
7498                t)))
7499   (gnus-summary-show-article))
7500
7501 (defun gnus-summary-toggle-header (&optional arg)
7502   "Show the headers if they are hidden, or hide them if they are shown.
7503 If ARG is a positive number, show the entire header.
7504 If ARG is a negative number, hide the unwanted header lines."
7505   (interactive "P")
7506   (save-excursion
7507     (set-buffer gnus-article-buffer)
7508     (save-restriction
7509       (let* ((buffer-read-only nil)
7510              (inhibit-point-motion-hooks t)
7511              hidden e)
7512         (setq hidden
7513               (if (numberp arg)
7514                   (>= arg 0)
7515                 (save-restriction
7516                   (article-narrow-to-head)
7517                   (gnus-article-hidden-text-p 'headers))))
7518         (goto-char (point-min))
7519         (when (search-forward "\n\n" nil t)
7520           (delete-region (point-min) (1- (point))))
7521         (goto-char (point-min))
7522         (save-excursion
7523           (set-buffer gnus-original-article-buffer)
7524           (goto-char (point-min))
7525           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7526         (insert-buffer-substring gnus-original-article-buffer 1 e)
7527         (save-restriction
7528           (narrow-to-region (point-min) (point))
7529           (article-decode-encoded-words)
7530           (if  hidden
7531               (let ((gnus-treat-hide-headers nil)
7532                     (gnus-treat-hide-boring-headers nil))
7533                 (setq gnus-article-wash-types
7534                       (delq 'headers gnus-article-wash-types))
7535                 (gnus-treat-article 'head))
7536             (gnus-treat-article 'head)))
7537         (gnus-set-mode-line 'article)))))
7538
7539 (defun gnus-summary-show-all-headers ()
7540   "Make all header lines visible."
7541   (interactive)
7542   (gnus-article-show-all-headers))
7543
7544 (defun gnus-summary-caesar-message (&optional arg)
7545   "Caesar rotate the current article by 13.
7546 The numerical prefix specifies how many places to rotate each letter
7547 forward."
7548   (interactive "P")
7549   (gnus-summary-select-article)
7550   (let ((mail-header-separator ""))
7551     (gnus-eval-in-buffer-window gnus-article-buffer
7552       (save-restriction
7553         (widen)
7554         (let ((start (window-start))
7555               buffer-read-only)
7556           (message-caesar-buffer-body arg)
7557           (set-window-start (get-buffer-window (current-buffer)) start))))))
7558
7559 (defun gnus-summary-stop-page-breaking ()
7560   "Stop page breaking in the current article."
7561   (interactive)
7562   (gnus-summary-select-article)
7563   (gnus-eval-in-buffer-window gnus-article-buffer
7564     (widen)
7565     (when (gnus-visual-p 'page-marker)
7566       (let ((buffer-read-only nil))
7567         (gnus-remove-text-with-property 'gnus-prev)
7568         (gnus-remove-text-with-property 'gnus-next))
7569       (setq gnus-page-broken nil))))
7570
7571 (defun gnus-summary-move-article (&optional n to-newsgroup
7572                                             select-method action)
7573   "Move the current article to a different newsgroup.
7574 If N is a positive number, move the N next articles.
7575 If N is a negative number, move the N previous articles.
7576 If N is nil and any articles have been marked with the process mark,
7577 move those articles instead.
7578 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7579 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7580 re-spool using this method.
7581
7582 For this function to work, both the current newsgroup and the
7583 newsgroup that you want to move to have to support the `request-move'
7584 and `request-accept' functions.
7585
7586 ACTION can be either `move' (the default), `crosspost' or `copy'."
7587   (interactive "P")
7588   (unless action
7589     (setq action 'move))
7590   ;; Disable marking as read.
7591   (let (gnus-mark-article-hook)
7592     (save-window-excursion
7593       (gnus-summary-select-article)))
7594   ;; Check whether the source group supports the required functions.
7595   (cond ((and (eq action 'move)
7596               (not (gnus-check-backend-function
7597                     'request-move-article gnus-newsgroup-name)))
7598          (error "The current group does not support article moving"))
7599         ((and (eq action 'crosspost)
7600               (not (gnus-check-backend-function
7601                     'request-replace-article gnus-newsgroup-name)))
7602          (error "The current group does not support article editing")))
7603   (let ((articles (gnus-summary-work-articles n))
7604         (prefix (if (gnus-check-backend-function
7605                     'request-move-article gnus-newsgroup-name)
7606                     (gnus-group-real-prefix gnus-newsgroup-name)
7607                   ""))
7608         (names '((move "Move" "Moving")
7609                  (copy "Copy" "Copying")
7610                  (crosspost "Crosspost" "Crossposting")))
7611         (copy-buf (save-excursion
7612                     (nnheader-set-temp-buffer " *copy article*")))
7613         art-group to-method new-xref article to-groups)
7614     (unless (assq action names)
7615       (error "Unknown action %s" action))
7616     ;; Read the newsgroup name.
7617     (when (and (not to-newsgroup)
7618                (not select-method))
7619       (setq to-newsgroup
7620             (gnus-read-move-group-name
7621              (cadr (assq action names))
7622              (symbol-value (intern (format "gnus-current-%s-group" action)))
7623              articles prefix))
7624       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7625     (setq to-method (or select-method
7626                         (gnus-server-to-method
7627                          (gnus-group-method to-newsgroup))))
7628     ;; Check the method we are to move this article to...
7629     (unless (gnus-check-backend-function
7630              'request-accept-article (car to-method))
7631       (error "%s does not support article copying" (car to-method)))
7632     (unless (gnus-check-server to-method)
7633       (error "Can't open server %s" (car to-method)))
7634     (gnus-message 6 "%s to %s: %s..."
7635                   (caddr (assq action names))
7636                   (or (car select-method) to-newsgroup) articles)
7637     (while articles
7638       (setq article (pop articles))
7639       (setq
7640        art-group
7641        (cond
7642         ;; Move the article.
7643         ((eq action 'move)
7644          ;; Remove this article from future suppression.
7645          (gnus-dup-unsuppress-article article)
7646          (gnus-request-move-article
7647           article                       ; Article to move
7648           gnus-newsgroup-name           ; From newsgroup
7649           (nth 1 (gnus-find-method-for-group
7650                   gnus-newsgroup-name)) ; Server
7651           (list 'gnus-request-accept-article
7652                 to-newsgroup (list 'quote select-method)
7653                 (not articles) t)       ; Accept form
7654           (not articles)))              ; Only save nov last time
7655         ;; Copy the article.
7656         ((eq action 'copy)
7657          (save-excursion
7658            (set-buffer copy-buf)
7659            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7660              (gnus-request-accept-article
7661               to-newsgroup select-method (not articles) t))))
7662         ;; Crosspost the article.
7663         ((eq action 'crosspost)
7664          (let ((xref (message-tokenize-header
7665                       (mail-header-xref (gnus-summary-article-header article))
7666                       " ")))
7667            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7668                                   ":" article))
7669            (unless xref
7670              (setq xref (list (system-name))))
7671            (setq new-xref
7672                  (concat
7673                   (mapconcat 'identity
7674                              (delete "Xref:" (delete new-xref xref))
7675                              " ")
7676                   " " new-xref))
7677            (save-excursion
7678              (set-buffer copy-buf)
7679              ;; First put the article in the destination group.
7680              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7681              (when (consp (setq art-group
7682                                 (gnus-request-accept-article
7683                                  to-newsgroup select-method (not articles))))
7684                (setq new-xref (concat new-xref " " (car art-group)
7685                                       ":" (cdr art-group)))
7686                ;; Now we have the new Xrefs header, so we insert
7687                ;; it and replace the new article.
7688                (nnheader-replace-header "Xref" new-xref)
7689                (gnus-request-replace-article
7690                 (cdr art-group) to-newsgroup (current-buffer))
7691                art-group))))))
7692       (cond
7693        ((not art-group)
7694         (gnus-message 1 "Couldn't %s article %s: %s"
7695                       (cadr (assq action names)) article
7696                       (nnheader-get-report (car to-method))))
7697        ((eq art-group 'junk)
7698         (when (eq action 'move)
7699           (gnus-summary-mark-article article gnus-canceled-mark)
7700           (gnus-message 4 "Deleted article %s" article)))
7701        (t
7702         (let* ((pto-group (gnus-group-prefixed-name
7703                            (car art-group) to-method))
7704                (entry
7705                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7706                (info (nth 2 entry))
7707                (to-group (gnus-info-group info))
7708                to-marks)
7709           ;; Update the group that has been moved to.
7710           (when (and info
7711                      (memq action '(move copy)))
7712             (unless (member to-group to-groups)
7713               (push to-group to-groups))
7714
7715             (unless (memq article gnus-newsgroup-unreads)
7716               (push 'read to-marks)
7717               (gnus-info-set-read
7718                info (gnus-add-to-range (gnus-info-read info)
7719                                        (list (cdr art-group)))))
7720
7721             ;; See whether the article is to be put in the cache.
7722             (let ((marks gnus-article-mark-lists)
7723                   (to-article (cdr art-group)))
7724
7725               ;; Enter the article into the cache in the new group,
7726               ;; if that is required.
7727               (when gnus-use-cache
7728                 (gnus-cache-possibly-enter-article
7729                  to-group to-article
7730                  (memq article gnus-newsgroup-marked)
7731                  (memq article gnus-newsgroup-dormant)
7732                  (memq article gnus-newsgroup-unreads)))
7733
7734               (when gnus-preserve-marks
7735                 ;; Copy any marks over to the new group.
7736                 (when (and (equal to-group gnus-newsgroup-name)
7737                            (not (memq article gnus-newsgroup-unreads)))
7738                   ;; Mark this article as read in this group.
7739                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7740                   (setcdr (gnus-active to-group) to-article)
7741                   (setcdr gnus-newsgroup-active to-article))
7742
7743                 (while marks
7744                   (when (memq article (symbol-value
7745                                        (intern (format "gnus-newsgroup-%s"
7746                                                        (caar marks)))))
7747                     (push (cdar marks) to-marks)
7748                     ;; If the other group is the same as this group,
7749                     ;; then we have to add the mark to the list.
7750                     (when (equal to-group gnus-newsgroup-name)
7751                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7752                            (cons to-article
7753                                  (symbol-value
7754                                   (intern (format "gnus-newsgroup-%s"
7755                                                   (caar marks)))))))
7756                     ;; Copy the marks to other group.
7757                     (gnus-add-marked-articles
7758                      to-group (cdar marks) (list to-article) info))
7759                   (setq marks (cdr marks)))
7760
7761                 (gnus-request-set-mark to-group (list (list (list to-article)
7762                                                             'set
7763                                                             to-marks))))
7764
7765               (gnus-dribble-enter
7766                (concat "(gnus-group-set-info '"
7767                        (gnus-prin1-to-string (gnus-get-info to-group))
7768                        ")"))))
7769
7770           ;; Update the Xref header in this article to point to
7771           ;; the new crossposted article we have just created.
7772           (when (eq action 'crosspost)
7773             (save-excursion
7774               (set-buffer copy-buf)
7775               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7776               (nnheader-replace-header "Xref" new-xref)
7777               (gnus-request-replace-article
7778                article gnus-newsgroup-name (current-buffer)))))
7779
7780         ;;;!!!Why is this necessary?
7781         (set-buffer gnus-summary-buffer)
7782
7783         (gnus-summary-goto-subject article)
7784         (when (eq action 'move)
7785           (gnus-summary-mark-article article gnus-canceled-mark))))
7786       (gnus-summary-remove-process-mark article))
7787     ;; Re-activate all groups that have been moved to.
7788     (while to-groups
7789       (save-excursion
7790         (set-buffer gnus-group-buffer)
7791         (when (gnus-group-goto-group (car to-groups) t)
7792           (gnus-group-get-new-news-this-group 1 t))
7793         (pop to-groups)))
7794
7795     (gnus-kill-buffer copy-buf)
7796     (gnus-summary-position-point)
7797     (gnus-set-mode-line 'summary)))
7798
7799 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7800   "Move the current article to a different newsgroup.
7801 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7802 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7803 re-spool using this method."
7804   (interactive "P")
7805   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7806
7807 (defun gnus-summary-crosspost-article (&optional n)
7808   "Crosspost the current article to some other group."
7809   (interactive "P")
7810   (gnus-summary-move-article n nil nil 'crosspost))
7811
7812 (defcustom gnus-summary-respool-default-method nil
7813   "Default method for respooling an article.
7814 If nil, use to the current newsgroup method."
7815   :type '(choice (gnus-select-method :value (nnml ""))
7816                  (const nil))
7817   :group 'gnus-summary-mail)
7818
7819 (defun gnus-summary-respool-article (&optional n method)
7820   "Respool the current article.
7821 The article will be squeezed through the mail spooling process again,
7822 which means that it will be put in some mail newsgroup or other
7823 depending on `nnmail-split-methods'.
7824 If N is a positive number, respool the N next articles.
7825 If N is a negative number, respool the N previous articles.
7826 If N is nil and any articles have been marked with the process mark,
7827 respool those articles instead.
7828
7829 Respooling can be done both from mail groups and \"real\" newsgroups.
7830 In the former case, the articles in question will be moved from the
7831 current group into whatever groups they are destined to.  In the
7832 latter case, they will be copied into the relevant groups."
7833   (interactive
7834    (list current-prefix-arg
7835          (let* ((methods (gnus-methods-using 'respool))
7836                 (methname
7837                  (symbol-name (or gnus-summary-respool-default-method
7838                                   (car (gnus-find-method-for-group
7839                                         gnus-newsgroup-name)))))
7840                 (method
7841                  (gnus-completing-read
7842                   methname "What backend do you want to use when respooling?"
7843                   methods nil t nil 'gnus-mail-method-history))
7844                 ms)
7845            (cond
7846             ((zerop (length (setq ms (gnus-servers-using-backend
7847                                       (intern method)))))
7848              (list (intern method) ""))
7849             ((= 1 (length ms))
7850              (car ms))
7851             (t
7852              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7853                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7854                            ms-alist))))))))
7855   (unless method
7856     (error "No method given for respooling"))
7857   (if (assoc (symbol-name
7858               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7859              (gnus-methods-using 'respool))
7860       (gnus-summary-move-article n nil method)
7861     (gnus-summary-copy-article n nil method)))
7862
7863 (defun gnus-summary-import-article (file)
7864   "Import an arbitrary file into a mail newsgroup."
7865   (interactive "fImport file: ")
7866   (let ((group gnus-newsgroup-name)
7867         (now (current-time))
7868         atts lines)
7869     (unless (gnus-check-backend-function 'request-accept-article group)
7870       (error "%s does not support article importing" group))
7871     (or (file-readable-p file)
7872         (not (file-regular-p file))
7873         (error "Can't read %s" file))
7874     (save-excursion
7875       (set-buffer (gnus-get-buffer-create " *import file*"))
7876       (erase-buffer)
7877       (nnheader-insert-file-contents file)
7878       (goto-char (point-min))
7879       (unless (nnheader-article-p)
7880         ;; This doesn't look like an article, so we fudge some headers.
7881         (setq atts (file-attributes file)
7882               lines (count-lines (point-min) (point-max)))
7883         (insert "From: " (read-string "From: ") "\n"
7884                 "Subject: " (read-string "Subject: ") "\n"
7885                 "Date: " (message-make-date (nth 5 atts))
7886                 "\n"
7887                 "Message-ID: " (message-make-message-id) "\n"
7888                 "Lines: " (int-to-string lines) "\n"
7889                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7890       (gnus-request-accept-article group nil t)
7891       (kill-buffer (current-buffer)))))
7892
7893 (defun gnus-summary-article-posted-p ()
7894   "Say whether the current (mail) article is available from news as well.
7895 This will be the case if the article has both been mailed and posted."
7896   (interactive)
7897   (let ((id (mail-header-references (gnus-summary-article-header)))
7898         (gnus-override-method (car (gnus-refer-article-methods))))
7899     (if (gnus-request-head id "")
7900         (gnus-message 2 "The current message was found on %s"
7901                       gnus-override-method)
7902       (gnus-message 2 "The current message couldn't be found on %s"
7903                     gnus-override-method)
7904       nil)))
7905
7906 (defun gnus-summary-expire-articles (&optional now)
7907   "Expire all articles that are marked as expirable in the current group."
7908   (interactive)
7909   (when (gnus-check-backend-function
7910          'request-expire-articles gnus-newsgroup-name)
7911     ;; This backend supports expiry.
7912     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7913            (expirable (if total
7914                           (progn
7915                             ;; We need to update the info for
7916                             ;; this group for `gnus-list-of-read-articles'
7917                             ;; to give us the right answer.
7918                             (gnus-run-hooks 'gnus-exit-group-hook)
7919                             (gnus-summary-update-info)
7920                             (gnus-list-of-read-articles gnus-newsgroup-name))
7921                         (setq gnus-newsgroup-expirable
7922                               (sort gnus-newsgroup-expirable '<))))
7923            (expiry-wait (if now 'immediate
7924                           (gnus-group-find-parameter
7925                            gnus-newsgroup-name 'expiry-wait)))
7926            (nnmail-expiry-target
7927             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7928                 nnmail-expiry-target))
7929            es)
7930       (when expirable
7931         ;; There are expirable articles in this group, so we run them
7932         ;; through the expiry process.
7933         (gnus-message 6 "Expiring articles...")
7934         (unless (gnus-check-group gnus-newsgroup-name)
7935           (error "Can't open server for %s" gnus-newsgroup-name))
7936         ;; The list of articles that weren't expired is returned.
7937         (save-excursion
7938           (if expiry-wait
7939               (let ((nnmail-expiry-wait-function nil)
7940                     (nnmail-expiry-wait expiry-wait))
7941                 (setq es (gnus-request-expire-articles
7942                           expirable gnus-newsgroup-name)))
7943             (setq es (gnus-request-expire-articles
7944                       expirable gnus-newsgroup-name)))
7945           (unless total
7946             (setq gnus-newsgroup-expirable es))
7947           ;; We go through the old list of expirable, and mark all
7948           ;; really expired articles as nonexistent.
7949           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7950             (let ((gnus-use-cache nil))
7951               (while expirable
7952                 (unless (memq (car expirable) es)
7953                   (when (gnus-data-find (car expirable))
7954                     (gnus-summary-mark-article
7955                      (car expirable) gnus-canceled-mark)))
7956                 (setq expirable (cdr expirable))))))
7957         (gnus-message 6 "Expiring articles...done")))))
7958
7959 (defun gnus-summary-expire-articles-now ()
7960   "Expunge all expirable articles in the current group.
7961 This means that *all* articles that are marked as expirable will be
7962 deleted forever, right now."
7963   (interactive)
7964   (or gnus-expert-user
7965       (gnus-yes-or-no-p
7966        "Are you really, really, really sure you want to delete all these messages? ")
7967       (error "Phew!"))
7968   (gnus-summary-expire-articles t))
7969
7970 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7971 (defun gnus-summary-delete-article (&optional n)
7972   "Delete the N next (mail) articles.
7973 This command actually deletes articles.  This is not a marking
7974 command.  The article will disappear forever from your life, never to
7975 return.
7976 If N is negative, delete backwards.
7977 If N is nil and articles have been marked with the process mark,
7978 delete these instead."
7979   (interactive "P")
7980   (unless (gnus-check-backend-function 'request-expire-articles
7981                                        gnus-newsgroup-name)
7982     (error "The current newsgroup does not support article deletion"))
7983   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7984     (error "Couldn't open server"))
7985   ;; Compute the list of articles to delete.
7986   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7987         not-deleted)
7988     (if (and gnus-novice-user
7989              (not (gnus-yes-or-no-p
7990                    (format "Do you really want to delete %s forever? "
7991                            (if (> (length articles) 1)
7992                                (format "these %s articles" (length articles))
7993                              "this article")))))
7994         ()
7995       ;; Delete the articles.
7996       (setq not-deleted (gnus-request-expire-articles
7997                          articles gnus-newsgroup-name 'force))
7998       (while articles
7999         (gnus-summary-remove-process-mark (car articles))
8000         ;; The backend might not have been able to delete the article
8001         ;; after all.
8002         (unless (memq (car articles) not-deleted)
8003           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8004         (setq articles (cdr articles)))
8005       (when not-deleted
8006         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8007     (gnus-summary-position-point)
8008     (gnus-set-mode-line 'summary)
8009     not-deleted))
8010
8011 (defun gnus-summary-edit-article (&optional arg)
8012   "Edit the current article.
8013 This will have permanent effect only in mail groups.
8014 If ARG is nil, edit the decoded articles.
8015 If ARG is 1, edit the raw articles. 
8016 If ARG is 2, edit the raw articles even in read-only groups.
8017 If ARG is 3, edit the articles with the current handles.
8018 Otherwise, allow editing of articles even in read-only
8019 groups."
8020   (interactive "P")
8021   (let (force raw current-handles)
8022     (cond 
8023      ((null arg))
8024      ((eq arg 1) (setq raw t))
8025      ((eq arg 2) (setq raw t
8026                        force t))
8027      ((eq arg 3) (setq current-handles 
8028                        (and (gnus-buffer-live-p gnus-article-buffer)
8029                             (with-current-buffer gnus-article-buffer
8030                               (prog1
8031                                   gnus-article-mime-handles
8032                                   (setq gnus-article-mime-handles nil))))))
8033      (t (setq force t)))
8034     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8035         (error "Can't edit the raw article in group nndraft:drafts."))
8036     (save-excursion
8037       (set-buffer gnus-summary-buffer)
8038       (let ((mail-parse-charset gnus-newsgroup-charset)
8039             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8040         (gnus-set-global-variables)
8041         (when (and (not force)
8042                    (gnus-group-read-only-p))
8043           (error "The current newsgroup does not support article editing"))
8044         (gnus-summary-show-article t)
8045         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8046           (with-current-buffer gnus-article-buffer
8047             (mm-enable-multibyte-mule4)))
8048         (if (equal gnus-newsgroup-name "nndraft:drafts")
8049             (setq raw t))
8050         (gnus-article-edit-article
8051          (if raw 'ignore 
8052            `(lambda () 
8053               (let ((mbl mml-buffer-list))
8054                 (setq mml-buffer-list nil)
8055                 (mime-to-mml ,'current-handles)
8056                 (make-local-hook 'kill-buffer-hook)
8057                 (let ((mbl1 mml-buffer-list))
8058                   (setq mml-buffer-list mbl)
8059                   (set (make-local-variable 'mml-buffer-list) mbl1))
8060                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8061          `(lambda (no-highlight)
8062             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8063                   (message-options message-options)
8064                   (message-options-set-recipient)
8065                   (mail-parse-ignored-charsets 
8066                    ',gnus-newsgroup-ignored-charsets))
8067               ,(if (not raw) '(progn 
8068                                 (mml-to-mime)
8069                                 (mml-destroy-buffers)
8070                                 (remove-hook 'kill-buffer-hook 
8071                                              'mml-destroy-buffers t)
8072                                 (kill-local-variable 'mml-buffer-list)))
8073               (gnus-summary-edit-article-done
8074                ,(or (mail-header-references gnus-current-headers) "")
8075                ,(gnus-group-read-only-p) 
8076                ,gnus-summary-buffer no-highlight))))))))
8077
8078 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8079
8080 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8081                                                  no-highlight)
8082   "Make edits to the current article permanent."
8083   (interactive)
8084   (save-excursion
8085     ;; The buffer restriction contains the entire article if it exists.
8086     (when (article-goto-body)
8087       (let ((lines (count-lines (point) (point-max)))
8088             (length (- (point-max) (point)))
8089             (case-fold-search t)
8090             (body (copy-marker (point))))
8091         (goto-char (point-min))
8092         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8093           (delete-region (match-beginning 1) (match-end 1))
8094           (insert (number-to-string length)))
8095         (goto-char (point-min))
8096         (when (re-search-forward
8097                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8098           (delete-region (match-beginning 1) (match-end 1))
8099           (insert (number-to-string length)))
8100         (goto-char (point-min))
8101         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8102           (delete-region (match-beginning 1) (match-end 1))
8103           (insert (number-to-string lines))))))
8104   ;; Replace the article.
8105   (let ((buf (current-buffer)))
8106     (with-temp-buffer
8107       (insert-buffer-substring buf)
8108       
8109       (if (and (not read-only)
8110                (not (gnus-request-replace-article
8111                      (cdr gnus-article-current) (car gnus-article-current)
8112                      (current-buffer) t)))
8113           (error "Couldn't replace article")
8114         ;; Update the summary buffer.
8115         (if (and references
8116                  (equal (message-tokenize-header references " ")
8117                         (message-tokenize-header
8118                          (or (message-fetch-field "references") "") " ")))
8119             ;; We only have to update this line.
8120             (save-excursion
8121               (save-restriction
8122                 (message-narrow-to-head)
8123                 (let ((head (buffer-string))
8124                       header)
8125                   (with-temp-buffer
8126                     (insert (format "211 %d Article retrieved.\n"
8127                                     (cdr gnus-article-current)))
8128                     (insert head)
8129                     (insert ".\n")
8130                     (let ((nntp-server-buffer (current-buffer)))
8131                       (setq header (car (gnus-get-newsgroup-headers
8132                                          (save-excursion
8133                                            (set-buffer gnus-summary-buffer)
8134                                            gnus-newsgroup-dependencies)
8135                                          t))))
8136                     (save-excursion
8137                       (set-buffer gnus-summary-buffer)
8138                       (gnus-data-set-header
8139                        (gnus-data-find (cdr gnus-article-current))
8140                        header)
8141                       (gnus-summary-update-article-line
8142                        (cdr gnus-article-current) header))))))
8143           ;; Update threads.
8144           (set-buffer (or buffer gnus-summary-buffer))
8145           (gnus-summary-update-article (cdr gnus-article-current)))
8146         ;; Prettify the article buffer again.
8147         (unless no-highlight
8148           (save-excursion
8149             (set-buffer gnus-article-buffer)
8150             ;;;!!! Fix this -- article should be rehighlighted.
8151             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8152             (set-buffer gnus-original-article-buffer)
8153             (gnus-request-article
8154              (cdr gnus-article-current)
8155              (car gnus-article-current) (current-buffer))))
8156         ;; Prettify the summary buffer line.
8157         (when (gnus-visual-p 'summary-highlight 'highlight)
8158           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8159
8160 (defun gnus-summary-edit-wash (key)
8161   "Perform editing command KEY in the article buffer."
8162   (interactive
8163    (list
8164     (progn
8165       (message "%s" (concat (this-command-keys) "- "))
8166       (read-char))))
8167   (message "")
8168   (gnus-summary-edit-article)
8169   (execute-kbd-macro (concat (this-command-keys) key))
8170   (gnus-article-edit-done))
8171
8172 ;;; Respooling
8173
8174 (defun gnus-summary-respool-query (&optional silent trace)
8175   "Query where the respool algorithm would put this article."
8176   (interactive)
8177   (let (gnus-mark-article-hook)
8178     (gnus-summary-select-article)
8179     (save-excursion
8180       (set-buffer gnus-original-article-buffer)
8181       (save-restriction
8182         (message-narrow-to-head)
8183         (let ((groups (nnmail-article-group 'identity trace)))
8184           (unless silent
8185             (if groups
8186                 (message "This message would go to %s"
8187                          (mapconcat 'car groups ", "))
8188               (message "This message would go to no groups"))
8189             groups))))))
8190
8191 (defun gnus-summary-respool-trace ()
8192   "Trace where the respool algorithm would put this article.
8193 Display a buffer showing all fancy splitting patterns which matched."
8194   (interactive)
8195   (gnus-summary-respool-query nil t))
8196
8197 ;; Summary marking commands.
8198
8199 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8200   "Mark articles which has the same subject as read, and then select the next.
8201 If UNMARK is positive, remove any kind of mark.
8202 If UNMARK is negative, tick articles."
8203   (interactive "P")
8204   (when unmark
8205     (setq unmark (prefix-numeric-value unmark)))
8206   (let ((count
8207          (gnus-summary-mark-same-subject
8208           (gnus-summary-article-subject) unmark)))
8209     ;; Select next unread article.  If auto-select-same mode, should
8210     ;; select the first unread article.
8211     (gnus-summary-next-article t (and gnus-auto-select-same
8212                                       (gnus-summary-article-subject)))
8213     (gnus-message 7 "%d article%s marked as %s"
8214                   count (if (= count 1) " is" "s are")
8215                   (if unmark "unread" "read"))))
8216
8217 (defun gnus-summary-kill-same-subject (&optional unmark)
8218   "Mark articles which has the same subject as read.
8219 If UNMARK is positive, remove any kind of mark.
8220 If UNMARK is negative, tick articles."
8221   (interactive "P")
8222   (when unmark
8223     (setq unmark (prefix-numeric-value unmark)))
8224   (let ((count
8225          (gnus-summary-mark-same-subject
8226           (gnus-summary-article-subject) unmark)))
8227     ;; If marked as read, go to next unread subject.
8228     (when (null unmark)
8229       ;; Go to next unread subject.
8230       (gnus-summary-next-subject 1 t))
8231     (gnus-message 7 "%d articles are marked as %s"
8232                   count (if unmark "unread" "read"))))
8233
8234 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8235   "Mark articles with same SUBJECT as read, and return marked number.
8236 If optional argument UNMARK is positive, remove any kinds of marks.
8237 If optional argument UNMARK is negative, mark articles as unread instead."
8238   (let ((count 1))
8239     (save-excursion
8240       (cond
8241        ((null unmark)                   ; Mark as read.
8242         (while (and
8243                 (progn
8244                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8245                   (gnus-summary-show-thread) t)
8246                 (gnus-summary-find-subject subject))
8247           (setq count (1+ count))))
8248        ((> unmark 0)                    ; Tick.
8249         (while (and
8250                 (progn
8251                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8252                   (gnus-summary-show-thread) t)
8253                 (gnus-summary-find-subject subject))
8254           (setq count (1+ count))))
8255        (t                               ; Mark as unread.
8256         (while (and
8257                 (progn
8258                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8259                   (gnus-summary-show-thread) t)
8260                 (gnus-summary-find-subject subject))
8261           (setq count (1+ count)))))
8262       (gnus-set-mode-line 'summary)
8263       ;; Return the number of marked articles.
8264       count)))
8265
8266 (defun gnus-summary-mark-as-processable (n &optional unmark)
8267   "Set the process mark on the next N articles.
8268 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8269 the process mark instead.  The difference between N and the actual
8270 number of articles marked is returned."
8271   (interactive "p")
8272   (let ((backward (< n 0))
8273         (n (abs n)))
8274     (while (and
8275             (> n 0)
8276             (if unmark
8277                 (gnus-summary-remove-process-mark
8278                  (gnus-summary-article-number))
8279               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8280             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8281       (setq n (1- n)))
8282     (when (/= 0 n)
8283       (gnus-message 7 "No more articles"))
8284     (gnus-summary-recenter)
8285     (gnus-summary-position-point)
8286     n))
8287
8288 (defun gnus-summary-unmark-as-processable (n)
8289   "Remove the process mark from the next N articles.
8290 If N is negative, unmark backward instead.  The difference between N and
8291 the actual number of articles unmarked is returned."
8292   (interactive "p")
8293   (gnus-summary-mark-as-processable n t))
8294
8295 (defun gnus-summary-unmark-all-processable ()
8296   "Remove the process mark from all articles."
8297   (interactive)
8298   (save-excursion
8299     (while gnus-newsgroup-processable
8300       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8301   (gnus-summary-position-point))
8302
8303 (defun gnus-summary-add-mark (article type)
8304   "Mark ARTICLE with a mark of TYPE."
8305   (let ((vtype (car (assq type gnus-article-mark-lists)))
8306         var)
8307     (if (not vtype)
8308         (error "No such mark type: %s" type)
8309       (setq var (intern (format "gnus-newsgroup-%s" type)))
8310       (set var (cons article (symbol-value var)))
8311       (if (memq type '(processable cached replied saved))
8312           (gnus-summary-update-secondary-mark article)
8313         ;;; !!! This is bobus.  We should find out what primary
8314         ;;; !!! mark we want to set.
8315         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8316         
8317 (defun gnus-summary-mark-as-expirable (n)
8318   "Mark N articles forward as expirable.
8319 If N is negative, mark backward instead.  The difference between N and
8320 the actual number of articles marked is returned."
8321   (interactive "p")
8322   (gnus-summary-mark-forward n gnus-expirable-mark))
8323
8324 (defun gnus-summary-mark-article-as-replied (article)
8325   "Mark ARTICLE replied and update the summary line."
8326   (push article gnus-newsgroup-replied)
8327   (let ((buffer-read-only nil))
8328     (when (gnus-summary-goto-subject article nil t)
8329       (gnus-summary-update-secondary-mark article))))
8330
8331 (defun gnus-summary-set-bookmark (article)
8332   "Set a bookmark in current article."
8333   (interactive (list (gnus-summary-article-number)))
8334   (when (or (not (get-buffer gnus-article-buffer))
8335             (not gnus-current-article)
8336             (not gnus-article-current)
8337             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8338     (error "No current article selected"))
8339   ;; Remove old bookmark, if one exists.
8340   (let ((old (assq article gnus-newsgroup-bookmarks)))
8341     (when old
8342       (setq gnus-newsgroup-bookmarks
8343             (delq old gnus-newsgroup-bookmarks))))
8344   ;; Set the new bookmark, which is on the form
8345   ;; (article-number . line-number-in-body).
8346   (push
8347    (cons article
8348          (save-excursion
8349            (set-buffer gnus-article-buffer)
8350            (count-lines
8351             (min (point)
8352                  (save-excursion
8353                    (goto-char (point-min))
8354                    (search-forward "\n\n" nil t)
8355                    (point)))
8356             (point))))
8357    gnus-newsgroup-bookmarks)
8358   (gnus-message 6 "A bookmark has been added to the current article."))
8359
8360 (defun gnus-summary-remove-bookmark (article)
8361   "Remove the bookmark from the current article."
8362   (interactive (list (gnus-summary-article-number)))
8363   ;; Remove old bookmark, if one exists.
8364   (let ((old (assq article gnus-newsgroup-bookmarks)))
8365     (if old
8366         (progn
8367           (setq gnus-newsgroup-bookmarks
8368                 (delq old gnus-newsgroup-bookmarks))
8369           (gnus-message 6 "Removed bookmark."))
8370       (gnus-message 6 "No bookmark in current article."))))
8371
8372 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8373 (defun gnus-summary-mark-as-dormant (n)
8374   "Mark N articles forward as dormant.
8375 If N is negative, mark backward instead.  The difference between N and
8376 the actual number of articles marked is returned."
8377   (interactive "p")
8378   (gnus-summary-mark-forward n gnus-dormant-mark))
8379
8380 (defun gnus-summary-set-process-mark (article)
8381   "Set the process mark on ARTICLE and update the summary line."
8382   (setq gnus-newsgroup-processable
8383         (cons article
8384               (delq article gnus-newsgroup-processable)))
8385   (when (gnus-summary-goto-subject article)
8386     (gnus-summary-show-thread)
8387     (gnus-summary-goto-subject article)
8388     (gnus-summary-update-secondary-mark article)))
8389
8390 (defun gnus-summary-remove-process-mark (article)
8391   "Remove the process mark from ARTICLE and update the summary line."
8392   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8393   (when (gnus-summary-goto-subject article)
8394     (gnus-summary-show-thread)
8395     (gnus-summary-goto-subject article)
8396     (gnus-summary-update-secondary-mark article)))
8397
8398 (defun gnus-summary-set-saved-mark (article)
8399   "Set the process mark on ARTICLE and update the summary line."
8400   (push article gnus-newsgroup-saved)
8401   (when (gnus-summary-goto-subject article)
8402     (gnus-summary-update-secondary-mark article)))
8403
8404 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8405   "Mark N articles as read forwards.
8406 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8407 The difference between N and the actual number of articles marked is
8408 returned.
8409 Iff NO-EXPIRE, auto-expiry will be inhibited."
8410   (interactive "p")
8411   (gnus-summary-show-thread)
8412   (let ((backward (< n 0))
8413         (gnus-summary-goto-unread
8414          (and gnus-summary-goto-unread
8415               (not (eq gnus-summary-goto-unread 'never))
8416               (not (memq mark (list gnus-unread-mark
8417                                     gnus-ticked-mark gnus-dormant-mark)))))
8418         (n (abs n))
8419         (mark (or mark gnus-del-mark)))
8420     (while (and (> n 0)
8421                 (gnus-summary-mark-article nil mark no-expire)
8422                 (zerop (gnus-summary-next-subject
8423                         (if backward -1 1)
8424                         (and gnus-summary-goto-unread
8425                              (not (eq gnus-summary-goto-unread 'never)))
8426                         t)))
8427       (setq n (1- n)))
8428     (when (/= 0 n)
8429       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8430     (gnus-summary-recenter)
8431     (gnus-summary-position-point)
8432     (gnus-set-mode-line 'summary)
8433     n))
8434
8435 (defun gnus-summary-mark-article-as-read (mark)
8436   "Mark the current article quickly as read with MARK."
8437   (let ((article (gnus-summary-article-number)))
8438     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8439     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8440     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8441     (push (cons article mark) gnus-newsgroup-reads)
8442     ;; Possibly remove from cache, if that is used.
8443     (when gnus-use-cache
8444       (gnus-cache-enter-remove-article article))
8445     ;; Allow the backend to change the mark.
8446     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8447     ;; Check for auto-expiry.
8448     (when (and gnus-newsgroup-auto-expire
8449                (memq mark gnus-auto-expirable-marks))
8450       (setq mark gnus-expirable-mark)
8451       ;; Let the backend know about the mark change.
8452       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8453       (push article gnus-newsgroup-expirable))
8454     ;; Set the mark in the buffer.
8455     (gnus-summary-update-mark mark 'unread)
8456     t))
8457
8458 (defun gnus-summary-mark-article-as-unread (mark)
8459   "Mark the current article quickly as unread with MARK."
8460   (let* ((article (gnus-summary-article-number))
8461          (old-mark (gnus-summary-article-mark article)))
8462     ;; Allow the backend to change the mark.
8463     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8464     (if (eq mark old-mark)
8465         t
8466       (if (<= article 0)
8467           (progn
8468             (gnus-error 1 "Can't mark negative article numbers")
8469             nil)
8470         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8471         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8472         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8473         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8474         (cond ((= mark gnus-ticked-mark)
8475                (push article gnus-newsgroup-marked))
8476               ((= mark gnus-dormant-mark)
8477                (push article gnus-newsgroup-dormant))
8478               (t
8479                (push article gnus-newsgroup-unreads)))
8480         (gnus-pull article gnus-newsgroup-reads)
8481
8482         ;; See whether the article is to be put in the cache.
8483         (and gnus-use-cache
8484              (vectorp (gnus-summary-article-header article))
8485              (save-excursion
8486                (gnus-cache-possibly-enter-article
8487                 gnus-newsgroup-name article
8488                 (= mark gnus-ticked-mark)
8489                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8490
8491         ;; Fix the mark.
8492         (gnus-summary-update-mark mark 'unread)
8493         t))))
8494
8495 (defun gnus-summary-mark-article (&optional article mark no-expire)
8496   "Mark ARTICLE with MARK.  MARK can be any character.
8497 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8498 `??' (dormant) and `?E' (expirable).
8499 If MARK is nil, then the default character `?r' is used.
8500 If ARTICLE is nil, then the article on the current line will be
8501 marked.
8502 Iff NO-EXPIRE, auto-expiry will be inhibited."
8503   ;; The mark might be a string.
8504   (when (stringp mark)
8505     (setq mark (aref mark 0)))
8506   ;; If no mark is given, then we check auto-expiring.
8507   (when (null mark)
8508     (setq mark gnus-del-mark))
8509   (when (and (not no-expire)
8510              gnus-newsgroup-auto-expire
8511              (memq mark gnus-auto-expirable-marks))
8512     (setq mark gnus-expirable-mark))
8513   (let ((article (or article (gnus-summary-article-number)))
8514         (old-mark (gnus-summary-article-mark article)))
8515     ;; Allow the backend to change the mark.
8516     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8517     (if (eq mark old-mark)
8518         t
8519       (unless article
8520         (error "No article on current line"))
8521       (if (not (if (or (= mark gnus-unread-mark)
8522                        (= mark gnus-ticked-mark)
8523                        (= mark gnus-dormant-mark))
8524                    (gnus-mark-article-as-unread article mark)
8525                  (gnus-mark-article-as-read article mark)))
8526           t
8527         ;; See whether the article is to be put in the cache.
8528         (and gnus-use-cache
8529              (not (= mark gnus-canceled-mark))
8530              (vectorp (gnus-summary-article-header article))
8531              (save-excursion
8532                (gnus-cache-possibly-enter-article
8533                 gnus-newsgroup-name article
8534                 (= mark gnus-ticked-mark)
8535                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8536
8537         (when (gnus-summary-goto-subject article nil t)
8538           (let ((buffer-read-only nil))
8539             (gnus-summary-show-thread)
8540             ;; Fix the mark.
8541             (gnus-summary-update-mark mark 'unread)
8542             t))))))
8543
8544 (defun gnus-summary-update-secondary-mark (article)
8545   "Update the secondary (read, process, cache) mark."
8546   (gnus-summary-update-mark
8547    (cond ((memq article gnus-newsgroup-processable)
8548           gnus-process-mark)
8549          ((memq article gnus-newsgroup-cached)
8550           gnus-cached-mark)
8551          ((memq article gnus-newsgroup-replied)
8552           gnus-replied-mark)
8553          ((memq article gnus-newsgroup-saved)
8554           gnus-saved-mark)
8555          (t gnus-no-mark))
8556    'replied)
8557   (when (gnus-visual-p 'summary-highlight 'highlight)
8558     (gnus-run-hooks 'gnus-summary-update-hook))
8559   t)
8560
8561 (defun gnus-summary-update-mark (mark type)
8562   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8563         (buffer-read-only nil))
8564     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8565     (when forward
8566       (when (looking-at "\r")
8567         (incf forward))
8568       (when (<= (+ forward (point)) (point-max))
8569         ;; Go to the right position on the line.
8570         (goto-char (+ forward (point)))
8571         ;; Replace the old mark with the new mark.
8572         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8573         ;; Optionally update the marks by some user rule.
8574         (when (eq type 'unread)
8575           (gnus-data-set-mark
8576            (gnus-data-find (gnus-summary-article-number)) mark)
8577           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8578
8579 (defun gnus-mark-article-as-read (article &optional mark)
8580   "Enter ARTICLE in the pertinent lists and remove it from others."
8581   ;; Make the article expirable.
8582   (let ((mark (or mark gnus-del-mark)))
8583     (if (= mark gnus-expirable-mark)
8584         (push article gnus-newsgroup-expirable)
8585       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8586     ;; Remove from unread and marked lists.
8587     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8588     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8589     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8590     (push (cons article mark) gnus-newsgroup-reads)
8591     ;; Possibly remove from cache, if that is used.
8592     (when gnus-use-cache
8593       (gnus-cache-enter-remove-article article))
8594     t))
8595
8596 (defun gnus-mark-article-as-unread (article &optional mark)
8597   "Enter ARTICLE in the pertinent lists and remove it from others."
8598   (let ((mark (or mark gnus-ticked-mark)))
8599     (if (<= article 0)
8600         (progn
8601           (gnus-error 1 "Can't mark negative article numbers")
8602           nil)
8603       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8604             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8605             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8606             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8607
8608       ;; Unsuppress duplicates?
8609       (when gnus-suppress-duplicates
8610         (gnus-dup-unsuppress-article article))
8611
8612       (cond ((= mark gnus-ticked-mark)
8613              (push article gnus-newsgroup-marked))
8614             ((= mark gnus-dormant-mark)
8615              (push article gnus-newsgroup-dormant))
8616             (t
8617              (push article gnus-newsgroup-unreads)))
8618       (gnus-pull article gnus-newsgroup-reads)
8619       t)))
8620
8621 (defalias 'gnus-summary-mark-as-unread-forward
8622   'gnus-summary-tick-article-forward)
8623 (make-obsolete 'gnus-summary-mark-as-unread-forward
8624                'gnus-summary-tick-article-forward)
8625 (defun gnus-summary-tick-article-forward (n)
8626   "Tick N articles forwards.
8627 If N is negative, tick backwards instead.
8628 The difference between N and the number of articles ticked is returned."
8629   (interactive "p")
8630   (gnus-summary-mark-forward n gnus-ticked-mark))
8631
8632 (defalias 'gnus-summary-mark-as-unread-backward
8633   'gnus-summary-tick-article-backward)
8634 (make-obsolete 'gnus-summary-mark-as-unread-backward
8635                'gnus-summary-tick-article-backward)
8636 (defun gnus-summary-tick-article-backward (n)
8637   "Tick N articles backwards.
8638 The difference between N and the number of articles ticked is returned."
8639   (interactive "p")
8640   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8641
8642 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8643 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8644 (defun gnus-summary-tick-article (&optional article clear-mark)
8645   "Mark current article as unread.
8646 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8647 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8648   (interactive)
8649   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8650                                        gnus-ticked-mark)))
8651
8652 (defun gnus-summary-mark-as-read-forward (n)
8653   "Mark N articles as read forwards.
8654 If N is negative, mark backwards instead.
8655 The difference between N and the actual number of articles marked is
8656 returned."
8657   (interactive "p")
8658   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8659
8660 (defun gnus-summary-mark-as-read-backward (n)
8661   "Mark the N articles as read backwards.
8662 The difference between N and the actual number of articles marked is
8663 returned."
8664   (interactive "p")
8665   (gnus-summary-mark-forward
8666    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8667
8668 (defun gnus-summary-mark-as-read (&optional article mark)
8669   "Mark current article as read.
8670 ARTICLE specifies the article to be marked as read.
8671 MARK specifies a string to be inserted at the beginning of the line."
8672   (gnus-summary-mark-article article mark))
8673
8674 (defun gnus-summary-clear-mark-forward (n)
8675   "Clear marks from N articles forward.
8676 If N is negative, clear backward instead.
8677 The difference between N and the number of marks cleared is returned."
8678   (interactive "p")
8679   (gnus-summary-mark-forward n gnus-unread-mark))
8680
8681 (defun gnus-summary-clear-mark-backward (n)
8682   "Clear marks from N articles backward.
8683 The difference between N and the number of marks cleared is returned."
8684   (interactive "p")
8685   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8686
8687 (defun gnus-summary-mark-unread-as-read ()
8688   "Intended to be used by `gnus-summary-mark-article-hook'."
8689   (when (memq gnus-current-article gnus-newsgroup-unreads)
8690     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8691
8692 (defun gnus-summary-mark-read-and-unread-as-read ()
8693   "Intended to be used by `gnus-summary-mark-article-hook'."
8694   (let ((mark (gnus-summary-article-mark)))
8695     (when (or (gnus-unread-mark-p mark)
8696               (gnus-read-mark-p mark))
8697       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8698
8699 (defun gnus-summary-mark-unread-as-ticked ()
8700    "Intended to be used by `gnus-summary-mark-article-hook'."
8701   (when (memq gnus-current-article gnus-newsgroup-unreads)
8702     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8703
8704 (defun gnus-summary-mark-region-as-read (point mark all)
8705   "Mark all unread articles between point and mark as read.
8706 If given a prefix, mark all articles between point and mark as read,
8707 even ticked and dormant ones."
8708   (interactive "r\nP")
8709   (save-excursion
8710     (let (article)
8711       (goto-char point)
8712       (beginning-of-line)
8713       (while (and
8714               (< (point) mark)
8715               (progn
8716                 (when (or all
8717                           (memq (setq article (gnus-summary-article-number))
8718                                 gnus-newsgroup-unreads))
8719                   (gnus-summary-mark-article article gnus-del-mark))
8720                 t)
8721               (gnus-summary-find-next))))))
8722
8723 (defun gnus-summary-mark-below (score mark)
8724   "Mark articles with score less than SCORE with MARK."
8725   (interactive "P\ncMark: ")
8726   (setq score (if score
8727                   (prefix-numeric-value score)
8728                 (or gnus-summary-default-score 0)))
8729   (save-excursion
8730     (set-buffer gnus-summary-buffer)
8731     (goto-char (point-min))
8732     (while
8733         (progn
8734           (and (< (gnus-summary-article-score) score)
8735                (gnus-summary-mark-article nil mark))
8736           (gnus-summary-find-next)))))
8737
8738 (defun gnus-summary-kill-below (&optional score)
8739   "Mark articles with score below SCORE as read."
8740   (interactive "P")
8741   (gnus-summary-mark-below score gnus-killed-mark))
8742
8743 (defun gnus-summary-clear-above (&optional score)
8744   "Clear all marks from articles with score above SCORE."
8745   (interactive "P")
8746   (gnus-summary-mark-above score gnus-unread-mark))
8747
8748 (defun gnus-summary-tick-above (&optional score)
8749   "Tick all articles with score above SCORE."
8750   (interactive "P")
8751   (gnus-summary-mark-above score gnus-ticked-mark))
8752
8753 (defun gnus-summary-mark-above (score mark)
8754   "Mark articles with score over SCORE with MARK."
8755   (interactive "P\ncMark: ")
8756   (setq score (if score
8757                   (prefix-numeric-value score)
8758                 (or gnus-summary-default-score 0)))
8759   (save-excursion
8760     (set-buffer gnus-summary-buffer)
8761     (goto-char (point-min))
8762     (while (and (progn
8763                   (when (> (gnus-summary-article-score) score)
8764                     (gnus-summary-mark-article nil mark))
8765                   t)
8766                 (gnus-summary-find-next)))))
8767
8768 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8769 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8770 (defun gnus-summary-limit-include-expunged (&optional no-error)
8771   "Display all the hidden articles that were expunged for low scores."
8772   (interactive)
8773   (let ((buffer-read-only nil))
8774     (let ((scored gnus-newsgroup-scored)
8775           headers h)
8776       (while scored
8777         (unless (gnus-number-to-header (caar scored))
8778           (and (setq h (gnus-summary-article-header (caar scored)))
8779                (< (cdar scored) gnus-summary-expunge-below)
8780                (push h headers)))
8781         (setq scored (cdr scored)))
8782       (if (not headers)
8783           (when (not no-error)
8784             (error "No expunged articles hidden"))
8785         (goto-char (point-min))
8786         (gnus-summary-prepare-unthreaded (nreverse headers))
8787         (goto-char (point-min))
8788         (gnus-summary-position-point)
8789         t))))
8790
8791 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8792   "Mark all unread articles in this newsgroup as read.
8793 If prefix argument ALL is non-nil, ticked and dormant articles will
8794 also be marked as read.
8795 If QUIETLY is non-nil, no questions will be asked.
8796 If TO-HERE is non-nil, it should be a point in the buffer.  All
8797 articles before this point will be marked as read.
8798 Note that this function will only catch up the unread article
8799 in the current summary buffer limitation.
8800 The number of articles marked as read is returned."
8801   (interactive "P")
8802   (prog1
8803       (save-excursion
8804         (when (or quietly
8805                   (not gnus-interactive-catchup) ;Without confirmation?
8806                   gnus-expert-user
8807                   (gnus-y-or-n-p
8808                    (if all
8809                        "Mark absolutely all articles as read? "
8810                      "Mark all unread articles as read? ")))
8811           (if (and not-mark
8812                    (not gnus-newsgroup-adaptive)
8813                    (not gnus-newsgroup-auto-expire)
8814                    (not gnus-suppress-duplicates)
8815                    (or (not gnus-use-cache)
8816                        (eq gnus-use-cache 'passive)))
8817               (progn
8818                 (when all
8819                   (setq gnus-newsgroup-marked nil
8820                         gnus-newsgroup-dormant nil))
8821                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8822             ;; We actually mark all articles as canceled, which we
8823             ;; have to do when using auto-expiry or adaptive scoring.
8824             (gnus-summary-show-all-threads)
8825             (when (gnus-summary-first-subject (not all) t)
8826               (while (and
8827                       (if to-here (< (point) to-here) t)
8828                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8829                       (gnus-summary-find-next (not all) nil nil t))))
8830             (gnus-set-mode-line 'summary))
8831           t))
8832     (gnus-summary-position-point)))
8833
8834 (defun gnus-summary-catchup-to-here (&optional all)
8835   "Mark all unticked articles before the current one as read.
8836 If ALL is non-nil, also mark ticked and dormant articles as read."
8837   (interactive "P")
8838   (save-excursion
8839     (gnus-save-hidden-threads
8840       (let ((beg (point)))
8841         ;; We check that there are unread articles.
8842         (when (or all (gnus-summary-find-prev))
8843           (gnus-summary-catchup all t beg)))))
8844   (gnus-summary-position-point))
8845
8846 (defun gnus-summary-catchup-all (&optional quietly)
8847   "Mark all articles in this newsgroup as read."
8848   (interactive "P")
8849   (gnus-summary-catchup t quietly))
8850
8851 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8852   "Mark all unread articles in this group as read, then exit.
8853 If prefix argument ALL is non-nil, all articles are marked as read."
8854   (interactive "P")
8855   (when (gnus-summary-catchup all quietly nil 'fast)
8856     ;; Select next newsgroup or exit.
8857     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8858              (eq gnus-auto-select-next 'quietly))
8859         (gnus-summary-next-group nil)
8860       (gnus-summary-exit))))
8861
8862 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8863   "Mark all articles in this newsgroup as read, and then exit."
8864   (interactive "P")
8865   (gnus-summary-catchup-and-exit t quietly))
8866
8867 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8868   "Mark all articles in this group as read and select the next group.
8869 If given a prefix, mark all articles, unread as well as ticked, as
8870 read."
8871   (interactive "P")
8872   (save-excursion
8873     (gnus-summary-catchup all))
8874   (gnus-summary-next-group))
8875
8876 ;;;
8877 ;;; with article
8878 ;;;
8879
8880 (defmacro gnus-with-article (article &rest forms)
8881   "Select ARTICLE and perform FORMS in the original article buffer.
8882 Then replace the article with the result."
8883   `(progn
8884      ;; We don't want the article to be marked as read.
8885      (let (gnus-mark-article-hook)
8886        (gnus-summary-select-article t t nil ,article))
8887      (set-buffer gnus-original-article-buffer)
8888      ,@forms
8889      (if (not (gnus-check-backend-function
8890                'request-replace-article (car gnus-article-current)))
8891          (gnus-message 5 "Read-only group; not replacing")
8892        (unless (gnus-request-replace-article
8893                 ,article (car gnus-article-current)
8894                 (current-buffer) t)
8895          (error "Couldn't replace article")))
8896      ;; The cache and backlog have to be flushed somewhat.
8897      (when gnus-keep-backlog
8898        (gnus-backlog-remove-article
8899         (car gnus-article-current) (cdr gnus-article-current)))
8900      (when gnus-use-cache
8901        (gnus-cache-update-article
8902         (car gnus-article-current) (cdr gnus-article-current)))))
8903
8904 (put 'gnus-with-article 'lisp-indent-function 1)
8905 (put 'gnus-with-article 'edebug-form-spec '(form body))
8906
8907 ;; Thread-based commands.
8908
8909 (defun gnus-summary-articles-in-thread (&optional article)
8910   "Return a list of all articles in the current thread.
8911 If ARTICLE is non-nil, return all articles in the thread that starts
8912 with that article."
8913   (let* ((article (or article (gnus-summary-article-number)))
8914          (data (gnus-data-find-list article))
8915          (top-level (gnus-data-level (car data)))
8916          (top-subject
8917           (cond ((null gnus-thread-operation-ignore-subject)
8918                  (gnus-simplify-subject-re
8919                   (mail-header-subject (gnus-data-header (car data)))))
8920                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8921                  (gnus-simplify-subject-fuzzy
8922                   (mail-header-subject (gnus-data-header (car data)))))
8923                 (t nil)))
8924          (end-point (save-excursion
8925                       (if (gnus-summary-go-to-next-thread)
8926                           (point) (point-max))))
8927          articles)
8928     (while (and data
8929                 (< (gnus-data-pos (car data)) end-point))
8930       (when (or (not top-subject)
8931                 (string= top-subject
8932                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8933                              (gnus-simplify-subject-fuzzy
8934                               (mail-header-subject
8935                                (gnus-data-header (car data))))
8936                            (gnus-simplify-subject-re
8937                             (mail-header-subject
8938                              (gnus-data-header (car data)))))))
8939         (push (gnus-data-number (car data)) articles))
8940       (unless (and (setq data (cdr data))
8941                    (> (gnus-data-level (car data)) top-level))
8942         (setq data nil)))
8943     ;; Return the list of articles.
8944     (nreverse articles)))
8945
8946 (defun gnus-summary-rethread-current ()
8947   "Rethread the thread the current article is part of."
8948   (interactive)
8949   (let* ((gnus-show-threads t)
8950          (article (gnus-summary-article-number))
8951          (id (mail-header-id (gnus-summary-article-header)))
8952          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8953     (unless id
8954       (error "No article on the current line"))
8955     (gnus-rebuild-thread id)
8956     (gnus-summary-goto-subject article)))
8957
8958 (defun gnus-summary-reparent-thread ()
8959   "Make the current article child of the marked (or previous) article.
8960
8961 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8962 is non-nil or the Subject: of both articles are the same."
8963   (interactive)
8964   (unless (not (gnus-group-read-only-p))
8965     (error "The current newsgroup does not support article editing"))
8966   (unless (<= (length gnus-newsgroup-processable) 1)
8967     (error "No more than one article may be marked"))
8968   (save-window-excursion
8969     (let ((gnus-article-buffer " *reparent*")
8970           (current-article (gnus-summary-article-number))
8971           ;; First grab the marked article, otherwise one line up.
8972           (parent-article (if (not (null gnus-newsgroup-processable))
8973                               (car gnus-newsgroup-processable)
8974                             (save-excursion
8975                               (if (eq (forward-line -1) 0)
8976                                   (gnus-summary-article-number)
8977                                 (error "Beginning of summary buffer"))))))
8978       (unless (not (eq current-article parent-article))
8979         (error "An article may not be self-referential"))
8980       (let ((message-id (mail-header-id
8981                          (gnus-summary-article-header parent-article))))
8982         (unless (and message-id (not (equal message-id "")))
8983           (error "No message-id in desired parent"))
8984         (gnus-with-article current-article
8985           (save-restriction
8986             (goto-char (point-min))
8987             (message-narrow-to-head)
8988             (if (re-search-forward "^References: " nil t)
8989                 (progn
8990                   (re-search-forward "^[^ \t]" nil t)
8991                   (forward-line -1)
8992                   (end-of-line)
8993                   (insert " " message-id))
8994               (insert "References: " message-id "\n"))))
8995         (set-buffer gnus-summary-buffer)
8996         (gnus-summary-unmark-all-processable)
8997         (gnus-summary-update-article current-article)
8998         (gnus-summary-rethread-current)
8999         (gnus-message 3 "Article %d is now the child of article %d"
9000                       current-article parent-article)))))
9001
9002 (defun gnus-summary-toggle-threads (&optional arg)
9003   "Toggle showing conversation threads.
9004 If ARG is positive number, turn showing conversation threads on."
9005   (interactive "P")
9006   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9007     (setq gnus-show-threads
9008           (if (null arg) (not gnus-show-threads)
9009             (> (prefix-numeric-value arg) 0)))
9010     (gnus-summary-prepare)
9011     (gnus-summary-goto-subject current)
9012     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9013     (gnus-summary-position-point)))
9014
9015 (defun gnus-summary-show-all-threads ()
9016   "Show all threads."
9017   (interactive)
9018   (save-excursion
9019     (let ((buffer-read-only nil))
9020       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9021   (gnus-summary-position-point))
9022
9023 (defun gnus-summary-show-thread ()
9024   "Show thread subtrees.
9025 Returns nil if no thread was there to be shown."
9026   (interactive)
9027   (let ((buffer-read-only nil)
9028         (orig (point))
9029         ;; first goto end then to beg, to have point at beg after let
9030         (end (progn (end-of-line) (point)))
9031         (beg (progn (beginning-of-line) (point))))
9032     (prog1
9033         ;; Any hidden lines here?
9034         (search-forward "\r" end t)
9035       (subst-char-in-region beg end ?\^M ?\n t)
9036       (goto-char orig)
9037       (gnus-summary-position-point))))
9038
9039 (defun gnus-summary-hide-all-threads ()
9040   "Hide all thread subtrees."
9041   (interactive)
9042   (save-excursion
9043     (goto-char (point-min))
9044     (gnus-summary-hide-thread)
9045     (while (zerop (gnus-summary-next-thread 1 t))
9046       (gnus-summary-hide-thread)))
9047   (gnus-summary-position-point))
9048
9049 (defun gnus-summary-hide-thread ()
9050   "Hide thread subtrees.
9051 Returns nil if no threads were there to be hidden."
9052   (interactive)
9053   (let ((buffer-read-only nil)
9054         (start (point))
9055         (article (gnus-summary-article-number)))
9056     (goto-char start)
9057     ;; Go forward until either the buffer ends or the subthread
9058     ;; ends.
9059     (when (and (not (eobp))
9060                (or (zerop (gnus-summary-next-thread 1 t))
9061                    (goto-char (point-max))))
9062       (prog1
9063           (if (and (> (point) start)
9064                    (search-backward "\n" start t))
9065               (progn
9066                 (subst-char-in-region start (point) ?\n ?\^M)
9067                 (gnus-summary-goto-subject article))
9068             (goto-char start)
9069             nil)))))
9070
9071 (defun gnus-summary-go-to-next-thread (&optional previous)
9072   "Go to the same level (or less) next thread.
9073 If PREVIOUS is non-nil, go to previous thread instead.
9074 Return the article number moved to, or nil if moving was impossible."
9075   (let ((level (gnus-summary-thread-level))
9076         (way (if previous -1 1))
9077         (beg (point)))
9078     (forward-line way)
9079     (while (and (not (eobp))
9080                 (< level (gnus-summary-thread-level)))
9081       (forward-line way))
9082     (if (eobp)
9083         (progn
9084           (goto-char beg)
9085           nil)
9086       (setq beg (point))
9087       (prog1
9088           (gnus-summary-article-number)
9089         (goto-char beg)))))
9090
9091 (defun gnus-summary-next-thread (n &optional silent)
9092   "Go to the same level next N'th thread.
9093 If N is negative, search backward instead.
9094 Returns the difference between N and the number of skips actually
9095 done.
9096
9097 If SILENT, don't output messages."
9098   (interactive "p")
9099   (let ((backward (< n 0))
9100         (n (abs n)))
9101     (while (and (> n 0)
9102                 (gnus-summary-go-to-next-thread backward))
9103       (decf n))
9104     (unless silent
9105       (gnus-summary-position-point))
9106     (when (and (not silent) (/= 0 n))
9107       (gnus-message 7 "No more threads"))
9108     n))
9109
9110 (defun gnus-summary-prev-thread (n)
9111   "Go to the same level previous N'th thread.
9112 Returns the difference between N and the number of skips actually
9113 done."
9114   (interactive "p")
9115   (gnus-summary-next-thread (- n)))
9116
9117 (defun gnus-summary-go-down-thread ()
9118   "Go down one level in the current thread."
9119   (let ((children (gnus-summary-article-children)))
9120     (when children
9121       (gnus-summary-goto-subject (car children)))))
9122
9123 (defun gnus-summary-go-up-thread ()
9124   "Go up one level in the current thread."
9125   (let ((parent (gnus-summary-article-parent)))
9126     (when parent
9127       (gnus-summary-goto-subject parent))))
9128
9129 (defun gnus-summary-down-thread (n)
9130   "Go down thread N steps.
9131 If N is negative, go up instead.
9132 Returns the difference between N and how many steps down that were
9133 taken."
9134   (interactive "p")
9135   (let ((up (< n 0))
9136         (n (abs n)))
9137     (while (and (> n 0)
9138                 (if up (gnus-summary-go-up-thread)
9139                   (gnus-summary-go-down-thread)))
9140       (setq n (1- n)))
9141     (gnus-summary-position-point)
9142     (when (/= 0 n)
9143       (gnus-message 7 "Can't go further"))
9144     n))
9145
9146 (defun gnus-summary-up-thread (n)
9147   "Go up thread N steps.
9148 If N is negative, go up instead.
9149 Returns the difference between N and how many steps down that were
9150 taken."
9151   (interactive "p")
9152   (gnus-summary-down-thread (- n)))
9153
9154 (defun gnus-summary-top-thread ()
9155   "Go to the top of the thread."
9156   (interactive)
9157   (while (gnus-summary-go-up-thread))
9158   (gnus-summary-article-number))
9159
9160 (defun gnus-summary-kill-thread (&optional unmark)
9161   "Mark articles under current thread as read.
9162 If the prefix argument is positive, remove any kinds of marks.
9163 If the prefix argument is negative, tick articles instead."
9164   (interactive "P")
9165   (when unmark
9166     (setq unmark (prefix-numeric-value unmark)))
9167   (let ((articles (gnus-summary-articles-in-thread)))
9168     (save-excursion
9169       ;; Expand the thread.
9170       (gnus-summary-show-thread)
9171       ;; Mark all the articles.
9172       (while articles
9173         (gnus-summary-goto-subject (car articles))
9174         (cond ((null unmark)
9175                (gnus-summary-mark-article-as-read gnus-killed-mark))
9176               ((> unmark 0)
9177                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9178               (t
9179                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9180         (setq articles (cdr articles))))
9181     ;; Hide killed subtrees.
9182     (and (null unmark)
9183          gnus-thread-hide-killed
9184          (gnus-summary-hide-thread))
9185     ;; If marked as read, go to next unread subject.
9186     (when (null unmark)
9187       ;; Go to next unread subject.
9188       (gnus-summary-next-subject 1 t)))
9189   (gnus-set-mode-line 'summary))
9190
9191 ;; Summary sorting commands
9192
9193 (defun gnus-summary-sort-by-number (&optional reverse)
9194   "Sort the summary buffer by article number.
9195 Argument REVERSE means reverse order."
9196   (interactive "P")
9197   (gnus-summary-sort 'number reverse))
9198
9199 (defun gnus-summary-sort-by-author (&optional reverse)
9200   "Sort the summary buffer by author name alphabetically.
9201 If `case-fold-search' is non-nil, case of letters is ignored.
9202 Argument REVERSE means reverse order."
9203   (interactive "P")
9204   (gnus-summary-sort 'author reverse))
9205
9206 (defun gnus-summary-sort-by-subject (&optional reverse)
9207   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9208 If `case-fold-search' is non-nil, case of letters is ignored.
9209 Argument REVERSE means reverse order."
9210   (interactive "P")
9211   (gnus-summary-sort 'subject reverse))
9212
9213 (defun gnus-summary-sort-by-date (&optional reverse)
9214   "Sort the summary buffer by date.
9215 Argument REVERSE means reverse order."
9216   (interactive "P")
9217   (gnus-summary-sort 'date reverse))
9218
9219 (defun gnus-summary-sort-by-score (&optional reverse)
9220   "Sort the summary buffer by score.
9221 Argument REVERSE means reverse order."
9222   (interactive "P")
9223   (gnus-summary-sort 'score reverse))
9224
9225 (defun gnus-summary-sort-by-lines (&optional reverse)
9226   "Sort the summary buffer by the number of lines.
9227 Argument REVERSE means reverse order."
9228   (interactive "P")
9229   (gnus-summary-sort 'lines reverse))
9230
9231 (defun gnus-summary-sort-by-chars (&optional reverse)
9232   "Sort the summary buffer by article length.
9233 Argument REVERSE means reverse order."
9234   (interactive "P")
9235   (gnus-summary-sort 'chars reverse))
9236
9237 (defun gnus-summary-sort (predicate reverse)
9238   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9239   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9240          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9241          (gnus-thread-sort-functions
9242           (if (not reverse)
9243               thread
9244             `(lambda (t1 t2)
9245                (,thread t2 t1))))
9246          (gnus-sort-gathered-threads-function
9247           gnus-thread-sort-functions)
9248          (gnus-article-sort-functions
9249           (if (not reverse)
9250               article
9251             `(lambda (t1 t2)
9252                (,article t2 t1))))
9253          (buffer-read-only)
9254          (gnus-summary-prepare-hook nil))
9255     ;; We do the sorting by regenerating the threads.
9256     (gnus-summary-prepare)
9257     ;; Hide subthreads if needed.
9258     (when (and gnus-show-threads gnus-thread-hide-subtree)
9259       (gnus-summary-hide-all-threads))))
9260
9261 ;; Summary saving commands.
9262
9263 (defun gnus-summary-save-article (&optional n not-saved)
9264   "Save the current article using the default saver function.
9265 If N is a positive number, save the N next articles.
9266 If N is a negative number, save the N previous articles.
9267 If N is nil and any articles have been marked with the process mark,
9268 save those articles instead.
9269 The variable `gnus-default-article-saver' specifies the saver function."
9270   (interactive "P")
9271   (let* ((articles (gnus-summary-work-articles n))
9272          (save-buffer (save-excursion
9273                         (nnheader-set-temp-buffer " *Gnus Save*")))
9274          (num (length articles))
9275          header file)
9276     (dolist (article articles)
9277       (setq header (gnus-summary-article-header article))
9278       (if (not (vectorp header))
9279           ;; This is a pseudo-article.
9280           (if (assq 'name header)
9281               (gnus-copy-file (cdr (assq 'name header)))
9282             (gnus-message 1 "Article %d is unsaveable" article))
9283         ;; This is a real article.
9284         (save-window-excursion
9285           (gnus-summary-select-article t nil nil article))
9286         (save-excursion
9287           (set-buffer save-buffer)
9288           (erase-buffer)
9289           (insert-buffer-substring gnus-original-article-buffer))
9290         (setq file (gnus-article-save save-buffer file num))
9291         (gnus-summary-remove-process-mark article)
9292         (unless not-saved
9293           (gnus-summary-set-saved-mark article))))
9294     (gnus-kill-buffer save-buffer)
9295     (gnus-summary-position-point)
9296     (gnus-set-mode-line 'summary)
9297     n))
9298
9299 (defun gnus-summary-pipe-output (&optional arg)
9300   "Pipe the current article to a subprocess.
9301 If N is a positive number, pipe the N next articles.
9302 If N is a negative number, pipe the N previous articles.
9303 If N is nil and any articles have been marked with the process mark,
9304 pipe those articles instead."
9305   (interactive "P")
9306   (require 'gnus-art)
9307   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9308     (gnus-summary-save-article arg t))
9309   (gnus-configure-windows 'pipe))
9310
9311 (defun gnus-summary-save-article-mail (&optional arg)
9312   "Append the current article to an mail file.
9313 If N is a positive number, save the N next articles.
9314 If N is a negative number, save the N previous articles.
9315 If N is nil and any articles have been marked with the process mark,
9316 save those articles instead."
9317   (interactive "P")
9318   (require 'gnus-art)
9319   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9320     (gnus-summary-save-article arg)))
9321
9322 (defun gnus-summary-save-article-rmail (&optional arg)
9323   "Append the current article to an rmail file.
9324 If N is a positive number, save the N next articles.
9325 If N is a negative number, save the N previous articles.
9326 If N is nil and any articles have been marked with the process mark,
9327 save those articles instead."
9328   (interactive "P")
9329   (require 'gnus-art)
9330   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9331     (gnus-summary-save-article arg)))
9332
9333 (defun gnus-summary-save-article-file (&optional arg)
9334   "Append the current article to a file.
9335 If N is a positive number, save the N next articles.
9336 If N is a negative number, save the N previous articles.
9337 If N is nil and any articles have been marked with the process mark,
9338 save those articles instead."
9339   (interactive "P")
9340   (require 'gnus-art)
9341   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9342     (gnus-summary-save-article arg)))
9343
9344 (defun gnus-summary-write-article-file (&optional arg)
9345   "Write the current article to a file, deleting the previous file.
9346 If N is a positive number, save the N next articles.
9347 If N is a negative number, save the N previous articles.
9348 If N is nil and any articles have been marked with the process mark,
9349 save those articles instead."
9350   (interactive "P")
9351   (require 'gnus-art)
9352   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9353     (gnus-summary-save-article arg)))
9354
9355 (defun gnus-summary-save-article-body-file (&optional arg)
9356   "Append the current article body to a file.
9357 If N is a positive number, save the N next articles.
9358 If N is a negative number, save the N previous articles.
9359 If N is nil and any articles have been marked with the process mark,
9360 save those articles instead."
9361   (interactive "P")
9362   (require 'gnus-art)
9363   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9364     (gnus-summary-save-article arg)))
9365
9366 (defun gnus-summary-pipe-message (program)
9367   "Pipe the current article through PROGRAM."
9368   (interactive "sProgram: ")
9369   (gnus-summary-select-article)
9370   (let ((mail-header-separator ""))
9371     (gnus-eval-in-buffer-window gnus-article-buffer
9372       (save-restriction
9373         (widen)
9374         (let ((start (window-start))
9375               buffer-read-only)
9376           (message-pipe-buffer-body program)
9377           (set-window-start (get-buffer-window (current-buffer)) start))))))
9378
9379 (defun gnus-get-split-value (methods)
9380   "Return a value based on the split METHODS."
9381   (let (split-name method result match)
9382     (when methods
9383       (save-excursion
9384         (set-buffer gnus-original-article-buffer)
9385         (save-restriction
9386           (nnheader-narrow-to-headers)
9387           (while (and methods (not split-name))
9388             (goto-char (point-min))
9389             (setq method (pop methods))
9390             (setq match (car method))
9391             (when (cond
9392                    ((stringp match)
9393                     ;; Regular expression.
9394                     (ignore-errors
9395                       (re-search-forward match nil t)))
9396                    ((gnus-functionp match)
9397                     ;; Function.
9398                     (save-restriction
9399                       (widen)
9400                       (setq result (funcall match gnus-newsgroup-name))))
9401                    ((consp match)
9402                     ;; Form.
9403                     (save-restriction
9404                       (widen)
9405                       (setq result (eval match)))))
9406               (setq split-name (cdr method))
9407               (cond ((stringp result)
9408                      (push (expand-file-name
9409                             result gnus-article-save-directory)
9410                            split-name))
9411                     ((consp result)
9412                      (setq split-name (append result split-name)))))))))
9413     (nreverse split-name)))
9414
9415 (defun gnus-valid-move-group-p (group)
9416   (and (boundp group)
9417        (symbol-name group)
9418        (symbol-value group)
9419        (gnus-get-function (gnus-find-method-for-group
9420                            (symbol-name group)) 'request-accept-article t)))
9421
9422 (defun gnus-read-move-group-name (prompt default articles prefix)
9423   "Read a group name."
9424   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9425          (minibuffer-confirm-incomplete nil) ; XEmacs
9426          (prom
9427           (format "%s %s to:"
9428                   prompt
9429                   (if (> (length articles) 1)
9430                       (format "these %d articles" (length articles))
9431                     "this article")))
9432          (to-newsgroup
9433           (cond
9434            ((null split-name)
9435             (gnus-completing-read default prom
9436                                   gnus-active-hashtb
9437                                   'gnus-valid-move-group-p
9438                                   nil prefix
9439                                   'gnus-group-history))
9440            ((= 1 (length split-name))
9441             (gnus-completing-read (car split-name) prom
9442                                   gnus-active-hashtb
9443                                   'gnus-valid-move-group-p
9444                                   nil nil
9445                                   'gnus-group-history))
9446            (t
9447             (gnus-completing-read nil prom
9448                                   (mapcar (lambda (el) (list el))
9449                                           (nreverse split-name))
9450                                   nil nil nil
9451                                   'gnus-group-history))))
9452          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9453     (when to-newsgroup
9454       (if (or (string= to-newsgroup "")
9455               (string= to-newsgroup prefix))
9456           (setq to-newsgroup default))
9457       (unless to-newsgroup
9458         (error "No group name entered"))
9459       (or (gnus-active to-newsgroup)
9460           (gnus-activate-group to-newsgroup nil nil to-method)
9461           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9462                                      to-newsgroup))
9463               (or (and (gnus-request-create-group to-newsgroup to-method)
9464                        (gnus-activate-group
9465                         to-newsgroup nil nil to-method)
9466                        (gnus-subscribe-group to-newsgroup))
9467                   (error "Couldn't create group %s" to-newsgroup)))
9468           (error "No such group: %s" to-newsgroup)))
9469     to-newsgroup))
9470
9471 (defun gnus-summary-save-parts (type dir n &optional reverse)
9472   "Save parts matching TYPE to DIR.
9473 If REVERSE, save parts that do not match TYPE."
9474   (interactive
9475    (list (read-string "Save parts of type: " 
9476                       (or (car gnus-summary-save-parts-type-history)
9477                           gnus-summary-save-parts-default-mime)
9478                       'gnus-summary-save-parts-type-history)
9479          (setq gnus-summary-save-parts-last-directory
9480                (read-file-name "Save to directory: " 
9481                                gnus-summary-save-parts-last-directory
9482                                nil t))
9483          current-prefix-arg))
9484   (gnus-summary-iterate n
9485     (let ((gnus-display-mime-function nil)
9486           (gnus-inhibit-treatment t))
9487       (gnus-summary-select-article))
9488     (save-excursion
9489       (set-buffer gnus-article-buffer)
9490       (let ((handles (or gnus-article-mime-handles
9491                          (mm-dissect-buffer) (mm-uu-dissect))))
9492         (when handles
9493           (gnus-summary-save-parts-1 type dir handles reverse)
9494           (unless gnus-article-mime-handles ;; Don't destroy this case.
9495             (mm-destroy-parts handles)))))))
9496
9497 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9498   (if (stringp (car handle))
9499       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9500               (cdr handle))
9501     (when (if reverse
9502               (not (string-match type (mm-handle-media-type handle)))
9503             (string-match type (mm-handle-media-type handle)))
9504       (let ((file (expand-file-name
9505                    (file-name-nondirectory
9506                     (or
9507                      (mail-content-type-get
9508                       (mm-handle-disposition handle) 'filename)
9509                      (concat gnus-newsgroup-name
9510                              "." (number-to-string
9511                                   (cdr gnus-article-current)))))
9512                    dir)))
9513         (unless (file-exists-p file)
9514           (mm-save-part-to-file handle file))))))
9515
9516 ;; Summary extract commands
9517
9518 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9519   (let ((buffer-read-only nil)
9520         (article (gnus-summary-article-number))
9521         after-article b e)
9522     (unless (gnus-summary-goto-subject article)
9523       (error "No such article: %d" article))
9524     (gnus-summary-position-point)
9525     ;; If all commands are to be bunched up on one line, we collect
9526     ;; them here.
9527     (unless gnus-view-pseudos-separately
9528       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9529             files action)
9530         (while ps
9531           (setq action (cdr (assq 'action (car ps))))
9532           (setq files (list (cdr (assq 'name (car ps)))))
9533           (while (and ps (cdr ps)
9534                       (string= (or action "1")
9535                                (or (cdr (assq 'action (cadr ps))) "2")))
9536             (push (cdr (assq 'name (cadr ps))) files)
9537             (setcdr ps (cddr ps)))
9538           (when files
9539             (when (not (string-match "%s" action))
9540               (push " " files))
9541             (push " " files)
9542             (when (assq 'execute (car ps))
9543               (setcdr (assq 'execute (car ps))
9544                       (funcall (if (string-match "%s" action)
9545                                    'format 'concat)
9546                                action
9547                                (mapconcat
9548                                 (lambda (f)
9549                                   (if (equal f " ")
9550                                       f
9551                                     (mm-quote-arg f)))
9552                                 files " ")))))
9553           (setq ps (cdr ps)))))
9554     (if (and gnus-view-pseudos (not not-view))
9555         (while pslist
9556           (when (assq 'execute (car pslist))
9557             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9558                                   (eq gnus-view-pseudos 'not-confirm)))
9559           (setq pslist (cdr pslist)))
9560       (save-excursion
9561         (while pslist
9562           (setq after-article (or (cdr (assq 'article (car pslist)))
9563                                   (gnus-summary-article-number)))
9564           (gnus-summary-goto-subject after-article)
9565           (forward-line 1)
9566           (setq b (point))
9567           (insert "    " (file-name-nondirectory
9568                           (cdr (assq 'name (car pslist))))
9569                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9570           (setq e (point))
9571           (forward-line -1)             ; back to `b'
9572           (gnus-add-text-properties
9573            b (1- e) (list 'gnus-number gnus-reffed-article-number
9574                           gnus-mouse-face-prop gnus-mouse-face))
9575           (gnus-data-enter
9576            after-article gnus-reffed-article-number
9577            gnus-unread-mark b (car pslist) 0 (- e b))
9578           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9579           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9580           (setq pslist (cdr pslist)))))))
9581
9582 (defun gnus-pseudos< (p1 p2)
9583   (let ((c1 (cdr (assq 'action p1)))
9584         (c2 (cdr (assq 'action p2))))
9585     (and c1 c2 (string< c1 c2))))
9586
9587 (defun gnus-request-pseudo-article (props)
9588   (cond ((assq 'execute props)
9589          (gnus-execute-command (cdr (assq 'execute props)))))
9590   (let ((gnus-current-article (gnus-summary-article-number)))
9591     (gnus-run-hooks 'gnus-mark-article-hook)))
9592
9593 (defun gnus-execute-command (command &optional automatic)
9594   (save-excursion
9595     (gnus-article-setup-buffer)
9596     (set-buffer gnus-article-buffer)
9597     (setq buffer-read-only nil)
9598     (let ((command (if automatic command
9599                      (read-string "Command: " (cons command 0)))))
9600       (erase-buffer)
9601       (insert "$ " command "\n\n")
9602       (if gnus-view-pseudo-asynchronously
9603           (start-process "gnus-execute" (current-buffer) shell-file-name
9604                          shell-command-switch command)
9605         (call-process shell-file-name nil t nil
9606                       shell-command-switch command)))))
9607
9608 ;; Summary kill commands.
9609
9610 (defun gnus-summary-edit-global-kill (article)
9611   "Edit the \"global\" kill file."
9612   (interactive (list (gnus-summary-article-number)))
9613   (gnus-group-edit-global-kill article))
9614
9615 (defun gnus-summary-edit-local-kill ()
9616   "Edit a local kill file applied to the current newsgroup."
9617   (interactive)
9618   (setq gnus-current-headers (gnus-summary-article-header))
9619   (gnus-group-edit-local-kill
9620    (gnus-summary-article-number) gnus-newsgroup-name))
9621
9622 ;;; Header reading.
9623
9624 (defun gnus-read-header (id &optional header)
9625   "Read the headers of article ID and enter them into the Gnus system."
9626   (let ((group gnus-newsgroup-name)
9627         (gnus-override-method
9628          (or
9629           gnus-override-method
9630           (and (gnus-news-group-p gnus-newsgroup-name)
9631                (car (gnus-refer-article-methods)))))
9632         where)
9633     ;; First we check to see whether the header in question is already
9634     ;; fetched.
9635     (if (stringp id)
9636         ;; This is a Message-ID.
9637         (setq header (or header (gnus-id-to-header id)))
9638       ;; This is an article number.
9639       (setq header (or header (gnus-summary-article-header id))))
9640     (if (and header
9641              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9642         ;; We have found the header.
9643         header
9644       ;; If this is a sparse article, we have to nix out its
9645       ;; previous entry in the thread hashtb.
9646       (when (and header
9647                  (gnus-summary-article-sparse-p (mail-header-number header)))
9648         (let* ((parent (gnus-parent-id (mail-header-references header)))
9649                (thread (and parent (gnus-id-to-thread parent))))
9650           (when thread
9651             (delq (assq header thread) thread))))
9652       ;; We have to really fetch the header to this article.
9653       (save-excursion
9654         (set-buffer nntp-server-buffer)
9655         (when (setq where (gnus-request-head id group))
9656           (nnheader-fold-continuation-lines)
9657           (goto-char (point-max))
9658           (insert ".\n")
9659           (goto-char (point-min))
9660           (insert "211 ")
9661           (princ (cond
9662                   ((numberp id) id)
9663                   ((cdr where) (cdr where))
9664                   (header (mail-header-number header))
9665                   (t gnus-reffed-article-number))
9666                  (current-buffer))
9667           (insert " Article retrieved.\n"))
9668         (if (or (not where)
9669                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9670             ()                          ; Malformed head.
9671           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9672             (when (and (stringp id)
9673                        (not (string= (gnus-group-real-name group)
9674                                      (car where))))
9675               ;; If we fetched by Message-ID and the article came
9676               ;; from a different group, we fudge some bogus article
9677               ;; numbers for this article.
9678               (mail-header-set-number header gnus-reffed-article-number))
9679             (save-excursion
9680               (set-buffer gnus-summary-buffer)
9681               (decf gnus-reffed-article-number)
9682               (gnus-remove-header (mail-header-number header))
9683               (push header gnus-newsgroup-headers)
9684               (setq gnus-current-headers header)
9685               (push (mail-header-number header) gnus-newsgroup-limit)))
9686           header)))))
9687
9688 (defun gnus-remove-header (number)
9689   "Remove header NUMBER from `gnus-newsgroup-headers'."
9690   (if (and gnus-newsgroup-headers
9691            (= number (mail-header-number (car gnus-newsgroup-headers))))
9692       (pop gnus-newsgroup-headers)
9693     (let ((headers gnus-newsgroup-headers))
9694       (while (and (cdr headers)
9695                   (not (= number (mail-header-number (cadr headers)))))
9696         (pop headers))
9697       (when (cdr headers)
9698         (setcdr headers (cddr headers))))))
9699
9700 ;;;
9701 ;;; summary highlights
9702 ;;;
9703
9704 (defun gnus-highlight-selected-summary ()
9705   "Highlight selected article in summary buffer."
9706   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9707   (when gnus-summary-selected-face
9708     (save-excursion
9709       (let* ((beg (progn (beginning-of-line) (point)))
9710              (end (progn (end-of-line) (point)))
9711              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9712              (from (if (get-text-property beg gnus-mouse-face-prop)
9713                        beg
9714                      (or (next-single-property-change
9715                           beg gnus-mouse-face-prop nil end)
9716                          beg)))
9717              (to
9718               (if (= from end)
9719                   (- from 2)
9720                 (or (next-single-property-change
9721                      from gnus-mouse-face-prop nil end)
9722                     end))))
9723         ;; If no mouse-face prop on line we will have to = from = end,
9724         ;; so we highlight the entire line instead.
9725         (when (= (+ to 2) from)
9726           (setq from beg)
9727           (setq to end))
9728         (if gnus-newsgroup-selected-overlay
9729             ;; Move old overlay.
9730             (gnus-move-overlay
9731              gnus-newsgroup-selected-overlay from to (current-buffer))
9732           ;; Create new overlay.
9733           (gnus-overlay-put
9734            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9735            'face gnus-summary-selected-face))))))
9736
9737 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9738 (defun gnus-summary-highlight-line ()
9739   "Highlight current line according to `gnus-summary-highlight'."
9740   (let* ((list gnus-summary-highlight)
9741          (p (point))
9742          (end (progn (end-of-line) (point)))
9743          ;; now find out where the line starts and leave point there.
9744          (beg (progn (beginning-of-line) (point)))
9745          (article (gnus-summary-article-number))
9746          (score (or (cdr (assq (or article gnus-current-article)
9747                                gnus-newsgroup-scored))
9748                     gnus-summary-default-score 0))
9749          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9750          (inhibit-read-only t))
9751     ;; Eval the cars of the lists until we find a match.
9752     (let ((default gnus-summary-default-score))
9753       (while (and list
9754                   (not (eval (caar list))))
9755         (setq list (cdr list))))
9756     (let ((face (cdar list)))
9757       (unless (eq face (get-text-property beg 'face))
9758         (gnus-put-text-property-excluding-characters-with-faces
9759          beg end 'face
9760          (setq face (if (boundp face) (symbol-value face) face)))
9761         (when gnus-summary-highlight-line-function
9762           (funcall gnus-summary-highlight-line-function article face))))
9763     (goto-char p)))
9764
9765 (defun gnus-update-read-articles (group unread &optional compute)
9766   "Update the list of read articles in GROUP."
9767   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9768          (entry (gnus-gethash group gnus-newsrc-hashtb))
9769          (info (nth 2 entry))
9770          (prev 1)
9771          (unread (sort (copy-sequence unread) '<))
9772          read)
9773     (if (or (not info) (not active))
9774         ;; There is no info on this group if it was, in fact,
9775         ;; killed.  Gnus stores no information on killed groups, so
9776         ;; there's nothing to be done.
9777         ;; One could store the information somewhere temporarily,
9778         ;; perhaps...  Hmmm...
9779         ()
9780       ;; Remove any negative articles numbers.
9781       (while (and unread (< (car unread) 0))
9782         (setq unread (cdr unread)))
9783       ;; Remove any expired article numbers
9784       (while (and unread (< (car unread) (car active)))
9785         (setq unread (cdr unread)))
9786       ;; Compute the ranges of read articles by looking at the list of
9787       ;; unread articles.
9788       (while unread
9789         (when (/= (car unread) prev)
9790           (push (if (= prev (1- (car unread))) prev
9791                   (cons prev (1- (car unread))))
9792                 read))
9793         (setq prev (1+ (car unread)))
9794         (setq unread (cdr unread)))
9795       (when (<= prev (cdr active))
9796         (push (cons prev (cdr active)) read))
9797       (setq read (if (> (length read) 1) (nreverse read) read))
9798       (if compute
9799           read
9800         (save-excursion
9801           (let (setmarkundo)
9802             ;; Propagate the read marks to the backend.
9803             (when (gnus-check-backend-function 'request-set-mark group)
9804               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9805                     (add (gnus-remove-from-range read (gnus-info-read info))))
9806                 (when (or add del)
9807                   (unless (gnus-check-group group)
9808                     (error "Can't open server for %s" group))
9809                   (gnus-request-set-mark
9810                    group (delq nil (list (if add (list add 'add '(read)))
9811                                          (if del (list del 'del '(read))))))
9812                   (setq setmarkundo
9813                         `(gnus-request-set-mark
9814                           ,group
9815                           ',(delq nil (list
9816                                        (if del (list del 'add '(read)))
9817                                        (if add (list add 'del '(read))))))))))
9818             (set-buffer gnus-group-buffer)
9819             (gnus-undo-register
9820               `(progn
9821                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9822                  (gnus-info-set-read ',info ',(gnus-info-read info))
9823                  (gnus-get-unread-articles-in-group ',info 
9824                                                     (gnus-active ,group))
9825                  (gnus-group-update-group ,group t)
9826                  ,setmarkundo))))
9827         ;; Enter this list into the group info.
9828         (gnus-info-set-read info read)
9829         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9830         (gnus-get-unread-articles-in-group info (gnus-active group))
9831         t))))
9832
9833 (defun gnus-offer-save-summaries ()
9834   "Offer to save all active summary buffers."
9835   (save-excursion
9836     (let ((buflist (buffer-list))
9837           buffers bufname)
9838       ;; Go through all buffers and find all summaries.
9839       (while buflist
9840         (and (setq bufname (buffer-name (car buflist)))
9841              (string-match "Summary" bufname)
9842              (save-excursion
9843                (set-buffer bufname)
9844                ;; We check that this is, indeed, a summary buffer.
9845                (and (eq major-mode 'gnus-summary-mode)
9846                     ;; Also make sure this isn't bogus.
9847                     gnus-newsgroup-prepared
9848                     ;; Also make sure that this isn't a dead summary buffer.
9849                     (not gnus-dead-summary-mode)))
9850              (push bufname buffers))
9851         (setq buflist (cdr buflist)))
9852       ;; Go through all these summary buffers and offer to save them.
9853       (when buffers
9854         (map-y-or-n-p
9855          "Update summary buffer %s? "
9856          (lambda (buf)
9857            (switch-to-buffer buf)
9858            (gnus-summary-exit))
9859          buffers)))))
9860
9861 (defun gnus-summary-setup-default-charset ()
9862   "Setup newsgroup default charset."
9863   (if (equal gnus-newsgroup-name "nndraft:drafts")
9864       (setq gnus-newsgroup-charset nil)
9865     (let* ((name (and gnus-newsgroup-name
9866                       (gnus-group-real-name gnus-newsgroup-name)))
9867            (ignored-charsets
9868             (or gnus-newsgroup-ephemeral-ignored-charsets
9869                 (append
9870                  (and gnus-newsgroup-name
9871                       (or (gnus-group-find-parameter gnus-newsgroup-name
9872                                                      'ignored-charsets t)
9873                           (let ((alist gnus-group-ignored-charsets-alist)
9874                                 elem (charsets nil))
9875                             (while (setq elem (pop alist))
9876                               (when (and name
9877                                          (string-match (car elem) name))
9878                                 (setq alist nil
9879                                       charsets (cdr elem))))
9880                             charsets)))
9881                  gnus-newsgroup-ignored-charsets))))
9882       (setq gnus-newsgroup-charset
9883             (or gnus-newsgroup-ephemeral-charset
9884                 (and gnus-newsgroup-name
9885                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9886                          (let ((alist gnus-group-charset-alist)
9887                                elem charset)
9888                            (while (setq elem (pop alist))
9889                              (when (and name
9890                                         (string-match (car elem) name))
9891                                (setq alist nil
9892                                      charset (cadr elem))))
9893                            charset)))
9894                 gnus-default-charset))
9895       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9896            ignored-charsets))))
9897
9898 ;;;
9899 ;;; Mime Commands
9900 ;;;
9901
9902 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9903   "Display the current article buffer fully MIME-buttonized.
9904 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9905 treated as multipart/mixed."
9906   (interactive "P")
9907   (require 'gnus-art)
9908   (let ((gnus-unbuttonized-mime-types nil)
9909         (gnus-mime-display-multipart-as-mixed show-all-parts))
9910     (gnus-summary-show-article)))
9911
9912 (defun gnus-summary-repair-multipart (article)
9913   "Add a Content-Type header to a multipart article without one."
9914   (interactive (list (gnus-summary-article-number)))
9915   (gnus-with-article article
9916     (message-narrow-to-head)
9917     (message-remove-header "Mime-Version")
9918     (goto-char (point-max))
9919     (insert "Mime-Version: 1.0\n")
9920     (widen)
9921     (when (search-forward "\n--" nil t)
9922       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9923         (message-narrow-to-head)
9924         (message-remove-header "Content-Type")
9925         (goto-char (point-max))
9926         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9927                         separator))
9928         (widen))))
9929   (let (gnus-mark-article-hook)
9930     (gnus-summary-select-article t t nil article)))
9931
9932 (defun gnus-summary-toggle-display-buttonized ()
9933   "Toggle the buttonizing of the article buffer."
9934   (interactive)
9935   (require 'gnus-art)
9936   (if (setq gnus-inhibit-mime-unbuttonizing
9937             (not gnus-inhibit-mime-unbuttonizing))
9938       (let ((gnus-unbuttonized-mime-types nil))
9939         (gnus-summary-show-article))
9940     (gnus-summary-show-article)))
9941
9942 ;;;
9943 ;;; Generic summary marking commands
9944 ;;;
9945
9946 (defvar gnus-summary-marking-alist
9947   '((read gnus-del-mark "d")
9948     (unread gnus-unread-mark "u")
9949     (ticked gnus-ticked-mark "!")
9950     (dormant gnus-dormant-mark "?")
9951     (expirable gnus-expirable-mark "e"))
9952   "An alist of names/marks/keystrokes.")
9953
9954 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9955 (defvar gnus-summary-mark-map)
9956
9957 (defun gnus-summary-make-all-marking-commands ()
9958   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9959   (dolist (elem gnus-summary-marking-alist)
9960     (apply 'gnus-summary-make-marking-command elem)))
9961
9962 (defun gnus-summary-make-marking-command (name mark keystroke)
9963   (let ((map (make-sparse-keymap)))
9964     (define-key gnus-summary-generic-mark-map keystroke map)
9965     (dolist (lway `((next "next" next nil "n")
9966                     (next-unread "next unread" next t "N")
9967                     (prev "previous" prev nil "p")
9968                     (prev-unread "previous unread" prev t "P")
9969                     (nomove "" nil nil ,keystroke)))
9970       (let ((func (gnus-summary-make-marking-command-1
9971                    mark (car lway) lway name)))
9972         (setq func (eval func))
9973         (define-key map (nth 4 lway) func)))))
9974       
9975 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9976   `(defun ,(intern
9977             (format "gnus-summary-put-mark-as-%s%s"
9978                     name (if (eq way 'nomove)
9979                              ""
9980                            (concat "-" (symbol-name way)))))
9981      (n)
9982      ,(format
9983        "Mark the current article as %s%s.
9984 If N, the prefix, then repeat N times.
9985 If N is negative, move in reverse order.
9986 The difference between N and the actual number of articles marked is
9987 returned."
9988        name (cadr lway))
9989      (interactive "p")
9990      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9991     
9992 (defun gnus-summary-generic-mark (n mark move unread)
9993   "Mark N articles with MARK."
9994   (unless (eq major-mode 'gnus-summary-mode)
9995     (error "This command can only be used in the summary buffer"))
9996   (gnus-summary-show-thread)
9997   (let ((nummove
9998          (cond
9999           ((eq move 'next) 1)
10000           ((eq move 'prev) -1)
10001           (t 0))))
10002     (if (zerop nummove)
10003         (setq n 1)
10004       (when (< n 0)
10005         (setq n (abs n)
10006               nummove (* -1 nummove))))
10007     (while (and (> n 0)
10008                 (gnus-summary-mark-article nil mark)
10009                 (zerop (gnus-summary-next-subject nummove unread t)))
10010       (setq n (1- n)))
10011     (when (/= 0 n)
10012       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10013     (gnus-summary-recenter)
10014     (gnus-summary-position-point)
10015     (gnus-set-mode-line 'summary)
10016     n))
10017
10018 (gnus-summary-make-all-marking-commands)
10019
10020 (gnus-ems-redefine)
10021
10022 (provide 'gnus-sum)
10023
10024 (run-hooks 'gnus-sum-load-hook)
10025
10026 ;;; gnus-sum.el ends here