Merge changes in Emacs21.
[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 'mm-uu-dissect "mm-uu")
46
47 (defcustom gnus-kill-summary-on-exit t
48   "*If non-nil, kill the summary buffer when you exit from it.
49 If nil, the summary will become a \"*Dead Summary*\" buffer, and
50 it will be killed sometime later."
51   :group 'gnus-summary-exit
52   :type 'boolean)
53
54 (defcustom gnus-fetch-old-headers nil
55   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
56 If an unread article in the group refers to an older, already read (or
57 just marked as read) article, the old article will not normally be
58 displayed in the Summary buffer.  If this variable is non-nil, Gnus
59 will attempt to grab the headers to the old articles, and thereby
60 build complete threads.  If it has the value `some', only enough
61 headers to connect otherwise loose threads will be displayed.  This
62 variable can also be a number.  In that case, no more than that number
63 of old headers will be fetched.  If it has the value `invisible', all
64 old headers will be fetched, but none will be displayed.
65
66 The server has to support NOV for any of this to work."
67   :group 'gnus-thread
68   :type '(choice (const :tag "off" nil)
69                  (const some)
70                  number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-refer-thread-limit 200
74   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
75 If t, fetch all the available old headers."
76   :group 'gnus-thread
77   :type '(choice number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-summary-make-false-root 'adopt
81   "*nil means that Gnus won't gather loose threads.
82 If the root of a thread has expired or been read in a previous
83 session, the information necessary to build a complete thread has been
84 lost.  Instead of having many small sub-threads from this original thread
85 scattered all over the summary buffer, Gnus can gather them.
86
87 If non-nil, Gnus will try to gather all loose sub-threads from an
88 original thread into one large thread.
89
90 If this variable is non-nil, it should be one of `none', `adopt',
91 `dummy' or `empty'.
92
93 If this variable is `none', Gnus will not make a false root, but just
94 present the sub-threads after another.
95 If this variable is `dummy', Gnus will create a dummy root that will
96 have all the sub-threads as children.
97 If this variable is `adopt', Gnus will make one of the \"children\"
98 the parent and mark all the step-children as such.
99 If this variable is `empty', the \"children\" are printed with empty
100 subject fields.  (Or rather, they will be printed with a string
101 given by the `gnus-summary-same-subject' variable.)"
102   :group 'gnus-thread
103   :type '(choice (const :tag "off" nil)
104                  (const none)
105                  (const dummy)
106                  (const adopt)
107                  (const empty)))
108
109 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
110   "*A regexp to match subjects to be excluded from loose thread gathering.
111 As loose thread gathering is done on subjects only, that means that
112 there can be many false gatherings performed.  By rooting out certain
113 common subjects, gathering might become saner."
114   :group 'gnus-thread
115   :type 'regexp)
116
117 (defcustom gnus-summary-gather-subject-limit nil
118   "*Maximum length of subject comparisons when gathering loose threads.
119 Use nil to compare full subjects.  Setting this variable to a low
120 number will help gather threads that have been corrupted by
121 newsreaders chopping off subject lines, but it might also mean that
122 unrelated articles that have subject that happen to begin with the
123 same few characters will be incorrectly gathered.
124
125 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
126 comparing subjects."
127   :group 'gnus-thread
128   :type '(choice (const :tag "off" nil)
129                  (const fuzzy)
130                  (sexp :menu-tag "on" t)))
131
132 (defcustom gnus-simplify-subject-functions nil
133   "List of functions taking a string argument that simplify subjects.
134 The functions are applied recursively.
135
136 Useful functions to put in this list include: `gnus-simplify-subject-re',
137 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
138   :group 'gnus-thread
139   :type '(repeat function))
140
141 (defcustom gnus-simplify-ignored-prefixes nil
142   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  regexp))
146
147 (defcustom gnus-build-sparse-threads nil
148   "*If non-nil, fill in the gaps in threads.
149 If `some', only fill in the gaps that are needed to tie loose threads
150 together.  If `more', fill in all leaf nodes that Gnus can find.  If
151 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  (const some)
155                  (const more)
156                  (sexp :menu-tag "all" t)))
157
158 (defcustom gnus-summary-thread-gathering-function
159   'gnus-gather-threads-by-subject
160   "*Function used for gathering loose threads.
161 There are two pre-defined functions: `gnus-gather-threads-by-subject',
162 which only takes Subjects into consideration; and
163 `gnus-gather-threads-by-references', which compared the References
164 headers of the articles to find matches."
165   :group 'gnus-thread
166   :type '(radio (function-item gnus-gather-threads-by-subject)
167                 (function-item gnus-gather-threads-by-references)
168                 (function :tag "other")))
169
170 (defcustom gnus-summary-same-subject ""
171   "*String indicating that the current article has the same subject as the previous.
172 This variable will only be used if the value of
173 `gnus-summary-make-false-root' is `empty'."
174   :group 'gnus-summary-format
175   :type 'string)
176
177 (defcustom gnus-summary-goto-unread t
178   "*If t, many commands will go to the next unread article.
179 This applies to marking commands as well as other commands that
180 \"naturally\" select the next article, like, for instance, `SPC' at
181 the end of an article.
182
183 If nil, the marking commands do NOT go to the next unread article
184 (they go to the next article instead).  If `never', commands that
185 usually go to the next unread article, will go to the next article,
186 whether it is read or not."
187   :group 'gnus-summary-marks
188   :link '(custom-manual "(gnus)Setting Marks")
189   :type '(choice (const :tag "off" nil)
190                  (const never)
191                  (sexp :menu-tag "on" t)))
192
193 (defcustom gnus-summary-default-score 0
194   "*Default article score level.
195 All scores generated by the score files will be added to this score.
196 If this variable is nil, scoring will be disabled."
197   :group 'gnus-score-default
198   :type '(choice (const :tag "disable")
199                  integer))
200
201 (defcustom gnus-summary-zcore-fuzz 0
202   "*Fuzziness factor for the zcore in the summary buffer.
203 Articles with scores closer than this to `gnus-summary-default-score'
204 will not be marked."
205   :group 'gnus-summary-format
206   :type 'integer)
207
208 (defcustom gnus-simplify-subject-fuzzy-regexp nil
209   "*Strings to be removed when doing fuzzy matches.
210 This can either be a regular expression or list of regular expressions
211 that will be removed from subject strings if fuzzy subject
212 simplification is selected."
213   :group 'gnus-thread
214   :type '(repeat regexp))
215
216 (defcustom gnus-show-threads t
217   "*If non-nil, display threads in summary mode."
218   :group 'gnus-thread
219   :type 'boolean)
220
221 (defcustom gnus-thread-hide-subtree nil
222   "*If non-nil, hide all threads initially.
223 If threads are hidden, you have to run the command
224 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
225 to expose hidden threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-hide-killed t
230   "*If non-nil, hide killed threads automatically."
231   :group 'gnus-thread
232   :type 'boolean)
233
234 (defcustom gnus-thread-ignore-subject t
235   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
236 If nil, articles that have different subjects from their parents will
237 start separate threads."
238   :group 'gnus-thread
239   :type 'boolean)
240
241 (defcustom gnus-thread-operation-ignore-subject t
242   "*If non-nil, subjects will be ignored when doing thread commands.
243 This affects commands like `gnus-summary-kill-thread' and
244 `gnus-summary-lower-thread'.
245
246 If this variable is nil, articles in the same thread with different
247 subjects will not be included in the operation in question.  If this
248 variable is `fuzzy', only articles that have subjects that are fuzzily
249 equal will be included."
250   :group 'gnus-thread
251   :type '(choice (const :tag "off" nil)
252                  (const fuzzy)
253                  (sexp :tag "on" t)))
254
255 (defcustom gnus-thread-indent-level 4
256   "*Number that says how much each sub-thread should be indented."
257   :group 'gnus-thread
258   :type 'integer)
259
260 (defcustom gnus-auto-extend-newsgroup t
261   "*If non-nil, extend newsgroup forward and backward when requested."
262   :group 'gnus-summary-choose
263   :type 'boolean)
264
265 (defcustom gnus-auto-select-first t
266   "*If nil, don't select the first unread article when entering a group.
267 If this variable is `best', select the highest-scored unread article
268 in the group.  If t, select the first unread article.
269
270 This variable can also be a function to place point on a likely
271 subject line.  Useful values include `gnus-summary-first-unread-subject',
272 `gnus-summary-first-unread-article' and
273 `gnus-summary-best-unread-article'.
274
275 If you want to prevent automatic selection of the first unread article
276 in some newsgroups, set the variable to nil in
277 `gnus-select-group-hook'."
278   :group 'gnus-group-select
279   :type '(choice (const :tag "none" nil)
280                  (const best)
281                  (sexp :menu-tag "first" t)
282                  (function-item gnus-summary-first-unread-subject)
283                  (function-item gnus-summary-first-unread-article)
284                  (function-item gnus-summary-best-unread-article)))
285
286 (defcustom gnus-auto-select-next t
287   "*If non-nil, offer to go to the next group from the end of the previous.
288 If the value is t and the next newsgroup is empty, Gnus will exit
289 summary mode and go back to group mode.  If the value is neither nil
290 nor t, Gnus will select the following unread newsgroup.  In
291 particular, if the value is the symbol `quietly', the next unread
292 newsgroup will be selected without any confirmation, and if it is
293 `almost-quietly', the next group will be selected without any
294 confirmation if you are located on the last article in the group.
295 Finally, if this variable is `slightly-quietly', the `Z n' command
296 will go to the next group without confirmation."
297   :group 'gnus-summary-maneuvering
298   :type '(choice (const :tag "off" nil)
299                  (const quietly)
300                  (const almost-quietly)
301                  (const slightly-quietly)
302                  (sexp :menu-tag "on" t)))
303
304 (defcustom gnus-auto-select-same nil
305   "*If non-nil, select the next article with the same subject.
306 If there are no more articles with the same subject, go to
307 the first unread article."
308   :group 'gnus-summary-maneuvering
309   :type 'boolean)
310
311 (defcustom gnus-summary-check-current nil
312   "*If non-nil, consider the current article when moving.
313 The \"unread\" movement commands will stay on the same line if the
314 current article is unread."
315   :group 'gnus-summary-maneuvering
316   :type 'boolean)
317
318 (defcustom gnus-auto-center-summary t
319   "*If non-nil, always center the current summary buffer.
320 In particular, if `vertical' do only vertical recentering.  If non-nil
321 and non-`vertical', do both horizontal and vertical recentering."
322   :group 'gnus-summary-maneuvering
323   :type '(choice (const :tag "none" nil)
324                  (const vertical)
325                  (integer :tag "height")
326                  (sexp :menu-tag "both" t)))
327
328 (defcustom gnus-show-all-headers nil
329   "*If non-nil, don't hide any headers."
330   :group 'gnus-article-hiding
331   :group 'gnus-article-headers
332   :type 'boolean)
333
334 (defcustom gnus-summary-ignore-duplicates nil
335   "*If non-nil, ignore articles with identical Message-ID headers."
336   :group 'gnus-summary
337   :type 'boolean)
338
339 (defcustom gnus-single-article-buffer t
340   "*If non-nil, display all articles in the same buffer.
341 If nil, each group will get its own article buffer."
342   :group 'gnus-article-various
343   :type 'boolean)
344
345 (defcustom gnus-break-pages t
346   "*If non-nil, do page breaking on articles.
347 The page delimiter is specified by the `gnus-page-delimiter'
348 variable."
349   :group 'gnus-article-various
350   :type 'boolean)
351
352 (defcustom gnus-move-split-methods nil
353   "*Variable used to suggest where articles are to be moved to.
354 It uses the same syntax as the `gnus-split-methods' variable.
355 However, whereas `gnus-split-methods' specifies file names as targets,
356 this variable specifies group names."
357   :group 'gnus-summary-mail
358   :type '(repeat (choice (list :value (fun) function)
359                          (cons :value ("" "") regexp (repeat string))
360                          (sexp :value nil))))
361
362 (defcustom gnus-unread-mark ?  ;Whitespace
363   "*Mark used for unread articles."
364   :group 'gnus-summary-marks
365   :type 'character)
366
367 (defcustom gnus-ticked-mark ?!
368   "*Mark used for ticked articles."
369   :group 'gnus-summary-marks
370   :type 'character)
371
372 (defcustom gnus-dormant-mark ??
373   "*Mark used for dormant articles."
374   :group 'gnus-summary-marks
375   :type 'character)
376
377 (defcustom gnus-del-mark ?r
378   "*Mark used for del'd articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-read-mark ?R
383   "*Mark used for read articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-expirable-mark ?E
388   "*Mark used for expirable articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-killed-mark ?K
393   "*Mark used for killed articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-souped-mark ?F
398   "*Mark used for killed articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-kill-file-mark ?X
403   "*Mark used for articles killed by kill files."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-low-score-mark ?Y
408   "*Mark used for articles with a low score."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-catchup-mark ?C
413   "*Mark used for articles that are caught up."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-replied-mark ?A
418   "*Mark used for articles that have been replied to."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-cached-mark ?*
423   "*Mark used for articles that are in the cache."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-saved-mark ?S
428   "*Mark used for articles that have been saved to."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-ancient-mark ?O
433   "*Mark used for ancient articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-sparse-mark ?Q
438   "*Mark used for sparsely reffed articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-canceled-mark ?G
443   "*Mark used for canceled articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-duplicate-mark ?M
448   "*Mark used for duplicate articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-undownloaded-mark ?@
453   "*Mark used for articles that weren't downloaded."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-downloadable-mark ?%
458   "*Mark used for articles that are to be downloaded."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-unsendable-mark ?=
463   "*Mark used for articles that won't be sent."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-score-over-mark ?+
468   "*Score mark used for articles with high scores."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-score-below-mark ?-
473   "*Score mark used for articles with low scores."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-empty-thread-mark ?  ;Whitespace
478   "*There is no thread under the article."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-not-empty-thread-mark ?=
483   "*There is a thread under the article."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-view-pseudo-asynchronously nil
488   "*If non-nil, Gnus will view pseudo-articles asynchronously."
489   :group 'gnus-extract-view
490   :type 'boolean)
491
492 (defcustom gnus-auto-expirable-marks
493   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
494         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
495         gnus-souped-mark gnus-duplicate-mark)
496   "*The list of marks converted into expiration if a group is auto-expirable."
497   :version "21.1"
498   :group 'gnus-summary
499   :type '(repeat character))
500
501 (defcustom gnus-inhibit-user-auto-expire t
502   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
503   :version "21.1"
504   :group 'gnus-summary
505   :type 'boolean)
506
507 (defcustom gnus-view-pseudos nil
508   "*If `automatic', pseudo-articles will be viewed automatically.
509 If `not-confirm', pseudos will be viewed automatically, and the user
510 will not be asked to confirm the command."
511   :group 'gnus-extract-view
512   :type '(choice (const :tag "off" nil)
513                  (const automatic)
514                  (const not-confirm)))
515
516 (defcustom gnus-view-pseudos-separately t
517   "*If non-nil, one pseudo-article will be created for each file to be viewed.
518 If nil, all files that use the same viewing command will be given as a
519 list of parameters to that command."
520   :group 'gnus-extract-view
521   :type 'boolean)
522
523 (defcustom gnus-insert-pseudo-articles t
524   "*If non-nil, insert pseudo-articles when decoding articles."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-summary-dummy-line-format
529   "  %(:                          :%) %S\n"
530   "*The format specification for the dummy roots in the summary buffer.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions.
533
534 %S  The subject"
535   :group 'gnus-threading
536   :type 'string)
537
538 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
539   "*The format specification for the summary mode line.
540 It works along the same lines as a normal formatting string,
541 with some simple extensions:
542
543 %G  Group name
544 %p  Unprefixed group name
545 %A  Current article number
546 %z  Current article score
547 %V  Gnus version
548 %U  Number of unread articles in the group
549 %e  Number of unselected articles in the group
550 %Z  A string with unread/unselected article counts
551 %g  Shortish group name
552 %S  Subject of the current article
553 %u  User-defined spec
554 %s  Current score file name
555 %d  Number of dormant articles
556 %r  Number of articles that have been marked as read in this session
557 %E  Number of articles expunged by the score files"
558   :group 'gnus-summary-format
559   :type 'string)
560
561 (defcustom gnus-list-identifiers nil
562   "Regexp that matches list identifiers to be removed from subject.
563 This can also be a list of regexps."
564   :version "21.1"
565   :group 'gnus-summary-format
566   :group 'gnus-article-hiding
567   :type '(choice (const :tag "none" nil)
568                  (regexp :value ".*")
569                  (repeat :value (".*") regexp)))
570
571 (defcustom gnus-summary-mark-below 0
572   "*Mark all articles with a score below this variable as read.
573 This variable is local to each summary buffer and usually set by the
574 score file."
575   :group 'gnus-score-default
576   :type 'integer)
577
578 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
579   "*List of functions used for sorting articles in the summary buffer.
580 This variable is only used when not using a threaded display."
581   :group 'gnus-summary-sort
582   :type '(repeat (choice (function-item gnus-article-sort-by-number)
583                          (function-item gnus-article-sort-by-author)
584                          (function-item gnus-article-sort-by-subject)
585                          (function-item gnus-article-sort-by-date)
586                          (function-item gnus-article-sort-by-score)
587                          (function :tag "other"))))
588
589 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
590   "*List of functions used for sorting threads in the summary buffer.
591 By default, threads are sorted by article number.
592
593 Each function takes two threads and return non-nil if the first thread
594 should be sorted before the other.  If you use more than one function,
595 the primary sort function should be the last.  You should probably
596 always include `gnus-thread-sort-by-number' in the list of sorting
597 functions -- preferably first.
598
599 Ready-made functions include `gnus-thread-sort-by-number',
600 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
601 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
602 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
603   :group 'gnus-summary-sort
604   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
605                          (function-item gnus-thread-sort-by-author)
606                          (function-item gnus-thread-sort-by-subject)
607                          (function-item gnus-thread-sort-by-date)
608                          (function-item gnus-thread-sort-by-score)
609                          (function-item gnus-thread-sort-by-total-score)
610                          (function :tag "other"))))
611
612 (defcustom gnus-thread-score-function '+
613   "*Function used for calculating the total score of a thread.
614
615 The function is called with the scores of the article and each
616 subthread and should then return the score of the thread.
617
618 Some functions you can use are `+', `max', or `min'."
619   :group 'gnus-summary-sort
620   :type 'function)
621
622 (defcustom gnus-summary-expunge-below nil
623   "All articles that have a score less than this variable will be expunged.
624 This variable is local to the summary buffers."
625   :group 'gnus-score-default
626   :type '(choice (const :tag "off" nil)
627                  integer))
628
629 (defcustom gnus-thread-expunge-below nil
630   "All threads that have a total score less than this variable will be expunged.
631 See `gnus-thread-score-function' for en explanation of what a
632 \"thread score\" is.
633
634 This variable is local to the summary buffers."
635   :group 'gnus-threading
636   :group 'gnus-score-default
637   :type '(choice (const :tag "off" nil)
638                  integer))
639
640 (defcustom gnus-summary-mode-hook nil
641   "*A hook for Gnus summary mode.
642 This hook is run before any variables are set in the summary buffer."
643   :options '(turn-on-gnus-mailing-list-mode)
644   :group 'gnus-summary-various
645   :type 'hook)
646
647 (defcustom gnus-summary-menu-hook nil
648   "*Hook run after the creation of the summary mode menu."
649   :group 'gnus-summary-visual
650   :type 'hook)
651
652 (defcustom gnus-summary-exit-hook nil
653   "*A hook called on exit from the summary buffer.
654 It will be called with point in the group buffer."
655   :group 'gnus-summary-exit
656   :type 'hook)
657
658 (defcustom gnus-summary-prepare-hook nil
659   "*A hook called after the summary buffer has been generated.
660 If you want to modify the summary buffer, you can use this hook."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-summary-prepared-hook nil
665   "*A hook called as the last thing after the summary buffer has been generated."
666   :group 'gnus-summary-various
667   :type 'hook)
668
669 (defcustom gnus-summary-generate-hook nil
670   "*A hook run just before generating the summary buffer.
671 This hook is commonly used to customize threading variables and the
672 like."
673   :group 'gnus-summary-various
674   :type 'hook)
675
676 (defcustom gnus-select-group-hook nil
677   "*A hook called when a newsgroup is selected.
678
679 If you'd like to simplify subjects like the
680 `gnus-summary-next-same-subject' command does, you can use the
681 following hook:
682
683  (setq gnus-select-group-hook
684       (list
685         (lambda ()
686           (mapcar (lambda (header)
687                      (mail-header-set-subject
688                       header
689                       (gnus-simplify-subject
690                        (mail-header-subject header) 're-only)))
691                   gnus-newsgroup-headers))))"
692   :group 'gnus-group-select
693   :type 'hook)
694
695 (defcustom gnus-select-article-hook nil
696   "*A hook called when an article is selected."
697   :group 'gnus-summary-choose
698   :type 'hook)
699
700 (defcustom gnus-visual-mark-article-hook
701   (list 'gnus-highlight-selected-summary)
702   "*Hook run after selecting an article in the summary buffer.
703 It is meant to be used for highlighting the article in some way.  It
704 is not run if `gnus-visual' is nil."
705   :group 'gnus-summary-visual
706   :type 'hook)
707
708 (defcustom gnus-parse-headers-hook nil
709   "*A hook called before parsing the headers."
710   :group 'gnus-various
711   :type 'hook)
712
713 (defcustom gnus-exit-group-hook nil
714   "*A hook called when exiting summary mode.
715 This hook is not called from the non-updating exit commands like `Q'."
716   :group 'gnus-various
717   :type 'hook)
718
719 (defcustom gnus-summary-update-hook
720   (list 'gnus-summary-highlight-line)
721   "*A hook called when a summary line is changed.
722 The hook will not be called if `gnus-visual' is nil.
723
724 The default function `gnus-summary-highlight-line' will
725 highlight the line according to the `gnus-summary-highlight'
726 variable."
727   :group 'gnus-summary-visual
728   :type 'hook)
729
730 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
731   "*A hook called when an article is selected for the first time.
732 The hook is intended to mark an article as read (or unread)
733 automatically when it is selected."
734   :group 'gnus-summary-choose
735   :type 'hook)
736
737 (defcustom gnus-group-no-more-groups-hook nil
738   "*A hook run when returning to group mode having no more (unread) groups."
739   :group 'gnus-group-select
740   :type 'hook)
741
742 (defcustom gnus-ps-print-hook nil
743   "*A hook run before ps-printing something from Gnus."
744   :group 'gnus-summary
745   :type 'hook)
746
747 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
748   "Face used for highlighting the current article in the summary buffer."
749   :group 'gnus-summary-visual
750   :type 'face)
751
752 (defcustom gnus-summary-highlight
753   '(((= mark gnus-canceled-mark)
754      . gnus-summary-cancelled-face)
755     ((and (> score default)
756           (or (= mark gnus-dormant-mark)
757               (= mark gnus-ticked-mark)))
758      . gnus-summary-high-ticked-face)
759     ((and (< score default)
760           (or (= mark gnus-dormant-mark)
761               (= mark gnus-ticked-mark)))
762      . gnus-summary-low-ticked-face)
763     ((or (= mark gnus-dormant-mark)
764          (= mark gnus-ticked-mark))
765      . gnus-summary-normal-ticked-face)
766     ((and (> score default) (= mark gnus-ancient-mark))
767      . gnus-summary-high-ancient-face)
768     ((and (< score default) (= mark gnus-ancient-mark))
769      . gnus-summary-low-ancient-face)
770     ((= mark gnus-ancient-mark)
771      . gnus-summary-normal-ancient-face)
772     ((and (> score default) (= mark gnus-unread-mark))
773      . gnus-summary-high-unread-face)
774     ((and (< score default) (= mark gnus-unread-mark))
775      . gnus-summary-low-unread-face)
776     ((= mark gnus-unread-mark)
777      . gnus-summary-normal-unread-face)
778     ((and (> score default) (memq mark (list gnus-downloadable-mark
779                                              gnus-undownloaded-mark)))
780      . gnus-summary-high-unread-face)
781     ((and (< score default) (memq mark (list gnus-downloadable-mark
782                                              gnus-undownloaded-mark)))
783      . gnus-summary-low-unread-face)
784     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
785      . gnus-summary-normal-unread-face)
786     ((> score default)
787      . gnus-summary-high-read-face)
788     ((< score default)
789      . gnus-summary-low-read-face)
790     (t
791      . gnus-summary-normal-read-face))
792   "*Controls the highlighting of summary buffer lines.
793
794 A list of (FORM . FACE) pairs.  When deciding how a a particular
795 summary line should be displayed, each form is evaluated.  The content
796 of the face field after the first true form is used.  You can change
797 how those summary lines are displayed, by editing the face field.
798
799 You can use the following variables in the FORM field.
800
801 score:   The articles score
802 default: The default article score.
803 below:   The score below which articles are automatically marked as read.
804 mark:    The articles mark."
805   :group 'gnus-summary-visual
806   :type '(repeat (cons (sexp :tag "Form" nil)
807                        face)))
808
809 (defcustom gnus-alter-header-function nil
810   "Function called to allow alteration of article header structures.
811 The function is called with one parameter, the article header vector,
812 which it may alter in any way.")
813
814 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
815   "Variable that says which function should be used to decode a string with encoded words.")
816
817 (defcustom gnus-extra-headers nil
818   "*Extra headers to parse."
819   :version "21.1"
820   :group 'gnus-summary
821   :type '(repeat symbol))
822
823 (defcustom gnus-ignored-from-addresses
824   (and user-mail-address (regexp-quote user-mail-address))
825   "*Regexp of From headers that may be suppressed in favor of To headers."
826   :version "21.1"
827   :group 'gnus-summary
828   :type 'regexp)
829
830 (defcustom gnus-group-charset-alist
831   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
832     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
833     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
834     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
835     ("^relcom\\>" koi8-r)
836     ("^fido7\\>" koi8-r)
837     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
838     ("^israel\\>" iso-8859-1)
839     ("^han\\>" euc-kr)
840     ("^alt.chinese.text.big5\\>" chinese-big5)
841     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
842     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
843     (".*" iso-8859-1))
844   "Alist of regexps (to match group names) and default charsets to be used when reading."
845   :type '(repeat (list (regexp :tag "Group")
846                        (symbol :tag "Charset")))
847   :group 'gnus-charset)
848
849 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
850   "List of charsets that should be ignored.
851 When these charsets are used in the \"charset\" parameter, the
852 default charset will be used instead."
853   :version "21.1"
854   :type '(repeat symbol)
855   :group 'gnus-charset)
856
857 (defcustom gnus-group-ignored-charsets-alist
858   '(("alt\\.chinese\\.text" iso-8859-1))
859   "Alist of regexps (to match group names) and charsets that should be ignored.
860 When these charsets are used in the \"charset\" parameter, the
861 default charset will be used instead."
862   :type '(repeat (cons (regexp :tag "Group")
863                        (repeat symbol)))
864   :group 'gnus-charset)
865
866 (defcustom gnus-group-highlight-words-alist nil
867   "Alist of group regexps and highlight regexps.
868 This variable uses the same syntax as `gnus-emphasis-alist'."
869   :version "21.1"
870   :type '(repeat (cons (regexp :tag "Group")
871                        (repeat (list (regexp :tag "Highlight regexp")
872                                      (number :tag "Group for entire word" 0)
873                                      (number :tag "Group for displayed part" 0)
874                                      (symbol :tag "Face"
875                                              gnus-emphasis-highlight-words)))))
876   :group 'gnus-summary-visual)
877
878 (defcustom gnus-summary-show-article-charset-alist
879   nil
880   "Alist of number and charset.
881 The article will be shown with the charset corresponding to the
882 numbered argument.
883 For example: ((1 . cn-gb-2312) (2 . big5))."
884   :version "21.1"
885   :type '(repeat (cons (number :tag "Argument" 1)
886                        (symbol :tag "Charset")))
887   :group 'gnus-charset)
888
889 (defcustom gnus-preserve-marks t
890   "Whether marks are preserved when moving, copying and respooling messages."
891   :version "21.1"
892   :type 'boolean
893   :group 'gnus-summary-marks)
894
895 (defcustom gnus-alter-articles-to-read-function nil
896   "Function to be called to alter the list of articles to be selected."
897   :type 'function
898   :group 'gnus-summary)
899
900 (defcustom gnus-orphan-score nil
901   "*All orphans get this score added.  Set in the score file."
902   :group 'gnus-score-default
903   :type '(choice (const nil)
904                  integer))
905
906 (defcustom gnus-summary-save-parts-default-mime "image/.*"
907   "*A regexp to match MIME parts when saving multiple parts of a message
908 with gnus-summary-save-parts (X m). This regexp will be used by default
909 when prompting the user for which type of files to save."
910   :group 'gnus-summary
911   :type 'regexp)
912
913
914 (defcustom gnus-summary-save-parts-default-mime "image/.*"
915   "*A regexp to match MIME parts when saving multiple parts of a message
916 with gnus-summary-save-parts (X m). This regexp will be used by default
917 when prompting the user for which type of files to save."
918   :group 'gnus-summary
919   :type 'regexp)
920
921
922 ;;; Internal variables
923
924 (defvar gnus-article-mime-handles nil)
925 (defvar gnus-article-decoded-p nil)
926 (defvar gnus-article-charset nil)
927 (defvar gnus-article-ignored-charsets nil)
928 (defvar gnus-scores-exclude-files nil)
929 (defvar gnus-page-broken nil)
930 (defvar gnus-inhibit-mime-unbuttonizing nil)
931
932 (defvar gnus-original-article nil)
933 (defvar gnus-article-internal-prepare-hook nil)
934 (defvar gnus-newsgroup-process-stack nil)
935
936 (defvar gnus-thread-indent-array nil)
937 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
938 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
939   "Function called to sort the articles within a thread after it has been gathered together.")
940
941 (defvar gnus-summary-save-parts-type-history nil)
942 (defvar gnus-summary-save-parts-last-directory nil)
943
944 (defvar gnus-summary-save-parts-type-history nil)
945 (defvar gnus-summary-save-parts-last-directory nil)
946
947 ;; Avoid highlighting in kill files.
948 (defvar gnus-summary-inhibit-highlight nil)
949 (defvar gnus-newsgroup-selected-overlay nil)
950 (defvar gnus-inhibit-limiting nil)
951 (defvar gnus-newsgroup-adaptive-score-file nil)
952 (defvar gnus-current-score-file nil)
953 (defvar gnus-current-move-group nil)
954 (defvar gnus-current-copy-group nil)
955 (defvar gnus-current-crosspost-group nil)
956
957 (defvar gnus-newsgroup-dependencies nil)
958 (defvar gnus-newsgroup-adaptive nil)
959 (defvar gnus-summary-display-article-function nil)
960 (defvar gnus-summary-highlight-line-function nil
961   "Function called after highlighting a summary line.")
962
963 (defvar gnus-summary-line-format-alist
964   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
965     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
966     (?s gnus-tmp-subject-or-nil ?s)
967     (?n gnus-tmp-name ?s)
968     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
969         ?s)
970     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
971             gnus-tmp-from) ?s)
972     (?F gnus-tmp-from ?s)
973     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
974     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
975     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
976     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
977     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
978     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
979     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
980     (?L gnus-tmp-lines ?d)
981     (?I gnus-tmp-indentation ?s)
982     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
983     (?R gnus-tmp-replied ?c)
984     (?\[ gnus-tmp-opening-bracket ?c)
985     (?\] gnus-tmp-closing-bracket ?c)
986     (?\> (make-string gnus-tmp-level ? ) ?s)
987     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
988     (?i gnus-tmp-score ?d)
989     (?z gnus-tmp-score-char ?c)
990     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
991     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
992     (?U gnus-tmp-unread ?c)
993     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
994     (?t (gnus-summary-number-of-articles-in-thread
995          (and (boundp 'thread) (car thread)) gnus-tmp-level)
996         ?d)
997     (?e (gnus-summary-number-of-articles-in-thread
998          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
999         ?c)
1000     (?u gnus-tmp-user-defined ?s)
1001     (?P (gnus-pick-line-number) ?d))
1002   "An alist of format specifications that can appear in summary lines.
1003 These are paired with what variables they correspond with, along with
1004 the type of the variable (string, integer, character, etc).")
1005
1006 (defvar gnus-summary-dummy-line-format-alist
1007   `((?S gnus-tmp-subject ?s)
1008     (?N gnus-tmp-number ?d)
1009     (?u gnus-tmp-user-defined ?s)))
1010
1011 (defvar gnus-summary-mode-line-format-alist
1012   `((?G gnus-tmp-group-name ?s)
1013     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1014     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1015     (?A gnus-tmp-article-number ?d)
1016     (?Z gnus-tmp-unread-and-unselected ?s)
1017     (?V gnus-version ?s)
1018     (?U gnus-tmp-unread-and-unticked ?d)
1019     (?S gnus-tmp-subject ?s)
1020     (?e gnus-tmp-unselected ?d)
1021     (?u gnus-tmp-user-defined ?s)
1022     (?d (length gnus-newsgroup-dormant) ?d)
1023     (?t (length gnus-newsgroup-marked) ?d)
1024     (?r (length gnus-newsgroup-reads) ?d)
1025     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1026     (?E gnus-newsgroup-expunged-tally ?d)
1027     (?s (gnus-current-score-file-nondirectory) ?s)))
1028
1029 (defvar gnus-last-search-regexp nil
1030   "Default regexp for article search command.")
1031
1032 (defvar gnus-last-shell-command nil
1033   "Default shell command on article.")
1034
1035 (defvar gnus-newsgroup-begin nil)
1036 (defvar gnus-newsgroup-end nil)
1037 (defvar gnus-newsgroup-last-rmail nil)
1038 (defvar gnus-newsgroup-last-mail nil)
1039 (defvar gnus-newsgroup-last-folder nil)
1040 (defvar gnus-newsgroup-last-file nil)
1041 (defvar gnus-newsgroup-auto-expire nil)
1042 (defvar gnus-newsgroup-active nil)
1043
1044 (defvar gnus-newsgroup-data nil)
1045 (defvar gnus-newsgroup-data-reverse nil)
1046 (defvar gnus-newsgroup-limit nil)
1047 (defvar gnus-newsgroup-limits nil)
1048
1049 (defvar gnus-newsgroup-unreads nil
1050   "List of unread articles in the current newsgroup.")
1051
1052 (defvar gnus-newsgroup-unselected nil
1053   "List of unselected unread articles in the current newsgroup.")
1054
1055 (defvar gnus-newsgroup-reads nil
1056   "Alist of read articles and article marks in the current newsgroup.")
1057
1058 (defvar gnus-newsgroup-expunged-tally nil)
1059
1060 (defvar gnus-newsgroup-marked nil
1061   "List of ticked articles in the current newsgroup (a subset of unread art).")
1062
1063 (defvar gnus-newsgroup-killed nil
1064   "List of ranges of articles that have been through the scoring process.")
1065
1066 (defvar gnus-newsgroup-cached nil
1067   "List of articles that come from the article cache.")
1068
1069 (defvar gnus-newsgroup-saved nil
1070   "List of articles that have been saved.")
1071
1072 (defvar gnus-newsgroup-kill-headers nil)
1073
1074 (defvar gnus-newsgroup-replied nil
1075   "List of articles that have been replied to in the current newsgroup.")
1076
1077 (defvar gnus-newsgroup-expirable nil
1078   "List of articles in the current newsgroup that can be expired.")
1079
1080 (defvar gnus-newsgroup-processable nil
1081   "List of articles in the current newsgroup that can be processed.")
1082
1083 (defvar gnus-newsgroup-downloadable nil
1084   "List of articles in the current newsgroup that can be processed.")
1085
1086 (defvar gnus-newsgroup-undownloaded nil
1087   "List of articles in the current newsgroup that haven't been downloaded..")
1088
1089 (defvar gnus-newsgroup-unsendable nil
1090   "List of articles in the current newsgroup that won't be sent.")
1091
1092 (defvar gnus-newsgroup-bookmarks nil
1093   "List of articles in the current newsgroup that have bookmarks.")
1094
1095 (defvar gnus-newsgroup-dormant nil
1096   "List of dormant articles in the current newsgroup.")
1097
1098 (defvar gnus-newsgroup-scored nil
1099   "List of scored articles in the current newsgroup.")
1100
1101 (defvar gnus-newsgroup-headers nil
1102   "List of article headers in the current newsgroup.")
1103
1104 (defvar gnus-newsgroup-threads nil)
1105
1106 (defvar gnus-newsgroup-prepared nil
1107   "Whether the current group has been prepared properly.")
1108
1109 (defvar gnus-newsgroup-ancient nil
1110   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1111
1112 (defvar gnus-newsgroup-sparse nil)
1113
1114 (defvar gnus-current-article nil)
1115 (defvar gnus-article-current nil)
1116 (defvar gnus-current-headers nil)
1117 (defvar gnus-have-all-headers nil)
1118 (defvar gnus-last-article nil)
1119 (defvar gnus-newsgroup-history nil)
1120 (defvar gnus-newsgroup-charset nil)
1121 (defvar gnus-newsgroup-ephemeral-charset nil)
1122 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1123
1124 (defvar gnus-article-before-search nil)
1125
1126 (defconst gnus-summary-local-variables
1127   '(gnus-newsgroup-name
1128     gnus-newsgroup-begin gnus-newsgroup-end
1129     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1130     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1131     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1132     gnus-newsgroup-unselected gnus-newsgroup-marked
1133     gnus-newsgroup-reads gnus-newsgroup-saved
1134     gnus-newsgroup-replied gnus-newsgroup-expirable
1135     gnus-newsgroup-processable gnus-newsgroup-killed
1136     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1137     gnus-newsgroup-unsendable
1138     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1139     gnus-newsgroup-headers gnus-newsgroup-threads
1140     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1141     gnus-current-article gnus-current-headers gnus-have-all-headers
1142     gnus-last-article gnus-article-internal-prepare-hook
1143     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1144     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1145     gnus-thread-expunge-below
1146     gnus-score-alist gnus-current-score-file
1147     (gnus-summary-expunge-below . global)
1148     (gnus-summary-mark-below . global)
1149     (gnus-orphan-score . global)
1150     gnus-newsgroup-active gnus-scores-exclude-files
1151     gnus-newsgroup-history gnus-newsgroup-ancient
1152     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1153     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1154     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1155     (gnus-newsgroup-expunged-tally . 0)
1156     gnus-cache-removable-articles gnus-newsgroup-cached
1157     gnus-newsgroup-data gnus-newsgroup-data-reverse
1158     gnus-newsgroup-limit gnus-newsgroup-limits
1159     gnus-newsgroup-charset)
1160   "Variables that are buffer-local to the summary buffers.")
1161
1162 (defvar gnus-newsgroup-variables nil
1163   "Variables that have separate values in the newsgroups.")
1164
1165 ;; Byte-compiler warning.
1166 (eval-when-compile (defvar gnus-article-mode-map))
1167
1168 ;; MIME stuff.
1169
1170 (defvar gnus-decode-encoded-word-methods
1171   '(mail-decode-encoded-word-string)
1172   "List of methods used to decode encoded words.
1173
1174 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1175 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1176 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1177 whose names match REGEXP.
1178
1179 For example:
1180 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1181  mail-decode-encoded-word-string
1182  (\"chinese\" . rfc1843-decode-string))")
1183
1184 (defvar gnus-decode-encoded-word-methods-cache nil)
1185
1186 (defun gnus-multi-decode-encoded-word-string (string)
1187   "Apply the functions from `gnus-encoded-word-methods' that match."
1188   (unless (and gnus-decode-encoded-word-methods-cache
1189                (eq gnus-newsgroup-name
1190                    (car gnus-decode-encoded-word-methods-cache)))
1191     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1192     (mapcar (lambda (x)
1193               (if (symbolp x)
1194                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1195                 (if (and gnus-newsgroup-name
1196                          (string-match (car x) gnus-newsgroup-name))
1197                     (nconc gnus-decode-encoded-word-methods-cache
1198                            (list (cdr x))))))
1199           gnus-decode-encoded-word-methods))
1200   (let ((xlist gnus-decode-encoded-word-methods-cache))
1201     (pop xlist)
1202     (while xlist
1203       (setq string (funcall (pop xlist) string))))
1204   string)
1205
1206 ;; Subject simplification.
1207
1208 (defun gnus-simplify-whitespace (str)
1209   "Remove excessive whitespace from STR."
1210   (let ((mystr str))
1211     ;; Multiple spaces.
1212     (while (string-match "[ \t][ \t]+" mystr)
1213       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1214                           " "
1215                           (substring mystr (match-end 0)))))
1216     ;; Leading spaces.
1217     (when (string-match "^[ \t]+" mystr)
1218       (setq mystr (substring mystr (match-end 0))))
1219     ;; Trailing spaces.
1220     (when (string-match "[ \t]+$" mystr)
1221       (setq mystr (substring mystr 0 (match-beginning 0))))
1222     mystr))
1223
1224 (defsubst gnus-simplify-subject-re (subject)
1225   "Remove \"Re:\" from subject lines."
1226   (if (string-match "^[Rr][Ee]: *" subject)
1227       (substring subject (match-end 0))
1228     subject))
1229
1230 (defun gnus-simplify-subject (subject &optional re-only)
1231   "Remove `Re:' and words in parentheses.
1232 If RE-ONLY is non-nil, strip leading `Re:'s only."
1233   (let ((case-fold-search t))           ;Ignore case.
1234     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1235     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1236       (setq subject (substring subject (match-end 0))))
1237     ;; Remove uninteresting prefixes.
1238     (when (and (not re-only)
1239                gnus-simplify-ignored-prefixes
1240                (string-match gnus-simplify-ignored-prefixes subject))
1241       (setq subject (substring subject (match-end 0))))
1242     ;; Remove words in parentheses from end.
1243     (unless re-only
1244       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1245         (setq subject (substring subject 0 (match-beginning 0)))))
1246     ;; Return subject string.
1247     subject))
1248
1249 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1250 ;; all whitespace.
1251 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1252   (goto-char (point-min))
1253   (while (re-search-forward regexp nil t)
1254     (replace-match (or newtext ""))))
1255
1256 (defun gnus-simplify-buffer-fuzzy ()
1257   "Simplify string in the buffer fuzzily.
1258 The string in the accessible portion of the current buffer is simplified.
1259 It is assumed to be a single-line subject.
1260 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1261 matter is removed.  Additional things can be deleted by setting
1262 `gnus-simplify-subject-fuzzy-regexp'."
1263   (let ((case-fold-search t)
1264         (modified-tick))
1265     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1266
1267     (while (not (eq modified-tick (buffer-modified-tick)))
1268       (setq modified-tick (buffer-modified-tick))
1269       (cond
1270        ((listp gnus-simplify-subject-fuzzy-regexp)
1271         (mapcar 'gnus-simplify-buffer-fuzzy-step
1272                 gnus-simplify-subject-fuzzy-regexp))
1273        (gnus-simplify-subject-fuzzy-regexp
1274         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1275       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1276       (gnus-simplify-buffer-fuzzy-step
1277        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1278       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1279
1280     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1281     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1282     (gnus-simplify-buffer-fuzzy-step " $")
1283     (gnus-simplify-buffer-fuzzy-step "^ +")))
1284
1285 (defun gnus-simplify-subject-fuzzy (subject)
1286   "Simplify a subject string fuzzily.
1287 See `gnus-simplify-buffer-fuzzy' for details."
1288   (save-excursion
1289     (gnus-set-work-buffer)
1290     (let ((case-fold-search t))
1291       ;; Remove uninteresting prefixes.
1292       (when (and gnus-simplify-ignored-prefixes
1293                  (string-match gnus-simplify-ignored-prefixes subject))
1294         (setq subject (substring subject (match-end 0))))
1295       (insert subject)
1296       (inline (gnus-simplify-buffer-fuzzy))
1297       (buffer-string))))
1298
1299 (defsubst gnus-simplify-subject-fully (subject)
1300   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1301   (cond
1302    (gnus-simplify-subject-functions
1303     (gnus-map-function gnus-simplify-subject-functions subject))
1304    ((null gnus-summary-gather-subject-limit)
1305     (gnus-simplify-subject-re subject))
1306    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1307     (gnus-simplify-subject-fuzzy subject))
1308    ((numberp gnus-summary-gather-subject-limit)
1309     (gnus-limit-string (gnus-simplify-subject-re subject)
1310                        gnus-summary-gather-subject-limit))
1311    (t
1312     subject)))
1313
1314 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1315   "Check whether two subjects are equal.
1316 If optional argument simple-first is t, first argument is already
1317 simplified."
1318   (cond
1319    ((null simple-first)
1320     (equal (gnus-simplify-subject-fully s1)
1321            (gnus-simplify-subject-fully s2)))
1322    (t
1323     (equal s1
1324            (gnus-simplify-subject-fully s2)))))
1325
1326 (defun gnus-summary-bubble-group ()
1327   "Increase the score of the current group.
1328 This is a handy function to add to `gnus-summary-exit-hook' to
1329 increase the score of each group you read."
1330   (gnus-group-add-score gnus-newsgroup-name))
1331
1332 \f
1333 ;;;
1334 ;;; Gnus summary mode
1335 ;;;
1336
1337 (put 'gnus-summary-mode 'mode-class 'special)
1338
1339 (when t
1340   ;; Non-orthogonal keys
1341
1342   (gnus-define-keys gnus-summary-mode-map
1343     " " gnus-summary-next-page
1344     "\177" gnus-summary-prev-page
1345     [delete] gnus-summary-prev-page
1346     [backspace] gnus-summary-prev-page
1347     "\r" gnus-summary-scroll-up
1348     "\M-\r" gnus-summary-scroll-down
1349     "n" gnus-summary-next-unread-article
1350     "p" gnus-summary-prev-unread-article
1351     "N" gnus-summary-next-article
1352     "P" gnus-summary-prev-article
1353     "\M-\C-n" gnus-summary-next-same-subject
1354     "\M-\C-p" gnus-summary-prev-same-subject
1355     "\M-n" gnus-summary-next-unread-subject
1356     "\M-p" gnus-summary-prev-unread-subject
1357     "." gnus-summary-first-unread-article
1358     "," gnus-summary-best-unread-article
1359     "\M-s" gnus-summary-search-article-forward
1360     "\M-r" gnus-summary-search-article-backward
1361     "<" gnus-summary-beginning-of-article
1362     ">" gnus-summary-end-of-article
1363     "j" gnus-summary-goto-article
1364     "^" gnus-summary-refer-parent-article
1365     "\M-^" gnus-summary-refer-article
1366     "u" gnus-summary-tick-article-forward
1367     "!" gnus-summary-tick-article-forward
1368     "U" gnus-summary-tick-article-backward
1369     "d" gnus-summary-mark-as-read-forward
1370     "D" gnus-summary-mark-as-read-backward
1371     "E" gnus-summary-mark-as-expirable
1372     "\M-u" gnus-summary-clear-mark-forward
1373     "\M-U" gnus-summary-clear-mark-backward
1374     "k" gnus-summary-kill-same-subject-and-select
1375     "\C-k" gnus-summary-kill-same-subject
1376     "\M-\C-k" gnus-summary-kill-thread
1377     "\M-\C-l" gnus-summary-lower-thread
1378     "e" gnus-summary-edit-article
1379     "#" gnus-summary-mark-as-processable
1380     "\M-#" gnus-summary-unmark-as-processable
1381     "\M-\C-t" gnus-summary-toggle-threads
1382     "\M-\C-s" gnus-summary-show-thread
1383     "\M-\C-h" gnus-summary-hide-thread
1384     "\M-\C-f" gnus-summary-next-thread
1385     "\M-\C-b" gnus-summary-prev-thread
1386     [(meta down)] gnus-summary-next-thread
1387     [(meta up)] gnus-summary-prev-thread
1388     "\M-\C-u" gnus-summary-up-thread
1389     "\M-\C-d" gnus-summary-down-thread
1390     "&" gnus-summary-execute-command
1391     "c" gnus-summary-catchup-and-exit
1392     "\C-w" gnus-summary-mark-region-as-read
1393     "\C-t" gnus-summary-toggle-truncation
1394     "?" gnus-summary-mark-as-dormant
1395     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1396     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1397     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1398     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1399     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1400     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1401     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1402     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1403     "=" gnus-summary-expand-window
1404     "\C-x\C-s" gnus-summary-reselect-current-group
1405     "\M-g" gnus-summary-rescan-group
1406     "w" gnus-summary-stop-page-breaking
1407     "\C-c\C-r" gnus-summary-caesar-message
1408     "f" gnus-summary-followup
1409     "F" gnus-summary-followup-with-original
1410     "C" gnus-summary-cancel-article
1411     "r" gnus-summary-reply
1412     "R" gnus-summary-reply-with-original
1413     "\C-c\C-f" gnus-summary-mail-forward
1414     "o" gnus-summary-save-article
1415     "\C-o" gnus-summary-save-article-mail
1416     "|" gnus-summary-pipe-output
1417     "\M-k" gnus-summary-edit-local-kill
1418     "\M-K" gnus-summary-edit-global-kill
1419     ;; "V" gnus-version
1420     "\C-c\C-d" gnus-summary-describe-group
1421     "q" gnus-summary-exit
1422     "Q" gnus-summary-exit-no-update
1423     "\C-c\C-i" gnus-info-find-node
1424     gnus-mouse-2 gnus-mouse-pick-article
1425     "m" gnus-summary-mail-other-window
1426     "a" gnus-summary-post-news
1427     "x" gnus-summary-limit-to-unread
1428     "s" gnus-summary-isearch-article
1429     "t" gnus-summary-toggle-header
1430     "g" gnus-summary-show-article
1431     "l" gnus-summary-goto-last-article
1432     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1433     "\C-d" gnus-summary-enter-digest-group
1434     "\M-\C-d" gnus-summary-read-document
1435     "\M-\C-e" gnus-summary-edit-parameters
1436     "\M-\C-a" gnus-summary-customize-parameters
1437     "\C-c\C-b" gnus-bug
1438     "*" gnus-cache-enter-article
1439     "\M-*" gnus-cache-remove-article
1440     "\M-&" gnus-summary-universal-argument
1441     "\C-l" gnus-recenter
1442     "I" gnus-summary-increase-score
1443     "L" gnus-summary-lower-score
1444     "\M-i" gnus-symbolic-argument
1445     "h" gnus-summary-select-article-buffer
1446
1447     "b" gnus-article-view-part
1448     "\M-t" gnus-summary-toggle-display-buttonized
1449
1450     "V" gnus-summary-score-map
1451     "X" gnus-uu-extract-map
1452     "S" gnus-summary-send-map)
1453
1454   ;; Sort of orthogonal keymap
1455   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1456     "t" gnus-summary-tick-article-forward
1457     "!" gnus-summary-tick-article-forward
1458     "d" gnus-summary-mark-as-read-forward
1459     "r" gnus-summary-mark-as-read-forward
1460     "c" gnus-summary-clear-mark-forward
1461     " " gnus-summary-clear-mark-forward
1462     "e" gnus-summary-mark-as-expirable
1463     "x" gnus-summary-mark-as-expirable
1464     "?" gnus-summary-mark-as-dormant
1465     "b" gnus-summary-set-bookmark
1466     "B" gnus-summary-remove-bookmark
1467     "#" gnus-summary-mark-as-processable
1468     "\M-#" gnus-summary-unmark-as-processable
1469     "S" gnus-summary-limit-include-expunged
1470     "C" gnus-summary-catchup
1471     "H" gnus-summary-catchup-to-here
1472     "\C-c" gnus-summary-catchup-all
1473     "k" gnus-summary-kill-same-subject-and-select
1474     "K" gnus-summary-kill-same-subject
1475     "P" gnus-uu-mark-map)
1476
1477   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1478     "c" gnus-summary-clear-above
1479     "u" gnus-summary-tick-above
1480     "m" gnus-summary-mark-above
1481     "k" gnus-summary-kill-below)
1482
1483   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1484     "/" gnus-summary-limit-to-subject
1485     "n" gnus-summary-limit-to-articles
1486     "w" gnus-summary-pop-limit
1487     "s" gnus-summary-limit-to-subject
1488     "a" gnus-summary-limit-to-author
1489     "u" gnus-summary-limit-to-unread
1490     "m" gnus-summary-limit-to-marks
1491     "M" gnus-summary-limit-exclude-marks
1492     "v" gnus-summary-limit-to-score
1493     "*" gnus-summary-limit-include-cached
1494     "D" gnus-summary-limit-include-dormant
1495     "T" gnus-summary-limit-include-thread
1496     "d" gnus-summary-limit-exclude-dormant
1497     "t" gnus-summary-limit-to-age
1498     "x" gnus-summary-limit-to-extra
1499     "E" gnus-summary-limit-include-expunged
1500     "c" gnus-summary-limit-exclude-childless-dormant
1501     "C" gnus-summary-limit-mark-excluded-as-read)
1502
1503   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1504     "n" gnus-summary-next-unread-article
1505     "p" gnus-summary-prev-unread-article
1506     "N" gnus-summary-next-article
1507     "P" gnus-summary-prev-article
1508     "\C-n" gnus-summary-next-same-subject
1509     "\C-p" gnus-summary-prev-same-subject
1510     "\M-n" gnus-summary-next-unread-subject
1511     "\M-p" gnus-summary-prev-unread-subject
1512     "f" gnus-summary-first-unread-article
1513     "b" gnus-summary-best-unread-article
1514     "j" gnus-summary-goto-article
1515     "g" gnus-summary-goto-subject
1516     "l" gnus-summary-goto-last-article
1517     "o" gnus-summary-pop-article)
1518
1519   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1520     "k" gnus-summary-kill-thread
1521     "l" gnus-summary-lower-thread
1522     "i" gnus-summary-raise-thread
1523     "T" gnus-summary-toggle-threads
1524     "t" gnus-summary-rethread-current
1525     "^" gnus-summary-reparent-thread
1526     "s" gnus-summary-show-thread
1527     "S" gnus-summary-show-all-threads
1528     "h" gnus-summary-hide-thread
1529     "H" gnus-summary-hide-all-threads
1530     "n" gnus-summary-next-thread
1531     "p" gnus-summary-prev-thread
1532     "u" gnus-summary-up-thread
1533     "o" gnus-summary-top-thread
1534     "d" gnus-summary-down-thread
1535     "#" gnus-uu-mark-thread
1536     "\M-#" gnus-uu-unmark-thread)
1537
1538   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1539     "g" gnus-summary-prepare
1540     "c" gnus-summary-insert-cached-articles)
1541
1542   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1543     "c" gnus-summary-catchup-and-exit
1544     "C" gnus-summary-catchup-all-and-exit
1545     "E" gnus-summary-exit-no-update
1546     "Q" gnus-summary-exit
1547     "Z" gnus-summary-exit
1548     "n" gnus-summary-catchup-and-goto-next-group
1549     "R" gnus-summary-reselect-current-group
1550     "G" gnus-summary-rescan-group
1551     "N" gnus-summary-next-group
1552     "s" gnus-summary-save-newsrc
1553     "P" gnus-summary-prev-group)
1554
1555   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1556     " " gnus-summary-next-page
1557     "n" gnus-summary-next-page
1558     "\177" gnus-summary-prev-page
1559     [delete] gnus-summary-prev-page
1560     "p" gnus-summary-prev-page
1561     "\r" gnus-summary-scroll-up
1562     "\M-\r" gnus-summary-scroll-down
1563     "<" gnus-summary-beginning-of-article
1564     ">" gnus-summary-end-of-article
1565     "b" gnus-summary-beginning-of-article
1566     "e" gnus-summary-end-of-article
1567     "^" gnus-summary-refer-parent-article
1568     "r" gnus-summary-refer-parent-article
1569     "D" gnus-summary-enter-digest-group
1570     "R" gnus-summary-refer-references
1571     "T" gnus-summary-refer-thread
1572     "g" gnus-summary-show-article
1573     "s" gnus-summary-isearch-article
1574     "P" gnus-summary-print-article
1575     "M" gnus-mailing-list-insinuate
1576     "t" gnus-article-babel)
1577
1578   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1579     "b" gnus-article-add-buttons
1580     "B" gnus-article-add-buttons-to-head
1581     "o" gnus-article-treat-overstrike
1582     "e" gnus-article-emphasize
1583     "w" gnus-article-fill-cited-article
1584     "Q" gnus-article-fill-long-lines
1585     "C" gnus-article-capitalize-sentences
1586     "c" gnus-article-remove-cr
1587     "q" gnus-article-de-quoted-unreadable
1588     "6" gnus-article-de-base64-unreadable
1589     "Z" gnus-article-decode-HZ
1590     "h" gnus-article-wash-html
1591     "s" gnus-summary-force-verify-and-decrypt
1592     "f" gnus-article-display-x-face
1593     "l" gnus-summary-stop-page-breaking
1594     "r" gnus-summary-caesar-message
1595     "t" gnus-summary-toggle-header
1596     "v" gnus-summary-verbose-headers
1597     "H" gnus-article-strip-headers-in-body
1598     "p" gnus-article-verify-x-pgp-sig
1599     "d" gnus-article-treat-dumbquotes)
1600
1601   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1602     "a" gnus-article-hide
1603     "h" gnus-article-hide-headers
1604     "b" gnus-article-hide-boring-headers
1605     "s" gnus-article-hide-signature
1606     "c" gnus-article-hide-citation
1607     "C" gnus-article-hide-citation-in-followups
1608     "l" gnus-article-hide-list-identifiers
1609     "p" gnus-article-hide-pgp
1610     "B" gnus-article-strip-banner
1611     "P" gnus-article-hide-pem
1612     "\C-c" gnus-article-hide-citation-maybe)
1613
1614   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1615     "a" gnus-article-highlight
1616     "h" gnus-article-highlight-headers
1617     "c" gnus-article-highlight-citation
1618     "s" gnus-article-highlight-signature)
1619
1620   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1621     "w" gnus-article-decode-mime-words
1622     "c" gnus-article-decode-charset
1623     "v" gnus-mime-view-all-parts
1624     "b" gnus-article-view-part)
1625
1626   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1627     "z" gnus-article-date-ut
1628     "u" gnus-article-date-ut
1629     "l" gnus-article-date-local
1630     "e" gnus-article-date-lapsed
1631     "o" gnus-article-date-original
1632     "i" gnus-article-date-iso8601
1633     "s" gnus-article-date-user)
1634
1635   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1636     "t" gnus-article-remove-trailing-blank-lines
1637     "l" gnus-article-strip-leading-blank-lines
1638     "m" gnus-article-strip-multiple-blank-lines
1639     "a" gnus-article-strip-blank-lines
1640     "A" gnus-article-strip-all-blank-lines
1641     "s" gnus-article-strip-leading-space
1642     "e" gnus-article-strip-trailing-space)
1643
1644   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1645     "v" gnus-version
1646     "f" gnus-summary-fetch-faq
1647     "d" gnus-summary-describe-group
1648     "h" gnus-summary-describe-briefly
1649     "i" gnus-info-find-node)
1650
1651   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1652     "e" gnus-summary-expire-articles
1653     "\M-\C-e" gnus-summary-expire-articles-now
1654     "\177" gnus-summary-delete-article
1655     [delete] gnus-summary-delete-article
1656     [backspace] gnus-summary-delete-article
1657     "m" gnus-summary-move-article
1658     "r" gnus-summary-respool-article
1659     "w" gnus-summary-edit-article
1660     "c" gnus-summary-copy-article
1661     "B" gnus-summary-crosspost-article
1662     "q" gnus-summary-respool-query
1663     "t" gnus-summary-respool-trace
1664     "i" gnus-summary-import-article
1665     "p" gnus-summary-article-posted-p)
1666
1667   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1668     "o" gnus-summary-save-article
1669     "m" gnus-summary-save-article-mail
1670     "F" gnus-summary-write-article-file
1671     "r" gnus-summary-save-article-rmail
1672     "f" gnus-summary-save-article-file
1673     "b" gnus-summary-save-article-body-file
1674     "h" gnus-summary-save-article-folder
1675     "v" gnus-summary-save-article-vm
1676     "p" gnus-summary-pipe-output
1677     "s" gnus-soup-add-article)
1678
1679   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1680     "b" gnus-summary-display-buttonized
1681     "m" gnus-summary-repair-multipart
1682     "v" gnus-article-view-part
1683     "o" gnus-article-save-part
1684     "c" gnus-article-copy-part
1685     "C" gnus-article-view-part-as-charset
1686     "e" gnus-article-externalize-part
1687     "E" gnus-article-encrypt-body
1688     "i" gnus-article-inline-part
1689     "|" gnus-article-pipe-part))
1690
1691 (defun gnus-summary-make-menu-bar ()
1692   (gnus-turn-off-edit-menu 'summary)
1693
1694   (unless (boundp 'gnus-summary-misc-menu)
1695
1696     (easy-menu-define
1697      gnus-summary-kill-menu gnus-summary-mode-map ""
1698      (cons
1699       "Score"
1700       (nconc
1701        (list
1702         ["Enter score..." gnus-summary-score-entry t]
1703         ["Customize" gnus-score-customize t])
1704        (gnus-make-score-map 'increase)
1705        (gnus-make-score-map 'lower)
1706        '(("Mark"
1707           ["Kill below" gnus-summary-kill-below t]
1708           ["Mark above" gnus-summary-mark-above t]
1709           ["Tick above" gnus-summary-tick-above t]
1710           ["Clear above" gnus-summary-clear-above t])
1711          ["Current score" gnus-summary-current-score t]
1712          ["Set score" gnus-summary-set-score t]
1713          ["Switch current score file..." gnus-score-change-score-file t]
1714          ["Set mark below..." gnus-score-set-mark-below t]
1715          ["Set expunge below..." gnus-score-set-expunge-below t]
1716          ["Edit current score file" gnus-score-edit-current-scores t]
1717          ["Edit score file" gnus-score-edit-file t]
1718          ["Trace score" gnus-score-find-trace t]
1719          ["Find words" gnus-score-find-favourite-words t]
1720          ["Rescore buffer" gnus-summary-rescore t]
1721          ["Increase score..." gnus-summary-increase-score t]
1722          ["Lower score..." gnus-summary-lower-score t]))))
1723
1724     ;; Define both the Article menu in the summary buffer and the equivalent
1725     ;; Commands menu in the article buffer here for consistency.
1726     (let ((innards
1727            '(("Hide"
1728               ["All" gnus-article-hide t]
1729               ["Headers" gnus-article-hide-headers t]
1730               ["Signature" gnus-article-hide-signature t]
1731               ["Citation" gnus-article-hide-citation t]
1732               ["List identifiers" gnus-article-hide-list-identifiers t]
1733               ["PGP" gnus-article-hide-pgp t]
1734               ["Banner" gnus-article-strip-banner t]
1735               ["Boring headers" gnus-article-hide-boring-headers t])
1736              ("Highlight"
1737               ["All" gnus-article-highlight t]
1738               ["Headers" gnus-article-highlight-headers t]
1739               ["Signature" gnus-article-highlight-signature t]
1740               ["Citation" gnus-article-highlight-citation t])
1741              ("MIME"
1742               ["Words" gnus-article-decode-mime-words t]
1743               ["Charset" gnus-article-decode-charset t]
1744               ["QP" gnus-article-de-quoted-unreadable t]
1745               ["Base64" gnus-article-de-base64-unreadable t]
1746               ["View all" gnus-mime-view-all-parts t]
1747               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1748               ["Encrypt body" gnus-article-encrypt-body t])
1749              ("Date"
1750               ["Local" gnus-article-date-local t]
1751               ["ISO8601" gnus-article-date-iso8601 t]
1752               ["UT" gnus-article-date-ut t]
1753               ["Original" gnus-article-date-original t]
1754               ["Lapsed" gnus-article-date-lapsed t]
1755               ["User-defined" gnus-article-date-user t])
1756              ("Washing"
1757               ("Remove Blanks"
1758                ["Leading" gnus-article-strip-leading-blank-lines t]
1759                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1760                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1761                ["All of the above" gnus-article-strip-blank-lines t]
1762                ["All" gnus-article-strip-all-blank-lines t]
1763                ["Leading space" gnus-article-strip-leading-space t]
1764                ["Trailing space" gnus-article-strip-trailing-space t])
1765               ["Overstrike" gnus-article-treat-overstrike t]
1766               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1767               ["Emphasis" gnus-article-emphasize t]
1768               ["Word wrap" gnus-article-fill-cited-article t]
1769               ["Fill long lines" gnus-article-fill-long-lines t]
1770               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1771               ["CR" gnus-article-remove-cr t]
1772               ["Show X-Face" gnus-article-display-x-face t]
1773               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1774               ["Base64" gnus-article-de-base64-unreadable t]
1775               ["Rot 13" gnus-summary-caesar-message
1776                :help "\"Caesar rotate\" article by 13"]
1777               ["Unix pipe" gnus-summary-pipe-message t]
1778               ["Add buttons" gnus-article-add-buttons t]
1779               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1780               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1781               ["Verbose header" gnus-summary-verbose-headers t]
1782               ["Toggle header" gnus-summary-toggle-header t]
1783               ["Html" gnus-article-wash-html t]
1784               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1785               ["HZ" gnus-article-decode-HZ t])
1786              ("Output"
1787               ["Save in default format" gnus-summary-save-article
1788                :help "Save article using default method"]
1789               ["Save in file" gnus-summary-save-article-file
1790                :help "Save article in file"]
1791               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1792               ["Save in MH folder" gnus-summary-save-article-folder t]
1793               ["Save in VM folder" gnus-summary-save-article-vm t]
1794               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1795               ["Save body in file" gnus-summary-save-article-body-file t]
1796               ["Pipe through a filter" gnus-summary-pipe-output t]
1797               ["Add to SOUP packet" gnus-soup-add-article t]
1798               ["Print" gnus-summary-print-article t])
1799              ("Backend"
1800               ["Respool article..." gnus-summary-respool-article t]
1801               ["Move article..." gnus-summary-move-article
1802                (gnus-check-backend-function
1803                 'request-move-article gnus-newsgroup-name)]
1804               ["Copy article..." gnus-summary-copy-article t]
1805               ["Crosspost article..." gnus-summary-crosspost-article
1806                (gnus-check-backend-function
1807                 'request-replace-article gnus-newsgroup-name)]
1808               ["Import file..." gnus-summary-import-article t]
1809               ["Check if posted" gnus-summary-article-posted-p t]
1810               ["Edit article" gnus-summary-edit-article
1811                (not (gnus-group-read-only-p))]
1812               ["Delete article" gnus-summary-delete-article
1813                (gnus-check-backend-function
1814                 'request-expire-articles gnus-newsgroup-name)]
1815               ["Query respool" gnus-summary-respool-query t]
1816               ["Trace respool" gnus-summary-respool-trace t]
1817               ["Delete expirable articles" gnus-summary-expire-articles-now
1818                (gnus-check-backend-function
1819                 'request-expire-articles gnus-newsgroup-name)])
1820              ("Extract"
1821               ["Uudecode" gnus-uu-decode-uu
1822                :help "Decode uuencoded article(s)"]
1823               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1824               ["Unshar" gnus-uu-decode-unshar t]
1825               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1826               ["Save" gnus-uu-decode-save t]
1827               ["Binhex" gnus-uu-decode-binhex t]
1828               ["Postscript" gnus-uu-decode-postscript t])
1829              ("Cache"
1830               ["Enter article" gnus-cache-enter-article t]
1831               ["Remove article" gnus-cache-remove-article t])
1832              ["Translate" gnus-article-babel t]
1833              ["Select article buffer" gnus-summary-select-article-buffer t]
1834              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1835              ["Isearch article..." gnus-summary-isearch-article t]
1836              ["Beginning of the article" gnus-summary-beginning-of-article t]
1837              ["End of the article" gnus-summary-end-of-article t]
1838              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1839              ["Fetch referenced articles" gnus-summary-refer-references t]
1840              ["Fetch current thread" gnus-summary-refer-thread t]
1841              ["Fetch article with id..." gnus-summary-refer-article t]
1842              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1843              ["Redisplay" gnus-summary-show-article t])))
1844       (easy-menu-define
1845        gnus-summary-article-menu gnus-summary-mode-map ""
1846        (cons "Article" innards))
1847
1848       (easy-menu-define
1849        gnus-article-commands-menu gnus-article-mode-map ""
1850        (cons "Commands" innards)))
1851
1852     (easy-menu-define
1853      gnus-summary-thread-menu gnus-summary-mode-map ""
1854      '("Threads"
1855        ["Toggle threading" gnus-summary-toggle-threads t]
1856        ["Hide threads" gnus-summary-hide-all-threads t]
1857        ["Show threads" gnus-summary-show-all-threads t]
1858        ["Hide thread" gnus-summary-hide-thread t]
1859        ["Show thread" gnus-summary-show-thread t]
1860        ["Go to next thread" gnus-summary-next-thread t]
1861        ["Go to previous thread" gnus-summary-prev-thread t]
1862        ["Go down thread" gnus-summary-down-thread t]
1863        ["Go up thread" gnus-summary-up-thread t]
1864        ["Top of thread" gnus-summary-top-thread t]
1865        ["Mark thread as read" gnus-summary-kill-thread t]
1866        ["Lower thread score" gnus-summary-lower-thread t]
1867        ["Raise thread score" gnus-summary-raise-thread t]
1868        ["Rethread current" gnus-summary-rethread-current t]))
1869
1870     (easy-menu-define
1871      gnus-summary-post-menu gnus-summary-mode-map ""
1872      '("Post"
1873        ["Post an article" gnus-summary-post-news
1874         :help "Post an article"]
1875        ["Followup" gnus-summary-followup
1876         :help "Post followup to this article"]
1877        ["Followup and yank" gnus-summary-followup-with-original
1878         :help "Post followup to this article, quoting its contents"]
1879        ["Supersede article" gnus-summary-supersede-article t]
1880        ["Cancel article" gnus-summary-cancel-article
1881         :help "Cancel an article you posted"]
1882        ["Reply" gnus-summary-reply t]
1883        ["Reply and yank" gnus-summary-reply-with-original t]
1884        ["Wide reply" gnus-summary-wide-reply t]
1885        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1886         :help "Mail a reply, quoting this article"]
1887        ["Mail forward" gnus-summary-mail-forward t]
1888        ["Post forward" gnus-summary-post-forward t]
1889        ["Digest and mail" gnus-uu-digest-mail-forward t]
1890        ["Digest and post" gnus-uu-digest-post-forward t]
1891        ["Resend message" gnus-summary-resend-message t]
1892        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1893        ["Send a mail" gnus-summary-mail-other-window t]
1894        ["Uuencode and post" gnus-uu-post-news
1895         :help "Post a uuencoded article"]
1896        ["Followup via news" gnus-summary-followup-to-mail t]
1897        ["Followup via news and yank"
1898         gnus-summary-followup-to-mail-with-original t]
1899        ;;("Draft"
1900        ;;["Send" gnus-summary-send-draft t]
1901        ;;["Send bounced" gnus-resend-bounced-mail t])
1902        ))
1903
1904     (easy-menu-define
1905      gnus-summary-misc-menu gnus-summary-mode-map ""
1906      '("Misc"
1907        ("Mark Read"
1908         ["Mark as read" gnus-summary-mark-as-read-forward t]
1909         ["Mark same subject and select"
1910          gnus-summary-kill-same-subject-and-select t]
1911         ["Mark same subject" gnus-summary-kill-same-subject t]
1912         ["Catchup" gnus-summary-catchup
1913          :help "Mark unread articles in this group as read"]
1914         ["Catchup all" gnus-summary-catchup-all t]
1915         ["Catchup to here" gnus-summary-catchup-to-here t]
1916         ["Catchup region" gnus-summary-mark-region-as-read t]
1917         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1918        ("Mark Various"
1919         ["Tick" gnus-summary-tick-article-forward t]
1920         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1921         ["Remove marks" gnus-summary-clear-mark-forward t]
1922         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1923         ["Set bookmark" gnus-summary-set-bookmark t]
1924         ["Remove bookmark" gnus-summary-remove-bookmark t])
1925        ("Mark Limit"
1926         ["Marks..." gnus-summary-limit-to-marks t]
1927         ["Subject..." gnus-summary-limit-to-subject t]
1928         ["Author..." gnus-summary-limit-to-author t]
1929         ["Age..." gnus-summary-limit-to-age t]
1930         ["Extra..." gnus-summary-limit-to-extra t]
1931         ["Score" gnus-summary-limit-to-score t]
1932         ["Unread" gnus-summary-limit-to-unread t]
1933         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1934         ["Articles" gnus-summary-limit-to-articles t]
1935         ["Pop limit" gnus-summary-pop-limit t]
1936         ["Show dormant" gnus-summary-limit-include-dormant t]
1937         ["Hide childless dormant"
1938          gnus-summary-limit-exclude-childless-dormant t]
1939         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1940         ["Hide marked" gnus-summary-limit-exclude-marks t]
1941         ["Show expunged" gnus-summary-show-all-expunged t])
1942        ("Process Mark"
1943         ["Set mark" gnus-summary-mark-as-processable t]
1944         ["Remove mark" gnus-summary-unmark-as-processable t]
1945         ["Remove all marks" gnus-summary-unmark-all-processable t]
1946         ["Mark above" gnus-uu-mark-over t]
1947         ["Mark series" gnus-uu-mark-series t]
1948         ["Mark region" gnus-uu-mark-region t]
1949         ["Unmark region" gnus-uu-unmark-region t]
1950         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1951         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1952         ["Mark all" gnus-uu-mark-all t]
1953         ["Mark buffer" gnus-uu-mark-buffer t]
1954         ["Mark sparse" gnus-uu-mark-sparse t]
1955         ["Mark thread" gnus-uu-mark-thread t]
1956         ["Unmark thread" gnus-uu-unmark-thread t]
1957         ("Process Mark Sets"
1958          ["Kill" gnus-summary-kill-process-mark t]
1959          ["Yank" gnus-summary-yank-process-mark
1960           gnus-newsgroup-process-stack]
1961          ["Save" gnus-summary-save-process-mark t]))
1962        ("Scroll article"
1963         ["Page forward" gnus-summary-next-page
1964          :help "Show next page of article"]
1965         ["Page backward" gnus-summary-prev-page
1966          :help "Show previous page of article"]
1967         ["Line forward" gnus-summary-scroll-up t])
1968        ("Move"
1969         ["Next unread article" gnus-summary-next-unread-article t]
1970         ["Previous unread article" gnus-summary-prev-unread-article t]
1971         ["Next article" gnus-summary-next-article t]
1972         ["Previous article" gnus-summary-prev-article t]
1973         ["Next unread subject" gnus-summary-next-unread-subject t]
1974         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1975         ["Next article same subject" gnus-summary-next-same-subject t]
1976         ["Previous article same subject" gnus-summary-prev-same-subject t]
1977         ["First unread article" gnus-summary-first-unread-article t]
1978         ["Best unread article" gnus-summary-best-unread-article t]
1979         ["Go to subject number..." gnus-summary-goto-subject t]
1980         ["Go to article number..." gnus-summary-goto-article t]
1981         ["Go to the last article" gnus-summary-goto-last-article t]
1982         ["Pop article off history" gnus-summary-pop-article t])
1983        ("Sort"
1984         ["Sort by number" gnus-summary-sort-by-number t]
1985         ["Sort by author" gnus-summary-sort-by-author t]
1986         ["Sort by subject" gnus-summary-sort-by-subject t]
1987         ["Sort by date" gnus-summary-sort-by-date t]
1988         ["Sort by score" gnus-summary-sort-by-score t]
1989         ["Sort by lines" gnus-summary-sort-by-lines t]
1990         ["Sort by characters" gnus-summary-sort-by-chars t])
1991        ("Help"
1992         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1993         ["Describe group" gnus-summary-describe-group t]
1994         ["Read manual" gnus-info-find-node t])
1995        ("Modes"
1996         ["Pick and read" gnus-pick-mode t]
1997         ["Binary" gnus-binary-mode t])
1998        ("Regeneration"
1999         ["Regenerate" gnus-summary-prepare t]
2000         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2001         ["Toggle threading" gnus-summary-toggle-threads t])
2002        ["Filter articles..." gnus-summary-execute-command t]
2003        ["Run command on subjects..." gnus-summary-universal-argument t]
2004        ["Search articles forward..." gnus-summary-search-article-forward t]
2005        ["Search articles backward..." gnus-summary-search-article-backward t]
2006        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2007        ["Expand window" gnus-summary-expand-window t]
2008        ["Expire expirable articles" gnus-summary-expire-articles
2009         (gnus-check-backend-function
2010          'request-expire-articles gnus-newsgroup-name)]
2011        ["Edit local kill file" gnus-summary-edit-local-kill t]
2012        ["Edit main kill file" gnus-summary-edit-global-kill t]
2013        ["Edit group parameters" gnus-summary-edit-parameters t]
2014        ["Customize group parameters" gnus-summary-customize-parameters t]
2015        ["Send a bug report" gnus-bug t]
2016        ("Exit"
2017         ["Catchup and exit" gnus-summary-catchup-and-exit
2018          :help "Mark unread articles in this group as read, then exit"]
2019         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2020         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2021         ["Exit group" gnus-summary-exit
2022          :help "Exit current group, return to group selection mode"]
2023         ["Exit group without updating" gnus-summary-exit-no-update t]
2024         ["Exit and goto next group" gnus-summary-next-group t]
2025         ["Exit and goto prev group" gnus-summary-prev-group t]
2026         ["Reselect group" gnus-summary-reselect-current-group t]
2027         ["Rescan group" gnus-summary-rescan-group t]
2028         ["Update dribble" gnus-summary-save-newsrc t])))
2029
2030     (gnus-run-hooks 'gnus-summary-menu-hook)))
2031
2032 (defvar gnus-summary-tool-bar-map nil)
2033
2034 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2035 (defun gnus-summary-make-tool-bar ()
2036   (if (and (fboundp 'tool-bar-add-item-from-menu)
2037            (default-value 'tool-bar-mode)
2038            (not gnus-summary-tool-bar-map))
2039       (setq gnus-summary-tool-bar-map
2040             (let ((tool-bar-map (make-sparse-keymap)))
2041               (tool-bar-add-item-from-menu
2042                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2043               (tool-bar-add-item-from-menu
2044                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2045               (tool-bar-add-item-from-menu
2046                'gnus-summary-post-news "post" gnus-summary-mode-map)
2047               (tool-bar-add-item-from-menu
2048                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2049               (tool-bar-add-item-from-menu
2050                'gnus-summary-followup "followup" gnus-summary-mode-map)
2051               (tool-bar-add-item-from-menu
2052                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2053               (tool-bar-add-item-from-menu
2054                'gnus-summary-reply "reply" gnus-summary-mode-map)
2055               (tool-bar-add-item-from-menu
2056                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2057               (tool-bar-add-item-from-menu
2058                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2059               (tool-bar-add-item-from-menu
2060                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2061               (tool-bar-add-item-from-menu
2062                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2063               (tool-bar-add-item-from-menu
2064                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2065               (tool-bar-add-item-from-menu
2066                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2067               (tool-bar-add-item-from-menu
2068                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2069               (tool-bar-add-item-from-menu
2070                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2071               tool-bar-map)))
2072   (if gnus-summary-tool-bar-map
2073       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2074
2075 (defun gnus-score-set-default (var value)
2076   "A version of set that updates the GNU Emacs menu-bar."
2077   (set var value)
2078   ;; It is the message that forces the active status to be updated.
2079   (message ""))
2080
2081 (defun gnus-make-score-map (type)
2082   "Make a summary score map of type TYPE."
2083   (if t
2084       nil
2085     (let ((headers '(("author" "from" string)
2086                      ("subject" "subject" string)
2087                      ("article body" "body" string)
2088                      ("article head" "head" string)
2089                      ("xref" "xref" string)
2090                      ("extra header" "extra" string)
2091                      ("lines" "lines" number)
2092                      ("followups to author" "followup" string)))
2093           (types '((number ("less than" <)
2094                            ("greater than" >)
2095                            ("equal" =))
2096                    (string ("substring" s)
2097                            ("exact string" e)
2098                            ("fuzzy string" f)
2099                            ("regexp" r))))
2100           (perms '(("temporary" (current-time-string))
2101                    ("permanent" nil)
2102                    ("immediate" now)))
2103           header)
2104       (list
2105        (apply
2106         'nconc
2107         (list
2108          (if (eq type 'lower)
2109              "Lower score"
2110            "Increase score"))
2111         (let (outh)
2112           (while headers
2113             (setq header (car headers))
2114             (setq outh
2115                   (cons
2116                    (apply
2117                     'nconc
2118                     (list (car header))
2119                     (let ((ts (cdr (assoc (nth 2 header) types)))
2120                           outt)
2121                       (while ts
2122                         (setq outt
2123                               (cons
2124                                (apply
2125                                 'nconc
2126                                 (list (caar ts))
2127                                 (let ((ps perms)
2128                                       outp)
2129                                   (while ps
2130                                     (setq outp
2131                                           (cons
2132                                            (vector
2133                                             (caar ps)
2134                                             (list
2135                                              'gnus-summary-score-entry
2136                                              (nth 1 header)
2137                                              (if (or (string= (nth 1 header)
2138                                                               "head")
2139                                                      (string= (nth 1 header)
2140                                                               "body"))
2141                                                  ""
2142                                                (list 'gnus-summary-header
2143                                                      (nth 1 header)))
2144                                              (list 'quote (nth 1 (car ts)))
2145                                              (list 'gnus-score-delta-default
2146                                                    nil)
2147                                              (nth 1 (car ps))
2148                                              t)
2149                                             t)
2150                                            outp))
2151                                     (setq ps (cdr ps)))
2152                                   (list (nreverse outp))))
2153                                outt))
2154                         (setq ts (cdr ts)))
2155                       (list (nreverse outt))))
2156                    outh))
2157             (setq headers (cdr headers)))
2158           (list (nreverse outh))))))))
2159
2160 \f
2161
2162 (defun gnus-summary-mode (&optional group)
2163   "Major mode for reading articles.
2164
2165 All normal editing commands are switched off.
2166 \\<gnus-summary-mode-map>
2167 Each line in this buffer represents one article.  To read an
2168 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2169 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2170 respectively.
2171
2172 You can also post articles and send mail from this buffer.  To
2173 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2174 of an article, type `\\[gnus-summary-reply]'.
2175
2176 There are approx. one gazillion commands you can execute in this
2177 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2178
2179 The following commands are available:
2180
2181 \\{gnus-summary-mode-map}"
2182   (interactive)
2183   (kill-all-local-variables)
2184   (when (gnus-visual-p 'summary-menu 'menu)
2185     (gnus-summary-make-menu-bar)
2186     (gnus-summary-make-tool-bar))
2187   (gnus-summary-make-local-variables)
2188   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2189     (gnus-summary-make-local-variables))
2190   (gnus-make-thread-indent-array)
2191   (gnus-simplify-mode-line)
2192   (setq major-mode 'gnus-summary-mode)
2193   (setq mode-name "Summary")
2194   (make-local-variable 'minor-mode-alist)
2195   (use-local-map gnus-summary-mode-map)
2196   (buffer-disable-undo)
2197   (setq buffer-read-only t)             ;Disable modification
2198   (setq truncate-lines t)
2199   (setq selective-display t)
2200   (setq selective-display-ellipses t)   ;Display `...'
2201   (gnus-summary-set-display-table)
2202   (gnus-set-default-directory)
2203   (setq gnus-newsgroup-name group)
2204   (make-local-variable 'gnus-summary-line-format)
2205   (make-local-variable 'gnus-summary-line-format-spec)
2206   (make-local-variable 'gnus-summary-dummy-line-format)
2207   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2208   (make-local-variable 'gnus-summary-mark-positions)
2209   (make-local-hook 'pre-command-hook)
2210   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2211   (gnus-run-hooks 'gnus-summary-mode-hook)
2212   (mm-enable-multibyte-mule4)
2213   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2214   (gnus-update-summary-mark-positions))
2215
2216 (defun gnus-summary-make-local-variables ()
2217   "Make all the local summary buffer variables."
2218   (let (global)
2219     (dolist (local gnus-summary-local-variables)
2220       (if (consp local)
2221           (progn
2222             (if (eq (cdr local) 'global)
2223                 ;; Copy the global value of the variable.
2224                 (setq global (symbol-value (car local)))
2225               ;; Use the value from the list.
2226               (setq global (eval (cdr local))))
2227             (set (make-local-variable (car local)) global))
2228         ;; Simple nil-valued local variable.
2229         (set (make-local-variable local) nil)))))
2230
2231 (defun gnus-summary-clear-local-variables ()
2232   (let ((locals gnus-summary-local-variables))
2233     (while locals
2234       (if (consp (car locals))
2235           (and (vectorp (caar locals))
2236                (set (caar locals) nil))
2237         (and (vectorp (car locals))
2238              (set (car locals) nil)))
2239       (setq locals (cdr locals)))))
2240
2241 ;; Summary data functions.
2242
2243 (defmacro gnus-data-number (data)
2244   `(car ,data))
2245
2246 (defmacro gnus-data-set-number (data number)
2247   `(setcar ,data ,number))
2248
2249 (defmacro gnus-data-mark (data)
2250   `(nth 1 ,data))
2251
2252 (defmacro gnus-data-set-mark (data mark)
2253   `(setcar (nthcdr 1 ,data) ,mark))
2254
2255 (defmacro gnus-data-pos (data)
2256   `(nth 2 ,data))
2257
2258 (defmacro gnus-data-set-pos (data pos)
2259   `(setcar (nthcdr 2 ,data) ,pos))
2260
2261 (defmacro gnus-data-header (data)
2262   `(nth 3 ,data))
2263
2264 (defmacro gnus-data-set-header (data header)
2265   `(setf (nth 3 ,data) ,header))
2266
2267 (defmacro gnus-data-level (data)
2268   `(nth 4 ,data))
2269
2270 (defmacro gnus-data-unread-p (data)
2271   `(= (nth 1 ,data) gnus-unread-mark))
2272
2273 (defmacro gnus-data-read-p (data)
2274   `(/= (nth 1 ,data) gnus-unread-mark))
2275
2276 (defmacro gnus-data-pseudo-p (data)
2277   `(consp (nth 3 ,data)))
2278
2279 (defmacro gnus-data-find (number)
2280   `(assq ,number gnus-newsgroup-data))
2281
2282 (defmacro gnus-data-find-list (number &optional data)
2283   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2284      (memq (assq ,number bdata)
2285            bdata)))
2286
2287 (defmacro gnus-data-make (number mark pos header level)
2288   `(list ,number ,mark ,pos ,header ,level))
2289
2290 (defun gnus-data-enter (after-article number mark pos header level offset)
2291   (let ((data (gnus-data-find-list after-article)))
2292     (unless data
2293       (error "No such article: %d" after-article))
2294     (setcdr data (cons (gnus-data-make number mark pos header level)
2295                        (cdr data)))
2296     (setq gnus-newsgroup-data-reverse nil)
2297     (gnus-data-update-list (cddr data) offset)))
2298
2299 (defun gnus-data-enter-list (after-article list &optional offset)
2300   (when list
2301     (let ((data (and after-article (gnus-data-find-list after-article)))
2302           (ilist list))
2303       (if (not (or data
2304                    after-article))
2305           (let ((odata gnus-newsgroup-data))
2306             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2307             (when offset
2308               (gnus-data-update-list odata offset)))
2309         ;; Find the last element in the list to be spliced into the main
2310         ;; list.
2311         (while (cdr list)
2312           (setq list (cdr list)))
2313         (if (not data)
2314             (progn
2315               (setcdr list gnus-newsgroup-data)
2316               (setq gnus-newsgroup-data ilist)
2317               (when offset
2318                 (gnus-data-update-list (cdr list) offset)))
2319           (setcdr list (cdr data))
2320           (setcdr data ilist)
2321           (when offset
2322             (gnus-data-update-list (cdr list) offset))))
2323       (setq gnus-newsgroup-data-reverse nil))))
2324
2325 (defun gnus-data-remove (article &optional offset)
2326   (let ((data gnus-newsgroup-data))
2327     (if (= (gnus-data-number (car data)) article)
2328         (progn
2329           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2330                 gnus-newsgroup-data-reverse nil)
2331           (when offset
2332             (gnus-data-update-list gnus-newsgroup-data offset)))
2333       (while (cdr data)
2334         (when (= (gnus-data-number (cadr data)) article)
2335           (setcdr data (cddr data))
2336           (when offset
2337             (gnus-data-update-list (cdr data) offset))
2338           (setq data nil
2339                 gnus-newsgroup-data-reverse nil))
2340         (setq data (cdr data))))))
2341
2342 (defmacro gnus-data-list (backward)
2343   `(if ,backward
2344        (or gnus-newsgroup-data-reverse
2345            (setq gnus-newsgroup-data-reverse
2346                  (reverse gnus-newsgroup-data)))
2347      gnus-newsgroup-data))
2348
2349 (defun gnus-data-update-list (data offset)
2350   "Add OFFSET to the POS of all data entries in DATA."
2351   (setq gnus-newsgroup-data-reverse nil)
2352   (while data
2353     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2354     (setq data (cdr data))))
2355
2356 (defun gnus-summary-article-pseudo-p (article)
2357   "Say whether this article is a pseudo article or not."
2358   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2359
2360 (defmacro gnus-summary-article-sparse-p (article)
2361   "Say whether this article is a sparse article or not."
2362   `(memq ,article gnus-newsgroup-sparse))
2363
2364 (defmacro gnus-summary-article-ancient-p (article)
2365   "Say whether this article is a sparse article or not."
2366   `(memq ,article gnus-newsgroup-ancient))
2367
2368 (defun gnus-article-parent-p (number)
2369   "Say whether this article is a parent or not."
2370   (let ((data (gnus-data-find-list number)))
2371     (and (cdr data)                     ; There has to be an article after...
2372          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2373             (gnus-data-level (nth 1 data))))))
2374
2375 (defun gnus-article-children (number)
2376   "Return a list of all children to NUMBER."
2377   (let* ((data (gnus-data-find-list number))
2378          (level (gnus-data-level (car data)))
2379          children)
2380     (setq data (cdr data))
2381     (while (and data
2382                 (= (gnus-data-level (car data)) (1+ level)))
2383       (push (gnus-data-number (car data)) children)
2384       (setq data (cdr data)))
2385     children))
2386
2387 (defmacro gnus-summary-skip-intangible ()
2388   "If the current article is intangible, then jump to a different article."
2389   '(let ((to (get-text-property (point) 'gnus-intangible)))
2390      (and to (gnus-summary-goto-subject to))))
2391
2392 (defmacro gnus-summary-article-intangible-p ()
2393   "Say whether this article is intangible or not."
2394   '(get-text-property (point) 'gnus-intangible))
2395
2396 (defun gnus-article-read-p (article)
2397   "Say whether ARTICLE is read or not."
2398   (not (or (memq article gnus-newsgroup-marked)
2399            (memq article gnus-newsgroup-unreads)
2400            (memq article gnus-newsgroup-unselected)
2401            (memq article gnus-newsgroup-dormant))))
2402
2403 ;; Some summary mode macros.
2404
2405 (defmacro gnus-summary-article-number ()
2406   "The article number of the article on the current line.
2407 If there isn's an article number here, then we return the current
2408 article number."
2409   '(progn
2410      (gnus-summary-skip-intangible)
2411      (or (get-text-property (point) 'gnus-number)
2412          (gnus-summary-last-subject))))
2413
2414 (defmacro gnus-summary-article-header (&optional number)
2415   "Return the header of article NUMBER."
2416   `(gnus-data-header (gnus-data-find
2417                       ,(or number '(gnus-summary-article-number)))))
2418
2419 (defmacro gnus-summary-thread-level (&optional number)
2420   "Return the level of thread that starts with article NUMBER."
2421   `(if (and (eq gnus-summary-make-false-root 'dummy)
2422             (get-text-property (point) 'gnus-intangible))
2423        0
2424      (gnus-data-level (gnus-data-find
2425                        ,(or number '(gnus-summary-article-number))))))
2426
2427 (defmacro gnus-summary-article-mark (&optional number)
2428   "Return the mark of article NUMBER."
2429   `(gnus-data-mark (gnus-data-find
2430                     ,(or number '(gnus-summary-article-number)))))
2431
2432 (defmacro gnus-summary-article-pos (&optional number)
2433   "Return the position of the line of article NUMBER."
2434   `(gnus-data-pos (gnus-data-find
2435                    ,(or number '(gnus-summary-article-number)))))
2436
2437 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2438 (defmacro gnus-summary-article-subject (&optional number)
2439   "Return current subject string or nil if nothing."
2440   `(let ((headers
2441           ,(if number
2442                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2443              '(gnus-data-header (assq (gnus-summary-article-number)
2444                                       gnus-newsgroup-data)))))
2445      (and headers
2446           (vectorp headers)
2447           (mail-header-subject headers))))
2448
2449 (defmacro gnus-summary-article-score (&optional number)
2450   "Return current article score."
2451   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2452                   gnus-newsgroup-scored))
2453        gnus-summary-default-score 0))
2454
2455 (defun gnus-summary-article-children (&optional number)
2456   "Return a list of article numbers that are children of article NUMBER."
2457   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2458          (level (gnus-data-level (car data)))
2459          l children)
2460     (while (and (setq data (cdr data))
2461                 (> (setq l (gnus-data-level (car data))) level))
2462       (and (= (1+ level) l)
2463            (push (gnus-data-number (car data))
2464                  children)))
2465     (nreverse children)))
2466
2467 (defun gnus-summary-article-parent (&optional number)
2468   "Return the article number of the parent of article NUMBER."
2469   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2470                                     (gnus-data-list t)))
2471          (level (gnus-data-level (car data))))
2472     (if (zerop level)
2473         ()                              ; This is a root.
2474       ;; We search until we find an article with a level less than
2475       ;; this one.  That function has to be the parent.
2476       (while (and (setq data (cdr data))
2477                   (not (< (gnus-data-level (car data)) level))))
2478       (and data (gnus-data-number (car data))))))
2479
2480 (defun gnus-unread-mark-p (mark)
2481   "Say whether MARK is the unread mark."
2482   (= mark gnus-unread-mark))
2483
2484 (defun gnus-read-mark-p (mark)
2485   "Say whether MARK is one of the marks that mark as read.
2486 This is all marks except unread, ticked, dormant, and expirable."
2487   (not (or (= mark gnus-unread-mark)
2488            (= mark gnus-ticked-mark)
2489            (= mark gnus-dormant-mark)
2490            (= mark gnus-expirable-mark))))
2491
2492 (defmacro gnus-article-mark (number)
2493   "Return the MARK of article NUMBER.
2494 This macro should only be used when computing the mark the \"first\"
2495 time; i.e., when generating the summary lines.  After that,
2496 `gnus-summary-article-mark' should be used to examine the
2497 marks of articles."
2498   `(cond
2499     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2500     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2501     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2502     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2503     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2504     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2505     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2506     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2507            gnus-ancient-mark))))
2508
2509 ;; Saving hidden threads.
2510
2511 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2512 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2513
2514 (defmacro gnus-save-hidden-threads (&rest forms)
2515   "Save hidden threads, eval FORMS, and restore the hidden threads."
2516   (let ((config (make-symbol "config")))
2517     `(let ((,config (gnus-hidden-threads-configuration)))
2518        (unwind-protect
2519            (save-excursion
2520              ,@forms)
2521          (gnus-restore-hidden-threads-configuration ,config)))))
2522
2523 (defun gnus-data-compute-positions ()
2524   "Compute the positions of all articles."
2525   (setq gnus-newsgroup-data-reverse nil)
2526   (let ((data gnus-newsgroup-data))
2527     (save-excursion
2528       (gnus-save-hidden-threads
2529         (gnus-summary-show-all-threads)
2530         (goto-char (point-min))
2531         (while data
2532           (while (get-text-property (point) 'gnus-intangible)
2533             (forward-line 1))
2534           (gnus-data-set-pos (car data) (+ (point) 3))
2535           (setq data (cdr data))
2536           (forward-line 1))))))
2537
2538 (defun gnus-hidden-threads-configuration ()
2539   "Return the current hidden threads configuration."
2540   (save-excursion
2541     (let (config)
2542       (goto-char (point-min))
2543       (while (search-forward "\r" nil t)
2544         (push (1- (point)) config))
2545       config)))
2546
2547 (defun gnus-restore-hidden-threads-configuration (config)
2548   "Restore hidden threads configuration from CONFIG."
2549   (save-excursion
2550     (let (point buffer-read-only)
2551       (while (setq point (pop config))
2552         (when (and (< point (point-max))
2553                    (goto-char point)
2554                    (eq (char-after) ?\n))
2555           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2556
2557 ;; Various summary mode internalish functions.
2558
2559 (defun gnus-mouse-pick-article (e)
2560   (interactive "e")
2561   (mouse-set-point e)
2562   (gnus-summary-next-page nil t))
2563
2564 (defun gnus-summary-set-display-table ()
2565   "Change the display table.
2566 Odd characters have a tendency to mess
2567 up nicely formatted displays - we make all possible glyphs
2568 display only a single character."
2569
2570   ;; We start from the standard display table, if any.
2571   (let ((table (or (copy-sequence standard-display-table)
2572                    (make-display-table)))
2573         (i 32))
2574     ;; Nix out all the control chars...
2575     (while (>= (setq i (1- i)) 0)
2576       (aset table i [??]))
2577     ;; ... but not newline and cr, of course.  (cr is necessary for the
2578     ;; selective display).
2579     (aset table ?\n nil)
2580     (aset table ?\r nil)
2581     ;; We keep TAB as well.
2582     (aset table ?\t nil)
2583     ;; We nix out any glyphs over 126 that are not set already.
2584     (let ((i 256))
2585       (while (>= (setq i (1- i)) 127)
2586         ;; Only modify if the entry is nil.
2587         (unless (aref table i)
2588           (aset table i [??]))))
2589     (setq buffer-display-table table)))
2590
2591 (defun gnus-summary-setup-buffer (group)
2592   "Initialize summary buffer."
2593   (let ((buffer (concat "*Summary " group "*")))
2594     (if (get-buffer buffer)
2595         (progn
2596           (set-buffer buffer)
2597           (setq gnus-summary-buffer (current-buffer))
2598           (not gnus-newsgroup-prepared))
2599       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2600       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2601       (gnus-summary-mode group)
2602       (when gnus-carpal
2603         (gnus-carpal-setup-buffer 'summary))
2604       (unless gnus-single-article-buffer
2605         (make-local-variable 'gnus-article-buffer)
2606         (make-local-variable 'gnus-article-current)
2607         (make-local-variable 'gnus-original-article-buffer))
2608       (setq gnus-newsgroup-name group)
2609       t)))
2610
2611 (defun gnus-set-global-variables ()
2612   "Set the global equivalents of the buffer-local variables.
2613 They are set to the latest values they had.  These reflect the summary
2614 buffer that was in action when the last article was fetched."
2615   (when (eq major-mode 'gnus-summary-mode)
2616     (setq gnus-summary-buffer (current-buffer))
2617     (let ((name gnus-newsgroup-name)
2618           (marked gnus-newsgroup-marked)
2619           (unread gnus-newsgroup-unreads)
2620           (headers gnus-current-headers)
2621           (data gnus-newsgroup-data)
2622           (summary gnus-summary-buffer)
2623           (article-buffer gnus-article-buffer)
2624           (original gnus-original-article-buffer)
2625           (gac gnus-article-current)
2626           (reffed gnus-reffed-article-number)
2627           (score-file gnus-current-score-file)
2628           (default-charset gnus-newsgroup-charset)
2629           vlist)
2630       (let ((locals gnus-newsgroup-variables))
2631         (while locals
2632           (if (consp (car locals))
2633               (push (eval (caar locals)) vlist)
2634             (push (eval (car locals)) vlist))
2635           (setq locals (cdr locals)))
2636         (setq vlist (nreverse vlist)))
2637       (save-excursion
2638         (set-buffer gnus-group-buffer)
2639         (setq gnus-newsgroup-name name
2640               gnus-newsgroup-marked marked
2641               gnus-newsgroup-unreads unread
2642               gnus-current-headers headers
2643               gnus-newsgroup-data data
2644               gnus-article-current gac
2645               gnus-summary-buffer summary
2646               gnus-article-buffer article-buffer
2647               gnus-original-article-buffer original
2648               gnus-reffed-article-number reffed
2649               gnus-current-score-file score-file
2650               gnus-newsgroup-charset default-charset)
2651         (let ((locals gnus-newsgroup-variables))
2652           (while locals
2653             (if (consp (car locals))
2654                 (set (caar locals) (pop vlist))
2655               (set (car locals) (pop vlist)))
2656             (setq locals (cdr locals))))
2657         ;; The article buffer also has local variables.
2658         (when (gnus-buffer-live-p gnus-article-buffer)
2659           (set-buffer gnus-article-buffer)
2660           (setq gnus-summary-buffer summary))))))
2661
2662 (defun gnus-summary-article-unread-p (article)
2663   "Say whether ARTICLE is unread or not."
2664   (memq article gnus-newsgroup-unreads))
2665
2666 (defun gnus-summary-first-article-p (&optional article)
2667   "Return whether ARTICLE is the first article in the buffer."
2668   (if (not (setq article (or article (gnus-summary-article-number))))
2669       nil
2670     (eq article (caar gnus-newsgroup-data))))
2671
2672 (defun gnus-summary-last-article-p (&optional article)
2673   "Return whether ARTICLE is the last article in the buffer."
2674   (if (not (setq article (or article (gnus-summary-article-number))))
2675       ;; All non-existent numbers are the last article.  :-)
2676       t
2677     (not (cdr (gnus-data-find-list article)))))
2678
2679 (defun gnus-make-thread-indent-array ()
2680   (let ((n 200))
2681     (unless (and gnus-thread-indent-array
2682                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2683       (setq gnus-thread-indent-array (make-vector 201 "")
2684             gnus-thread-indent-array-level gnus-thread-indent-level)
2685       (while (>= n 0)
2686         (aset gnus-thread-indent-array n
2687               (make-string (* n gnus-thread-indent-level) ? ))
2688         (setq n (1- n))))))
2689
2690 (defun gnus-update-summary-mark-positions ()
2691   "Compute where the summary marks are to go."
2692   (save-excursion
2693     (when (gnus-buffer-exists-p gnus-summary-buffer)
2694       (set-buffer gnus-summary-buffer))
2695     (let ((gnus-replied-mark 129)
2696           (gnus-score-below-mark 130)
2697           (gnus-score-over-mark 130)
2698           (gnus-download-mark 131)
2699           (spec gnus-summary-line-format-spec)
2700           gnus-visual pos)
2701       (save-excursion
2702         (gnus-set-work-buffer)
2703         (let ((gnus-summary-line-format-spec spec)
2704               (gnus-newsgroup-downloadable '((0 . t))))
2705           (gnus-summary-insert-line
2706            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2707           (goto-char (point-min))
2708           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2709                                              (- (point) 2)))))
2710           (goto-char (point-min))
2711           (push (cons 'replied (and (search-forward "\201" nil t)
2712                                     (- (point) 2)))
2713                 pos)
2714           (goto-char (point-min))
2715           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2716                 pos)
2717           (goto-char (point-min))
2718           (push (cons 'download
2719                       (and (search-forward "\203" nil t) (- (point) 2)))
2720                 pos)))
2721       (setq gnus-summary-mark-positions pos))))
2722
2723 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2724   "Insert a dummy root in the summary buffer."
2725   (beginning-of-line)
2726   (gnus-add-text-properties
2727    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2728    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2729
2730 (defun gnus-summary-from-or-to-or-newsgroups (header)
2731   (let ((to (cdr (assq 'To (mail-header-extra header))))
2732         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2733         (mail-parse-charset gnus-newsgroup-charset)
2734         (mail-parse-ignored-charsets
2735          (save-excursion (set-buffer gnus-summary-buffer)
2736                          gnus-newsgroup-ignored-charsets)))
2737     (cond
2738      ((and to
2739            gnus-ignored-from-addresses
2740            (string-match gnus-ignored-from-addresses
2741                          (mail-header-from header)))
2742       (concat "-> "
2743               (or (car (funcall gnus-extract-address-components
2744                                 (funcall
2745                                  gnus-decode-encoded-word-function to)))
2746                   (funcall gnus-decode-encoded-word-function to))))
2747      ((and newsgroups
2748            gnus-ignored-from-addresses
2749            (string-match gnus-ignored-from-addresses
2750                          (mail-header-from header)))
2751       (concat "=> " newsgroups))
2752      (t
2753       (or (car (funcall gnus-extract-address-components
2754                         (mail-header-from header)))
2755           (mail-header-from header))))))
2756
2757 (defun gnus-summary-insert-line (gnus-tmp-header
2758                                  gnus-tmp-level gnus-tmp-current
2759                                  gnus-tmp-unread gnus-tmp-replied
2760                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2761                                  &optional gnus-tmp-dummy gnus-tmp-score
2762                                  gnus-tmp-process)
2763   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2764          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2765          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2766          (gnus-tmp-score-char
2767           (if (or (null gnus-summary-default-score)
2768                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2769                       gnus-summary-zcore-fuzz))
2770               ?  ;Whitespace
2771             (if (< gnus-tmp-score gnus-summary-default-score)
2772                 gnus-score-below-mark gnus-score-over-mark)))
2773          (gnus-tmp-replied
2774           (cond (gnus-tmp-process gnus-process-mark)
2775                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2776                  gnus-cached-mark)
2777                 (gnus-tmp-replied gnus-replied-mark)
2778                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2779                  gnus-saved-mark)
2780                 (t gnus-unread-mark)))
2781          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2782          (gnus-tmp-name
2783           (cond
2784            ((string-match "<[^>]+> *$" gnus-tmp-from)
2785             (let ((beg (match-beginning 0)))
2786               (or (and (string-match "^\".+\"" gnus-tmp-from)
2787                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2788                   (substring gnus-tmp-from 0 beg))))
2789            ((string-match "(.+)" gnus-tmp-from)
2790             (substring gnus-tmp-from
2791                        (1+ (match-beginning 0)) (1- (match-end 0))))
2792            (t gnus-tmp-from)))
2793          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2794          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2795          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2796          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2797          (buffer-read-only nil))
2798     (when (string= gnus-tmp-name "")
2799       (setq gnus-tmp-name gnus-tmp-from))
2800     (unless (numberp gnus-tmp-lines)
2801       (setq gnus-tmp-lines 0))
2802     (gnus-put-text-property
2803      (point)
2804      (progn (eval gnus-summary-line-format-spec) (point))
2805      'gnus-number gnus-tmp-number)
2806     (when (gnus-visual-p 'summary-highlight 'highlight)
2807       (forward-line -1)
2808       (gnus-run-hooks 'gnus-summary-update-hook)
2809       (forward-line 1))))
2810
2811 (defun gnus-summary-update-line (&optional dont-update)
2812   "Update summary line after change."
2813   (when (and gnus-summary-default-score
2814              (not gnus-summary-inhibit-highlight))
2815     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2816            (article (gnus-summary-article-number))
2817            (score (gnus-summary-article-score article)))
2818       (unless dont-update
2819         (if (and gnus-summary-mark-below
2820                  (< (gnus-summary-article-score)
2821                     gnus-summary-mark-below))
2822             ;; This article has a low score, so we mark it as read.
2823             (when (memq article gnus-newsgroup-unreads)
2824               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2825           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2826             ;; This article was previously marked as read on account
2827             ;; of a low score, but now it has risen, so we mark it as
2828             ;; unread.
2829             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2830         (gnus-summary-update-mark
2831          (if (or (null gnus-summary-default-score)
2832                  (<= (abs (- score gnus-summary-default-score))
2833                      gnus-summary-zcore-fuzz))
2834              ?  ;Whitespace
2835            (if (< score gnus-summary-default-score)
2836                gnus-score-below-mark gnus-score-over-mark))
2837          'score))
2838       ;; Do visual highlighting.
2839       (when (gnus-visual-p 'summary-highlight 'highlight)
2840         (gnus-run-hooks 'gnus-summary-update-hook)))))
2841
2842 (defvar gnus-tmp-new-adopts nil)
2843
2844 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2845   "Return the number of articles in THREAD.
2846 This may be 0 in some cases -- if none of the articles in
2847 the thread are to be displayed."
2848   (let* ((number
2849           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2850           (cond
2851            ((not (listp thread))
2852             1)
2853            ((and (consp thread) (cdr thread))
2854             (apply
2855              '+ 1 (mapcar
2856                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2857            ((null thread)
2858             1)
2859            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2860             1)
2861            (t 0))))
2862     (when (and level (zerop level) gnus-tmp-new-adopts)
2863       (incf number
2864             (apply '+ (mapcar
2865                        'gnus-summary-number-of-articles-in-thread
2866                        gnus-tmp-new-adopts))))
2867     (if char
2868         (if (> number 1) gnus-not-empty-thread-mark
2869           gnus-empty-thread-mark)
2870       number)))
2871
2872 (defun gnus-summary-set-local-parameters (group)
2873   "Go through the local params of GROUP and set all variable specs in that list."
2874   (let ((params (gnus-group-find-parameter group))
2875         elem)
2876     (while params
2877       (setq elem (car params)
2878             params (cdr params))
2879       (and (consp elem)                 ; Has to be a cons.
2880            (consp (cdr elem))           ; The cdr has to be a list.
2881            (symbolp (car elem))         ; Has to be a symbol in there.
2882            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2883            (ignore-errors               ; So we set it.
2884              (make-local-variable (car elem))
2885              (set (car elem) (eval (nth 1 elem))))))))
2886
2887 (defun gnus-summary-read-group (group &optional show-all no-article
2888                                       kill-buffer no-display backward
2889                                       select-articles)
2890   "Start reading news in newsgroup GROUP.
2891 If SHOW-ALL is non-nil, already read articles are also listed.
2892 If NO-ARTICLE is non-nil, no article is selected initially.
2893 If NO-DISPLAY, don't generate a summary buffer."
2894   (let (result)
2895     (while (and group
2896                 (null (setq result
2897                             (let ((gnus-auto-select-next nil))
2898                               (or (gnus-summary-read-group-1
2899                                    group show-all no-article
2900                                    kill-buffer no-display
2901                                    select-articles)
2902                                   (setq show-all nil
2903                                         select-articles nil)))))
2904                 (eq gnus-auto-select-next 'quietly))
2905       (set-buffer gnus-group-buffer)
2906       ;; The entry function called above goes to the next
2907       ;; group automatically, so we go two groups back
2908       ;; if we are searching for the previous group.
2909       (when backward
2910         (gnus-group-prev-unread-group 2))
2911       (if (not (equal group (gnus-group-group-name)))
2912           (setq group (gnus-group-group-name))
2913         (setq group nil)))
2914     result))
2915
2916 (defun gnus-summary-read-group-1 (group show-all no-article
2917                                         kill-buffer no-display
2918                                         &optional select-articles)
2919   ;; Killed foreign groups can't be entered.
2920   (when (and (not (gnus-group-native-p group))
2921              (not (gnus-gethash group gnus-newsrc-hashtb)))
2922     (error "Dead non-native groups can't be entered"))
2923   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2924   (let* ((new-group (gnus-summary-setup-buffer group))
2925          (quit-config (gnus-group-quit-config group))
2926          (did-select (and new-group (gnus-select-newsgroup
2927                                      group show-all select-articles))))
2928     (cond
2929      ;; This summary buffer exists already, so we just select it.
2930      ((not new-group)
2931       (gnus-set-global-variables)
2932       (when kill-buffer
2933         (gnus-kill-or-deaden-summary kill-buffer))
2934       (gnus-configure-windows 'summary 'force)
2935       (gnus-set-mode-line 'summary)
2936       (gnus-summary-position-point)
2937       (message "")
2938       t)
2939      ;; We couldn't select this group.
2940      ((null did-select)
2941       (when (and (eq major-mode 'gnus-summary-mode)
2942                  (not (equal (current-buffer) kill-buffer)))
2943         (kill-buffer (current-buffer))
2944         (if (not quit-config)
2945             (progn
2946               ;; Update the info -- marks might need to be removed,
2947               ;; for instance.
2948               (gnus-summary-update-info)
2949               (set-buffer gnus-group-buffer)
2950               (gnus-group-jump-to-group group)
2951               (gnus-group-next-unread-group 1))
2952           (gnus-handle-ephemeral-exit quit-config)))
2953       (gnus-message 3 "Can't select group")
2954       nil)
2955      ;; The user did a `C-g' while prompting for number of articles,
2956      ;; so we exit this group.
2957      ((eq did-select 'quit)
2958       (and (eq major-mode 'gnus-summary-mode)
2959            (not (equal (current-buffer) kill-buffer))
2960            (kill-buffer (current-buffer)))
2961       (when kill-buffer
2962         (gnus-kill-or-deaden-summary kill-buffer))
2963       (if (not quit-config)
2964           (progn
2965             (set-buffer gnus-group-buffer)
2966             (gnus-group-jump-to-group group)
2967             (gnus-group-next-unread-group 1)
2968             (gnus-configure-windows 'group 'force))
2969         (gnus-handle-ephemeral-exit quit-config))
2970       ;; Finally signal the quit.
2971       (signal 'quit nil))
2972      ;; The group was successfully selected.
2973      (t
2974       (gnus-set-global-variables)
2975       ;; Save the active value in effect when the group was entered.
2976       (setq gnus-newsgroup-active
2977             (gnus-copy-sequence
2978              (gnus-active gnus-newsgroup-name)))
2979       ;; You can change the summary buffer in some way with this hook.
2980       (gnus-run-hooks 'gnus-select-group-hook)
2981       ;; Set any local variables in the group parameters.
2982       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2983       (gnus-update-format-specifications
2984        nil 'summary 'summary-mode 'summary-dummy)
2985       (gnus-update-summary-mark-positions)
2986       ;; Do score processing.
2987       (when gnus-use-scoring
2988         (gnus-possibly-score-headers))
2989       ;; Check whether to fill in the gaps in the threads.
2990       (when gnus-build-sparse-threads
2991         (gnus-build-sparse-threads))
2992       ;; Find the initial limit.
2993       (if gnus-show-threads
2994           (if show-all
2995               (let ((gnus-newsgroup-dormant nil))
2996                 (gnus-summary-initial-limit show-all))
2997             (gnus-summary-initial-limit show-all))
2998         ;; When untreaded, all articles are always shown.
2999         (setq gnus-newsgroup-limit
3000               (mapcar
3001                (lambda (header) (mail-header-number header))
3002                gnus-newsgroup-headers)))
3003       ;; Generate the summary buffer.
3004       (unless no-display
3005         (gnus-summary-prepare))
3006       (when gnus-use-trees
3007         (gnus-tree-open group)
3008         (setq gnus-summary-highlight-line-function
3009               'gnus-tree-highlight-article))
3010       ;; If the summary buffer is empty, but there are some low-scored
3011       ;; articles or some excluded dormants, we include these in the
3012       ;; buffer.
3013       (when (and (zerop (buffer-size))
3014                  (not no-display))
3015         (cond (gnus-newsgroup-dormant
3016                (gnus-summary-limit-include-dormant))
3017               ((and gnus-newsgroup-scored show-all)
3018                (gnus-summary-limit-include-expunged t))))
3019       ;; Function `gnus-apply-kill-file' must be called in this hook.
3020       (gnus-run-hooks 'gnus-apply-kill-hook)
3021       (if (and (zerop (buffer-size))
3022                (not no-display))
3023           (progn
3024             ;; This newsgroup is empty.
3025             (gnus-summary-catchup-and-exit nil t)
3026             (gnus-message 6 "No unread news")
3027             (when kill-buffer
3028               (gnus-kill-or-deaden-summary kill-buffer))
3029             ;; Return nil from this function.
3030             nil)
3031         ;; Hide conversation thread subtrees.  We cannot do this in
3032         ;; gnus-summary-prepare-hook since kill processing may not
3033         ;; work with hidden articles.
3034         (and gnus-show-threads
3035              gnus-thread-hide-subtree
3036              (gnus-summary-hide-all-threads))
3037         (when kill-buffer
3038           (gnus-kill-or-deaden-summary kill-buffer))
3039         ;; Show first unread article if requested.
3040         (if (and (not no-article)
3041                  (not no-display)
3042                  gnus-newsgroup-unreads
3043                  gnus-auto-select-first)
3044             (progn
3045               (gnus-configure-windows 'summary)
3046               (cond
3047                ((eq gnus-auto-select-first 'best)
3048                 (gnus-summary-best-unread-article))
3049                ((eq gnus-auto-select-first t)
3050                 (gnus-summary-first-unread-article))
3051                ((gnus-functionp gnus-auto-select-first)
3052                 (funcall gnus-auto-select-first))))
3053           ;; Don't select any articles, just move point to the first
3054           ;; article in the group.
3055           (goto-char (point-min))
3056           (gnus-summary-position-point)
3057           (gnus-configure-windows 'summary 'force)
3058           (gnus-set-mode-line 'summary))
3059         (when (get-buffer-window gnus-group-buffer t)
3060           ;; Gotta use windows, because recenter does weird stuff if
3061           ;; the current buffer ain't the displayed window.
3062           (let ((owin (selected-window)))
3063             (select-window (get-buffer-window gnus-group-buffer t))
3064             (when (gnus-group-goto-group group)
3065               (recenter))
3066             (select-window owin)))
3067         ;; Mark this buffer as "prepared".
3068         (setq gnus-newsgroup-prepared t)
3069         (gnus-run-hooks 'gnus-summary-prepared-hook)
3070         t)))))
3071
3072 (defun gnus-summary-prepare ()
3073   "Generate the summary buffer."
3074   (interactive)
3075   (let ((buffer-read-only nil))
3076     (erase-buffer)
3077     (setq gnus-newsgroup-data nil
3078           gnus-newsgroup-data-reverse nil)
3079     (gnus-run-hooks 'gnus-summary-generate-hook)
3080     ;; Generate the buffer, either with threads or without.
3081     (when gnus-newsgroup-headers
3082       (gnus-summary-prepare-threads
3083        (if gnus-show-threads
3084            (gnus-sort-gathered-threads
3085             (funcall gnus-summary-thread-gathering-function
3086                      (gnus-sort-threads
3087                       (gnus-cut-threads (gnus-make-threads)))))
3088          ;; Unthreaded display.
3089          (gnus-sort-articles gnus-newsgroup-headers))))
3090     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3091     ;; Call hooks for modifying summary buffer.
3092     (goto-char (point-min))
3093     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3094
3095 (defsubst gnus-general-simplify-subject (subject)
3096   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3097   (setq subject
3098         (cond
3099          ;; Truncate the subject.
3100          (gnus-simplify-subject-functions
3101           (gnus-map-function gnus-simplify-subject-functions subject))
3102          ((numberp gnus-summary-gather-subject-limit)
3103           (setq subject (gnus-simplify-subject-re subject))
3104           (if (> (length subject) gnus-summary-gather-subject-limit)
3105               (substring subject 0 gnus-summary-gather-subject-limit)
3106             subject))
3107          ;; Fuzzily simplify it.
3108          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3109           (gnus-simplify-subject-fuzzy subject))
3110          ;; Just remove the leading "Re:".
3111          (t
3112           (gnus-simplify-subject-re subject))))
3113
3114   (if (and gnus-summary-gather-exclude-subject
3115            (string-match gnus-summary-gather-exclude-subject subject))
3116       nil                               ; This article shouldn't be gathered
3117     subject))
3118
3119 (defun gnus-summary-simplify-subject-query ()
3120   "Query where the respool algorithm would put this article."
3121   (interactive)
3122   (gnus-summary-select-article)
3123   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3124
3125 (defun gnus-gather-threads-by-subject (threads)
3126   "Gather threads by looking at Subject headers."
3127   (if (not gnus-summary-make-false-root)
3128       threads
3129     (let ((hashtb (gnus-make-hashtable 1024))
3130           (prev threads)
3131           (result threads)
3132           subject hthread whole-subject)
3133       (while threads
3134         (setq subject (gnus-general-simplify-subject
3135                        (setq whole-subject (mail-header-subject
3136                                             (caar threads)))))
3137         (when subject
3138           (if (setq hthread (gnus-gethash subject hashtb))
3139               (progn
3140                 ;; We enter a dummy root into the thread, if we
3141                 ;; haven't done that already.
3142                 (unless (stringp (caar hthread))
3143                   (setcar hthread (list whole-subject (car hthread))))
3144                 ;; We add this new gathered thread to this gathered
3145                 ;; thread.
3146                 (setcdr (car hthread)
3147                         (nconc (cdar hthread) (list (car threads))))
3148                 ;; Remove it from the list of threads.
3149                 (setcdr prev (cdr threads))
3150                 (setq threads prev))
3151             ;; Enter this thread into the hash table.
3152             (gnus-sethash subject threads hashtb)))
3153         (setq prev threads)
3154         (setq threads (cdr threads)))
3155       result)))
3156
3157 (defun gnus-gather-threads-by-references (threads)
3158   "Gather threads by looking at References headers."
3159   (let ((idhashtb (gnus-make-hashtable 1024))
3160         (thhashtb (gnus-make-hashtable 1024))
3161         (prev threads)
3162         (result threads)
3163         ids references id gthread gid entered ref)
3164     (while threads
3165       (when (setq references (mail-header-references (caar threads)))
3166         (setq id (mail-header-id (caar threads))
3167               ids (gnus-split-references references)
3168               entered nil)
3169         (while (setq ref (pop ids))
3170           (setq ids (delete ref ids))
3171           (if (not (setq gid (gnus-gethash ref idhashtb)))
3172               (progn
3173                 (gnus-sethash ref id idhashtb)
3174                 (gnus-sethash id threads thhashtb))
3175             (setq gthread (gnus-gethash gid thhashtb))
3176             (unless entered
3177               ;; We enter a dummy root into the thread, if we
3178               ;; haven't done that already.
3179               (unless (stringp (caar gthread))
3180                 (setcar gthread (list (mail-header-subject (caar gthread))
3181                                       (car gthread))))
3182               ;; We add this new gathered thread to this gathered
3183               ;; thread.
3184               (setcdr (car gthread)
3185                       (nconc (cdar gthread) (list (car threads)))))
3186             ;; Add it into the thread hash table.
3187             (gnus-sethash id gthread thhashtb)
3188             (setq entered t)
3189             ;; Remove it from the list of threads.
3190             (setcdr prev (cdr threads))
3191             (setq threads prev))))
3192       (setq prev threads)
3193       (setq threads (cdr threads)))
3194     result))
3195
3196 (defun gnus-sort-gathered-threads (threads)
3197   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3198   (let ((result threads))
3199     (while threads
3200       (when (stringp (caar threads))
3201         (setcdr (car threads)
3202                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3203       (setq threads (cdr threads)))
3204     result))
3205
3206 (defun gnus-thread-loop-p (root thread)
3207   "Say whether ROOT is in THREAD."
3208   (let ((stack (list thread))
3209         (infloop 0)
3210         th)
3211     (while (setq thread (pop stack))
3212       (setq th (cdr thread))
3213       (while (and th
3214                   (not (eq (caar th) root)))
3215         (pop th))
3216       (if th
3217           ;; We have found a loop.
3218           (let (ref-dep)
3219             (setcdr thread (delq (car th) (cdr thread)))
3220             (if (boundp (setq ref-dep (intern "none"
3221                                               gnus-newsgroup-dependencies)))
3222                 (setcdr (symbol-value ref-dep)
3223                         (nconc (cdr (symbol-value ref-dep))
3224                                (list (car th))))
3225               (set ref-dep (list nil (car th))))
3226             (setq infloop 1
3227                   stack nil))
3228         ;; Push all the subthreads onto the stack.
3229         (push (cdr thread) stack)))
3230     infloop))
3231
3232 (defun gnus-make-threads ()
3233   "Go through the dependency hashtb and find the roots.  Return all threads."
3234   (let (threads)
3235     (while (catch 'infloop
3236              (mapatoms
3237               (lambda (refs)
3238                 ;; Deal with self-referencing References loops.
3239                 (when (and (car (symbol-value refs))
3240                            (not (zerop
3241                                  (apply
3242                                   '+
3243                                   (mapcar
3244                                    (lambda (thread)
3245                                      (gnus-thread-loop-p
3246                                       (car (symbol-value refs)) thread))
3247                                    (cdr (symbol-value refs)))))))
3248                   (setq threads nil)
3249                   (throw 'infloop t))
3250                 (unless (car (symbol-value refs))
3251                   ;; These threads do not refer back to any other articles,
3252                   ;; so they're roots.
3253                   (setq threads (append (cdr (symbol-value refs)) threads))))
3254               gnus-newsgroup-dependencies)))
3255     threads))
3256
3257 ;; Build the thread tree.
3258 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3259   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3260
3261 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3262 if it was already present.
3263
3264 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3265 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3266 Message-IDs will be renamed be renamed to a unique Message-ID before
3267 being entered.
3268
3269 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3270   (let* ((id (mail-header-id header))
3271          (id-dep (and id (intern id dependencies)))
3272          ref ref-dep ref-header)
3273     ;; Enter this `header' in the `dependencies' table.
3274     (cond
3275      ((not id-dep)
3276       (setq header nil))
3277      ;; The first two cases do the normal part: enter a new `header'
3278      ;; in the `dependencies' table.
3279      ((not (boundp id-dep))
3280       (set id-dep (list header)))
3281      ((null (car (symbol-value id-dep)))
3282       (setcar (symbol-value id-dep) header))
3283
3284      ;; From here the `header' was already present in the
3285      ;; `dependencies' table.
3286      (force-new
3287       ;; Overrides an existing entry;
3288       ;; just set the header part of the entry.
3289       (setcar (symbol-value id-dep) header))
3290
3291      ;; Renames the existing `header' to a unique Message-ID.
3292      ((not gnus-summary-ignore-duplicates)
3293       ;; An article with this Message-ID has already been seen.
3294       ;; We rename the Message-ID.
3295       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3296            (list header))
3297       (mail-header-set-id header id))
3298
3299      ;; The last case ignores an existing entry, except it adds any
3300      ;; additional Xrefs (in case the two articles came from different
3301      ;; servers.
3302      ;; Also sets `header' to `nil' meaning that the `dependencies'
3303      ;; table was *not* modified.
3304      (t
3305       (mail-header-set-xref
3306        (car (symbol-value id-dep))
3307        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3308                    "")
3309                (or (mail-header-xref header) "")))
3310       (setq header nil)))
3311
3312     (when header
3313       ;; First check if that we are not creating a References loop.
3314       (setq ref (gnus-parent-id (mail-header-references header)))
3315       (while (and ref
3316                   (setq ref-dep (intern-soft ref dependencies))
3317                   (boundp ref-dep)
3318                   (setq ref-header (car (symbol-value ref-dep))))
3319         (if (string= id ref)
3320             ;; Yuk!  This is a reference loop.  Make the article be a
3321             ;; root article.
3322             (progn
3323               (mail-header-set-references (car (symbol-value id-dep)) "none")
3324               (setq ref nil))
3325           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3326       (setq ref (gnus-parent-id (mail-header-references header)))
3327       (setq ref-dep (intern (or ref "none") dependencies))
3328       (if (boundp ref-dep)
3329           (setcdr (symbol-value ref-dep)
3330                   (nconc (cdr (symbol-value ref-dep))
3331                          (list (symbol-value id-dep))))
3332         (set ref-dep (list nil (symbol-value id-dep)))))
3333     header))
3334
3335 (defun gnus-build-sparse-threads ()
3336   (let ((headers gnus-newsgroup-headers)
3337         (mail-parse-charset gnus-newsgroup-charset)
3338         (gnus-summary-ignore-duplicates t)
3339         header references generation relations
3340         subject child end new-child date)
3341     ;; First we create an alist of generations/relations, where
3342     ;; generations is how much we trust the relation, and the relation
3343     ;; is parent/child.
3344     (gnus-message 7 "Making sparse threads...")
3345     (save-excursion
3346       (nnheader-set-temp-buffer " *gnus sparse threads*")
3347       (while (setq header (pop headers))
3348         (when (and (setq references (mail-header-references header))
3349                    (not (string= references "")))
3350           (insert references)
3351           (setq child (mail-header-id header)
3352                 subject (mail-header-subject header)
3353                 date (mail-header-date header)
3354                 generation 0)
3355           (while (search-backward ">" nil t)
3356             (setq end (1+ (point)))
3357             (when (search-backward "<" nil t)
3358               (setq new-child (buffer-substring (point) end))
3359               (push (list (incf generation)
3360                           child (setq child new-child)
3361                           subject date)
3362                     relations)))
3363           (when child
3364             (push (list (1+ generation) child nil subject) relations))
3365           (erase-buffer)))
3366       (kill-buffer (current-buffer)))
3367     ;; Sort over trustworthiness.
3368     (mapcar
3369      (lambda (relation)
3370        (when (gnus-dependencies-add-header
3371               (make-full-mail-header
3372                gnus-reffed-article-number
3373                (nth 3 relation) "" (or (nth 4 relation) "")
3374                (nth 1 relation)
3375                (or (nth 2 relation) "") 0 0 "")
3376               gnus-newsgroup-dependencies nil)
3377          (push gnus-reffed-article-number gnus-newsgroup-limit)
3378          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3379          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3380                gnus-newsgroup-reads)
3381          (decf gnus-reffed-article-number)))
3382      (sort relations 'car-less-than-car))
3383     (gnus-message 7 "Making sparse threads...done")))
3384
3385 (defun gnus-build-old-threads ()
3386   ;; Look at all the articles that refer back to old articles, and
3387   ;; fetch the headers for the articles that aren't there.  This will
3388   ;; build complete threads - if the roots haven't been expired by the
3389   ;; server, that is.
3390   (let ((mail-parse-charset gnus-newsgroup-charset)
3391         id heads)
3392     (mapatoms
3393      (lambda (refs)
3394        (when (not (car (symbol-value refs)))
3395          (setq heads (cdr (symbol-value refs)))
3396          (while heads
3397            (if (memq (mail-header-number (caar heads))
3398                      gnus-newsgroup-dormant)
3399                (setq heads (cdr heads))
3400              (setq id (symbol-name refs))
3401              (while (and (setq id (gnus-build-get-header id))
3402                          (not (car (gnus-id-to-thread id)))))
3403              (setq heads nil)))))
3404      gnus-newsgroup-dependencies)))
3405
3406 ;; This function has to be called with point after the article number
3407 ;; on the beginning of the line.
3408 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3409   (let ((eol (gnus-point-at-eol))
3410         (buffer (current-buffer))
3411         header)
3412
3413     ;; overview: [num subject from date id refs chars lines misc]
3414     (unwind-protect
3415         (progn
3416           (narrow-to-region (point) eol)
3417           (unless (eobp)
3418             (forward-char))
3419
3420           (setq header
3421                 (make-full-mail-header
3422                  number                 ; number
3423                  (funcall gnus-decode-encoded-word-function
3424                           (nnheader-nov-field)) ; subject
3425                  (funcall gnus-decode-encoded-word-function
3426                           (nnheader-nov-field)) ; from
3427                  (nnheader-nov-field)   ; date
3428                  (nnheader-nov-read-message-id) ; id
3429                  (nnheader-nov-field)   ; refs
3430                  (nnheader-nov-read-integer) ; chars
3431                  (nnheader-nov-read-integer) ; lines
3432                  (unless (eobp)
3433                    (if (looking-at "Xref: ")
3434                        (goto-char (match-end 0)))
3435                    (nnheader-nov-field)) ; Xref
3436                  (nnheader-nov-parse-extra)))) ; extra
3437
3438       (widen))
3439
3440     (when gnus-alter-header-function
3441       (funcall gnus-alter-header-function header))
3442     (gnus-dependencies-add-header header dependencies force-new)))
3443
3444 (defun gnus-build-get-header (id)
3445   "Look through the buffer of NOV lines and find the header to ID.
3446 Enter this line into the dependencies hash table, and return
3447 the id of the parent article (if any)."
3448   (let ((deps gnus-newsgroup-dependencies)
3449         found header)
3450     (prog1
3451         (save-excursion
3452           (set-buffer nntp-server-buffer)
3453           (let ((case-fold-search nil))
3454             (goto-char (point-min))
3455             (while (and (not found)
3456                         (search-forward id nil t))
3457               (beginning-of-line)
3458               (setq found (looking-at
3459                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3460                                    (regexp-quote id))))
3461               (or found (beginning-of-line 2)))
3462             (when found
3463               (beginning-of-line)
3464               (and
3465                (setq header (gnus-nov-parse-line
3466                              (read (current-buffer)) deps))
3467                (gnus-parent-id (mail-header-references header))))))
3468       (when header
3469         (let ((number (mail-header-number header)))
3470           (push number gnus-newsgroup-limit)
3471           (push header gnus-newsgroup-headers)
3472           (if (memq number gnus-newsgroup-unselected)
3473               (progn
3474                 (push number gnus-newsgroup-unreads)
3475                 (setq gnus-newsgroup-unselected
3476                       (delq number gnus-newsgroup-unselected)))
3477             (push number gnus-newsgroup-ancient)))))))
3478
3479 (defun gnus-build-all-threads ()
3480   "Read all the headers."
3481   (let ((gnus-summary-ignore-duplicates t)
3482         (mail-parse-charset gnus-newsgroup-charset)
3483         (dependencies gnus-newsgroup-dependencies)
3484         header article)
3485     (save-excursion
3486       (set-buffer nntp-server-buffer)
3487       (let ((case-fold-search nil))
3488         (goto-char (point-min))
3489         (while (not (eobp))
3490           (ignore-errors
3491             (setq article (read (current-buffer))
3492                   header (gnus-nov-parse-line article dependencies)))
3493           (when header
3494             (save-excursion
3495               (set-buffer gnus-summary-buffer)
3496               (push header gnus-newsgroup-headers)
3497               (if (memq (setq article (mail-header-number header))
3498                         gnus-newsgroup-unselected)
3499                   (progn
3500                     (push article gnus-newsgroup-unreads)
3501                     (setq gnus-newsgroup-unselected
3502                           (delq article gnus-newsgroup-unselected)))
3503                 (push article gnus-newsgroup-ancient)))
3504             (forward-line 1)))))))
3505
3506 (defun gnus-summary-update-article-line (article header)
3507   "Update the line for ARTICLE using HEADERS."
3508   (let* ((id (mail-header-id header))
3509          (thread (gnus-id-to-thread id)))
3510     (unless thread
3511       (error "Article in no thread"))
3512     ;; Update the thread.
3513     (setcar thread header)
3514     (gnus-summary-goto-subject article)
3515     (let* ((datal (gnus-data-find-list article))
3516            (data (car datal))
3517            (length (when (cdr datal)
3518                      (- (gnus-data-pos data)
3519                         (gnus-data-pos (cadr datal)))))
3520            (buffer-read-only nil)
3521            (level (gnus-summary-thread-level)))
3522       (gnus-delete-line)
3523       (gnus-summary-insert-line
3524        header level nil (gnus-article-mark article)
3525        (memq article gnus-newsgroup-replied)
3526        (memq article gnus-newsgroup-expirable)
3527        ;; Only insert the Subject string when it's different
3528        ;; from the previous Subject string.
3529        (if (and
3530             gnus-show-threads
3531             (gnus-subject-equal
3532              (condition-case ()
3533                  (mail-header-subject
3534                   (gnus-data-header
3535                    (cadr
3536                     (gnus-data-find-list
3537                      article
3538                      (gnus-data-list t)))))
3539                ;; Error on the side of excessive subjects.
3540                (error ""))
3541              (mail-header-subject header)))
3542            ""
3543          (mail-header-subject header))
3544        nil (cdr (assq article gnus-newsgroup-scored))
3545        (memq article gnus-newsgroup-processable))
3546       (when length
3547         (gnus-data-update-list
3548          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3549
3550 (defun gnus-summary-update-article (article &optional iheader)
3551   "Update ARTICLE in the summary buffer."
3552   (set-buffer gnus-summary-buffer)
3553   (let* ((header (gnus-summary-article-header article))
3554          (id (mail-header-id header))
3555          (data (gnus-data-find article))
3556          (thread (gnus-id-to-thread id))
3557          (references (mail-header-references header))
3558          (parent
3559           (gnus-id-to-thread
3560            (or (gnus-parent-id
3561                 (when (and references
3562                            (not (equal "" references)))
3563                   references))
3564                "none")))
3565          (buffer-read-only nil)
3566          (old (car thread)))
3567     (when thread
3568       (unless iheader
3569         (setcar thread nil)
3570         (when parent
3571           (delq thread parent)))
3572       (if (gnus-summary-insert-subject id header)
3573           ;; Set the (possibly) new article number in the data structure.
3574           (gnus-data-set-number data (gnus-id-to-article id))
3575         (setcar thread old)
3576         nil))))
3577
3578 (defun gnus-rebuild-thread (id &optional line)
3579   "Rebuild the thread containing ID.
3580 If LINE, insert the rebuilt thread starting on line LINE."
3581   (let ((buffer-read-only nil)
3582         old-pos current thread data)
3583     (if (not gnus-show-threads)
3584         (setq thread (list (car (gnus-id-to-thread id))))
3585       ;; Get the thread this article is part of.
3586       (setq thread (gnus-remove-thread id)))
3587     (setq old-pos (gnus-point-at-bol))
3588     (setq current (save-excursion
3589                     (and (zerop (forward-line -1))
3590                          (gnus-summary-article-number))))
3591     ;; If this is a gathered thread, we have to go some re-gathering.
3592     (when (stringp (car thread))
3593       (let ((subject (car thread))
3594             roots thr)
3595         (setq thread (cdr thread))
3596         (while thread
3597           (unless (memq (setq thr (gnus-id-to-thread
3598                                    (gnus-root-id
3599                                     (mail-header-id (caar thread)))))
3600                         roots)
3601             (push thr roots))
3602           (setq thread (cdr thread)))
3603         ;; We now have all (unique) roots.
3604         (if (= (length roots) 1)
3605             ;; All the loose roots are now one solid root.
3606             (setq thread (car roots))
3607           (setq thread (cons subject (gnus-sort-threads roots))))))
3608     (let (threads)
3609       ;; We then insert this thread into the summary buffer.
3610       (when line
3611         (goto-char (point-min))
3612         (forward-line (1- line)))
3613       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3614         (if gnus-show-threads
3615             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3616           (gnus-summary-prepare-unthreaded thread))
3617         (setq data (nreverse gnus-newsgroup-data))
3618         (setq threads gnus-newsgroup-threads))
3619       ;; We splice the new data into the data structure.
3620       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3621       ;;!!! then we want to insert at the beginning of the buffer.
3622       ;;!!! That happens to be true with Gnus now, but that may
3623       ;;!!! change in the future.  Perhaps.
3624       (gnus-data-enter-list
3625        (if line nil current) data (- (point) old-pos))
3626       (setq gnus-newsgroup-threads
3627             (nconc threads gnus-newsgroup-threads))
3628       (gnus-data-compute-positions))))
3629
3630 (defun gnus-number-to-header (number)
3631   "Return the header for article NUMBER."
3632   (let ((headers gnus-newsgroup-headers))
3633     (while (and headers
3634                 (not (= number (mail-header-number (car headers)))))
3635       (pop headers))
3636     (when headers
3637       (car headers))))
3638
3639 (defun gnus-parent-headers (in-headers &optional generation)
3640   "Return the headers of the GENERATIONeth parent of HEADERS."
3641   (unless generation
3642     (setq generation 1))
3643   (let ((parent t)
3644         (headers in-headers)
3645         references)
3646     (while (and parent
3647                 (not (zerop generation))
3648                 (setq references (mail-header-references headers)))
3649       (setq headers (if (and references
3650                              (setq parent (gnus-parent-id references)))
3651                         (car (gnus-id-to-thread parent))
3652                       nil))
3653       (decf generation))
3654     (and (not (eq headers in-headers))
3655          headers)))
3656
3657 (defun gnus-id-to-thread (id)
3658   "Return the (sub-)thread where ID appears."
3659   (gnus-gethash id gnus-newsgroup-dependencies))
3660
3661 (defun gnus-id-to-article (id)
3662   "Return the article number of ID."
3663   (let ((thread (gnus-id-to-thread id)))
3664     (when (and thread
3665                (car thread))
3666       (mail-header-number (car thread)))))
3667
3668 (defun gnus-id-to-header (id)
3669   "Return the article headers of ID."
3670   (car (gnus-id-to-thread id)))
3671
3672 (defun gnus-article-displayed-root-p (article)
3673   "Say whether ARTICLE is a root(ish) article."
3674   (let ((level (gnus-summary-thread-level article))
3675         (refs (mail-header-references  (gnus-summary-article-header article)))
3676         particle)
3677     (cond
3678      ((null level) nil)
3679      ((zerop level) t)
3680      ((null refs) t)
3681      ((null (gnus-parent-id refs)) t)
3682      ((and (= 1 level)
3683            (null (setq particle (gnus-id-to-article
3684                                  (gnus-parent-id refs))))
3685            (null (gnus-summary-thread-level particle)))))))
3686
3687 (defun gnus-root-id (id)
3688   "Return the id of the root of the thread where ID appears."
3689   (let (last-id prev)
3690     (while (and id (setq prev (car (gnus-id-to-thread id))))
3691       (setq last-id id
3692             id (gnus-parent-id (mail-header-references prev))))
3693     last-id))
3694
3695 (defun gnus-articles-in-thread (thread)
3696   "Return the list of articles in THREAD."
3697   (cons (mail-header-number (car thread))
3698         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3699
3700 (defun gnus-remove-thread (id &optional dont-remove)
3701   "Remove the thread that has ID in it."
3702   (let (headers thread last-id)
3703     ;; First go up in this thread until we find the root.
3704     (setq last-id (gnus-root-id id)
3705           headers (message-flatten-list (gnus-id-to-thread last-id)))
3706     ;; We have now found the real root of this thread.  It might have
3707     ;; been gathered into some loose thread, so we have to search
3708     ;; through the threads to find the thread we wanted.
3709     (let ((threads gnus-newsgroup-threads)
3710           sub)
3711       (while threads
3712         (setq sub (car threads))
3713         (if (stringp (car sub))
3714             ;; This is a gathered thread, so we look at the roots
3715             ;; below it to find whether this article is in this
3716             ;; gathered root.
3717             (progn
3718               (setq sub (cdr sub))
3719               (while sub
3720                 (when (member (caar sub) headers)
3721                   (setq thread (car threads)
3722                         threads nil
3723                         sub nil))
3724                 (setq sub (cdr sub))))
3725           ;; It's an ordinary thread, so we check it.
3726           (when (eq (car sub) (car headers))
3727             (setq thread sub
3728                   threads nil)))
3729         (setq threads (cdr threads)))
3730       ;; If this article is in no thread, then it's a root.
3731       (if thread
3732           (unless dont-remove
3733             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3734         (setq thread (gnus-id-to-thread last-id)))
3735       (when thread
3736         (prog1
3737             thread                      ; We return this thread.
3738           (unless dont-remove
3739             (if (stringp (car thread))
3740                 (progn
3741                   ;; If we use dummy roots, then we have to remove the
3742                   ;; dummy root as well.
3743                   (when (eq gnus-summary-make-false-root 'dummy)
3744                     ;; We go to the dummy root by going to
3745                     ;; the first sub-"thread", and then one line up.
3746                     (gnus-summary-goto-article
3747                      (mail-header-number (caadr thread)))
3748                     (forward-line -1)
3749                     (gnus-delete-line)
3750                     (gnus-data-compute-positions))
3751                   (setq thread (cdr thread))
3752                   (while thread
3753                     (gnus-remove-thread-1 (car thread))
3754                     (setq thread (cdr thread))))
3755               (gnus-remove-thread-1 thread))))))))
3756
3757 (defun gnus-remove-thread-1 (thread)
3758   "Remove the thread THREAD recursively."
3759   (let ((number (mail-header-number (pop thread)))
3760         d)
3761     (setq thread (reverse thread))
3762     (while thread
3763       (gnus-remove-thread-1 (pop thread)))
3764     (when (setq d (gnus-data-find number))
3765       (goto-char (gnus-data-pos d))
3766       (gnus-summary-show-thread)
3767       (gnus-data-remove
3768        number
3769        (- (gnus-point-at-bol)
3770           (prog1
3771               (1+ (gnus-point-at-eol))
3772             (gnus-delete-line)))))))
3773
3774 (defun gnus-sort-threads-1 (threads func)
3775   (sort (mapcar (lambda (thread)
3776                   (cons (car thread)
3777                         (and (cdr thread)
3778                              (gnus-sort-threads-1 (cdr thread) func))))
3779                 threads) func))
3780
3781 (defun gnus-sort-threads (threads)
3782   "Sort THREADS."
3783   (if (not gnus-thread-sort-functions)
3784       threads
3785     (gnus-message 8 "Sorting threads...")
3786     (prog1
3787         (gnus-sort-threads-1 
3788          threads 
3789          (gnus-make-sort-function gnus-thread-sort-functions))
3790       (gnus-message 8 "Sorting threads...done"))))
3791
3792 (defun gnus-sort-articles (articles)
3793   "Sort ARTICLES."
3794   (when gnus-article-sort-functions
3795     (gnus-message 7 "Sorting articles...")
3796     (prog1
3797         (setq gnus-newsgroup-headers
3798               (sort articles (gnus-make-sort-function
3799                               gnus-article-sort-functions)))
3800       (gnus-message 7 "Sorting articles...done"))))
3801
3802 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3803 (defmacro gnus-thread-header (thread)
3804   "Return header of first article in THREAD.
3805 Note that THREAD must never, ever be anything else than a variable -
3806 using some other form will lead to serious barfage."
3807   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3808   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3809   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3810         (vector thread) 2))
3811
3812 (defsubst gnus-article-sort-by-number (h1 h2)
3813   "Sort articles by article number."
3814   (< (mail-header-number h1)
3815      (mail-header-number h2)))
3816
3817 (defun gnus-thread-sort-by-number (h1 h2)
3818   "Sort threads by root article number."
3819   (gnus-article-sort-by-number
3820    (gnus-thread-header h1) (gnus-thread-header h2)))
3821
3822 (defsubst gnus-article-sort-by-lines (h1 h2)
3823   "Sort articles by article Lines header."
3824   (< (mail-header-lines h1)
3825      (mail-header-lines h2)))
3826
3827 (defun gnus-thread-sort-by-lines (h1 h2)
3828   "Sort threads by root article Lines header."
3829   (gnus-article-sort-by-lines
3830    (gnus-thread-header h1) (gnus-thread-header h2)))
3831
3832 (defsubst gnus-article-sort-by-chars (h1 h2)
3833   "Sort articles by octet length."
3834   (< (mail-header-chars h1)
3835      (mail-header-chars h2)))
3836
3837 (defun gnus-thread-sort-by-chars (h1 h2)
3838   "Sort threads by root article octet length."
3839   (gnus-article-sort-by-chars
3840    (gnus-thread-header h1) (gnus-thread-header h2)))
3841
3842 (defsubst gnus-article-sort-by-author (h1 h2)
3843   "Sort articles by root author."
3844   (string-lessp
3845    (let ((extract (funcall
3846                    gnus-extract-address-components
3847                    (mail-header-from h1))))
3848      (or (car extract) (cadr extract) ""))
3849    (let ((extract (funcall
3850                    gnus-extract-address-components
3851                    (mail-header-from h2))))
3852      (or (car extract) (cadr extract) ""))))
3853
3854 (defun gnus-thread-sort-by-author (h1 h2)
3855   "Sort threads by root author."
3856   (gnus-article-sort-by-author
3857    (gnus-thread-header h1)  (gnus-thread-header h2)))
3858
3859 (defsubst gnus-article-sort-by-subject (h1 h2)
3860   "Sort articles by root subject."
3861   (string-lessp
3862    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3863    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3864
3865 (defun gnus-thread-sort-by-subject (h1 h2)
3866   "Sort threads by root subject."
3867   (gnus-article-sort-by-subject
3868    (gnus-thread-header h1) (gnus-thread-header h2)))
3869
3870 (defsubst gnus-article-sort-by-date (h1 h2)
3871   "Sort articles by root article date."
3872   (time-less-p
3873    (gnus-date-get-time (mail-header-date h1))
3874    (gnus-date-get-time (mail-header-date h2))))
3875
3876 (defun gnus-thread-sort-by-date (h1 h2)
3877   "Sort threads by root article date."
3878   (gnus-article-sort-by-date
3879    (gnus-thread-header h1) (gnus-thread-header h2)))
3880
3881 (defsubst gnus-article-sort-by-score (h1 h2)
3882   "Sort articles by root article score.
3883 Unscored articles will be counted as having a score of zero."
3884   (> (or (cdr (assq (mail-header-number h1)
3885                     gnus-newsgroup-scored))
3886          gnus-summary-default-score 0)
3887      (or (cdr (assq (mail-header-number h2)
3888                     gnus-newsgroup-scored))
3889          gnus-summary-default-score 0)))
3890
3891 (defun gnus-thread-sort-by-score (h1 h2)
3892   "Sort threads by root article score."
3893   (gnus-article-sort-by-score
3894    (gnus-thread-header h1) (gnus-thread-header h2)))
3895
3896 (defun gnus-thread-sort-by-total-score (h1 h2)
3897   "Sort threads by the sum of all scores in the thread.
3898 Unscored articles will be counted as having a score of zero."
3899   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3900
3901 (defun gnus-thread-total-score (thread)
3902   ;; This function find the total score of THREAD.
3903   (cond ((null thread)
3904          0)
3905         ((consp thread)
3906          (if (stringp (car thread))
3907              (apply gnus-thread-score-function 0
3908                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3909            (gnus-thread-total-score-1 thread)))
3910         (t
3911          (gnus-thread-total-score-1 (list thread)))))
3912
3913 (defun gnus-thread-total-score-1 (root)
3914   ;; This function find the total score of the thread below ROOT.
3915   (setq root (car root))
3916   (apply gnus-thread-score-function
3917          (or (append
3918               (mapcar 'gnus-thread-total-score
3919                       (cdr (gnus-id-to-thread (mail-header-id root))))
3920               (when (> (mail-header-number root) 0)
3921                 (list (or (cdr (assq (mail-header-number root)
3922                                      gnus-newsgroup-scored))
3923                           gnus-summary-default-score 0))))
3924              (list gnus-summary-default-score)
3925              '(0))))
3926
3927 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3928 (defvar gnus-tmp-prev-subject nil)
3929 (defvar gnus-tmp-false-parent nil)
3930 (defvar gnus-tmp-root-expunged nil)
3931 (defvar gnus-tmp-dummy-line nil)
3932
3933 (eval-when-compile (defvar gnus-tmp-header))
3934 (defun gnus-extra-header (type &optional header)
3935   "Return the extra header of TYPE."
3936   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3937       ""))
3938
3939 (defun gnus-summary-prepare-threads (threads)
3940   "Prepare summary buffer from THREADS and indentation LEVEL.
3941 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3942 or a straight list of headers."
3943   (gnus-message 7 "Generating summary...")
3944
3945   (setq gnus-newsgroup-threads threads)
3946   (beginning-of-line)
3947
3948   (let ((gnus-tmp-level 0)
3949         (default-score (or gnus-summary-default-score 0))
3950         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3951         thread number subject stack state gnus-tmp-gathered beg-match
3952         new-roots gnus-tmp-new-adopts thread-end
3953         gnus-tmp-header gnus-tmp-unread
3954         gnus-tmp-replied gnus-tmp-subject-or-nil
3955         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3956         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3957         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3958
3959     (setq gnus-tmp-prev-subject nil)
3960
3961     (if (vectorp (car threads))
3962         ;; If this is a straight (sic) list of headers, then a
3963         ;; threaded summary display isn't required, so we just create
3964         ;; an unthreaded one.
3965         (gnus-summary-prepare-unthreaded threads)
3966
3967       ;; Do the threaded display.
3968
3969       (while (or threads stack gnus-tmp-new-adopts new-roots)
3970
3971         (if (and (= gnus-tmp-level 0)
3972                  (or (not stack)
3973                      (= (caar stack) 0))
3974                  (not gnus-tmp-false-parent)
3975                  (or gnus-tmp-new-adopts new-roots))
3976             (if gnus-tmp-new-adopts
3977                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3978                       thread (list (car gnus-tmp-new-adopts))
3979                       gnus-tmp-header (caar thread)
3980                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3981               (when new-roots
3982                 (setq thread (list (car new-roots))
3983                       gnus-tmp-header (caar thread)
3984                       new-roots (cdr new-roots))))
3985
3986           (if threads
3987               ;; If there are some threads, we do them before the
3988               ;; threads on the stack.
3989               (setq thread threads
3990                     gnus-tmp-header (caar thread))
3991             ;; There were no current threads, so we pop something off
3992             ;; the stack.
3993             (setq state (car stack)
3994                   gnus-tmp-level (car state)
3995                   thread (cdr state)
3996                   stack (cdr stack)
3997                   gnus-tmp-header (caar thread))))
3998
3999         (setq gnus-tmp-false-parent nil)
4000         (setq gnus-tmp-root-expunged nil)
4001         (setq thread-end nil)
4002
4003         (if (stringp gnus-tmp-header)
4004             ;; The header is a dummy root.
4005             (cond
4006              ((eq gnus-summary-make-false-root 'adopt)
4007               ;; We let the first article adopt the rest.
4008               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4009                                                (cddar thread)))
4010               (setq gnus-tmp-gathered
4011                     (nconc (mapcar
4012                             (lambda (h) (mail-header-number (car h)))
4013                             (cddar thread))
4014                            gnus-tmp-gathered))
4015               (setq thread (cons (list (caar thread)
4016                                        (cadar thread))
4017                                  (cdr thread)))
4018               (setq gnus-tmp-level -1
4019                     gnus-tmp-false-parent t))
4020              ((eq gnus-summary-make-false-root 'empty)
4021               ;; We print adopted articles with empty subject fields.
4022               (setq gnus-tmp-gathered
4023                     (nconc (mapcar
4024                             (lambda (h) (mail-header-number (car h)))
4025                             (cddar thread))
4026                            gnus-tmp-gathered))
4027               (setq gnus-tmp-level -1))
4028              ((eq gnus-summary-make-false-root 'dummy)
4029               ;; We remember that we probably want to output a dummy
4030               ;; root.
4031               (setq gnus-tmp-dummy-line gnus-tmp-header)
4032               (setq gnus-tmp-prev-subject gnus-tmp-header))
4033              (t
4034               ;; We do not make a root for the gathered
4035               ;; sub-threads at all.
4036               (setq gnus-tmp-level -1)))
4037
4038           (setq number (mail-header-number gnus-tmp-header)
4039                 subject (mail-header-subject gnus-tmp-header))
4040
4041           (cond
4042            ;; If the thread has changed subject, we might want to make
4043            ;; this subthread into a root.
4044            ((and (null gnus-thread-ignore-subject)
4045                  (not (zerop gnus-tmp-level))
4046                  gnus-tmp-prev-subject
4047                  (not (inline
4048                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4049             (setq new-roots (nconc new-roots (list (car thread)))
4050                   thread-end t
4051                   gnus-tmp-header nil))
4052            ;; If the article lies outside the current limit,
4053            ;; then we do not display it.
4054            ((not (memq number gnus-newsgroup-limit))
4055             (setq gnus-tmp-gathered
4056                   (nconc (mapcar
4057                           (lambda (h) (mail-header-number (car h)))
4058                           (cdar thread))
4059                          gnus-tmp-gathered))
4060             (setq gnus-tmp-new-adopts (if (cdar thread)
4061                                           (append gnus-tmp-new-adopts
4062                                                   (cdar thread))
4063                                         gnus-tmp-new-adopts)
4064                   thread-end t
4065                   gnus-tmp-header nil)
4066             (when (zerop gnus-tmp-level)
4067               (setq gnus-tmp-root-expunged t)))
4068            ;; Perhaps this article is to be marked as read?
4069            ((and gnus-summary-mark-below
4070                  (< (or (cdr (assq number gnus-newsgroup-scored))
4071                         default-score)
4072                     gnus-summary-mark-below)
4073                  ;; Don't touch sparse articles.
4074                  (not (gnus-summary-article-sparse-p number))
4075                  (not (gnus-summary-article-ancient-p number)))
4076             (setq gnus-newsgroup-unreads
4077                   (delq number gnus-newsgroup-unreads))
4078             (if gnus-newsgroup-auto-expire
4079                 (push number gnus-newsgroup-expirable)
4080               (push (cons number gnus-low-score-mark)
4081                     gnus-newsgroup-reads))))
4082
4083           (when gnus-tmp-header
4084             ;; We may have an old dummy line to output before this
4085             ;; article.
4086             (when (and gnus-tmp-dummy-line
4087                        (gnus-subject-equal
4088                         gnus-tmp-dummy-line
4089                         (mail-header-subject gnus-tmp-header)))
4090               (gnus-summary-insert-dummy-line
4091                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4092               (setq gnus-tmp-dummy-line nil))
4093
4094             ;; Compute the mark.
4095             (setq gnus-tmp-unread (gnus-article-mark number))
4096
4097             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4098                                   gnus-tmp-header gnus-tmp-level)
4099                   gnus-newsgroup-data)
4100
4101             ;; Actually insert the line.
4102             (setq
4103              gnus-tmp-subject-or-nil
4104              (cond
4105               ((and gnus-thread-ignore-subject
4106                     gnus-tmp-prev-subject
4107                     (not (inline (gnus-subject-equal
4108                                   gnus-tmp-prev-subject subject))))
4109                subject)
4110               ((zerop gnus-tmp-level)
4111                (if (and (eq gnus-summary-make-false-root 'empty)
4112                         (memq number gnus-tmp-gathered)
4113                         gnus-tmp-prev-subject
4114                         (inline (gnus-subject-equal
4115                                  gnus-tmp-prev-subject subject)))
4116                    gnus-summary-same-subject
4117                  subject))
4118               (t gnus-summary-same-subject)))
4119             (if (and (eq gnus-summary-make-false-root 'adopt)
4120                      (= gnus-tmp-level 1)
4121                      (memq number gnus-tmp-gathered))
4122                 (setq gnus-tmp-opening-bracket ?\<
4123                       gnus-tmp-closing-bracket ?\>)
4124               (setq gnus-tmp-opening-bracket ?\[
4125                     gnus-tmp-closing-bracket ?\]))
4126             (setq
4127              gnus-tmp-indentation
4128              (aref gnus-thread-indent-array gnus-tmp-level)
4129              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4130              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4131                                 gnus-summary-default-score 0)
4132              gnus-tmp-score-char
4133              (if (or (null gnus-summary-default-score)
4134                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4135                          gnus-summary-zcore-fuzz))
4136                  ?  ;Whitespace
4137                (if (< gnus-tmp-score gnus-summary-default-score)
4138                    gnus-score-below-mark gnus-score-over-mark))
4139              gnus-tmp-replied
4140              (cond ((memq number gnus-newsgroup-processable)
4141                     gnus-process-mark)
4142                    ((memq number gnus-newsgroup-cached)
4143                     gnus-cached-mark)
4144                    ((memq number gnus-newsgroup-replied)
4145                     gnus-replied-mark)
4146                    ((memq number gnus-newsgroup-saved)
4147                     gnus-saved-mark)
4148                    (t gnus-unread-mark))
4149              gnus-tmp-from (mail-header-from gnus-tmp-header)
4150              gnus-tmp-name
4151              (cond
4152               ((string-match "<[^>]+> *$" gnus-tmp-from)
4153                (setq beg-match (match-beginning 0))
4154                (or (and (string-match "^\".+\"" gnus-tmp-from)
4155                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4156                    (substring gnus-tmp-from 0 beg-match)))
4157               ((string-match "(.+)" gnus-tmp-from)
4158                (substring gnus-tmp-from
4159                           (1+ (match-beginning 0)) (1- (match-end 0))))
4160               (t gnus-tmp-from)))
4161             (when (string= gnus-tmp-name "")
4162               (setq gnus-tmp-name gnus-tmp-from))
4163             (unless (numberp gnus-tmp-lines)
4164               (setq gnus-tmp-lines 0))
4165             (gnus-put-text-property
4166              (point)
4167              (progn (eval gnus-summary-line-format-spec) (point))
4168              'gnus-number number)
4169             (when gnus-visual-p
4170               (forward-line -1)
4171               (gnus-run-hooks 'gnus-summary-update-hook)
4172               (forward-line 1))
4173
4174             (setq gnus-tmp-prev-subject subject)))
4175
4176         (when (nth 1 thread)
4177           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4178         (incf gnus-tmp-level)
4179         (setq threads (if thread-end nil (cdar thread)))
4180         (unless threads
4181           (setq gnus-tmp-level 0)))))
4182   (gnus-message 7 "Generating summary...done"))
4183
4184 (defun gnus-summary-prepare-unthreaded (headers)
4185   "Generate an unthreaded summary buffer based on HEADERS."
4186   (let (header number mark)
4187
4188     (beginning-of-line)
4189
4190     (while headers
4191       ;; We may have to root out some bad articles...
4192       (when (memq (setq number (mail-header-number
4193                                 (setq header (pop headers))))
4194                   gnus-newsgroup-limit)
4195         ;; Mark article as read when it has a low score.
4196         (when (and gnus-summary-mark-below
4197                    (< (or (cdr (assq number gnus-newsgroup-scored))
4198                           gnus-summary-default-score 0)
4199                       gnus-summary-mark-below)
4200                    (not (gnus-summary-article-ancient-p number)))
4201           (setq gnus-newsgroup-unreads
4202                 (delq number gnus-newsgroup-unreads))
4203           (if gnus-newsgroup-auto-expire
4204               (push number gnus-newsgroup-expirable)
4205             (push (cons number gnus-low-score-mark)
4206                   gnus-newsgroup-reads)))
4207
4208         (setq mark (gnus-article-mark number))
4209         (push (gnus-data-make number mark (1+ (point)) header 0)
4210               gnus-newsgroup-data)
4211         (gnus-summary-insert-line
4212          header 0 number
4213          mark (memq number gnus-newsgroup-replied)
4214          (memq number gnus-newsgroup-expirable)
4215          (mail-header-subject header) nil
4216          (cdr (assq number gnus-newsgroup-scored))
4217          (memq number gnus-newsgroup-processable))))))
4218
4219 (defun gnus-summary-remove-list-identifiers ()
4220   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4221   (let ((regexp (if (stringp gnus-list-identifiers)
4222                     gnus-list-identifiers
4223                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4224     (dolist (header gnus-newsgroup-headers)
4225       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4226                                   " *\\)\\)+\\(Re: +\\)?\\)")
4227                           (mail-header-subject header))
4228         (mail-header-set-subject
4229          header (concat (substring (mail-header-subject header)
4230                                    0 (match-beginning 1))
4231                         (or
4232                          (match-string 3 (mail-header-subject header))
4233                          (match-string 5 (mail-header-subject header)))
4234                         (substring (mail-header-subject header)
4235                                    (match-end 1))))))))
4236
4237 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4238   "Select newsgroup GROUP.
4239 If READ-ALL is non-nil, all articles in the group are selected.
4240 If SELECT-ARTICLES, only select those articles from GROUP."
4241   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4242          ;;!!! Dirty hack; should be removed.
4243          (gnus-summary-ignore-duplicates
4244           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4245               t
4246             gnus-summary-ignore-duplicates))
4247          (info (nth 2 entry))
4248          articles fetched-articles cached)
4249
4250     (unless (gnus-check-server
4251              (setq gnus-current-select-method
4252                    (gnus-find-method-for-group group)))
4253       (error "Couldn't open server"))
4254
4255     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4256         (gnus-activate-group group)     ; Or we can activate it...
4257         (progn                          ; Or we bug out.
4258           (when (equal major-mode 'gnus-summary-mode)
4259             (kill-buffer (current-buffer)))
4260           (error "Couldn't activate group %s: %s"
4261                  group (gnus-status-message group))))
4262
4263     (unless (gnus-request-group group t)
4264       (when (equal major-mode 'gnus-summary-mode)
4265         (kill-buffer (current-buffer)))
4266       (error "Couldn't request group %s: %s"
4267              group (gnus-status-message group)))
4268
4269     (setq gnus-newsgroup-name group)
4270     (setq gnus-newsgroup-unselected nil)
4271     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4272     (gnus-summary-setup-default-charset)
4273
4274     ;; Adjust and set lists of article marks.
4275     (when info
4276       (gnus-adjust-marked-articles info))
4277
4278     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4279     (when (gnus-virtual-group-p group)
4280       (setq cached gnus-newsgroup-cached))
4281
4282     (setq gnus-newsgroup-unreads
4283           (gnus-set-difference
4284            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4285            gnus-newsgroup-dormant))
4286
4287     (setq gnus-newsgroup-processable nil)
4288
4289     (gnus-update-read-articles group gnus-newsgroup-unreads)
4290
4291     (if (setq articles select-articles)
4292         (setq gnus-newsgroup-unselected
4293               (gnus-sorted-intersection
4294                gnus-newsgroup-unreads
4295                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4296       (setq articles (gnus-articles-to-read group read-all)))
4297
4298     (cond
4299      ((null articles)
4300       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4301       'quit)
4302      ((eq articles 0) nil)
4303      (t
4304       ;; Init the dependencies hash table.
4305       (setq gnus-newsgroup-dependencies
4306             (gnus-make-hashtable (length articles)))
4307       (gnus-set-global-variables)
4308       ;; Retrieve the headers and read them in.
4309       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4310       (setq gnus-newsgroup-headers
4311             (if (eq 'nov
4312                     (setq gnus-headers-retrieved-by
4313                           (gnus-retrieve-headers
4314                            articles gnus-newsgroup-name
4315                            ;; We might want to fetch old headers, but
4316                            ;; not if there is only 1 article.
4317                            (and (or (and
4318                                      (not (eq gnus-fetch-old-headers 'some))
4319                                      (not (numberp gnus-fetch-old-headers)))
4320                                     (> (length articles) 1))
4321                                 gnus-fetch-old-headers))))
4322                 (gnus-get-newsgroup-headers-xover
4323                  articles nil nil gnus-newsgroup-name t)
4324               (gnus-get-newsgroup-headers)))
4325       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4326
4327       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4328       (when cached
4329         (setq gnus-newsgroup-cached cached))
4330
4331       ;; Suppress duplicates?
4332       (when gnus-suppress-duplicates
4333         (gnus-dup-suppress-articles))
4334
4335       ;; Set the initial limit.
4336       (setq gnus-newsgroup-limit (copy-sequence articles))
4337       ;; Remove canceled articles from the list of unread articles.
4338       (setq gnus-newsgroup-unreads
4339             (gnus-set-sorted-intersection
4340              gnus-newsgroup-unreads
4341              (setq fetched-articles
4342                    (mapcar (lambda (headers) (mail-header-number headers))
4343                            gnus-newsgroup-headers))))
4344       ;; Removed marked articles that do not exist.
4345       (gnus-update-missing-marks
4346        (gnus-sorted-complement fetched-articles articles))
4347       ;; We might want to build some more threads first.
4348       (when (and gnus-fetch-old-headers
4349                  (eq gnus-headers-retrieved-by 'nov))
4350         (if (eq gnus-fetch-old-headers 'invisible)
4351             (gnus-build-all-threads)
4352           (gnus-build-old-threads)))
4353       ;; Let the Gnus agent mark articles as read.
4354       (when gnus-agent
4355         (gnus-agent-get-undownloaded-list))
4356       ;; Remove list identifiers from subject
4357       (when gnus-list-identifiers
4358         (gnus-summary-remove-list-identifiers))
4359       ;; Check whether auto-expire is to be done in this group.
4360       (setq gnus-newsgroup-auto-expire
4361             (gnus-group-auto-expirable-p group))
4362       ;; Set up the article buffer now, if necessary.
4363       (unless gnus-single-article-buffer
4364         (gnus-article-setup-buffer))
4365       ;; First and last article in this newsgroup.
4366       (when gnus-newsgroup-headers
4367         (setq gnus-newsgroup-begin
4368               (mail-header-number (car gnus-newsgroup-headers))
4369               gnus-newsgroup-end
4370               (mail-header-number
4371                (gnus-last-element gnus-newsgroup-headers))))
4372       ;; GROUP is successfully selected.
4373       (or gnus-newsgroup-headers t)))))
4374
4375 (defun gnus-articles-to-read (group &optional read-all)
4376   "Find out what articles the user wants to read."
4377   (let* ((articles
4378           ;; Select all articles if `read-all' is non-nil, or if there
4379           ;; are no unread articles.
4380           (if (or read-all
4381                   (and (zerop (length gnus-newsgroup-marked))
4382                        (zerop (length gnus-newsgroup-unreads)))
4383                   (eq (gnus-group-find-parameter group 'display)
4384                       'all))
4385               (or
4386                (gnus-uncompress-range (gnus-active group))
4387                (gnus-cache-articles-in-group group))
4388             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4389                           (copy-sequence gnus-newsgroup-unreads))
4390                   '<)))
4391          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4392          (scored (length scored-list))
4393          (number (length articles))
4394          (marked (+ (length gnus-newsgroup-marked)
4395                     (length gnus-newsgroup-dormant)))
4396          (select
4397           (cond
4398            ((numberp read-all)
4399             read-all)
4400            (t
4401             (condition-case ()
4402                 (cond
4403                  ((and (or (<= scored marked) (= scored number))
4404                        (numberp gnus-large-newsgroup)
4405                        (> number gnus-large-newsgroup))
4406                   (let ((input
4407                          (read-string
4408                           (format
4409                            "How many articles from %s (default %d): "
4410                            (gnus-limit-string gnus-newsgroup-name 35)
4411                            number))))
4412                     (if (string-match "^[ \t]*$" input) number input)))
4413                  ((and (> scored marked) (< scored number)
4414                        (> (- scored number) 20))
4415                   (let ((input
4416                          (read-string
4417                           (format "%s %s (%d scored, %d total): "
4418                                   "How many articles from"
4419                                   group scored number))))
4420                     (if (string-match "^[ \t]*$" input)
4421                         number input)))
4422                  (t number))
4423               (quit
4424                (message "Quit getting the articles to read")
4425                nil))))))
4426     (setq select (if (stringp select) (string-to-number select) select))
4427     (if (or (null select) (zerop select))
4428         select
4429       (if (and (not (zerop scored)) (<= (abs select) scored))
4430           (progn
4431             (setq articles (sort scored-list '<))
4432             (setq number (length articles)))
4433         (setq articles (copy-sequence articles)))
4434
4435       (when (< (abs select) number)
4436         (if (< select 0)
4437             ;; Select the N oldest articles.
4438             (setcdr (nthcdr (1- (abs select)) articles) nil)
4439           ;; Select the N most recent articles.
4440           (setq articles (nthcdr (- number select) articles))))
4441       (setq gnus-newsgroup-unselected
4442             (gnus-sorted-intersection
4443              gnus-newsgroup-unreads
4444              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4445       (when gnus-alter-articles-to-read-function
4446         (setq gnus-newsgroup-unreads
4447               (sort 
4448                (funcall gnus-alter-articles-to-read-function
4449                         gnus-newsgroup-name gnus-newsgroup-unreads)
4450                '<)))
4451       articles)))
4452
4453 (defun gnus-killed-articles (killed articles)
4454   (let (out)
4455     (while articles
4456       (when (inline (gnus-member-of-range (car articles) killed))
4457         (push (car articles) out))
4458       (setq articles (cdr articles)))
4459     out))
4460
4461 (defun gnus-uncompress-marks (marks)
4462   "Uncompress the mark ranges in MARKS."
4463   (let ((uncompressed '(score bookmark))
4464         out)
4465     (while marks
4466       (if (memq (caar marks) uncompressed)
4467           (push (car marks) out)
4468         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4469       (setq marks (cdr marks)))
4470     out))
4471
4472 (defun gnus-adjust-marked-articles (info)
4473   "Set all article lists and remove all marks that are no longer valid."
4474   (let* ((marked-lists (gnus-info-marks info))
4475          (active (gnus-active (gnus-info-group info)))
4476          (min (car active))
4477          (max (cdr active))
4478          (types gnus-article-mark-lists)
4479          (uncompressed '(score bookmark killed))
4480          marks var articles article mark)
4481
4482     (while marked-lists
4483       (setq marks (pop marked-lists))
4484       (set (setq var (intern (format "gnus-newsgroup-%s"
4485                                      (car (rassq (setq mark (car marks))
4486                                                  types)))))
4487            (if (memq (car marks) uncompressed) (cdr marks)
4488              (gnus-uncompress-range (cdr marks))))
4489
4490       (setq articles (symbol-value var))
4491
4492       ;; All articles have to be subsets of the active articles.
4493       (cond
4494        ;; Adjust "simple" lists.
4495        ((memq mark '(tick dormant expire reply save))
4496         (while articles
4497           (when (or (< (setq article (pop articles)) min) (> article max))
4498             (set var (delq article (symbol-value var))))))
4499        ;; Adjust assocs.
4500        ((memq mark uncompressed)
4501         (when (not (listp (cdr (symbol-value var))))
4502           (set var (list (symbol-value var))))
4503         (when (not (listp (cdr articles)))
4504           (setq articles (list articles)))
4505         (while articles
4506           (when (or (not (consp (setq article (pop articles))))
4507                     (< (car article) min)
4508                     (> (car article) max))
4509             (set var (delq article (symbol-value var))))))))))
4510
4511 (defun gnus-update-missing-marks (missing)
4512   "Go through the list of MISSING articles and remove them from the mark lists."
4513   (when missing
4514     (let ((types gnus-article-mark-lists)
4515           var m)
4516       ;; Go through all types.
4517       (while types
4518         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4519         (when (symbol-value var)
4520           ;; This list has articles.  So we delete all missing articles
4521           ;; from it.
4522           (setq m missing)
4523           (while m
4524             (set var (delq (pop m) (symbol-value var)))))))))
4525
4526 (defun gnus-update-marks ()
4527   "Enter the various lists of marked articles into the newsgroup info list."
4528   (let ((types gnus-article-mark-lists)
4529         (info (gnus-get-info gnus-newsgroup-name))
4530         (uncompressed '(score bookmark killed))
4531         type list newmarked symbol delta-marks)
4532     (when info
4533       ;; Add all marks lists to the list of marks lists.
4534       (while (setq type (pop types))
4535         (setq list (symbol-value
4536                     (setq symbol
4537                           (intern (format "gnus-newsgroup-%s"
4538                                           (car type))))))
4539
4540         (when list
4541           ;; Get rid of the entries of the articles that have the
4542           ;; default score.
4543           (when (and (eq (cdr type) 'score)
4544                      gnus-save-score
4545                      list)
4546             (let* ((arts list)
4547                    (prev (cons nil list))
4548                    (all prev))
4549               (while arts
4550                 (if (or (not (consp (car arts)))
4551                         (= (cdar arts) gnus-summary-default-score))
4552                     (setcdr prev (cdr arts))
4553                   (setq prev arts))
4554                 (setq arts (cdr arts)))
4555               (setq list (cdr all)))))
4556
4557         (unless (memq (cdr type) uncompressed)
4558           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4559        
4560         (when (gnus-check-backend-function
4561                'request-set-mark gnus-newsgroup-name)
4562           ;; propagate flags to server, with the following exceptions:
4563           ;; uncompressed:s are not proper flags (they are cons cells)
4564           ;; cache is a internal gnus flag
4565           ;; download are local to one gnus installation (well)
4566           ;; unsend are for nndraft groups only
4567           ;; xxx: generality of this?  this suits nnimap anyway
4568           (unless (memq (cdr type) (append '(cache download unsend)
4569                                            uncompressed))
4570             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4571                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4572                    (add (gnus-remove-from-range
4573                          (gnus-copy-sequence list) old)))
4574               (when add
4575                 (push (list add 'add (list (cdr type))) delta-marks))
4576               (when del
4577                 (push (list del 'del (list (cdr type))) delta-marks)))))
4578           
4579         (when list
4580           (push (cons (cdr type) list) newmarked)))
4581
4582       (when delta-marks
4583         (unless (gnus-check-group gnus-newsgroup-name)
4584           (error "Can't open server for %s" gnus-newsgroup-name))
4585         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4586           
4587       ;; Enter these new marks into the info of the group.
4588       (if (nthcdr 3 info)
4589           (setcar (nthcdr 3 info) newmarked)
4590         ;; Add the marks lists to the end of the info.
4591         (when newmarked
4592           (setcdr (nthcdr 2 info) (list newmarked))))
4593
4594       ;; Cut off the end of the info if there's nothing else there.
4595       (let ((i 5))
4596         (while (and (> i 2)
4597                     (not (nth i info)))
4598           (when (nthcdr (decf i) info)
4599             (setcdr (nthcdr i info) nil)))))))
4600
4601 (defun gnus-set-mode-line (where)
4602   "Set the mode line of the article or summary buffers.
4603 If WHERE is `summary', the summary mode line format will be used."
4604   ;; Is this mode line one we keep updated?
4605   (when (and (memq where gnus-updated-mode-lines)
4606              (symbol-value
4607               (intern (format "gnus-%s-mode-line-format-spec" where))))
4608     (let (mode-string)
4609       (save-excursion
4610         ;; We evaluate this in the summary buffer since these
4611         ;; variables are buffer-local to that buffer.
4612         (set-buffer gnus-summary-buffer)
4613         ;; We bind all these variables that are used in the `eval' form
4614         ;; below.
4615         (let* ((mformat (symbol-value
4616                          (intern
4617                           (format "gnus-%s-mode-line-format-spec" where))))
4618                (gnus-tmp-group-name (gnus-group-name-decode 
4619                                      gnus-newsgroup-name
4620                                      (gnus-group-name-charset 
4621                                       nil
4622                                       gnus-newsgroup-name)))
4623                (gnus-tmp-article-number (or gnus-current-article 0))
4624                (gnus-tmp-unread gnus-newsgroup-unreads)
4625                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4626                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4627                (gnus-tmp-unread-and-unselected
4628                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4629                             (zerop gnus-tmp-unselected))
4630                        "")
4631                       ((zerop gnus-tmp-unselected)
4632                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4633                       (t (format "{%d(+%d) more}"
4634                                  gnus-tmp-unread-and-unticked
4635                                  gnus-tmp-unselected))))
4636                (gnus-tmp-subject
4637                 (if (and gnus-current-headers
4638                          (vectorp gnus-current-headers))
4639                     (gnus-mode-string-quote
4640                      (mail-header-subject gnus-current-headers))
4641                   ""))
4642                bufname-length max-len
4643                gnus-tmp-header);; passed as argument to any user-format-funcs
4644           (setq mode-string (eval mformat))
4645           (setq bufname-length (if (string-match "%b" mode-string)
4646                                    (- (length
4647                                        (buffer-name
4648                                         (if (eq where 'summary)
4649                                             nil
4650                                           (get-buffer gnus-article-buffer))))
4651                                       2)
4652                                  0))
4653           (setq max-len (max 4 (if gnus-mode-non-string-length
4654                                    (- (window-width)
4655                                       gnus-mode-non-string-length
4656                                       bufname-length)
4657                                  (length mode-string))))
4658           ;; We might have to chop a bit of the string off...
4659           (when (> (length mode-string) max-len)
4660             (setq mode-string
4661                   (concat (truncate-string-to-width mode-string (- max-len 3))
4662                           "...")))
4663           ;; Pad the mode string a bit.
4664           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4665       ;; Update the mode line.
4666       (setq mode-line-buffer-identification
4667             (gnus-mode-line-buffer-identification (list mode-string)))
4668       (set-buffer-modified-p t))))
4669
4670 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4671   "Go through the HEADERS list and add all Xrefs to a hash table.
4672 The resulting hash table is returned, or nil if no Xrefs were found."
4673   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4674          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4675          (xref-hashtb (gnus-make-hashtable))
4676          start group entry number xrefs header)
4677     (while headers
4678       (setq header (pop headers))
4679       (when (and (setq xrefs (mail-header-xref header))
4680                  (not (memq (setq number (mail-header-number header))
4681                             unreads)))
4682         (setq start 0)
4683         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4684           (setq start (match-end 0))
4685           (setq group (if prefix
4686                           (concat prefix (substring xrefs (match-beginning 1)
4687                                                     (match-end 1)))
4688                         (substring xrefs (match-beginning 1) (match-end 1))))
4689           (setq number
4690                 (string-to-int (substring xrefs (match-beginning 2)
4691                                           (match-end 2))))
4692           (if (setq entry (gnus-gethash group xref-hashtb))
4693               (setcdr entry (cons number (cdr entry)))
4694             (gnus-sethash group (cons number nil) xref-hashtb)))))
4695     (and start xref-hashtb)))
4696
4697 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4698   "Look through all the headers and mark the Xrefs as read."
4699   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4700         name entry info xref-hashtb idlist method nth4)
4701     (save-excursion
4702       (set-buffer gnus-group-buffer)
4703       (when (setq xref-hashtb
4704                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4705         (mapatoms
4706          (lambda (group)
4707            (unless (string= from-newsgroup (setq name (symbol-name group)))
4708              (setq idlist (symbol-value group))
4709              ;; Dead groups are not updated.
4710              (and (prog1
4711                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4712                             info (nth 2 entry))
4713                     (when (stringp (setq nth4 (gnus-info-method info)))
4714                       (setq nth4 (gnus-server-to-method nth4))))
4715                   ;; Only do the xrefs if the group has the same
4716                   ;; select method as the group we have just read.
4717                   (or (gnus-methods-equal-p
4718                        nth4 (gnus-find-method-for-group from-newsgroup))
4719                       virtual
4720                       (equal nth4 (setq method (gnus-find-method-for-group
4721                                                 from-newsgroup)))
4722                       (and (equal (car nth4) (car method))
4723                            (equal (nth 1 nth4) (nth 1 method))))
4724                   gnus-use-cross-reference
4725                   (or (not (eq gnus-use-cross-reference t))
4726                       virtual
4727                       ;; Only do cross-references on subscribed
4728                       ;; groups, if that is what is wanted.
4729                       (<= (gnus-info-level info) gnus-level-subscribed))
4730                   (gnus-group-make-articles-read name idlist))))
4731          xref-hashtb)))))
4732
4733 (defun gnus-compute-read-articles (group articles)
4734   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4735          (info (nth 2 entry))
4736          (active (gnus-active group))
4737          ninfo)
4738     (when entry
4739       ;; First peel off all invalid article numbers.
4740       (when active
4741         (let ((ids articles)
4742               id first)
4743           (while (setq id (pop ids))
4744             (when (and first (> id (cdr active)))
4745               ;; We'll end up in this situation in one particular
4746               ;; obscure situation.  If you re-scan a group and get
4747               ;; a new article that is cross-posted to a different
4748               ;; group that has not been re-scanned, you might get
4749               ;; crossposted article that has a higher number than
4750               ;; Gnus believes possible.  So we re-activate this
4751               ;; group as well.  This might mean doing the
4752               ;; crossposting thingy will *increase* the number
4753               ;; of articles in some groups.  Tsk, tsk.
4754               (setq active (or (gnus-activate-group group) active)))
4755             (when (or (> id (cdr active))
4756                       (< id (car active)))
4757               (setq articles (delq id articles))))))
4758       ;; If the read list is nil, we init it.
4759       (if (and active
4760                (null (gnus-info-read info))
4761                (> (car active) 1))
4762           (setq ninfo (cons 1 (1- (car active))))
4763         (setq ninfo (gnus-info-read info)))
4764       ;; Then we add the read articles to the range.
4765       (gnus-add-to-range
4766        ninfo (setq articles (sort articles '<))))))
4767
4768 (defun gnus-group-make-articles-read (group articles)
4769   "Update the info of GROUP to say that ARTICLES are read."
4770   (let* ((num 0)
4771          (entry (gnus-gethash group gnus-newsrc-hashtb))
4772          (info (nth 2 entry))
4773          (active (gnus-active group))
4774          range)
4775     (when entry
4776       (setq range (gnus-compute-read-articles group articles))
4777       (save-excursion
4778         (set-buffer gnus-group-buffer)
4779         (gnus-undo-register
4780           `(progn
4781              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4782              (gnus-info-set-read ',info ',(gnus-info-read info))
4783              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4784              (gnus-group-update-group ,group t))))
4785       ;; Add the read articles to the range.
4786       (gnus-info-set-read info range)
4787       ;; Then we have to re-compute how many unread
4788       ;; articles there are in this group.
4789       (when active
4790         (cond
4791          ((not range)
4792           (setq num (- (1+ (cdr active)) (car active))))
4793          ((not (listp (cdr range)))
4794           (setq num (- (cdr active) (- (1+ (cdr range))
4795                                        (car range)))))
4796          (t
4797           (while range
4798             (if (numberp (car range))
4799                 (setq num (1+ num))
4800               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4801             (setq range (cdr range)))
4802           (setq num (- (cdr active) num))))
4803         ;; Update the number of unread articles.
4804         (setcar entry num)
4805         ;; Update the group buffer.
4806         (gnus-group-update-group group t)))))
4807
4808 (defvar gnus-newsgroup-none-id 0)
4809
4810 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4811   (let ((cur nntp-server-buffer)
4812         (dependencies
4813          (or dependencies
4814              (save-excursion (set-buffer gnus-summary-buffer)
4815                              gnus-newsgroup-dependencies)))
4816         headers id end ref
4817         (mail-parse-charset gnus-newsgroup-charset)
4818         (mail-parse-ignored-charsets
4819          (save-excursion (condition-case nil
4820                              (set-buffer gnus-summary-buffer)
4821                            (error))
4822                          gnus-newsgroup-ignored-charsets)))
4823     (save-excursion
4824       (set-buffer nntp-server-buffer)
4825       ;; Translate all TAB characters into SPACE characters.
4826       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4827       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4828       (gnus-run-hooks 'gnus-parse-headers-hook)
4829       (let ((case-fold-search t)
4830             in-reply-to header p lines chars)
4831         (goto-char (point-min))
4832         ;; Search to the beginning of the next header.  Error messages
4833         ;; do not begin with 2 or 3.
4834         (while (re-search-forward "^[23][0-9]+ " nil t)
4835           (setq id nil
4836                 ref nil)
4837           ;; This implementation of this function, with nine
4838           ;; search-forwards instead of the one re-search-forward and
4839           ;; a case (which basically was the old function) is actually
4840           ;; about twice as fast, even though it looks messier.  You
4841           ;; can't have everything, I guess.  Speed and elegance
4842           ;; doesn't always go hand in hand.
4843           (setq
4844            header
4845            (vector
4846             ;; Number.
4847             (prog1
4848                 (read cur)
4849               (end-of-line)
4850               (setq p (point))
4851               (narrow-to-region (point)
4852                                 (or (and (search-forward "\n.\n" nil t)
4853                                          (- (point) 2))
4854                                     (point))))
4855             ;; Subject.
4856             (progn
4857               (goto-char p)
4858               (if (search-forward "\nsubject: " nil t)
4859                   (funcall gnus-decode-encoded-word-function
4860                            (nnheader-header-value))
4861                 "(none)"))
4862             ;; From.
4863             (progn
4864               (goto-char p)
4865               (if (or (search-forward "\nfrom: " nil t)
4866                       (search-forward "\nfrom:" nil t))
4867                   (funcall gnus-decode-encoded-word-function
4868                            (nnheader-header-value))
4869                 "(nobody)"))
4870             ;; Date.
4871             (progn
4872               (goto-char p)
4873               (if (search-forward "\ndate: " nil t)
4874                   (nnheader-header-value) ""))
4875             ;; Message-ID.
4876             (progn
4877               (goto-char p)
4878               (setq id (if (re-search-forward
4879                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4880                            ;; We do it this way to make sure the Message-ID
4881                            ;; is (somewhat) syntactically valid.
4882                            (buffer-substring (match-beginning 1)
4883                                              (match-end 1))
4884                          ;; If there was no message-id, we just fake one
4885                          ;; to make subsequent routines simpler.
4886                          (nnheader-generate-fake-message-id))))
4887             ;; References.
4888             (progn
4889               (goto-char p)
4890               (if (search-forward "\nreferences: " nil t)
4891                   (progn
4892                     (setq end (point))
4893                     (prog1
4894                         (nnheader-header-value)
4895                       (setq ref
4896                             (buffer-substring
4897                              (progn
4898                                (end-of-line)
4899                                (search-backward ">" end t)
4900                                (1+ (point)))
4901                              (progn
4902                                (search-backward "<" end t)
4903                                (point))))))
4904                 ;; Get the references from the in-reply-to header if there
4905                 ;; were no references and the in-reply-to header looks
4906                 ;; promising.
4907                 (if (and (search-forward "\nin-reply-to: " nil t)
4908                          (setq in-reply-to (nnheader-header-value))
4909                          (string-match "<[^>]+>" in-reply-to))
4910                     (let (ref2)
4911                       (setq ref (substring in-reply-to (match-beginning 0)
4912                                            (match-end 0)))
4913                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4914                         (setq ref2 (substring in-reply-to (match-beginning 0)
4915                                               (match-end 0)))
4916                         (when (> (length ref2) (length ref))
4917                           (setq ref ref2)))
4918                       ref)
4919                   (setq ref nil))))
4920             ;; Chars.
4921             (progn
4922               (goto-char p)
4923               (if (search-forward "\nchars: " nil t)
4924                   (if (numberp (setq chars (ignore-errors (read cur))))
4925                       chars 0)
4926                 0))
4927             ;; Lines.
4928             (progn
4929               (goto-char p)
4930               (if (search-forward "\nlines: " nil t)
4931                   (if (numberp (setq lines (ignore-errors (read cur))))
4932                       lines 0)
4933                 0))
4934             ;; Xref.
4935             (progn
4936               (goto-char p)
4937               (and (search-forward "\nxref: " nil t)
4938                    (nnheader-header-value)))
4939             ;; Extra.
4940             (when gnus-extra-headers
4941               (let ((extra gnus-extra-headers)
4942                     out)
4943                 (while extra
4944                   (goto-char p)
4945                   (when (search-forward
4946                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4947                     (push (cons (car extra) (nnheader-header-value))
4948                           out))
4949                   (pop extra))
4950                 out))))
4951           (when (equal id ref)
4952             (setq ref nil))
4953
4954           (when gnus-alter-header-function
4955             (funcall gnus-alter-header-function header)
4956             (setq id (mail-header-id header)
4957                   ref (gnus-parent-id (mail-header-references header))))
4958
4959           (when (setq header
4960                       (gnus-dependencies-add-header
4961                        header dependencies force-new))
4962             (push header headers))
4963           (goto-char (point-max))
4964           (widen))
4965         (nreverse headers)))))
4966
4967 ;; Goes through the xover lines and returns a list of vectors
4968 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4969                                                   force-new dependencies
4970                                                   group also-fetch-heads)
4971   "Parse the news overview data in the server buffer.
4972 Return a list of headers that match SEQUENCE (see
4973 `nntp-retrieve-headers')."
4974   ;; Get the Xref when the users reads the articles since most/some
4975   ;; NNTP servers do not include Xrefs when using XOVER.
4976   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4977   (let ((mail-parse-charset gnus-newsgroup-charset)
4978         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4979         (cur nntp-server-buffer)
4980         (dependencies (or dependencies gnus-newsgroup-dependencies))
4981         number headers header)
4982     (save-excursion
4983       (set-buffer nntp-server-buffer)
4984       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4985       ;; Allow the user to mangle the headers before parsing them.
4986       (gnus-run-hooks 'gnus-parse-headers-hook)
4987       (goto-char (point-min))
4988       (while (not (eobp))
4989         (condition-case ()
4990             (while (and sequence (not (eobp)))
4991               (setq number (read cur))
4992               (while (and sequence
4993                           (< (car sequence) number))
4994                 (setq sequence (cdr sequence)))
4995               (and sequence
4996                    (eq number (car sequence))
4997                    (progn
4998                      (setq sequence (cdr sequence))
4999                      (setq header (inline
5000                                     (gnus-nov-parse-line
5001                                      number dependencies force-new))))
5002                    (push header headers))
5003               (forward-line 1))
5004           (error
5005            (gnus-error 4 "Strange nov line (%d)"
5006                        (count-lines (point-min) (point)))))
5007         (forward-line 1))
5008       ;; A common bug in inn is that if you have posted an article and
5009       ;; then retrieves the active file, it will answer correctly --
5010       ;; the new article is included.  However, a NOV entry for the
5011       ;; article may not have been generated yet, so this may fail.
5012       ;; We work around this problem by retrieving the last few
5013       ;; headers using HEAD.
5014       (if (or (not also-fetch-heads)
5015               (not sequence))
5016           ;; We (probably) got all the headers.
5017           (nreverse headers)
5018         (let ((gnus-nov-is-evil t))
5019           (nconc
5020            (nreverse headers)
5021            (when (gnus-retrieve-headers sequence group)
5022              (gnus-get-newsgroup-headers))))))))
5023
5024 (defun gnus-article-get-xrefs ()
5025   "Fill in the Xref value in `gnus-current-headers', if necessary.
5026 This is meant to be called in `gnus-article-internal-prepare-hook'."
5027   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5028                                  gnus-current-headers)))
5029     (or (not gnus-use-cross-reference)
5030         (not headers)
5031         (and (mail-header-xref headers)
5032              (not (string= (mail-header-xref headers) "")))
5033         (let ((case-fold-search t)
5034               xref)
5035           (save-restriction
5036             (nnheader-narrow-to-headers)
5037             (goto-char (point-min))
5038             (when (or (and (not (eobp))
5039                            (eq (downcase (char-after)) ?x)
5040                            (looking-at "Xref:"))
5041                       (search-forward "\nXref:" nil t))
5042               (goto-char (1+ (match-end 0)))
5043               (setq xref (buffer-substring (point)
5044                                            (progn (end-of-line) (point))))
5045               (mail-header-set-xref headers xref)))))))
5046
5047 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5048   "Find article ID and insert the summary line for that article.
5049 OLD-HEADER can either be a header or a line number to insert
5050 the subject line on."
5051   (let* ((line (and (numberp old-header) old-header))
5052          (old-header (and (vectorp old-header) old-header))
5053          (header (cond ((and old-header use-old-header)
5054                         old-header)
5055                        ((and (numberp id)
5056                              (gnus-number-to-header id))
5057                         (gnus-number-to-header id))
5058                        (t
5059                         (gnus-read-header id))))
5060          (number (and (numberp id) id))
5061          d)
5062     (when header
5063       ;; Rebuild the thread that this article is part of and go to the
5064       ;; article we have fetched.
5065       (when (and (not gnus-show-threads)
5066                  old-header)
5067         (when (and number
5068                    (setq d (gnus-data-find (mail-header-number old-header))))
5069           (goto-char (gnus-data-pos d))
5070           (gnus-data-remove
5071            number
5072            (- (gnus-point-at-bol)
5073               (prog1
5074                   (1+ (gnus-point-at-eol))
5075                 (gnus-delete-line))))))
5076       (when old-header
5077         (mail-header-set-number header (mail-header-number old-header)))
5078       (setq gnus-newsgroup-sparse
5079             (delq (setq number (mail-header-number header))
5080                   gnus-newsgroup-sparse))
5081       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5082       (push number gnus-newsgroup-limit)
5083       (gnus-rebuild-thread (mail-header-id header) line)
5084       (gnus-summary-goto-subject number nil t))
5085     (when (and (numberp number)
5086                (> number 0))
5087       ;; We have to update the boundaries even if we can't fetch the
5088       ;; article if ID is a number -- so that the next `P' or `N'
5089       ;; command will fetch the previous (or next) article even
5090       ;; if the one we tried to fetch this time has been canceled.
5091       (when (> number gnus-newsgroup-end)
5092         (setq gnus-newsgroup-end number))
5093       (when (< number gnus-newsgroup-begin)
5094         (setq gnus-newsgroup-begin number))
5095       (setq gnus-newsgroup-unselected
5096             (delq number gnus-newsgroup-unselected)))
5097     ;; Report back a success?
5098     (and header (mail-header-number header))))
5099
5100 ;;; Process/prefix in the summary buffer
5101
5102 (defun gnus-summary-work-articles (n)
5103   "Return a list of articles to be worked upon.
5104 The prefix argument, the list of process marked articles, and the
5105 current article will be taken into consideration."
5106   (save-excursion
5107     (set-buffer gnus-summary-buffer)
5108     (cond
5109      (n
5110       ;; A numerical prefix has been given.
5111       (setq n (prefix-numeric-value n))
5112       (let ((backward (< n 0))
5113             (n (abs (prefix-numeric-value n)))
5114             articles article)
5115         (save-excursion
5116           (while
5117               (and (> n 0)
5118                    (push (setq article (gnus-summary-article-number))
5119                          articles)
5120                    (if backward
5121                        (gnus-summary-find-prev nil article)
5122                      (gnus-summary-find-next nil article)))
5123             (decf n)))
5124         (nreverse articles)))
5125      ((and (gnus-region-active-p) (mark))
5126       (message "region active")
5127       ;; Work on the region between point and mark.
5128       (let ((max (max (point) (mark)))
5129             articles article)
5130         (save-excursion
5131           (goto-char (min (min (point) (mark))))
5132           (while
5133               (and
5134                (push (setq article (gnus-summary-article-number)) articles)
5135                (gnus-summary-find-next nil article)
5136                (< (point) max)))
5137           (nreverse articles))))
5138      (gnus-newsgroup-processable
5139       ;; There are process-marked articles present.
5140       ;; Save current state.
5141       (gnus-summary-save-process-mark)
5142       ;; Return the list.
5143       (reverse gnus-newsgroup-processable))
5144      (t
5145       ;; Just return the current article.
5146       (list (gnus-summary-article-number))))))
5147
5148 (defmacro gnus-summary-iterate (arg &rest forms)
5149   "Iterate over the process/prefixed articles and do FORMS.
5150 ARG is the interactive prefix given to the command.  FORMS will be
5151 executed with point over the summary line of the articles."
5152   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5153     `(let ((,articles (gnus-summary-work-articles ,arg)))
5154        (while ,articles
5155          (gnus-summary-goto-subject (car ,articles))
5156          ,@forms
5157          (pop ,articles)))))
5158
5159 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5160 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5161
5162 (defun gnus-summary-save-process-mark ()
5163   "Push the current set of process marked articles on the stack."
5164   (interactive)
5165   (push (copy-sequence gnus-newsgroup-processable)
5166         gnus-newsgroup-process-stack))
5167
5168 (defun gnus-summary-kill-process-mark ()
5169   "Push the current set of process marked articles on the stack and unmark."
5170   (interactive)
5171   (gnus-summary-save-process-mark)
5172   (gnus-summary-unmark-all-processable))
5173
5174 (defun gnus-summary-yank-process-mark ()
5175   "Pop the last process mark state off the stack and restore it."
5176   (interactive)
5177   (unless gnus-newsgroup-process-stack
5178     (error "Empty mark stack"))
5179   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5180
5181 (defun gnus-summary-process-mark-set (set)
5182   "Make SET into the current process marked articles."
5183   (gnus-summary-unmark-all-processable)
5184   (while set
5185     (gnus-summary-set-process-mark (pop set))))
5186
5187 ;;; Searching and stuff
5188
5189 (defun gnus-summary-search-group (&optional backward use-level)
5190   "Search for next unread newsgroup.
5191 If optional argument BACKWARD is non-nil, search backward instead."
5192   (save-excursion
5193     (set-buffer gnus-group-buffer)
5194     (when (gnus-group-search-forward
5195            backward nil (if use-level (gnus-group-group-level) nil))
5196       (gnus-group-group-name))))
5197
5198 (defun gnus-summary-best-group (&optional exclude-group)
5199   "Find the name of the best unread group.
5200 If EXCLUDE-GROUP, do not go to this group."
5201   (save-excursion
5202     (set-buffer gnus-group-buffer)
5203     (save-excursion
5204       (gnus-group-best-unread-group exclude-group))))
5205
5206 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5207   (if backward (gnus-summary-find-prev)
5208     (let* ((dummy (gnus-summary-article-intangible-p))
5209            (article (or article (gnus-summary-article-number)))
5210            (arts (gnus-data-find-list article))
5211            result)
5212       (when (and (not dummy)
5213                  (or (not gnus-summary-check-current)
5214                      (not unread)
5215                      (not (gnus-data-unread-p (car arts)))))
5216         (setq arts (cdr arts)))
5217       (when (setq result
5218                   (if unread
5219                       (progn
5220                         (while arts
5221                           (when (or (and undownloaded
5222                                          (eq gnus-undownloaded-mark
5223                                              (gnus-data-mark (car arts))))
5224                                     (gnus-data-unread-p (car arts)))
5225                             (setq result (car arts)
5226                                   arts nil))
5227                           (setq arts (cdr arts)))
5228                         result)
5229                     (car arts)))
5230         (goto-char (gnus-data-pos result))
5231         (gnus-data-number result)))))
5232
5233 (defun gnus-summary-find-prev (&optional unread article)
5234   (let* ((eobp (eobp))
5235          (article (or article (gnus-summary-article-number)))
5236          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5237          result)
5238     (when (and (not eobp)
5239                (or (not gnus-summary-check-current)
5240                    (not unread)
5241                    (not (gnus-data-unread-p (car arts)))))
5242       (setq arts (cdr arts)))
5243     (when (setq result
5244                 (if unread
5245                     (progn
5246                       (while arts
5247                         (when (gnus-data-unread-p (car arts))
5248                           (setq result (car arts)
5249                                 arts nil))
5250                         (setq arts (cdr arts)))
5251                       result)
5252                   (car arts)))
5253       (goto-char (gnus-data-pos result))
5254       (gnus-data-number result))))
5255
5256 (defun gnus-summary-find-subject (subject &optional unread backward article)
5257   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5258          (article (or article (gnus-summary-article-number)))
5259          (articles (gnus-data-list backward))
5260          (arts (gnus-data-find-list article articles))
5261          result)
5262     (when (or (not gnus-summary-check-current)
5263               (not unread)
5264               (not (gnus-data-unread-p (car arts))))
5265       (setq arts (cdr arts)))
5266     (while arts
5267       (and (or (not unread)
5268                (gnus-data-unread-p (car arts)))
5269            (vectorp (gnus-data-header (car arts)))
5270            (gnus-subject-equal
5271             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5272            (setq result (car arts)
5273                  arts nil))
5274       (setq arts (cdr arts)))
5275     (and result
5276          (goto-char (gnus-data-pos result))
5277          (gnus-data-number result))))
5278
5279 (defun gnus-summary-search-forward (&optional unread subject backward)
5280   "Search forward for an article.
5281 If UNREAD, look for unread articles.  If SUBJECT, look for
5282 articles with that subject.  If BACKWARD, search backward instead."
5283   (cond (subject (gnus-summary-find-subject subject unread backward))
5284         (backward (gnus-summary-find-prev unread))
5285         (t (gnus-summary-find-next unread))))
5286
5287 (defun gnus-recenter (&optional n)
5288   "Center point in window and redisplay frame.
5289 Also do horizontal recentering."
5290   (interactive "P")
5291   (when (and gnus-auto-center-summary
5292              (not (eq gnus-auto-center-summary 'vertical)))
5293     (gnus-horizontal-recenter))
5294   (recenter n))
5295
5296 (defun gnus-summary-recenter ()
5297   "Center point in the summary window.
5298 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5299 displayed, no centering will be performed."
5300   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5301   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5302   (interactive)
5303   (let* ((top (cond ((< (window-height) 4) 0)
5304                     ((< (window-height) 7) 1)
5305                     (t (if (numberp gnus-auto-center-summary)
5306                            gnus-auto-center-summary
5307                          2))))
5308          (height (1- (window-height)))
5309          (bottom (save-excursion (goto-char (point-max))
5310                                  (forward-line (- height))
5311                                  (point)))
5312          (window (get-buffer-window (current-buffer))))
5313     ;; The user has to want it.
5314     (when gnus-auto-center-summary
5315       (when (get-buffer-window gnus-article-buffer)
5316         ;; Only do recentering when the article buffer is displayed,
5317         ;; Set the window start to either `bottom', which is the biggest
5318         ;; possible valid number, or the second line from the top,
5319         ;; whichever is the least.
5320         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5321           (if (> bottom top-pos)
5322               ;; Keep the second line from the top visible
5323               (set-window-start window top-pos t)
5324             ;; Try to keep the bottom line visible; if it's partially
5325             ;; obscured, either scroll one more line to make it fully
5326             ;; visible, or revert to using TOP-POS.
5327             (save-excursion
5328               (goto-char (point-max))
5329               (forward-line -1)
5330               (let ((last-line-start (point)))
5331                 (goto-char bottom)
5332                 (set-window-start window (point) t)
5333                 (when (not (pos-visible-in-window-p last-line-start window))
5334                   (forward-line 1)
5335                   (set-window-start window (min (point) top-pos) t)))))))
5336       ;; Do horizontal recentering while we're at it.
5337       (when (and (get-buffer-window (current-buffer) t)
5338                  (not (eq gnus-auto-center-summary 'vertical)))
5339         (let ((selected (selected-window)))
5340           (select-window (get-buffer-window (current-buffer) t))
5341           (gnus-summary-position-point)
5342           (gnus-horizontal-recenter)
5343           (select-window selected))))))
5344
5345 (defun gnus-summary-jump-to-group (newsgroup)
5346   "Move point to NEWSGROUP in group mode buffer."
5347   ;; Keep update point of group mode buffer if visible.
5348   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5349       (save-window-excursion
5350         ;; Take care of tree window mode.
5351         (when (get-buffer-window gnus-group-buffer)
5352           (pop-to-buffer gnus-group-buffer))
5353         (gnus-group-jump-to-group newsgroup))
5354     (save-excursion
5355       ;; Take care of tree window mode.
5356       (if (get-buffer-window gnus-group-buffer)
5357           (pop-to-buffer gnus-group-buffer)
5358         (set-buffer gnus-group-buffer))
5359       (gnus-group-jump-to-group newsgroup))))
5360
5361 ;; This function returns a list of article numbers based on the
5362 ;; difference between the ranges of read articles in this group and
5363 ;; the range of active articles.
5364 (defun gnus-list-of-unread-articles (group)
5365   (let* ((read (gnus-info-read (gnus-get-info group)))
5366          (active (or (gnus-active group) (gnus-activate-group group)))
5367          (last (cdr active))
5368          first nlast unread)
5369     ;; If none are read, then all are unread.
5370     (if (not read)
5371         (setq first (car active))
5372       ;; If the range of read articles is a single range, then the
5373       ;; first unread article is the article after the last read
5374       ;; article.  Sounds logical, doesn't it?
5375       (if (and (not (listp (cdr read)))
5376                (or (< (car read) (car active))
5377                    (progn (setq read (list read))
5378                           nil)))
5379           (setq first (max (car active) (1+ (cdr read))))
5380         ;; `read' is a list of ranges.
5381         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5382                                   (caar read)))
5383                   1)
5384           (setq first (car active)))
5385         (while read
5386           (when first
5387             (while (< first nlast)
5388               (push first unread)
5389               (setq first (1+ first))))
5390           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5391           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5392           (setq read (cdr read)))))
5393     ;; And add the last unread articles.
5394     (while (<= first last)
5395       (push first unread)
5396       (setq first (1+ first)))
5397     ;; Return the list of unread articles.
5398     (delq 0 (nreverse unread))))
5399
5400 (defun gnus-list-of-read-articles (group)
5401   "Return a list of unread, unticked and non-dormant articles."
5402   (let* ((info (gnus-get-info group))
5403          (marked (gnus-info-marks info))
5404          (active (gnus-active group)))
5405     (and info active
5406          (gnus-set-difference
5407           (gnus-sorted-complement
5408            (gnus-uncompress-range active)
5409            (gnus-list-of-unread-articles group))
5410           (append
5411            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5412            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5413
5414 ;; Various summary commands
5415
5416 (defun gnus-summary-select-article-buffer ()
5417   "Reconfigure windows to show article buffer."
5418   (interactive)
5419   (if (not (gnus-buffer-live-p gnus-article-buffer))
5420       (error "There is no article buffer for this summary buffer")
5421     (gnus-configure-windows 'article)
5422     (select-window (get-buffer-window gnus-article-buffer))))
5423
5424 (defun gnus-summary-universal-argument (arg)
5425   "Perform any operation on all articles that are process/prefixed."
5426   (interactive "P")
5427   (let ((articles (gnus-summary-work-articles arg))
5428         func article)
5429     (if (eq
5430          (setq
5431           func
5432           (key-binding
5433            (read-key-sequence
5434             (substitute-command-keys
5435              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5436          'undefined)
5437         (gnus-error 1 "Undefined key")
5438       (save-excursion
5439         (while articles
5440           (gnus-summary-goto-subject (setq article (pop articles)))
5441           (let (gnus-newsgroup-processable)
5442             (command-execute func))
5443           (gnus-summary-remove-process-mark article)))))
5444   (gnus-summary-position-point))
5445
5446 (defun gnus-summary-toggle-truncation (&optional arg)
5447   "Toggle truncation of summary lines.
5448 With arg, turn line truncation on iff arg is positive."
5449   (interactive "P")
5450   (setq truncate-lines
5451         (if (null arg) (not truncate-lines)
5452           (> (prefix-numeric-value arg) 0)))
5453   (redraw-display))
5454
5455 (defun gnus-summary-reselect-current-group (&optional all rescan)
5456   "Exit and then reselect the current newsgroup.
5457 The prefix argument ALL means to select all articles."
5458   (interactive "P")
5459   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5460     (error "Ephemeral groups can't be reselected"))
5461   (let ((current-subject (gnus-summary-article-number))
5462         (group gnus-newsgroup-name))
5463     (setq gnus-newsgroup-begin nil)
5464     (gnus-summary-exit)
5465     ;; We have to adjust the point of group mode buffer because
5466     ;; point was moved to the next unread newsgroup by exiting.
5467     (gnus-summary-jump-to-group group)
5468     (when rescan
5469       (save-excursion
5470         (gnus-group-get-new-news-this-group 1)))
5471     (gnus-group-read-group all t)
5472     (gnus-summary-goto-subject current-subject nil t)))
5473
5474 (defun gnus-summary-rescan-group (&optional all)
5475   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5476   (interactive "P")
5477   (gnus-summary-reselect-current-group all t))
5478
5479 (defun gnus-summary-update-info (&optional non-destructive)
5480   (save-excursion
5481     (let ((group gnus-newsgroup-name))
5482       (when group
5483         (when gnus-newsgroup-kill-headers
5484           (setq gnus-newsgroup-killed
5485                 (gnus-compress-sequence
5486                  (nconc
5487                   (gnus-set-sorted-intersection
5488                    (gnus-uncompress-range gnus-newsgroup-killed)
5489                    (setq gnus-newsgroup-unselected
5490                          (sort gnus-newsgroup-unselected '<)))
5491                   (setq gnus-newsgroup-unreads
5492                         (sort gnus-newsgroup-unreads '<)))
5493                  t)))
5494         (unless (listp (cdr gnus-newsgroup-killed))
5495           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5496         (let ((headers gnus-newsgroup-headers))
5497           ;; Set the new ranges of read articles.
5498           (save-excursion
5499             (set-buffer gnus-group-buffer)
5500             (gnus-undo-force-boundary))
5501           (gnus-update-read-articles
5502            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5503           ;; Set the current article marks.
5504           (let ((gnus-newsgroup-scored
5505                  (if (and (not gnus-save-score)
5506                           (not non-destructive))
5507                      nil
5508                    gnus-newsgroup-scored)))
5509             (save-excursion
5510               (gnus-update-marks)))
5511           ;; Do the cross-ref thing.
5512           (when gnus-use-cross-reference
5513             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5514           ;; Do not switch windows but change the buffer to work.
5515           (set-buffer gnus-group-buffer)
5516           (unless (gnus-ephemeral-group-p group)
5517             (gnus-group-update-group group)))))))
5518
5519 (defun gnus-summary-save-newsrc (&optional force)
5520   "Save the current number of read/marked articles in the dribble buffer.
5521 The dribble buffer will then be saved.
5522 If FORCE (the prefix), also save the .newsrc file(s)."
5523   (interactive "P")
5524   (gnus-summary-update-info t)
5525   (if force
5526       (gnus-save-newsrc-file)
5527     (gnus-dribble-save)))
5528
5529 (defun gnus-summary-exit (&optional temporary)
5530   "Exit reading current newsgroup, and then return to group selection mode.
5531 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5532   (interactive)
5533   (gnus-set-global-variables)
5534   (when (gnus-buffer-live-p gnus-article-buffer)
5535     (save-excursion
5536       (set-buffer gnus-article-buffer)
5537       (mm-destroy-parts gnus-article-mime-handles)
5538       ;; Set it to nil for safety reason.
5539       (setq gnus-article-mime-handle-alist nil)
5540       (setq gnus-article-mime-handles nil)))
5541   (gnus-kill-save-kill-buffer)
5542   (gnus-async-halt-prefetch)
5543   (let* ((group gnus-newsgroup-name)
5544          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5545          (mode major-mode)
5546          (group-point nil)
5547          (buf (current-buffer)))
5548     (unless quit-config
5549       ;; Do adaptive scoring, and possibly save score files.
5550       (when gnus-newsgroup-adaptive
5551         (gnus-score-adaptive))
5552       (when gnus-use-scoring
5553         (gnus-score-save)))
5554     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5555     ;; If we have several article buffers, we kill them at exit.
5556     (unless gnus-single-article-buffer
5557       (gnus-kill-buffer gnus-original-article-buffer)
5558       (setq gnus-article-current nil))
5559     (when gnus-use-cache
5560       (gnus-cache-possibly-remove-articles)
5561       (gnus-cache-save-buffers))
5562     (gnus-async-prefetch-remove-group group)
5563     (when gnus-suppress-duplicates
5564       (gnus-dup-enter-articles))
5565     (when gnus-use-trees
5566       (gnus-tree-close group))
5567     (when gnus-use-cache
5568       (gnus-cache-write-active))
5569     ;; Remove entries for this group.
5570     (nnmail-purge-split-history (gnus-group-real-name group))
5571     ;; Make all changes in this group permanent.
5572     (unless quit-config
5573       (gnus-run-hooks 'gnus-exit-group-hook)
5574       (gnus-summary-update-info))
5575     (gnus-close-group group)
5576     ;; Make sure where we were, and go to next newsgroup.
5577     (set-buffer gnus-group-buffer)
5578     (unless quit-config
5579       (gnus-group-jump-to-group group))
5580     (gnus-run-hooks 'gnus-summary-exit-hook)
5581     (unless (or quit-config
5582                 ;; If this group has disappeared from the summary
5583                 ;; buffer, don't skip forwards.
5584                 (not (string= group (gnus-group-group-name))))
5585       (gnus-group-next-unread-group 1))
5586     (setq group-point (point))
5587     (if temporary
5588         nil                             ;Nothing to do.
5589       ;; If we have several article buffers, we kill them at exit.
5590       (unless gnus-single-article-buffer
5591         (gnus-kill-buffer gnus-article-buffer)
5592         (gnus-kill-buffer gnus-original-article-buffer)
5593         (setq gnus-article-current nil))
5594       (set-buffer buf)
5595       (if (not gnus-kill-summary-on-exit)
5596           (gnus-deaden-summary)
5597         ;; We set all buffer-local variables to nil.  It is unclear why
5598         ;; this is needed, but if we don't, buffer-local variables are
5599         ;; not garbage-collected, it seems.  This would the lead to en
5600         ;; ever-growing Emacs.
5601         (gnus-summary-clear-local-variables)
5602         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5603           (gnus-summary-clear-local-variables))
5604         (when (get-buffer gnus-article-buffer)
5605           (bury-buffer gnus-article-buffer))
5606         ;; We clear the global counterparts of the buffer-local
5607         ;; variables as well, just to be on the safe side.
5608         (set-buffer gnus-group-buffer)
5609         (gnus-summary-clear-local-variables)
5610         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5611           (gnus-summary-clear-local-variables))
5612         ;; Return to group mode buffer.
5613         (when (eq mode 'gnus-summary-mode)
5614           (gnus-kill-buffer buf)))
5615       (setq gnus-current-select-method gnus-select-method)
5616       (pop-to-buffer gnus-group-buffer)
5617       (if (not quit-config)
5618           (progn
5619             (goto-char group-point)
5620             (gnus-configure-windows 'group 'force))
5621         (gnus-handle-ephemeral-exit quit-config))
5622       ;; Clear the current group name.
5623       (unless quit-config
5624         (setq gnus-newsgroup-name nil)))))
5625
5626 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5627 (defun gnus-summary-exit-no-update (&optional no-questions)
5628   "Quit reading current newsgroup without updating read article info."
5629   (interactive)
5630   (let* ((group gnus-newsgroup-name)
5631          (quit-config (gnus-group-quit-config group)))
5632     (when (or no-questions
5633               gnus-expert-user
5634               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5635       (gnus-async-halt-prefetch)
5636       (mapcar 'funcall
5637               (delq 'gnus-summary-expire-articles
5638                     (copy-sequence gnus-summary-prepare-exit-hook)))
5639       (when (gnus-buffer-live-p gnus-article-buffer)
5640         (save-excursion
5641           (set-buffer gnus-article-buffer)
5642           (mm-destroy-parts gnus-article-mime-handles)
5643           ;; Set it to nil for safety reason.
5644           (setq gnus-article-mime-handle-alist nil)
5645           (setq gnus-article-mime-handles nil)))
5646       ;; If we have several article buffers, we kill them at exit.
5647       (unless gnus-single-article-buffer
5648         (gnus-kill-buffer gnus-article-buffer)
5649         (gnus-kill-buffer gnus-original-article-buffer)
5650         (setq gnus-article-current nil))
5651       (if (not gnus-kill-summary-on-exit)
5652           (gnus-deaden-summary)
5653         (gnus-close-group group)
5654         (gnus-summary-clear-local-variables)
5655         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5656           (gnus-summary-clear-local-variables))
5657         (set-buffer gnus-group-buffer)
5658         (gnus-summary-clear-local-variables)
5659         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5660           (gnus-summary-clear-local-variables))
5661         (when (get-buffer gnus-summary-buffer)
5662           (kill-buffer gnus-summary-buffer)))
5663       (unless gnus-single-article-buffer
5664         (setq gnus-article-current nil))
5665       (when gnus-use-trees
5666         (gnus-tree-close group))
5667       (gnus-async-prefetch-remove-group group)
5668       (when (get-buffer gnus-article-buffer)
5669         (bury-buffer gnus-article-buffer))
5670       ;; Return to the group buffer.
5671       (gnus-configure-windows 'group 'force)
5672       ;; Clear the current group name.
5673       (setq gnus-newsgroup-name nil)
5674       (when (equal (gnus-group-group-name) group)
5675         (gnus-group-next-unread-group 1))
5676       (when quit-config
5677         (gnus-handle-ephemeral-exit quit-config)))))
5678
5679 (defun gnus-handle-ephemeral-exit (quit-config)
5680   "Handle movement when leaving an ephemeral group.
5681 The state which existed when entering the ephemeral is reset."
5682   (if (not (buffer-name (car quit-config)))
5683       (gnus-configure-windows 'group 'force)
5684     (set-buffer (car quit-config))
5685     (cond ((eq major-mode 'gnus-summary-mode)
5686            (gnus-set-global-variables))
5687           ((eq major-mode 'gnus-article-mode)
5688            (save-excursion
5689              ;; The `gnus-summary-buffer' variable may point
5690              ;; to the old summary buffer when using a single
5691              ;; article buffer.
5692              (unless (gnus-buffer-live-p gnus-summary-buffer)
5693                (set-buffer gnus-group-buffer))
5694              (set-buffer gnus-summary-buffer)
5695              (gnus-set-global-variables))))
5696     (if (or (eq (cdr quit-config) 'article)
5697             (eq (cdr quit-config) 'pick))
5698         (progn
5699           ;; The current article may be from the ephemeral group
5700           ;; thus it is best that we reload this article
5701           (gnus-summary-show-article)
5702           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5703               (gnus-configure-windows 'pick 'force)
5704             (gnus-configure-windows (cdr quit-config) 'force)))
5705       (gnus-configure-windows (cdr quit-config) 'force))
5706     (when (eq major-mode 'gnus-summary-mode)
5707       (gnus-summary-next-subject 1 nil t)
5708       (gnus-summary-recenter)
5709       (gnus-summary-position-point))))
5710
5711 ;;; Dead summaries.
5712
5713 (defvar gnus-dead-summary-mode-map nil)
5714
5715 (unless gnus-dead-summary-mode-map
5716   (setq gnus-dead-summary-mode-map (make-keymap))
5717   (suppress-keymap gnus-dead-summary-mode-map)
5718   (substitute-key-definition
5719    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5720   (let ((keys '("\C-d" "\r" "\177" [delete])))
5721     (while keys
5722       (define-key gnus-dead-summary-mode-map
5723         (pop keys) 'gnus-summary-wake-up-the-dead))))
5724
5725 (defvar gnus-dead-summary-mode nil
5726   "Minor mode for Gnus summary buffers.")
5727
5728 (defun gnus-dead-summary-mode (&optional arg)
5729   "Minor mode for Gnus summary buffers."
5730   (interactive "P")
5731   (when (eq major-mode 'gnus-summary-mode)
5732     (make-local-variable 'gnus-dead-summary-mode)
5733     (setq gnus-dead-summary-mode
5734           (if (null arg) (not gnus-dead-summary-mode)
5735             (> (prefix-numeric-value arg) 0)))
5736     (when gnus-dead-summary-mode
5737       (gnus-add-minor-mode
5738        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5739
5740 (defun gnus-deaden-summary ()
5741   "Make the current summary buffer into a dead summary buffer."
5742   ;; Kill any previous dead summary buffer.
5743   (when (and gnus-dead-summary
5744              (buffer-name gnus-dead-summary))
5745     (save-excursion
5746       (set-buffer gnus-dead-summary)
5747       (when gnus-dead-summary-mode
5748         (kill-buffer (current-buffer)))))
5749   ;; Make this the current dead summary.
5750   (setq gnus-dead-summary (current-buffer))
5751   (gnus-dead-summary-mode 1)
5752   (let ((name (buffer-name)))
5753     (when (string-match "Summary" name)
5754       (rename-buffer
5755        (concat (substring name 0 (match-beginning 0)) "Dead "
5756                (substring name (match-beginning 0)))
5757        t)
5758       (bury-buffer))))
5759
5760 (defun gnus-kill-or-deaden-summary (buffer)
5761   "Kill or deaden the summary BUFFER."
5762   (save-excursion
5763     (when (and (buffer-name buffer)
5764                (not gnus-single-article-buffer))
5765       (save-excursion
5766         (set-buffer buffer)
5767         (gnus-kill-buffer gnus-article-buffer)
5768         (gnus-kill-buffer gnus-original-article-buffer)))
5769     (cond (gnus-kill-summary-on-exit
5770            (when (and gnus-use-trees
5771                       (gnus-buffer-exists-p buffer))
5772              (save-excursion
5773                (set-buffer buffer)
5774                (gnus-tree-close gnus-newsgroup-name)))
5775            (gnus-kill-buffer buffer))
5776           ((gnus-buffer-exists-p buffer)
5777            (save-excursion
5778              (set-buffer buffer)
5779              (gnus-deaden-summary))))))
5780
5781 (defun gnus-summary-wake-up-the-dead (&rest args)
5782   "Wake up the dead summary buffer."
5783   (interactive)
5784   (gnus-dead-summary-mode -1)
5785   (let ((name (buffer-name)))
5786     (when (string-match "Dead " name)
5787       (rename-buffer
5788        (concat (substring name 0 (match-beginning 0))
5789                (substring name (match-end 0)))
5790        t)))
5791   (gnus-message 3 "This dead summary is now alive again"))
5792
5793 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5794 (defun gnus-summary-fetch-faq (&optional faq-dir)
5795   "Fetch the FAQ for the current group.
5796 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5797 in."
5798   (interactive
5799    (list
5800     (when current-prefix-arg
5801       (completing-read
5802        "Faq dir: " (and (listp gnus-group-faq-directory)
5803                         (mapcar (lambda (file) (list file))
5804                                 gnus-group-faq-directory))))))
5805   (let (gnus-faq-buffer)
5806     (when (setq gnus-faq-buffer
5807                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5808       (gnus-configure-windows 'summary-faq))))
5809
5810 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5811 (defun gnus-summary-describe-group (&optional force)
5812   "Describe the current newsgroup."
5813   (interactive "P")
5814   (gnus-group-describe-group force gnus-newsgroup-name))
5815
5816 (defun gnus-summary-describe-briefly ()
5817   "Describe summary mode commands briefly."
5818   (interactive)
5819   (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")))
5820
5821 ;; Walking around group mode buffer from summary mode.
5822
5823 (defun gnus-summary-next-group (&optional no-article target-group backward)
5824   "Exit current newsgroup and then select next unread newsgroup.
5825 If prefix argument NO-ARTICLE is non-nil, no article is selected
5826 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5827 previous group instead."
5828   (interactive "P")
5829   ;; Stop pre-fetching.
5830   (gnus-async-halt-prefetch)
5831   (let ((current-group gnus-newsgroup-name)
5832         (current-buffer (current-buffer))
5833         entered)
5834     ;; First we semi-exit this group to update Xrefs and all variables.
5835     ;; We can't do a real exit, because the window conf must remain
5836     ;; the same in case the user is prompted for info, and we don't
5837     ;; want the window conf to change before that...
5838     (gnus-summary-exit t)
5839     (while (not entered)
5840       ;; Then we find what group we are supposed to enter.
5841       (set-buffer gnus-group-buffer)
5842       (gnus-group-jump-to-group current-group)
5843       (setq target-group
5844             (or target-group
5845                 (if (eq gnus-keep-same-level 'best)
5846                     (gnus-summary-best-group gnus-newsgroup-name)
5847                   (gnus-summary-search-group backward gnus-keep-same-level))))
5848       (if (not target-group)
5849           ;; There are no further groups, so we return to the group
5850           ;; buffer.
5851           (progn
5852             (gnus-message 5 "Returning to the group buffer")
5853             (setq entered t)
5854             (when (gnus-buffer-live-p current-buffer)
5855               (set-buffer current-buffer)
5856               (gnus-summary-exit))
5857             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5858         ;; We try to enter the target group.
5859         (gnus-group-jump-to-group target-group)
5860         (let ((unreads (gnus-group-group-unread)))
5861           (if (and (or (eq t unreads)
5862                        (and unreads (not (zerop unreads))))
5863                    (gnus-summary-read-group
5864                     target-group nil no-article
5865                     (and (buffer-name current-buffer) current-buffer)
5866                     nil backward))
5867               (setq entered t)
5868             (setq current-group target-group
5869                   target-group nil)))))))
5870
5871 (defun gnus-summary-prev-group (&optional no-article)
5872   "Exit current newsgroup and then select previous unread newsgroup.
5873 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5874   (interactive "P")
5875   (gnus-summary-next-group no-article nil t))
5876
5877 ;; Walking around summary lines.
5878
5879 (defun gnus-summary-first-subject (&optional unread undownloaded)
5880   "Go to the first unread subject.
5881 If UNREAD is non-nil, go to the first unread article.
5882 Returns the article selected or nil if there are no unread articles."
5883   (interactive "P")
5884   (prog1
5885       (cond
5886        ;; Empty summary.
5887        ((null gnus-newsgroup-data)
5888         (gnus-message 3 "No articles in the group")
5889         nil)
5890        ;; Pick the first article.
5891        ((not unread)
5892         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5893         (gnus-data-number (car gnus-newsgroup-data)))
5894        ;; No unread articles.
5895        ((null gnus-newsgroup-unreads)
5896         (gnus-message 3 "No more unread articles")
5897         nil)
5898        ;; Find the first unread article.
5899        (t
5900         (let ((data gnus-newsgroup-data))
5901           (while (and data
5902                       (and (not (and undownloaded
5903                                      (eq gnus-undownloaded-mark
5904                                          (gnus-data-mark (car data)))))
5905                            (not (gnus-data-unread-p (car data)))))
5906             (setq data (cdr data)))
5907           (when data
5908             (goto-char (gnus-data-pos (car data)))
5909             (gnus-data-number (car data))))))
5910     (gnus-summary-position-point)))
5911
5912 (defun gnus-summary-next-subject (n &optional unread dont-display)
5913   "Go to next N'th summary line.
5914 If N is negative, go to the previous N'th subject line.
5915 If UNREAD is non-nil, only unread articles are selected.
5916 The difference between N and the actual number of steps taken is
5917 returned."
5918   (interactive "p")
5919   (let ((backward (< n 0))
5920         (n (abs n)))
5921     (while (and (> n 0)
5922                 (if backward
5923                     (gnus-summary-find-prev unread)
5924                   (gnus-summary-find-next unread)))
5925       (unless (zerop (setq n (1- n)))
5926         (gnus-summary-show-thread)))
5927     (when (/= 0 n)
5928       (gnus-message 7 "No more%s articles"
5929                     (if unread " unread" "")))
5930     (unless dont-display
5931       (gnus-summary-recenter)
5932       (gnus-summary-position-point))
5933     n))
5934
5935 (defun gnus-summary-next-unread-subject (n)
5936   "Go to next N'th unread summary line."
5937   (interactive "p")
5938   (gnus-summary-next-subject n t))
5939
5940 (defun gnus-summary-prev-subject (n &optional unread)
5941   "Go to previous N'th summary line.
5942 If optional argument UNREAD is non-nil, only unread article is selected."
5943   (interactive "p")
5944   (gnus-summary-next-subject (- n) unread))
5945
5946 (defun gnus-summary-prev-unread-subject (n)
5947   "Go to previous N'th unread summary line."
5948   (interactive "p")
5949   (gnus-summary-next-subject (- n) t))
5950
5951 (defun gnus-summary-goto-subject (article &optional force silent)
5952   "Go the subject line of ARTICLE.
5953 If FORCE, also allow jumping to articles not currently shown."
5954   (interactive "nArticle number: ")
5955   (let ((b (point))
5956         (data (gnus-data-find article)))
5957     ;; We read in the article if we have to.
5958     (and (not data)
5959          force
5960          (gnus-summary-insert-subject
5961           article
5962           (if (or (numberp force) (vectorp force)) force)
5963           t)
5964          (setq data (gnus-data-find article)))
5965     (goto-char b)
5966     (if (not data)
5967         (progn
5968           (unless silent
5969             (gnus-message 3 "Can't find article %d" article))
5970           nil)
5971       (goto-char (gnus-data-pos data))
5972       (gnus-summary-position-point)
5973       article)))
5974
5975 ;; Walking around summary lines with displaying articles.
5976
5977 (defun gnus-summary-expand-window (&optional arg)
5978   "Make the summary buffer take up the entire Emacs frame.
5979 Given a prefix, will force an `article' buffer configuration."
5980   (interactive "P")
5981   (if arg
5982       (gnus-configure-windows 'article 'force)
5983     (gnus-configure-windows 'summary 'force)))
5984
5985 (defun gnus-summary-display-article (article &optional all-header)
5986   "Display ARTICLE in article buffer."
5987   (when (gnus-buffer-live-p gnus-article-buffer)
5988     (with-current-buffer gnus-article-buffer
5989       (mm-enable-multibyte-mule4)))
5990   (gnus-set-global-variables)
5991   (when (gnus-buffer-live-p gnus-article-buffer)
5992     (with-current-buffer gnus-article-buffer
5993       (setq gnus-article-charset gnus-newsgroup-charset)
5994       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
5995       (mm-enable-multibyte-mule4)))
5996   (if (null article)
5997       nil
5998     (prog1
5999         (if gnus-summary-display-article-function
6000             (funcall gnus-summary-display-article-function article all-header)
6001           (gnus-article-prepare article all-header))
6002       (gnus-run-hooks 'gnus-select-article-hook)
6003       (when (and gnus-current-article
6004                  (not (zerop gnus-current-article)))
6005         (gnus-summary-goto-subject gnus-current-article))
6006       (gnus-summary-recenter)
6007       (when (and gnus-use-trees gnus-show-threads)
6008         (gnus-possibly-generate-tree article)
6009         (gnus-highlight-selected-tree article))
6010       ;; Successfully display article.
6011       (gnus-article-set-window-start
6012        (cdr (assq article gnus-newsgroup-bookmarks))))))
6013
6014 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6015   "Select the current article.
6016 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6017 non-nil, the article will be re-fetched even if it already present in
6018 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6019 be displayed."
6020   ;; Make sure we are in the summary buffer to work around bbdb bug.
6021   (unless (eq major-mode 'gnus-summary-mode)
6022     (set-buffer gnus-summary-buffer))
6023   (let ((article (or article (gnus-summary-article-number)))
6024         (all-headers (not (not all-headers))) ;Must be T or NIL.
6025         gnus-summary-display-article-function)
6026     (and (not pseudo)
6027          (gnus-summary-article-pseudo-p article)
6028          (error "This is a pseudo-article"))
6029     (save-excursion
6030       (set-buffer gnus-summary-buffer)
6031       (if (or (and gnus-single-article-buffer
6032                    (or (null gnus-current-article)
6033                        (null gnus-article-current)
6034                        (null (get-buffer gnus-article-buffer))
6035                        (not (eq article (cdr gnus-article-current)))
6036                        (not (equal (car gnus-article-current)
6037                                    gnus-newsgroup-name))))
6038               (and (not gnus-single-article-buffer)
6039                    (or (null gnus-current-article)
6040                        (not (eq gnus-current-article article))))
6041               force)
6042           ;; The requested article is different from the current article.
6043           (progn
6044             (gnus-summary-display-article article all-headers)
6045             (when (gnus-buffer-live-p gnus-article-buffer)
6046                (with-current-buffer gnus-article-buffer
6047                 (if (not gnus-article-decoded-p) ;; a local variable
6048                     (mm-disable-multibyte-mule4))))
6049             (when (or all-headers gnus-show-all-headers)
6050               (gnus-article-show-all-headers))
6051             (gnus-article-set-window-start
6052              (cdr (assq article gnus-newsgroup-bookmarks)))
6053             article)
6054         (when (or all-headers gnus-show-all-headers)
6055           (gnus-article-show-all-headers))
6056         'old))))
6057
6058 (defun gnus-summary-force-verify-and-decrypt ()
6059   (interactive)
6060   (let ((mm-verify-option 'known)
6061         (mm-decrypt-option 'known))
6062     (gnus-summary-select-article nil 'force)))
6063
6064 (defun gnus-summary-set-current-mark (&optional current-mark)
6065   "Obsolete function."
6066   nil)
6067
6068 (defun gnus-summary-next-article (&optional unread subject backward push)
6069   "Select the next article.
6070 If UNREAD, only unread articles are selected.
6071 If SUBJECT, only articles with SUBJECT are selected.
6072 If BACKWARD, the previous article is selected instead of the next."
6073   (interactive "P")
6074   (cond
6075    ;; Is there such an article?
6076    ((and (gnus-summary-search-forward unread subject backward)
6077          (or (gnus-summary-display-article (gnus-summary-article-number))
6078              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6079     (gnus-summary-position-point))
6080    ;; If not, we try the first unread, if that is wanted.
6081    ((and subject
6082          gnus-auto-select-same
6083          (gnus-summary-first-unread-article))
6084     (gnus-summary-position-point)
6085     (gnus-message 6 "Wrapped"))
6086    ;; Try to get next/previous article not displayed in this group.
6087    ((and gnus-auto-extend-newsgroup
6088          (not unread) (not subject))
6089     (gnus-summary-goto-article
6090      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6091      nil (count-lines (point-min) (point))))
6092    ;; Go to next/previous group.
6093    (t
6094     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6095       (gnus-summary-jump-to-group gnus-newsgroup-name))
6096     (let ((cmd last-command-char)
6097           (point
6098            (save-excursion
6099              (set-buffer gnus-group-buffer)
6100              (point)))
6101           (group
6102            (if (eq gnus-keep-same-level 'best)
6103                (gnus-summary-best-group gnus-newsgroup-name)
6104              (gnus-summary-search-group backward gnus-keep-same-level))))
6105       ;; For some reason, the group window gets selected.  We change
6106       ;; it back.
6107       (select-window (get-buffer-window (current-buffer)))
6108       ;; Select next unread newsgroup automagically.
6109       (cond
6110        ((or (not gnus-auto-select-next)
6111             (not cmd))
6112         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6113        ((or (eq gnus-auto-select-next 'quietly)
6114             (and (eq gnus-auto-select-next 'slightly-quietly)
6115                  push)
6116             (and (eq gnus-auto-select-next 'almost-quietly)
6117                  (gnus-summary-last-article-p)))
6118         ;; Select quietly.
6119         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6120             (gnus-summary-exit)
6121           (gnus-message 7 "No more%s articles (%s)..."
6122                         (if unread " unread" "")
6123                         (if group (concat "selecting " group)
6124                           "exiting"))
6125           (gnus-summary-next-group nil group backward)))
6126        (t
6127         (when (gnus-key-press-event-p last-input-event)
6128           (gnus-summary-walk-group-buffer
6129            gnus-newsgroup-name cmd unread backward point))))))))
6130
6131 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6132   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6133                       (?\C-p (gnus-group-prev-unread-group 1))))
6134         (cursor-in-echo-area t)
6135         keve key group ended)
6136     (save-excursion
6137       (set-buffer gnus-group-buffer)
6138       (goto-char start)
6139       (setq group
6140             (if (eq gnus-keep-same-level 'best)
6141                 (gnus-summary-best-group gnus-newsgroup-name)
6142               (gnus-summary-search-group backward gnus-keep-same-level))))
6143     (while (not ended)
6144       (gnus-message
6145        5 "No more%s articles%s" (if unread " unread" "")
6146        (if (and group
6147                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6148            (format " (Type %s for %s [%s])"
6149                    (single-key-description cmd) group
6150                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6151          (format " (Type %s to exit %s)"
6152                  (single-key-description cmd)
6153                  gnus-newsgroup-name)))
6154       ;; Confirm auto selection.
6155       (setq key (car (setq keve (gnus-read-event-char))))
6156       (setq ended t)
6157       (cond
6158        ((assq key keystrokes)
6159         (let ((obuf (current-buffer)))
6160           (switch-to-buffer gnus-group-buffer)
6161           (when group
6162             (gnus-group-jump-to-group group))
6163           (eval (cadr (assq key keystrokes)))
6164           (setq group (gnus-group-group-name))
6165           (switch-to-buffer obuf))
6166         (setq ended nil))
6167        ((equal key cmd)
6168         (if (or (not group)
6169                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6170             (gnus-summary-exit)
6171           (gnus-summary-next-group nil group backward)))
6172        (t
6173         (push (cdr keve) unread-command-events))))))
6174
6175 (defun gnus-summary-next-unread-article ()
6176   "Select unread article after current one."
6177   (interactive)
6178   (gnus-summary-next-article
6179    (or (not (eq gnus-summary-goto-unread 'never))
6180        (gnus-summary-last-article-p (gnus-summary-article-number)))
6181    (and gnus-auto-select-same
6182         (gnus-summary-article-subject))))
6183
6184 (defun gnus-summary-prev-article (&optional unread subject)
6185   "Select the article after the current one.
6186 If UNREAD is non-nil, only unread articles are selected."
6187   (interactive "P")
6188   (gnus-summary-next-article unread subject t))
6189
6190 (defun gnus-summary-prev-unread-article ()
6191   "Select unread article before current one."
6192   (interactive)
6193   (gnus-summary-prev-article
6194    (or (not (eq gnus-summary-goto-unread 'never))
6195        (gnus-summary-first-article-p (gnus-summary-article-number)))
6196    (and gnus-auto-select-same
6197         (gnus-summary-article-subject))))
6198
6199 (defun gnus-summary-next-page (&optional lines circular)
6200   "Show next page of the selected article.
6201 If at the end of the current article, select the next article.
6202 LINES says how many lines should be scrolled up.
6203
6204 If CIRCULAR is non-nil, go to the start of the article instead of
6205 selecting the next article when reaching the end of the current
6206 article."
6207   (interactive "P")
6208   (setq gnus-summary-buffer (current-buffer))
6209   (gnus-set-global-variables)
6210   (let ((article (gnus-summary-article-number))
6211         (article-window (get-buffer-window gnus-article-buffer t))
6212         endp)
6213     ;; If the buffer is empty, we have no article.
6214     (unless article
6215       (error "No article to select"))
6216     (gnus-configure-windows 'article)
6217     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6218         (if (and (eq gnus-summary-goto-unread 'never)
6219                  (not (gnus-summary-last-article-p article)))
6220             (gnus-summary-next-article)
6221           (gnus-summary-next-unread-article))
6222       (if (or (null gnus-current-article)
6223               (null gnus-article-current)
6224               (/= article (cdr gnus-article-current))
6225               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6226           ;; Selected subject is different from current article's.
6227           (gnus-summary-display-article article)
6228         (when article-window
6229           (gnus-eval-in-buffer-window gnus-article-buffer
6230             (setq endp (gnus-article-next-page lines)))
6231           (when endp
6232             (cond (circular
6233                    (gnus-summary-beginning-of-article))
6234                   (lines
6235                    (gnus-message 3 "End of message"))
6236                   ((null lines)
6237                    (if (and (eq gnus-summary-goto-unread 'never)
6238                             (not (gnus-summary-last-article-p article)))
6239                        (gnus-summary-next-article)
6240                      (gnus-summary-next-unread-article))))))))
6241     (gnus-summary-recenter)
6242     (gnus-summary-position-point)))
6243
6244 (defun gnus-summary-prev-page (&optional lines move)
6245   "Show previous page of selected article.
6246 Argument LINES specifies lines to be scrolled down.
6247 If MOVE, move to the previous unread article if point is at
6248 the beginning of the buffer."
6249   (interactive "P")
6250   (let ((article (gnus-summary-article-number))
6251         (article-window (get-buffer-window gnus-article-buffer t))
6252         endp)
6253     (gnus-configure-windows 'article)
6254     (if (or (null gnus-current-article)
6255             (null gnus-article-current)
6256             (/= article (cdr gnus-article-current))
6257             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6258         ;; Selected subject is different from current article's.
6259         (gnus-summary-display-article article)
6260       (gnus-summary-recenter)
6261       (when article-window
6262         (gnus-eval-in-buffer-window gnus-article-buffer
6263           (setq endp (gnus-article-prev-page lines)))
6264         (when (and move endp)
6265           (cond (lines
6266                  (gnus-message 3 "Beginning of message"))
6267                 ((null lines)
6268                  (if (and (eq gnus-summary-goto-unread 'never)
6269                           (not (gnus-summary-first-article-p article)))
6270                      (gnus-summary-prev-article)
6271                    (gnus-summary-prev-unread-article))))))))
6272   (gnus-summary-position-point))
6273
6274 (defun gnus-summary-prev-page-or-article (&optional lines)
6275   "Show previous page of selected article.
6276 Argument LINES specifies lines to be scrolled down.
6277 If at the beginning of the article, go to the next article."
6278   (interactive "P")
6279   (gnus-summary-prev-page lines t))
6280
6281 (defun gnus-summary-scroll-up (lines)
6282   "Scroll up (or down) one line current article.
6283 Argument LINES specifies lines to be scrolled up (or down if negative)."
6284   (interactive "p")
6285   (gnus-configure-windows 'article)
6286   (gnus-summary-show-thread)
6287   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6288     (gnus-eval-in-buffer-window gnus-article-buffer
6289       (cond ((> lines 0)
6290              (when (gnus-article-next-page lines)
6291                (gnus-message 3 "End of message")))
6292             ((< lines 0)
6293              (gnus-article-prev-page (- lines))))))
6294   (gnus-summary-recenter)
6295   (gnus-summary-position-point))
6296
6297 (defun gnus-summary-scroll-down (lines)
6298   "Scroll down (or up) one line current article.
6299 Argument LINES specifies lines to be scrolled down (or up if negative)."
6300   (interactive "p")
6301   (gnus-summary-scroll-up (- lines)))
6302
6303 (defun gnus-summary-next-same-subject ()
6304   "Select next article which has the same subject as current one."
6305   (interactive)
6306   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6307
6308 (defun gnus-summary-prev-same-subject ()
6309   "Select previous article which has the same subject as current one."
6310   (interactive)
6311   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6312
6313 (defun gnus-summary-next-unread-same-subject ()
6314   "Select next unread article which has the same subject as current one."
6315   (interactive)
6316   (gnus-summary-next-article t (gnus-summary-article-subject)))
6317
6318 (defun gnus-summary-prev-unread-same-subject ()
6319   "Select previous unread article which has the same subject as current one."
6320   (interactive)
6321   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6322
6323 (defun gnus-summary-first-unread-article ()
6324   "Select the first unread article.
6325 Return nil if there are no unread articles."
6326   (interactive)
6327   (prog1
6328       (when (gnus-summary-first-subject t)
6329         (gnus-summary-show-thread)
6330         (gnus-summary-first-subject t)
6331         (gnus-summary-display-article (gnus-summary-article-number)))
6332     (gnus-summary-position-point)))
6333
6334 (defun gnus-summary-first-unread-subject ()
6335   "Place the point on the subject line of the first unread article.
6336 Return nil if there are no unread articles."
6337   (interactive)
6338   (prog1
6339       (when (gnus-summary-first-subject t)
6340         (gnus-summary-show-thread)
6341         (gnus-summary-first-subject t))
6342     (gnus-summary-position-point)))
6343
6344 (defun gnus-summary-first-article ()
6345   "Select the first article.
6346 Return nil if there are no articles."
6347   (interactive)
6348   (prog1
6349       (when (gnus-summary-first-subject)
6350         (gnus-summary-show-thread)
6351         (gnus-summary-first-subject)
6352         (gnus-summary-display-article (gnus-summary-article-number)))
6353     (gnus-summary-position-point)))
6354
6355 (defun gnus-summary-best-unread-article ()
6356   "Select the unread article with the highest score."
6357   (interactive)
6358   (let ((best -1000000)
6359         (data gnus-newsgroup-data)
6360         article score)
6361     (while data
6362       (and (gnus-data-unread-p (car data))
6363            (> (setq score
6364                     (gnus-summary-article-score (gnus-data-number (car data))))
6365               best)
6366            (setq best score
6367                  article (gnus-data-number (car data))))
6368       (setq data (cdr data)))
6369     (prog1
6370         (if article
6371             (gnus-summary-goto-article article)
6372           (error "No unread articles"))
6373       (gnus-summary-position-point))))
6374
6375 (defun gnus-summary-last-subject ()
6376   "Go to the last displayed subject line in the group."
6377   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6378     (when article
6379       (gnus-summary-goto-subject article))))
6380
6381 (defun gnus-summary-goto-article (article &optional all-headers force)
6382   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6383 If ALL-HEADERS is non-nil, no header lines are hidden.
6384 If FORCE, go to the article even if it isn't displayed.  If FORCE
6385 is a number, it is the line the article is to be displayed on."
6386   (interactive
6387    (list
6388     (completing-read
6389      "Article number or Message-ID: "
6390      (mapcar (lambda (number) (list (int-to-string number)))
6391              gnus-newsgroup-limit))
6392     current-prefix-arg
6393     t))
6394   (prog1
6395       (if (and (stringp article)
6396                (string-match "@" article))
6397           (gnus-summary-refer-article article)
6398         (when (stringp article)
6399           (setq article (string-to-number article)))
6400         (if (gnus-summary-goto-subject article force)
6401             (gnus-summary-display-article article all-headers)
6402           (gnus-message 4 "Couldn't go to article %s" article) nil))
6403     (gnus-summary-position-point)))
6404
6405 (defun gnus-summary-goto-last-article ()
6406   "Go to the previously read article."
6407   (interactive)
6408   (prog1
6409       (when gnus-last-article
6410         (gnus-summary-goto-article gnus-last-article nil t))
6411     (gnus-summary-position-point)))
6412
6413 (defun gnus-summary-pop-article (number)
6414   "Pop one article off the history and go to the previous.
6415 NUMBER articles will be popped off."
6416   (interactive "p")
6417   (let (to)
6418     (setq gnus-newsgroup-history
6419           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6420     (if to
6421         (gnus-summary-goto-article (car to) nil t)
6422       (error "Article history empty")))
6423   (gnus-summary-position-point))
6424
6425 ;; Summary commands and functions for limiting the summary buffer.
6426
6427 (defun gnus-summary-limit-to-articles (n)
6428   "Limit the summary buffer to the next N articles.
6429 If not given a prefix, use the process marked articles instead."
6430   (interactive "P")
6431   (prog1
6432       (let ((articles (gnus-summary-work-articles n)))
6433         (setq gnus-newsgroup-processable nil)
6434         (gnus-summary-limit articles))
6435     (gnus-summary-position-point)))
6436
6437 (defun gnus-summary-pop-limit (&optional total)
6438   "Restore the previous limit.
6439 If given a prefix, remove all limits."
6440   (interactive "P")
6441   (when total
6442     (setq gnus-newsgroup-limits
6443           (list (mapcar (lambda (h) (mail-header-number h))
6444                         gnus-newsgroup-headers))))
6445   (unless gnus-newsgroup-limits
6446     (error "No limit to pop"))
6447   (prog1
6448       (gnus-summary-limit nil 'pop)
6449     (gnus-summary-position-point)))
6450
6451 (defun gnus-summary-limit-to-subject (subject &optional header)
6452   "Limit the summary buffer to articles that have subjects that match a regexp."
6453   (interactive "sLimit to subject (regexp): ")
6454   (unless header
6455     (setq header "subject"))
6456   (when (not (equal "" subject))
6457     (prog1
6458         (let ((articles (gnus-summary-find-matching
6459                          (or header "subject") subject 'all)))
6460           (unless articles
6461             (error "Found no matches for \"%s\"" subject))
6462           (gnus-summary-limit articles))
6463       (gnus-summary-position-point))))
6464
6465 (defun gnus-summary-limit-to-author (from)
6466   "Limit the summary buffer to articles that have authors that match a regexp."
6467   (interactive "sLimit to author (regexp): ")
6468   (gnus-summary-limit-to-subject from "from"))
6469
6470 (defun gnus-summary-limit-to-age (age &optional younger-p)
6471   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6472 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6473 articles that are younger than AGE days."
6474   (interactive
6475    (let ((younger current-prefix-arg)
6476          (days-got nil)
6477          days)
6478      (while (not days-got)
6479        (setq days (if younger
6480                       (read-string "Limit to articles within (in days): ")
6481                     (read-string "Limit to articles older than (in days): ")))
6482        (when (> (length days) 0)
6483          (setq days (read days)))
6484        (if (numberp days)
6485            (setq days-got t)
6486          (message "Please enter a number.")
6487          (sleep-for 1)))
6488      (list days younger)))
6489   (prog1
6490       (let ((data gnus-newsgroup-data)
6491             (cutoff (days-to-time age))
6492             articles d date is-younger)
6493         (while (setq d (pop data))
6494           (when (and (vectorp (gnus-data-header d))
6495                      (setq date (mail-header-date (gnus-data-header d))))
6496             (setq is-younger (time-less-p
6497                               (time-since (condition-case ()
6498                                               (date-to-time date)
6499                                             (error '(0 0))))
6500                               cutoff))
6501             (when (if younger-p
6502                       is-younger
6503                     (not is-younger))
6504               (push (gnus-data-number d) articles))))
6505         (gnus-summary-limit (nreverse articles)))
6506     (gnus-summary-position-point)))
6507
6508 (defun gnus-summary-limit-to-extra (header regexp)
6509   "Limit the summary buffer to articles that match an 'extra' header."
6510   (interactive
6511    (let ((header
6512           (intern
6513            (gnus-completing-read
6514             (symbol-name (car gnus-extra-headers))
6515             "Limit extra header:"
6516             (mapcar (lambda (x)
6517                       (cons (symbol-name x) x))
6518                     gnus-extra-headers)
6519             nil
6520             t))))
6521      (list header
6522            (read-string (format "Limit to header %s (regexp): " header)))))
6523   (when (not (equal "" regexp))
6524     (prog1
6525         (let ((articles (gnus-summary-find-matching
6526                          (cons 'extra header) regexp 'all)))
6527           (unless articles
6528             (error "Found no matches for \"%s\"" regexp))
6529           (gnus-summary-limit articles))
6530       (gnus-summary-position-point))))
6531
6532 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6533 (make-obsolete
6534  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6535
6536 (defun gnus-summary-limit-to-unread (&optional all)
6537   "Limit the summary buffer to articles that are not marked as read.
6538 If ALL is non-nil, limit strictly to unread articles."
6539   (interactive "P")
6540   (if all
6541       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6542     (gnus-summary-limit-to-marks
6543      ;; Concat all the marks that say that an article is read and have
6544      ;; those removed.
6545      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6546            gnus-killed-mark gnus-kill-file-mark
6547            gnus-low-score-mark gnus-expirable-mark
6548            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6549            gnus-duplicate-mark gnus-souped-mark)
6550      'reverse)))
6551
6552 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6553 (make-obsolete 'gnus-summary-delete-marked-with
6554                'gnus-summary-limit-exlude-marks)
6555
6556 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6557   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6558 If REVERSE, limit the summary buffer to articles that are marked
6559 with MARKS.  MARKS can either be a string of marks or a list of marks.
6560 Returns how many articles were removed."
6561   (interactive "sMarks: ")
6562   (gnus-summary-limit-to-marks marks t))
6563
6564 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6565   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6566 If REVERSE (the prefix), limit the summary buffer to articles that are
6567 not marked with MARKS.  MARKS can either be a string of marks or a
6568 list of marks.
6569 Returns how many articles were removed."
6570   (interactive "sMarks: \nP")
6571   (prog1
6572       (let ((data gnus-newsgroup-data)
6573             (marks (if (listp marks) marks
6574                      (append marks nil))) ; Transform to list.
6575             articles)
6576         (while data
6577           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6578                   (memq (gnus-data-mark (car data)) marks))
6579             (push (gnus-data-number (car data)) articles))
6580           (setq data (cdr data)))
6581         (gnus-summary-limit articles))
6582     (gnus-summary-position-point)))
6583
6584 (defun gnus-summary-limit-to-score (&optional score)
6585   "Limit to articles with score at or above SCORE."
6586   (interactive "P")
6587   (setq score (if score
6588                   (prefix-numeric-value score)
6589                 (or gnus-summary-default-score 0)))
6590   (let ((data gnus-newsgroup-data)
6591         articles)
6592     (while data
6593       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6594                 score)
6595         (push (gnus-data-number (car data)) articles))
6596       (setq data (cdr data)))
6597     (prog1
6598         (gnus-summary-limit articles)
6599       (gnus-summary-position-point))))
6600
6601 (defun gnus-summary-limit-include-thread (id)
6602   "Display all the hidden articles that in the current thread."
6603   (interactive (list (mail-header-id (gnus-summary-article-header))))
6604   (let ((articles (gnus-articles-in-thread
6605                    (gnus-id-to-thread (gnus-root-id id)))))
6606     (prog1
6607         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6608       (gnus-summary-position-point))))
6609
6610 (defun gnus-summary-limit-include-dormant ()
6611   "Display all the hidden articles that are marked as dormant.
6612 Note that this command only works on a subset of the articles currently
6613 fetched for this group."
6614   (interactive)
6615   (unless gnus-newsgroup-dormant
6616     (error "There are no dormant articles in this group"))
6617   (prog1
6618       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6619     (gnus-summary-position-point)))
6620
6621 (defun gnus-summary-limit-exclude-dormant ()
6622   "Hide all dormant articles."
6623   (interactive)
6624   (prog1
6625       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6626     (gnus-summary-position-point)))
6627
6628 (defun gnus-summary-limit-exclude-childless-dormant ()
6629   "Hide all dormant articles that have no children."
6630   (interactive)
6631   (let ((data (gnus-data-list t))
6632         articles d children)
6633     ;; Find all articles that are either not dormant or have
6634     ;; children.
6635     (while (setq d (pop data))
6636       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6637                 (and (setq children
6638                            (gnus-article-children (gnus-data-number d)))
6639                      (let (found)
6640                        (while children
6641                          (when (memq (car children) articles)
6642                            (setq children nil
6643                                  found t))
6644                          (pop children))
6645                        found)))
6646         (push (gnus-data-number d) articles)))
6647     ;; Do the limiting.
6648     (prog1
6649         (gnus-summary-limit articles)
6650       (gnus-summary-position-point))))
6651
6652 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6653   "Mark all unread excluded articles as read.
6654 If ALL, mark even excluded ticked and dormants as read."
6655   (interactive "P")
6656   (let ((articles (gnus-sorted-complement
6657                    (sort
6658                     (mapcar (lambda (h) (mail-header-number h))
6659                             gnus-newsgroup-headers)
6660                     '<)
6661                    (sort gnus-newsgroup-limit '<)))
6662         article)
6663     (setq gnus-newsgroup-unreads
6664           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6665     (if all
6666         (setq gnus-newsgroup-dormant nil
6667               gnus-newsgroup-marked nil
6668               gnus-newsgroup-reads
6669               (nconc
6670                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6671                gnus-newsgroup-reads))
6672       (while (setq article (pop articles))
6673         (unless (or (memq article gnus-newsgroup-dormant)
6674                     (memq article gnus-newsgroup-marked))
6675           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6676
6677 (defun gnus-summary-limit (articles &optional pop)
6678   (if pop
6679       ;; We pop the previous limit off the stack and use that.
6680       (setq articles (car gnus-newsgroup-limits)
6681             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6682     ;; We use the new limit, so we push the old limit on the stack.
6683     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6684   ;; Set the limit.
6685   (setq gnus-newsgroup-limit articles)
6686   (let ((total (length gnus-newsgroup-data))
6687         (data (gnus-data-find-list (gnus-summary-article-number)))
6688         (gnus-summary-mark-below nil)   ; Inhibit this.
6689         found)
6690     ;; This will do all the work of generating the new summary buffer
6691     ;; according to the new limit.
6692     (gnus-summary-prepare)
6693     ;; Hide any threads, possibly.
6694     (and gnus-show-threads
6695          gnus-thread-hide-subtree
6696          (gnus-summary-hide-all-threads))
6697     ;; Try to return to the article you were at, or one in the
6698     ;; neighborhood.
6699     (when data
6700       ;; We try to find some article after the current one.
6701       (while data
6702         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6703           (setq data nil
6704                 found t))
6705         (setq data (cdr data))))
6706     (unless found
6707       ;; If there is no data, that means that we were after the last
6708       ;; article.  The same goes when we can't find any articles
6709       ;; after the current one.
6710       (goto-char (point-max))
6711       (gnus-summary-find-prev))
6712     (gnus-set-mode-line 'summary)
6713     ;; We return how many articles were removed from the summary
6714     ;; buffer as a result of the new limit.
6715     (- total (length gnus-newsgroup-data))))
6716
6717 (defsubst gnus-invisible-cut-children (threads)
6718   (let ((num 0))
6719     (while threads
6720       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6721         (incf num))
6722       (pop threads))
6723     (< num 2)))
6724
6725 (defsubst gnus-cut-thread (thread)
6726   "Go forwards in the thread until we find an article that we want to display."
6727   (when (or (eq gnus-fetch-old-headers 'some)
6728             (eq gnus-fetch-old-headers 'invisible)
6729             (numberp gnus-fetch-old-headers)
6730             (eq gnus-build-sparse-threads 'some)
6731             (eq gnus-build-sparse-threads 'more))
6732     ;; Deal with old-fetched headers and sparse threads.
6733     (while (and
6734             thread
6735             (or
6736              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6737              (gnus-summary-article-ancient-p
6738               (mail-header-number (car thread))))
6739             (if (or (<= (length (cdr thread)) 1)
6740                     (eq gnus-fetch-old-headers 'invisible))
6741                 (setq gnus-newsgroup-limit
6742                       (delq (mail-header-number (car thread))
6743                             gnus-newsgroup-limit)
6744                       thread (cadr thread))
6745               (when (gnus-invisible-cut-children (cdr thread))
6746                 (let ((th (cdr thread)))
6747                   (while th
6748                     (if (memq (mail-header-number (caar th))
6749                               gnus-newsgroup-limit)
6750                         (setq thread (car th)
6751                               th nil)
6752                       (setq th (cdr th))))))))))
6753   thread)
6754
6755 (defun gnus-cut-threads (threads)
6756   "Cut off all uninteresting articles from the beginning of threads."
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     (let ((th threads))
6763       (while th
6764         (setcar th (gnus-cut-thread (car th)))
6765         (setq th (cdr th)))))
6766   ;; Remove nixed out threads.
6767   (delq nil threads))
6768
6769 (defun gnus-summary-initial-limit (&optional show-if-empty)
6770   "Figure out what the initial limit is supposed to be on group entry.
6771 This entails weeding out unwanted dormants, low-scored articles,
6772 fetch-old-headers verbiage, and so on."
6773   ;; Most groups have nothing to remove.
6774   (if (or gnus-inhibit-limiting
6775           (and (null gnus-newsgroup-dormant)
6776                (not (eq gnus-fetch-old-headers 'some))
6777                (not (numberp gnus-fetch-old-headers))
6778                (not (eq gnus-fetch-old-headers 'invisible))
6779                (null gnus-summary-expunge-below)
6780                (not (eq gnus-build-sparse-threads 'some))
6781                (not (eq gnus-build-sparse-threads 'more))
6782                (null gnus-thread-expunge-below)
6783                (not gnus-use-nocem)))
6784       ()                                ; Do nothing.
6785     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6786     (setq gnus-newsgroup-limit nil)
6787     (mapatoms
6788      (lambda (node)
6789        (unless (car (symbol-value node))
6790          ;; These threads have no parents -- they are roots.
6791          (let ((nodes (cdr (symbol-value node)))
6792                thread)
6793            (while nodes
6794              (if (and gnus-thread-expunge-below
6795                       (< (gnus-thread-total-score (car nodes))
6796                          gnus-thread-expunge-below))
6797                  (gnus-expunge-thread (pop nodes))
6798                (setq thread (pop nodes))
6799                (gnus-summary-limit-children thread))))))
6800      gnus-newsgroup-dependencies)
6801     ;; If this limitation resulted in an empty group, we might
6802     ;; pop the previous limit and use it instead.
6803     (when (and (not gnus-newsgroup-limit)
6804                show-if-empty)
6805       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6806     gnus-newsgroup-limit))
6807
6808 (defun gnus-summary-limit-children (thread)
6809   "Return 1 if this subthread is visible and 0 if it is not."
6810   ;; First we get the number of visible children to this thread.  This
6811   ;; is done by recursing down the thread using this function, so this
6812   ;; will really go down to a leaf article first, before slowly
6813   ;; working its way up towards the root.
6814   (when thread
6815     (let ((children
6816            (if (cdr thread)
6817                (apply '+ (mapcar 'gnus-summary-limit-children
6818                                  (cdr thread)))
6819              0))
6820           (number (mail-header-number (car thread)))
6821           score)
6822       (if (and
6823            (not (memq number gnus-newsgroup-marked))
6824            (or
6825             ;; If this article is dormant and has absolutely no visible
6826             ;; children, then this article isn't visible.
6827             (and (memq number gnus-newsgroup-dormant)
6828                  (zerop children))
6829             ;; If this is "fetch-old-headered" and there is no
6830             ;; visible children, then we don't want this article.
6831             (and (or (eq gnus-fetch-old-headers 'some)
6832                      (numberp gnus-fetch-old-headers))
6833                  (gnus-summary-article-ancient-p number)
6834                  (zerop children))
6835             ;; If this is "fetch-old-headered" and `invisible', then
6836             ;; we don't want this article.
6837             (and (eq gnus-fetch-old-headers 'invisible)
6838                  (gnus-summary-article-ancient-p number))
6839             ;; If this is a sparsely inserted article with no children,
6840             ;; we don't want it.
6841             (and (eq gnus-build-sparse-threads 'some)
6842                  (gnus-summary-article-sparse-p number)
6843                  (zerop children))
6844             ;; If we use expunging, and this article is really
6845             ;; low-scored, then we don't want this article.
6846             (when (and gnus-summary-expunge-below
6847                        (< (setq score
6848                                 (or (cdr (assq number gnus-newsgroup-scored))
6849                                     gnus-summary-default-score))
6850                           gnus-summary-expunge-below))
6851               ;; We increase the expunge-tally here, but that has
6852               ;; nothing to do with the limits, really.
6853               (incf gnus-newsgroup-expunged-tally)
6854               ;; We also mark as read here, if that's wanted.
6855               (when (and gnus-summary-mark-below
6856                          (< score gnus-summary-mark-below))
6857                 (setq gnus-newsgroup-unreads
6858                       (delq number gnus-newsgroup-unreads))
6859                 (if gnus-newsgroup-auto-expire
6860                     (push number gnus-newsgroup-expirable)
6861                   (push (cons number gnus-low-score-mark)
6862                         gnus-newsgroup-reads)))
6863               t)
6864             ;; Check NoCeM things.
6865             (if (and gnus-use-nocem
6866                      (gnus-nocem-unwanted-article-p
6867                       (mail-header-id (car thread))))
6868                 (progn
6869                   (setq gnus-newsgroup-unreads
6870                         (delq number gnus-newsgroup-unreads))
6871                   t))))
6872           ;; Nope, invisible article.
6873           0
6874         ;; Ok, this article is to be visible, so we add it to the limit
6875         ;; and return 1.
6876         (push number gnus-newsgroup-limit)
6877         1))))
6878
6879 (defun gnus-expunge-thread (thread)
6880   "Mark all articles in THREAD as read."
6881   (let* ((number (mail-header-number (car thread))))
6882     (incf gnus-newsgroup-expunged-tally)
6883     ;; We also mark as read here, if that's wanted.
6884     (setq gnus-newsgroup-unreads
6885           (delq number gnus-newsgroup-unreads))
6886     (if gnus-newsgroup-auto-expire
6887         (push number gnus-newsgroup-expirable)
6888       (push (cons number gnus-low-score-mark)
6889             gnus-newsgroup-reads)))
6890   ;; Go recursively through all subthreads.
6891   (mapcar 'gnus-expunge-thread (cdr thread)))
6892
6893 ;; Summary article oriented commands
6894
6895 (defun gnus-summary-refer-parent-article (n)
6896   "Refer parent article N times.
6897 If N is negative, go to ancestor -N instead.
6898 The difference between N and the number of articles fetched is returned."
6899   (interactive "p")
6900   (let ((skip 1)
6901         error header ref)
6902     (when (not (natnump n))
6903       (setq skip (abs n)
6904             n 1))
6905     (while (and (> n 0)
6906                 (not error))
6907       (setq header (gnus-summary-article-header))
6908       (if (and (eq (mail-header-number header)
6909                    (cdr gnus-article-current))
6910                (equal gnus-newsgroup-name
6911                       (car gnus-article-current)))
6912           ;; If we try to find the parent of the currently
6913           ;; displayed article, then we take a look at the actual
6914           ;; References header, since this is slightly more
6915           ;; reliable than the References field we got from the
6916           ;; server.
6917           (save-excursion
6918             (set-buffer gnus-original-article-buffer)
6919             (nnheader-narrow-to-headers)
6920             (unless (setq ref (message-fetch-field "references"))
6921               (setq ref (message-fetch-field "in-reply-to")))
6922             (widen))
6923         (setq ref
6924               ;; It's not the current article, so we take a bet on
6925               ;; the value we got from the server.
6926               (mail-header-references header)))
6927       (if (and ref
6928                (not (equal ref "")))
6929           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6930             (gnus-message 1 "Couldn't find parent"))
6931         (gnus-message 1 "No references in article %d"
6932                       (gnus-summary-article-number))
6933         (setq error t))
6934       (decf n))
6935     (gnus-summary-position-point)
6936     n))
6937
6938 (defun gnus-summary-refer-references ()
6939   "Fetch all articles mentioned in the References header.
6940 Return the number of articles fetched."
6941   (interactive)
6942   (let ((ref (mail-header-references (gnus-summary-article-header)))
6943         (current (gnus-summary-article-number))
6944         (n 0))
6945     (if (or (not ref)
6946             (equal ref ""))
6947         (error "No References in the current article")
6948       ;; For each Message-ID in the References header...
6949       (while (string-match "<[^>]*>" ref)
6950         (incf n)
6951         ;; ... fetch that article.
6952         (gnus-summary-refer-article
6953          (prog1 (match-string 0 ref)
6954            (setq ref (substring ref (match-end 0))))))
6955       (gnus-summary-goto-subject current)
6956       (gnus-summary-position-point)
6957       n)))
6958
6959 (defun gnus-summary-refer-thread (&optional limit)
6960   "Fetch all articles in the current thread.
6961 If LIMIT (the numerical prefix), fetch that many old headers instead
6962 of what's specified by the `gnus-refer-thread-limit' variable."
6963   (interactive "P")
6964   (let ((id (mail-header-id (gnus-summary-article-header)))
6965         (limit (if limit (prefix-numeric-value limit)
6966                  gnus-refer-thread-limit)))
6967     ;; We want to fetch LIMIT *old* headers, but we also have to
6968     ;; re-fetch all the headers in the current buffer, because many of
6969     ;; them may be undisplayed.  So we adjust LIMIT.
6970     (when (numberp limit)
6971       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6972     (unless (eq gnus-fetch-old-headers 'invisible)
6973       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6974       ;; Retrieve the headers and read them in.
6975       (if (eq (gnus-retrieve-headers
6976                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6977               'nov)
6978           (gnus-build-all-threads)
6979         (error "Can't fetch thread from backends that don't support NOV"))
6980       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6981     (gnus-summary-limit-include-thread id)))
6982
6983 (defun gnus-summary-refer-article (message-id)
6984   "Fetch an article specified by MESSAGE-ID."
6985   (interactive "sMessage-ID: ")
6986   (when (and (stringp message-id)
6987              (not (zerop (length message-id))))
6988     ;; Construct the correct Message-ID if necessary.
6989     ;; Suggested by tale@pawl.rpi.edu.
6990     (unless (string-match "^<" message-id)
6991       (setq message-id (concat "<" message-id)))
6992     (unless (string-match ">$" message-id)
6993       (setq message-id (concat message-id ">")))
6994     (let* ((header (gnus-id-to-header message-id))
6995            (sparse (and header
6996                         (gnus-summary-article-sparse-p
6997                          (mail-header-number header))
6998                         (memq (mail-header-number header)
6999                               gnus-newsgroup-limit)))
7000            number)
7001       (cond
7002        ;; If the article is present in the buffer we just go to it.
7003        ((and header
7004              (or (not (gnus-summary-article-sparse-p
7005                        (mail-header-number header)))
7006                  sparse))
7007         (prog1
7008             (gnus-summary-goto-article
7009              (mail-header-number header) nil t)
7010           (when sparse
7011             (gnus-summary-update-article (mail-header-number header)))))
7012        (t
7013         ;; We fetch the article.
7014         (catch 'found
7015           (dolist (gnus-override-method (gnus-refer-article-methods))
7016             (gnus-check-server gnus-override-method)
7017             ;; Fetch the header, and display the article.
7018             (when (setq number (gnus-summary-insert-subject message-id))
7019               (gnus-summary-select-article nil nil nil number)
7020               (throw 'found t)))
7021           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7022
7023 (defun gnus-refer-article-methods ()
7024   "Return a list of referrable methods."
7025   (cond
7026    ;; No method, so we default to current and native.
7027    ((null gnus-refer-article-method)
7028     (list gnus-current-select-method gnus-select-method))
7029    ;; Current.
7030    ((eq 'current gnus-refer-article-method)
7031     (list gnus-current-select-method))
7032    ;; List of select methods.
7033    ((not (and (symbolp (car gnus-refer-article-method))
7034               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7035     (let (out)
7036       (dolist (method gnus-refer-article-method)
7037         (push (if (eq 'current method)
7038                   gnus-current-select-method
7039                 method)
7040               out))
7041       (nreverse out)))
7042    ;; One single select method.
7043    (t
7044     (list gnus-refer-article-method))))
7045
7046 (defun gnus-summary-edit-parameters ()
7047   "Edit the group parameters of the current group."
7048   (interactive)
7049   (gnus-group-edit-group gnus-newsgroup-name 'params))
7050
7051 (defun gnus-summary-customize-parameters ()
7052   "Customize the group parameters of the current group."
7053   (interactive)
7054   (gnus-group-customize gnus-newsgroup-name))
7055
7056 (defun gnus-summary-enter-digest-group (&optional force)
7057   "Enter an nndoc group based on the current article.
7058 If FORCE, force a digest interpretation.  If not, try
7059 to guess what the document format is."
7060   (interactive "P")
7061   (let ((conf gnus-current-window-configuration))
7062     (save-excursion
7063       (gnus-summary-select-article))
7064     (setq gnus-current-window-configuration conf)
7065     (let* ((name (format "%s-%d"
7066                          (gnus-group-prefixed-name
7067                           gnus-newsgroup-name (list 'nndoc ""))
7068                          (save-excursion
7069                            (set-buffer gnus-summary-buffer)
7070                            gnus-current-article)))
7071            (ogroup gnus-newsgroup-name)
7072            (params (append (gnus-info-params (gnus-get-info ogroup))
7073                            (list (cons 'to-group ogroup))
7074                            (list (cons 'save-article-group ogroup))))
7075            (case-fold-search t)
7076            (buf (current-buffer))
7077            dig to-address)
7078       (save-excursion
7079         (set-buffer gnus-original-article-buffer)
7080         ;; Have the digest group inherit the main mail address of
7081         ;; the parent article.
7082         (when (setq to-address (or (message-fetch-field "reply-to")
7083                                    (message-fetch-field "from")))
7084           (setq params (append 
7085                         (list (cons 'to-address 
7086                                     (funcall gnus-decode-encoded-word-function
7087                                              to-address))))))
7088         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7089         (insert-buffer-substring gnus-original-article-buffer)
7090         ;; Remove lines that may lead nndoc to misinterpret the
7091         ;; document type.
7092         (narrow-to-region
7093          (goto-char (point-min))
7094          (or (search-forward "\n\n" nil t) (point)))
7095         (goto-char (point-min))
7096         (delete-matching-lines "^Path:\\|^From ")
7097         (widen))
7098       (unwind-protect
7099           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7100                     (gnus-newsgroup-ephemeral-ignored-charsets
7101                      gnus-newsgroup-ignored-charsets))
7102                 (gnus-group-read-ephemeral-group
7103                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7104                               (nndoc-article-type
7105                                ,(if force 'mbox 'guess))) t))
7106               ;; Make all postings to this group go to the parent group.
7107               (nconc (gnus-info-params (gnus-get-info name))
7108                      params)
7109             ;; Couldn't select this doc group.
7110             (switch-to-buffer buf)
7111             (gnus-set-global-variables)
7112             (gnus-configure-windows 'summary)
7113             (gnus-message 3 "Article couldn't be entered?"))
7114         (kill-buffer dig)))))
7115
7116 (defun gnus-summary-read-document (n)
7117   "Open a new group based on the current article(s).
7118 This will allow you to read digests and other similar
7119 documents as newsgroups.
7120 Obeys the standard process/prefix convention."
7121   (interactive "P")
7122   (let* ((articles (gnus-summary-work-articles n))
7123          (ogroup gnus-newsgroup-name)
7124          (params (append (gnus-info-params (gnus-get-info ogroup))
7125                          (list (cons 'to-group ogroup))))
7126          article group egroup groups vgroup)
7127     (while (setq article (pop articles))
7128       (setq group (format "%s-%d" gnus-newsgroup-name article))
7129       (gnus-summary-remove-process-mark article)
7130       (when (gnus-summary-display-article article)
7131         (save-excursion
7132           (with-temp-buffer
7133             (insert-buffer-substring gnus-original-article-buffer)
7134             ;; Remove some headers that may lead nndoc to make
7135             ;; the wrong guess.
7136             (message-narrow-to-head)
7137             (goto-char (point-min))
7138             (delete-matching-lines "^\\(Path\\):\\|^From ")
7139             (widen)
7140             (if (setq egroup
7141                       (gnus-group-read-ephemeral-group
7142                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7143                                      (nndoc-article-type guess))
7144                        t nil t))
7145                 (progn
7146                   ;; Make all postings to this group go to the parent group.
7147                   (nconc (gnus-info-params (gnus-get-info egroup))
7148                          params)
7149                   (push egroup groups))
7150               ;; Couldn't select this doc group.
7151               (gnus-error 3 "Article couldn't be entered"))))))
7152     ;; Now we have selected all the documents.
7153     (cond
7154      ((not groups)
7155       (error "None of the articles could be interpreted as documents"))
7156      ((gnus-group-read-ephemeral-group
7157        (setq vgroup (format
7158                      "nnvirtual:%s-%s" gnus-newsgroup-name
7159                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7160        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7161        t
7162        (cons (current-buffer) 'summary)))
7163      (t
7164       (error "Couldn't select virtual nndoc group")))))
7165
7166 (defun gnus-summary-isearch-article (&optional regexp-p)
7167   "Do incremental search forward on the current article.
7168 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7169   (interactive "P")
7170   (gnus-summary-select-article)
7171   (gnus-configure-windows 'article)
7172   (gnus-eval-in-buffer-window gnus-article-buffer
7173     (save-restriction
7174       (widen)
7175       (isearch-forward regexp-p))))
7176
7177 (defun gnus-summary-search-article-forward (regexp &optional backward)
7178   "Search for an article containing REGEXP forward.
7179 If BACKWARD, search backward instead."
7180   (interactive
7181    (list (read-string
7182           (format "Search article %s (regexp%s): "
7183                   (if current-prefix-arg "backward" "forward")
7184                   (if gnus-last-search-regexp
7185                       (concat ", default " gnus-last-search-regexp)
7186                     "")))
7187          current-prefix-arg))
7188   (if (string-equal regexp "")
7189       (setq regexp (or gnus-last-search-regexp ""))
7190     (setq gnus-last-search-regexp regexp)
7191     (setq gnus-article-before-search gnus-current-article))
7192   ;; Intentionally set gnus-last-article.
7193   (setq gnus-last-article gnus-article-before-search)
7194   (let ((gnus-last-article gnus-last-article))
7195     (if (gnus-summary-search-article regexp backward)
7196         (gnus-summary-show-thread)
7197       (error "Search failed: \"%s\"" regexp))))
7198
7199 (defun gnus-summary-search-article-backward (regexp)
7200   "Search for an article containing REGEXP backward."
7201   (interactive
7202    (list (read-string
7203           (format "Search article backward (regexp%s): "
7204                   (if gnus-last-search-regexp
7205                       (concat ", default " gnus-last-search-regexp)
7206                     "")))))
7207   (gnus-summary-search-article-forward regexp 'backward))
7208
7209 (defun gnus-summary-search-article (regexp &optional backward)
7210   "Search for an article containing REGEXP.
7211 Optional argument BACKWARD means do search for backward.
7212 `gnus-select-article-hook' is not called during the search."
7213   ;; We have to require this here to make sure that the following
7214   ;; dynamic binding isn't shadowed by autoloading.
7215   (require 'gnus-async)
7216   (require 'gnus-art)
7217   (let ((gnus-select-article-hook nil)  ;Disable hook.
7218         (gnus-article-prepare-hook nil)
7219         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7220         (gnus-use-article-prefetch nil)
7221         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7222         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7223         (sum (current-buffer))
7224         (gnus-display-mime-function nil)
7225         (found nil)
7226         point)
7227     (gnus-save-hidden-threads
7228       (gnus-summary-select-article)
7229       (set-buffer gnus-article-buffer)
7230       (goto-char (window-point (get-buffer-window (current-buffer))))
7231       (when backward
7232         (forward-line -1))
7233       (while (not found)
7234         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7235         (if (if backward
7236                 (re-search-backward regexp nil t)
7237               (re-search-forward regexp nil t))
7238             ;; We found the regexp.
7239             (progn
7240               (setq found 'found)
7241               (beginning-of-line)
7242               (set-window-start
7243                (get-buffer-window (current-buffer))
7244                (point))
7245               (forward-line 1)
7246               (set-window-point
7247                (get-buffer-window (current-buffer))
7248                (point))
7249               (set-buffer sum)
7250               (setq point (point)))
7251           ;; We didn't find it, so we go to the next article.
7252           (set-buffer sum)
7253           (setq found 'not)
7254           (while (eq found 'not)
7255             (if (not (if backward (gnus-summary-find-prev)
7256                        (gnus-summary-find-next)))
7257                 ;; No more articles.
7258                 (setq found t)
7259               ;; Select the next article and adjust point.
7260               (unless (gnus-summary-article-sparse-p
7261                        (gnus-summary-article-number))
7262                 (setq found nil)
7263                 (gnus-summary-select-article)
7264                 (set-buffer gnus-article-buffer)
7265                 (widen)
7266                 (goto-char (if backward (point-max) (point-min))))))))
7267       (gnus-message 7 ""))
7268     ;; Return whether we found the regexp.
7269     (when (eq found 'found)
7270       (goto-char point)
7271       (gnus-summary-show-thread)
7272       (gnus-summary-goto-subject gnus-current-article)
7273       (gnus-summary-position-point)
7274       t)))
7275
7276 (defun gnus-summary-find-matching (header regexp &optional backward unread
7277                                           not-case-fold)
7278   "Return a list of all articles that match REGEXP on HEADER.
7279 The search stars on the current article and goes forwards unless
7280 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7281 If UNREAD is non-nil, only unread articles will
7282 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7283 in the comparisons."
7284   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7285                 (gnus-data-find-list
7286                  (gnus-summary-article-number) (gnus-data-list backward))))
7287         (case-fold-search (not not-case-fold))
7288         articles d func)
7289     (if (consp header)
7290         (if (eq (car header) 'extra)
7291             (setq func
7292                   `(lambda (h)
7293                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7294                          "")))
7295           (error "%s is an invalid header" header))
7296       (unless (fboundp (intern (concat "mail-header-" header)))
7297         (error "%s is not a valid header" header))
7298       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7299     (while data
7300       (setq d (car data))
7301       (and (or (not unread)             ; We want all articles...
7302                (gnus-data-unread-p d))  ; Or just unreads.
7303            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7304            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7305            (push (gnus-data-number d) articles)) ; Success!
7306       (setq data (cdr data)))
7307     (nreverse articles)))
7308
7309 (defun gnus-summary-execute-command (header regexp command &optional backward)
7310   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7311 If HEADER is an empty string (or nil), the match is done on the entire
7312 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7313   (interactive
7314    (list (let ((completion-ignore-case t))
7315            (completing-read
7316             "Header name: "
7317             (mapcar (lambda (string) (list string))
7318                     '("Number" "Subject" "From" "Lines" "Date"
7319                       "Message-ID" "Xref" "References" "Body"))
7320             nil 'require-match))
7321          (read-string "Regexp: ")
7322          (read-key-sequence "Command: ")
7323          current-prefix-arg))
7324   (when (equal header "Body")
7325     (setq header ""))
7326   ;; Hidden thread subtrees must be searched as well.
7327   (gnus-summary-show-all-threads)
7328   ;; We don't want to change current point nor window configuration.
7329   (save-excursion
7330     (save-window-excursion
7331       (gnus-message 6 "Executing %s..." (key-description command))
7332       ;; We'd like to execute COMMAND interactively so as to give arguments.
7333       (gnus-execute header regexp
7334                     `(call-interactively ',(key-binding command))
7335                     backward)
7336       (gnus-message 6 "Executing %s...done" (key-description command)))))
7337
7338 (defun gnus-summary-beginning-of-article ()
7339   "Scroll the article back to the beginning."
7340   (interactive)
7341   (gnus-summary-select-article)
7342   (gnus-configure-windows 'article)
7343   (gnus-eval-in-buffer-window gnus-article-buffer
7344     (widen)
7345     (goto-char (point-min))
7346     (when gnus-page-broken
7347       (gnus-narrow-to-page))))
7348
7349 (defun gnus-summary-end-of-article ()
7350   "Scroll to the end of the article."
7351   (interactive)
7352   (gnus-summary-select-article)
7353   (gnus-configure-windows 'article)
7354   (gnus-eval-in-buffer-window gnus-article-buffer
7355     (widen)
7356     (goto-char (point-max))
7357     (recenter -3)
7358     (when gnus-page-broken
7359       (gnus-narrow-to-page))))
7360
7361 (defun gnus-summary-print-article (&optional filename n)
7362   "Generate and print a PostScript image of the N next (mail) articles.
7363
7364 If N is negative, print the N previous articles.  If N is nil and articles
7365 have been marked with the process mark, print these instead.
7366
7367 If the optional first argument FILENAME is nil, send the image to the
7368 printer.  If FILENAME is a string, save the PostScript image in a file with
7369 that name.  If FILENAME is a number, prompt the user for the name of the file
7370 to save in."
7371   (interactive (list (ps-print-preprint current-prefix-arg)
7372                      current-prefix-arg))
7373   (dolist (article (gnus-summary-work-articles n))
7374     (gnus-summary-select-article nil nil 'pseudo article)
7375     (gnus-eval-in-buffer-window gnus-article-buffer
7376       (let ((buffer (generate-new-buffer " *print*")))
7377         (unwind-protect
7378             (progn
7379               (copy-to-buffer buffer (point-min) (point-max))
7380               (set-buffer buffer)
7381               (gnus-article-delete-invisible-text)
7382               (let ((ps-left-header
7383                      (list
7384                       (concat "("
7385                               (mail-header-subject gnus-current-headers) ")")
7386                       (concat "("
7387                               (mail-header-from gnus-current-headers) ")")))
7388                     (ps-right-header
7389                      (list
7390                       "/pagenumberstring load"
7391                       (concat "("
7392                               (mail-header-date gnus-current-headers) ")"))))
7393                 (gnus-run-hooks 'gnus-ps-print-hook)
7394                 (save-excursion
7395                   (ps-print-buffer-with-faces filename))))
7396           (kill-buffer buffer))))))
7397
7398 (defun gnus-summary-show-article (&optional arg)
7399   "Force re-fetching of the current article.
7400 If ARG (the prefix) is a number, show the article with the charset
7401 defined in `gnus-summary-show-article-charset-alist', or the charset
7402 inputed.
7403 If ARG (the prefix) is non-nil and not a number, show the raw article
7404 without any article massaging functions being run."
7405   (interactive "P")
7406   (cond
7407    ((numberp arg)
7408     (let ((gnus-newsgroup-charset
7409            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7410                (read-coding-system "Charset: ")))
7411           (gnus-newsgroup-ignored-charsets 'gnus-all))
7412       (gnus-summary-select-article nil 'force)
7413       (let ((deps gnus-newsgroup-dependencies)
7414             head header)
7415         (save-excursion
7416           (set-buffer gnus-original-article-buffer)
7417           (save-restriction
7418             (message-narrow-to-head)
7419             (setq head (buffer-string)))
7420           (with-temp-buffer
7421             (insert (format "211 %d Article retrieved.\n"
7422                             (cdr gnus-article-current)))
7423             (insert head)
7424             (insert ".\n")
7425             (let ((nntp-server-buffer (current-buffer)))
7426               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7427         (gnus-data-set-header
7428          (gnus-data-find (cdr gnus-article-current))
7429          header)
7430         (gnus-summary-update-article-line
7431          (cdr gnus-article-current) header))))
7432    ((not arg)
7433     ;; Select the article the normal way.
7434     (gnus-summary-select-article nil 'force))
7435    (t
7436     ;; We have to require this here to make sure that the following
7437     ;; dynamic binding isn't shadowed by autoloading.
7438     (require 'gnus-async)
7439     (require 'gnus-art)
7440     ;; Bind the article treatment functions to nil.
7441     (let ((gnus-have-all-headers t)
7442           gnus-article-prepare-hook
7443           gnus-article-decode-hook
7444           gnus-display-mime-function
7445           gnus-break-pages)
7446       ;; Destroy any MIME parts.
7447       (when (gnus-buffer-live-p gnus-article-buffer)
7448         (save-excursion
7449           (set-buffer gnus-article-buffer)
7450           (mm-destroy-parts gnus-article-mime-handles)
7451           ;; Set it to nil for safety reason.
7452           (setq gnus-article-mime-handle-alist nil)
7453           (setq gnus-article-mime-handles nil)))
7454       (gnus-summary-select-article nil 'force))))
7455   (gnus-summary-goto-subject gnus-current-article)
7456   (gnus-summary-position-point))
7457
7458 (defun gnus-summary-verbose-headers (&optional arg)
7459   "Toggle permanent full header display.
7460 If ARG is a positive number, turn header display on.
7461 If ARG is a negative number, turn header display off."
7462   (interactive "P")
7463   (setq gnus-show-all-headers
7464         (cond ((or (not (numberp arg))
7465                    (zerop arg))
7466                (not gnus-show-all-headers))
7467               ((natnump arg)
7468                t)))
7469   (gnus-summary-show-article))
7470
7471 (defun gnus-summary-toggle-header (&optional arg)
7472   "Show the headers if they are hidden, or hide them if they are shown.
7473 If ARG is a positive number, show the entire header.
7474 If ARG is a negative number, hide the unwanted header lines."
7475   (interactive "P")
7476   (save-excursion
7477     (set-buffer gnus-article-buffer)
7478     (save-restriction
7479       (let* ((buffer-read-only nil)
7480              (inhibit-point-motion-hooks t)
7481              hidden e)
7482         (setq hidden
7483               (if (numberp arg)
7484                   (>= arg 0)
7485                 (save-restriction
7486                   (article-narrow-to-head)
7487                   (gnus-article-hidden-text-p 'headers))))
7488         (goto-char (point-min))
7489         (when (search-forward "\n\n" nil t)
7490           (delete-region (point-min) (1- (point))))
7491         (goto-char (point-min))
7492         (save-excursion
7493           (set-buffer gnus-original-article-buffer)
7494           (goto-char (point-min))
7495           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7496         (insert-buffer-substring gnus-original-article-buffer 1 e)
7497         (save-restriction
7498           (narrow-to-region (point-min) (point))
7499           (article-decode-encoded-words)
7500           (if  hidden
7501               (let ((gnus-treat-hide-headers nil)
7502                     (gnus-treat-hide-boring-headers nil))
7503                 (setq gnus-article-wash-types
7504                       (delq 'headers gnus-article-wash-types))
7505                 (gnus-treat-article 'head))
7506             (gnus-treat-article 'head)))
7507         (gnus-set-mode-line 'article)))))
7508
7509 (defun gnus-summary-show-all-headers ()
7510   "Make all header lines visible."
7511   (interactive)
7512   (gnus-article-show-all-headers))
7513
7514 (defun gnus-summary-caesar-message (&optional arg)
7515   "Caesar rotate the current article by 13.
7516 The numerical prefix specifies how many places to rotate each letter
7517 forward."
7518   (interactive "P")
7519   (gnus-summary-select-article)
7520   (let ((mail-header-separator ""))
7521     (gnus-eval-in-buffer-window gnus-article-buffer
7522       (save-restriction
7523         (widen)
7524         (let ((start (window-start))
7525               buffer-read-only)
7526           (message-caesar-buffer-body arg)
7527           (set-window-start (get-buffer-window (current-buffer)) start))))))
7528
7529 (defun gnus-summary-stop-page-breaking ()
7530   "Stop page breaking in the current article."
7531   (interactive)
7532   (gnus-summary-select-article)
7533   (gnus-eval-in-buffer-window gnus-article-buffer
7534     (widen)
7535     (when (gnus-visual-p 'page-marker)
7536       (let ((buffer-read-only nil))
7537         (gnus-remove-text-with-property 'gnus-prev)
7538         (gnus-remove-text-with-property 'gnus-next))
7539       (setq gnus-page-broken nil))))
7540
7541 (defun gnus-summary-move-article (&optional n to-newsgroup
7542                                             select-method action)
7543   "Move the current article to a different newsgroup.
7544 If N is a positive number, move the N next articles.
7545 If N is a negative number, move the N previous articles.
7546 If N is nil and any articles have been marked with the process mark,
7547 move those articles instead.
7548 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7549 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7550 re-spool using this method.
7551
7552 For this function to work, both the current newsgroup and the
7553 newsgroup that you want to move to have to support the `request-move'
7554 and `request-accept' functions.
7555
7556 ACTION can be either `move' (the default), `crosspost' or `copy'."
7557   (interactive "P")
7558   (unless action
7559     (setq action 'move))
7560   ;; Disable marking as read.
7561   (let (gnus-mark-article-hook)
7562     (save-window-excursion
7563       (gnus-summary-select-article)))
7564   ;; Check whether the source group supports the required functions.
7565   (cond ((and (eq action 'move)
7566               (not (gnus-check-backend-function
7567                     'request-move-article gnus-newsgroup-name)))
7568          (error "The current group does not support article moving"))
7569         ((and (eq action 'crosspost)
7570               (not (gnus-check-backend-function
7571                     'request-replace-article gnus-newsgroup-name)))
7572          (error "The current group does not support article editing")))
7573   (let ((articles (gnus-summary-work-articles n))
7574         (prefix (if (gnus-check-backend-function
7575                     'request-move-article gnus-newsgroup-name)
7576                     (gnus-group-real-prefix gnus-newsgroup-name)
7577                   ""))
7578         (names '((move "Move" "Moving")
7579                  (copy "Copy" "Copying")
7580                  (crosspost "Crosspost" "Crossposting")))
7581         (copy-buf (save-excursion
7582                     (nnheader-set-temp-buffer " *copy article*")))
7583         art-group to-method new-xref article to-groups)
7584     (unless (assq action names)
7585       (error "Unknown action %s" action))
7586     ;; Read the newsgroup name.
7587     (when (and (not to-newsgroup)
7588                (not select-method))
7589       (setq to-newsgroup
7590             (gnus-read-move-group-name
7591              (cadr (assq action names))
7592              (symbol-value (intern (format "gnus-current-%s-group" action)))
7593              articles prefix))
7594       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7595     (setq to-method (or select-method
7596                         (gnus-server-to-method
7597                          (gnus-group-method to-newsgroup))))
7598     ;; Check the method we are to move this article to...
7599     (unless (gnus-check-backend-function
7600              'request-accept-article (car to-method))
7601       (error "%s does not support article copying" (car to-method)))
7602     (unless (gnus-check-server to-method)
7603       (error "Can't open server %s" (car to-method)))
7604     (gnus-message 6 "%s to %s: %s..."
7605                   (caddr (assq action names))
7606                   (or (car select-method) to-newsgroup) articles)
7607     (while articles
7608       (setq article (pop articles))
7609       (setq
7610        art-group
7611        (cond
7612         ;; Move the article.
7613         ((eq action 'move)
7614          ;; Remove this article from future suppression.
7615          (gnus-dup-unsuppress-article article)
7616          (gnus-request-move-article
7617           article                       ; Article to move
7618           gnus-newsgroup-name           ; From newsgroup
7619           (nth 1 (gnus-find-method-for-group
7620                   gnus-newsgroup-name)) ; Server
7621           (list 'gnus-request-accept-article
7622                 to-newsgroup (list 'quote select-method)
7623                 (not articles) t)       ; Accept form
7624           (not articles)))              ; Only save nov last time
7625         ;; Copy the article.
7626         ((eq action 'copy)
7627          (save-excursion
7628            (set-buffer copy-buf)
7629            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7630              (gnus-request-accept-article
7631               to-newsgroup select-method (not articles) t))))
7632         ;; Crosspost the article.
7633         ((eq action 'crosspost)
7634          (let ((xref (message-tokenize-header
7635                       (mail-header-xref (gnus-summary-article-header article))
7636                       " ")))
7637            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7638                                   ":" article))
7639            (unless xref
7640              (setq xref (list (system-name))))
7641            (setq new-xref
7642                  (concat
7643                   (mapconcat 'identity
7644                              (delete "Xref:" (delete new-xref xref))
7645                              " ")
7646                   " " new-xref))
7647            (save-excursion
7648              (set-buffer copy-buf)
7649              ;; First put the article in the destination group.
7650              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7651              (when (consp (setq art-group
7652                                 (gnus-request-accept-article
7653                                  to-newsgroup select-method (not articles))))
7654                (setq new-xref (concat new-xref " " (car art-group)
7655                                       ":" (cdr art-group)))
7656                ;; Now we have the new Xrefs header, so we insert
7657                ;; it and replace the new article.
7658                (nnheader-replace-header "Xref" new-xref)
7659                (gnus-request-replace-article
7660                 (cdr art-group) to-newsgroup (current-buffer))
7661                art-group))))))
7662       (cond
7663        ((not art-group)
7664         (gnus-message 1 "Couldn't %s article %s: %s"
7665                       (cadr (assq action names)) article
7666                       (nnheader-get-report (car to-method))))
7667        ((eq art-group 'junk)
7668         (when (eq action 'move)
7669           (gnus-summary-mark-article article gnus-canceled-mark)
7670           (gnus-message 4 "Deleted article %s" article)))
7671        (t
7672         (let* ((pto-group (gnus-group-prefixed-name
7673                            (car art-group) to-method))
7674                (entry
7675                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7676                (info (nth 2 entry))
7677                (to-group (gnus-info-group info))
7678                to-marks)
7679           ;; Update the group that has been moved to.
7680           (when (and info
7681                      (memq action '(move copy)))
7682             (unless (member to-group to-groups)
7683               (push to-group to-groups))
7684
7685             (unless (memq article gnus-newsgroup-unreads)
7686               (push 'read to-marks)
7687               (gnus-info-set-read
7688                info (gnus-add-to-range (gnus-info-read info)
7689                                        (list (cdr art-group)))))
7690
7691             ;; See whether the article is to be put in the cache.
7692             (let ((marks gnus-article-mark-lists)
7693                   (to-article (cdr art-group)))
7694
7695               ;; Enter the article into the cache in the new group,
7696               ;; if that is required.
7697               (when gnus-use-cache
7698                 (gnus-cache-possibly-enter-article
7699                  to-group to-article
7700                  (memq article gnus-newsgroup-marked)
7701                  (memq article gnus-newsgroup-dormant)
7702                  (memq article gnus-newsgroup-unreads)))
7703
7704               (when gnus-preserve-marks
7705                 ;; Copy any marks over to the new group.
7706                 (when (and (equal to-group gnus-newsgroup-name)
7707                            (not (memq article gnus-newsgroup-unreads)))
7708                   ;; Mark this article as read in this group.
7709                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7710                   (setcdr (gnus-active to-group) to-article)
7711                   (setcdr gnus-newsgroup-active to-article))
7712
7713                 (while marks
7714                   (when (memq article (symbol-value
7715                                        (intern (format "gnus-newsgroup-%s"
7716                                                        (caar marks)))))
7717                     (push (cdar marks) to-marks)
7718                     ;; If the other group is the same as this group,
7719                     ;; then we have to add the mark to the list.
7720                     (when (equal to-group gnus-newsgroup-name)
7721                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7722                            (cons to-article
7723                                  (symbol-value
7724                                   (intern (format "gnus-newsgroup-%s"
7725                                                   (caar marks)))))))
7726                     ;; Copy the marks to other group.
7727                     (gnus-add-marked-articles
7728                      to-group (cdar marks) (list to-article) info))
7729                   (setq marks (cdr marks)))
7730
7731                 (gnus-request-set-mark to-group (list (list (list to-article)
7732                                                             'set
7733                                                             to-marks))))
7734
7735               (gnus-dribble-enter
7736                (concat "(gnus-group-set-info '"
7737                        (gnus-prin1-to-string (gnus-get-info to-group))
7738                        ")"))))
7739
7740           ;; Update the Xref header in this article to point to
7741           ;; the new crossposted article we have just created.
7742           (when (eq action 'crosspost)
7743             (save-excursion
7744               (set-buffer copy-buf)
7745               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7746               (nnheader-replace-header "Xref" new-xref)
7747               (gnus-request-replace-article
7748                article gnus-newsgroup-name (current-buffer)))))
7749
7750         ;;;!!!Why is this necessary?
7751         (set-buffer gnus-summary-buffer)
7752
7753         (gnus-summary-goto-subject article)
7754         (when (eq action 'move)
7755           (gnus-summary-mark-article article gnus-canceled-mark))))
7756       (gnus-summary-remove-process-mark article))
7757     ;; Re-activate all groups that have been moved to.
7758     (while to-groups
7759       (save-excursion
7760         (set-buffer gnus-group-buffer)
7761         (when (gnus-group-goto-group (car to-groups) t)
7762           (gnus-group-get-new-news-this-group 1 t))
7763         (pop to-groups)))
7764
7765     (gnus-kill-buffer copy-buf)
7766     (gnus-summary-position-point)
7767     (gnus-set-mode-line 'summary)))
7768
7769 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7770   "Move the current article to a different newsgroup.
7771 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7772 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7773 re-spool using this method."
7774   (interactive "P")
7775   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7776
7777 (defun gnus-summary-crosspost-article (&optional n)
7778   "Crosspost the current article to some other group."
7779   (interactive "P")
7780   (gnus-summary-move-article n nil nil 'crosspost))
7781
7782 (defcustom gnus-summary-respool-default-method nil
7783   "Default method for respooling an article.
7784 If nil, use to the current newsgroup method."
7785   :type '(choice (gnus-select-method :value (nnml ""))
7786                  (const nil))
7787   :group 'gnus-summary-mail)
7788
7789 (defun gnus-summary-respool-article (&optional n method)
7790   "Respool the current article.
7791 The article will be squeezed through the mail spooling process again,
7792 which means that it will be put in some mail newsgroup or other
7793 depending on `nnmail-split-methods'.
7794 If N is a positive number, respool the N next articles.
7795 If N is a negative number, respool the N previous articles.
7796 If N is nil and any articles have been marked with the process mark,
7797 respool those articles instead.
7798
7799 Respooling can be done both from mail groups and \"real\" newsgroups.
7800 In the former case, the articles in question will be moved from the
7801 current group into whatever groups they are destined to.  In the
7802 latter case, they will be copied into the relevant groups."
7803   (interactive
7804    (list current-prefix-arg
7805          (let* ((methods (gnus-methods-using 'respool))
7806                 (methname
7807                  (symbol-name (or gnus-summary-respool-default-method
7808                                   (car (gnus-find-method-for-group
7809                                         gnus-newsgroup-name)))))
7810                 (method
7811                  (gnus-completing-read
7812                   methname "What backend do you want to use when respooling?"
7813                   methods nil t nil 'gnus-mail-method-history))
7814                 ms)
7815            (cond
7816             ((zerop (length (setq ms (gnus-servers-using-backend
7817                                       (intern method)))))
7818              (list (intern method) ""))
7819             ((= 1 (length ms))
7820              (car ms))
7821             (t
7822              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7823                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7824                            ms-alist))))))))
7825   (unless method
7826     (error "No method given for respooling"))
7827   (if (assoc (symbol-name
7828               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7829              (gnus-methods-using 'respool))
7830       (gnus-summary-move-article n nil method)
7831     (gnus-summary-copy-article n nil method)))
7832
7833 (defun gnus-summary-import-article (file)
7834   "Import an arbitrary file into a mail newsgroup."
7835   (interactive "fImport file: ")
7836   (let ((group gnus-newsgroup-name)
7837         (now (current-time))
7838         atts lines)
7839     (unless (gnus-check-backend-function 'request-accept-article group)
7840       (error "%s does not support article importing" group))
7841     (or (file-readable-p file)
7842         (not (file-regular-p file))
7843         (error "Can't read %s" file))
7844     (save-excursion
7845       (set-buffer (gnus-get-buffer-create " *import file*"))
7846       (erase-buffer)
7847       (nnheader-insert-file-contents file)
7848       (goto-char (point-min))
7849       (unless (nnheader-article-p)
7850         ;; This doesn't look like an article, so we fudge some headers.
7851         (setq atts (file-attributes file)
7852               lines (count-lines (point-min) (point-max)))
7853         (insert "From: " (read-string "From: ") "\n"
7854                 "Subject: " (read-string "Subject: ") "\n"
7855                 "Date: " (message-make-date (nth 5 atts))
7856                 "\n"
7857                 "Message-ID: " (message-make-message-id) "\n"
7858                 "Lines: " (int-to-string lines) "\n"
7859                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7860       (gnus-request-accept-article group nil t)
7861       (kill-buffer (current-buffer)))))
7862
7863 (defun gnus-summary-article-posted-p ()
7864   "Say whether the current (mail) article is available from news as well.
7865 This will be the case if the article has both been mailed and posted."
7866   (interactive)
7867   (let ((id (mail-header-references (gnus-summary-article-header)))
7868         (gnus-override-method (car (gnus-refer-article-methods))))
7869     (if (gnus-request-head id "")
7870         (gnus-message 2 "The current message was found on %s"
7871                       gnus-override-method)
7872       (gnus-message 2 "The current message couldn't be found on %s"
7873                     gnus-override-method)
7874       nil)))
7875
7876 (defun gnus-summary-expire-articles (&optional now)
7877   "Expire all articles that are marked as expirable in the current group."
7878   (interactive)
7879   (when (gnus-check-backend-function
7880          'request-expire-articles gnus-newsgroup-name)
7881     ;; This backend supports expiry.
7882     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7883            (expirable (if total
7884                           (progn
7885                             ;; We need to update the info for
7886                             ;; this group for `gnus-list-of-read-articles'
7887                             ;; to give us the right answer.
7888                             (gnus-run-hooks 'gnus-exit-group-hook)
7889                             (gnus-summary-update-info)
7890                             (gnus-list-of-read-articles gnus-newsgroup-name))
7891                         (setq gnus-newsgroup-expirable
7892                               (sort gnus-newsgroup-expirable '<))))
7893            (expiry-wait (if now 'immediate
7894                           (gnus-group-find-parameter
7895                            gnus-newsgroup-name 'expiry-wait)))
7896            (nnmail-expiry-target
7897             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7898                 nnmail-expiry-target))
7899            es)
7900       (when expirable
7901         ;; There are expirable articles in this group, so we run them
7902         ;; through the expiry process.
7903         (gnus-message 6 "Expiring articles...")
7904         (unless (gnus-check-group gnus-newsgroup-name)
7905           (error "Can't open server for %s" gnus-newsgroup-name))
7906         ;; The list of articles that weren't expired is returned.
7907         (save-excursion
7908           (if expiry-wait
7909               (let ((nnmail-expiry-wait-function nil)
7910                     (nnmail-expiry-wait expiry-wait))
7911                 (setq es (gnus-request-expire-articles
7912                           expirable gnus-newsgroup-name)))
7913             (setq es (gnus-request-expire-articles
7914                       expirable gnus-newsgroup-name)))
7915           (unless total
7916             (setq gnus-newsgroup-expirable es))
7917           ;; We go through the old list of expirable, and mark all
7918           ;; really expired articles as nonexistent.
7919           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7920             (let ((gnus-use-cache nil))
7921               (while expirable
7922                 (unless (memq (car expirable) es)
7923                   (when (gnus-data-find (car expirable))
7924                     (gnus-summary-mark-article
7925                      (car expirable) gnus-canceled-mark)))
7926                 (setq expirable (cdr expirable))))))
7927         (gnus-message 6 "Expiring articles...done")))))
7928
7929 (defun gnus-summary-expire-articles-now ()
7930   "Expunge all expirable articles in the current group.
7931 This means that *all* articles that are marked as expirable will be
7932 deleted forever, right now."
7933   (interactive)
7934   (or gnus-expert-user
7935       (gnus-yes-or-no-p
7936        "Are you really, really, really sure you want to delete all these messages? ")
7937       (error "Phew!"))
7938   (gnus-summary-expire-articles t))
7939
7940 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7941 (defun gnus-summary-delete-article (&optional n)
7942   "Delete the N next (mail) articles.
7943 This command actually deletes articles.  This is not a marking
7944 command.  The article will disappear forever from your life, never to
7945 return.
7946 If N is negative, delete backwards.
7947 If N is nil and articles have been marked with the process mark,
7948 delete these instead."
7949   (interactive "P")
7950   (unless (gnus-check-backend-function 'request-expire-articles
7951                                        gnus-newsgroup-name)
7952     (error "The current newsgroup does not support article deletion"))
7953   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7954     (error "Couldn't open server"))
7955   ;; Compute the list of articles to delete.
7956   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7957         not-deleted)
7958     (if (and gnus-novice-user
7959              (not (gnus-yes-or-no-p
7960                    (format "Do you really want to delete %s forever? "
7961                            (if (> (length articles) 1)
7962                                (format "these %s articles" (length articles))
7963                              "this article")))))
7964         ()
7965       ;; Delete the articles.
7966       (setq not-deleted (gnus-request-expire-articles
7967                          articles gnus-newsgroup-name 'force))
7968       (while articles
7969         (gnus-summary-remove-process-mark (car articles))
7970         ;; The backend might not have been able to delete the article
7971         ;; after all.
7972         (unless (memq (car articles) not-deleted)
7973           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7974         (setq articles (cdr articles)))
7975       (when not-deleted
7976         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7977     (gnus-summary-position-point)
7978     (gnus-set-mode-line 'summary)
7979     not-deleted))
7980
7981 (defun gnus-summary-edit-article (&optional arg)
7982   "Edit the current article.
7983 This will have permanent effect only in mail groups.
7984 If ARG is nil, edit the decoded articles.
7985 If ARG is 1, edit the raw articles. 
7986 If ARG is 2, edit the raw articles even in read-only groups.
7987 If ARG is 3, edit the articles with the current handles.
7988 Otherwise, allow editing of articles even in read-only
7989 groups."
7990   (interactive "P")
7991   (let (force raw current-handles)
7992     (cond 
7993      ((null arg))
7994      ((eq arg 1) (setq raw t))
7995      ((eq arg 2) (setq raw t
7996                        force t))
7997      ((eq arg 3) (setq current-handles 
7998                        (and (gnus-buffer-live-p gnus-article-buffer)
7999                             (with-current-buffer gnus-article-buffer
8000                               (prog1
8001                                   gnus-article-mime-handles
8002                                   (setq gnus-article-mime-handles nil))))))
8003      (t (setq force t)))
8004     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8005         (error "Can't edit the raw article in group nndraft:drafts."))
8006     (save-excursion
8007       (set-buffer gnus-summary-buffer)
8008       (let ((mail-parse-charset gnus-newsgroup-charset)
8009             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8010         (gnus-set-global-variables)
8011         (when (and (not force)
8012                    (gnus-group-read-only-p))
8013           (error "The current newsgroup does not support article editing"))
8014         (gnus-summary-show-article t)
8015         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8016           (with-current-buffer gnus-article-buffer
8017             (mm-enable-multibyte-mule4)))
8018         (if (equal gnus-newsgroup-name "nndraft:drafts")
8019             (setq raw t))
8020         (gnus-article-edit-article
8021          (if raw 'ignore 
8022            `(lambda () 
8023               (let ((mbl mml-buffer-list))
8024                 (setq mml-buffer-list nil)
8025                 (mime-to-mml ,'current-handles)
8026                 (make-local-hook 'kill-buffer-hook)
8027                 (let ((mbl1 mml-buffer-list))
8028                   (setq mml-buffer-list mbl)
8029                   (set (make-local-variable 'mml-buffer-list) mbl1))
8030                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8031          `(lambda (no-highlight)
8032             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8033                   (message-options message-options)
8034                   (message-options-set-recipient)
8035                   (mail-parse-ignored-charsets 
8036                    ',gnus-newsgroup-ignored-charsets))
8037               ,(if (not raw) '(progn 
8038                                 (mml-to-mime)
8039                                 (mml-destroy-buffers)
8040                                 (remove-hook 'kill-buffer-hook 
8041                                              'mml-destroy-buffers t)
8042                                 (kill-local-variable 'mml-buffer-list)))
8043               (gnus-summary-edit-article-done
8044                ,(or (mail-header-references gnus-current-headers) "")
8045                ,(gnus-group-read-only-p) 
8046                ,gnus-summary-buffer no-highlight))))))))
8047
8048 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8049
8050 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8051                                                  no-highlight)
8052   "Make edits to the current article permanent."
8053   (interactive)
8054   (save-excursion
8055     ;; The buffer restriction contains the entire article if it exists.
8056     (when (article-goto-body)
8057       (let ((lines (count-lines (point) (point-max)))
8058             (length (- (point-max) (point)))
8059             (case-fold-search t)
8060             (body (copy-marker (point))))
8061         (goto-char (point-min))
8062         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8063           (delete-region (match-beginning 1) (match-end 1))
8064           (insert (number-to-string length)))
8065         (goto-char (point-min))
8066         (when (re-search-forward
8067                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8068           (delete-region (match-beginning 1) (match-end 1))
8069           (insert (number-to-string length)))
8070         (goto-char (point-min))
8071         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8072           (delete-region (match-beginning 1) (match-end 1))
8073           (insert (number-to-string lines))))))
8074   ;; Replace the article.
8075   (let ((buf (current-buffer)))
8076     (with-temp-buffer
8077       (insert-buffer-substring buf)
8078       
8079       (if (and (not read-only)
8080                (not (gnus-request-replace-article
8081                      (cdr gnus-article-current) (car gnus-article-current)
8082                      (current-buffer) t)))
8083           (error "Couldn't replace article")
8084         ;; Update the summary buffer.
8085         (if (and references
8086                  (equal (message-tokenize-header references " ")
8087                         (message-tokenize-header
8088                          (or (message-fetch-field "references") "") " ")))
8089             ;; We only have to update this line.
8090             (save-excursion
8091               (save-restriction
8092                 (message-narrow-to-head)
8093                 (let ((head (buffer-string))
8094                       header)
8095                   (with-temp-buffer
8096                     (insert (format "211 %d Article retrieved.\n"
8097                                     (cdr gnus-article-current)))
8098                     (insert head)
8099                     (insert ".\n")
8100                     (let ((nntp-server-buffer (current-buffer)))
8101                       (setq header (car (gnus-get-newsgroup-headers
8102                                          (save-excursion
8103                                            (set-buffer gnus-summary-buffer)
8104                                            gnus-newsgroup-dependencies)
8105                                          t))))
8106                     (save-excursion
8107                       (set-buffer gnus-summary-buffer)
8108                       (gnus-data-set-header
8109                        (gnus-data-find (cdr gnus-article-current))
8110                        header)
8111                       (gnus-summary-update-article-line
8112                        (cdr gnus-article-current) header))))))
8113           ;; Update threads.
8114           (set-buffer (or buffer gnus-summary-buffer))
8115           (gnus-summary-update-article (cdr gnus-article-current)))
8116         ;; Prettify the article buffer again.
8117         (unless no-highlight
8118           (save-excursion
8119             (set-buffer gnus-article-buffer)
8120             ;;;!!! Fix this -- article should be rehighlighted.
8121             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8122             (set-buffer gnus-original-article-buffer)
8123             (gnus-request-article
8124              (cdr gnus-article-current)
8125              (car gnus-article-current) (current-buffer))))
8126         ;; Prettify the summary buffer line.
8127         (when (gnus-visual-p 'summary-highlight 'highlight)
8128           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8129
8130 (defun gnus-summary-edit-wash (key)
8131   "Perform editing command KEY in the article buffer."
8132   (interactive
8133    (list
8134     (progn
8135       (message "%s" (concat (this-command-keys) "- "))
8136       (read-char))))
8137   (message "")
8138   (gnus-summary-edit-article)
8139   (execute-kbd-macro (concat (this-command-keys) key))
8140   (gnus-article-edit-done))
8141
8142 ;;; Respooling
8143
8144 (defun gnus-summary-respool-query (&optional silent trace)
8145   "Query where the respool algorithm would put this article."
8146   (interactive)
8147   (let (gnus-mark-article-hook)
8148     (gnus-summary-select-article)
8149     (save-excursion
8150       (set-buffer gnus-original-article-buffer)
8151       (save-restriction
8152         (message-narrow-to-head)
8153         (let ((groups (nnmail-article-group 'identity trace)))
8154           (unless silent
8155             (if groups
8156                 (message "This message would go to %s"
8157                          (mapconcat 'car groups ", "))
8158               (message "This message would go to no groups"))
8159             groups))))))
8160
8161 (defun gnus-summary-respool-trace ()
8162   "Trace where the respool algorithm would put this article.
8163 Display a buffer showing all fancy splitting patterns which matched."
8164   (interactive)
8165   (gnus-summary-respool-query nil t))
8166
8167 ;; Summary marking commands.
8168
8169 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8170   "Mark articles which has the same subject as read, and then select the next.
8171 If UNMARK is positive, remove any kind of mark.
8172 If UNMARK is negative, tick articles."
8173   (interactive "P")
8174   (when unmark
8175     (setq unmark (prefix-numeric-value unmark)))
8176   (let ((count
8177          (gnus-summary-mark-same-subject
8178           (gnus-summary-article-subject) unmark)))
8179     ;; Select next unread article.  If auto-select-same mode, should
8180     ;; select the first unread article.
8181     (gnus-summary-next-article t (and gnus-auto-select-same
8182                                       (gnus-summary-article-subject)))
8183     (gnus-message 7 "%d article%s marked as %s"
8184                   count (if (= count 1) " is" "s are")
8185                   (if unmark "unread" "read"))))
8186
8187 (defun gnus-summary-kill-same-subject (&optional unmark)
8188   "Mark articles which has the same subject as read.
8189 If UNMARK is positive, remove any kind of mark.
8190 If UNMARK is negative, tick articles."
8191   (interactive "P")
8192   (when unmark
8193     (setq unmark (prefix-numeric-value unmark)))
8194   (let ((count
8195          (gnus-summary-mark-same-subject
8196           (gnus-summary-article-subject) unmark)))
8197     ;; If marked as read, go to next unread subject.
8198     (when (null unmark)
8199       ;; Go to next unread subject.
8200       (gnus-summary-next-subject 1 t))
8201     (gnus-message 7 "%d articles are marked as %s"
8202                   count (if unmark "unread" "read"))))
8203
8204 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8205   "Mark articles with same SUBJECT as read, and return marked number.
8206 If optional argument UNMARK is positive, remove any kinds of marks.
8207 If optional argument UNMARK is negative, mark articles as unread instead."
8208   (let ((count 1))
8209     (save-excursion
8210       (cond
8211        ((null unmark)                   ; Mark as read.
8212         (while (and
8213                 (progn
8214                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8215                   (gnus-summary-show-thread) t)
8216                 (gnus-summary-find-subject subject))
8217           (setq count (1+ count))))
8218        ((> unmark 0)                    ; Tick.
8219         (while (and
8220                 (progn
8221                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8222                   (gnus-summary-show-thread) t)
8223                 (gnus-summary-find-subject subject))
8224           (setq count (1+ count))))
8225        (t                               ; Mark as unread.
8226         (while (and
8227                 (progn
8228                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8229                   (gnus-summary-show-thread) t)
8230                 (gnus-summary-find-subject subject))
8231           (setq count (1+ count)))))
8232       (gnus-set-mode-line 'summary)
8233       ;; Return the number of marked articles.
8234       count)))
8235
8236 (defun gnus-summary-mark-as-processable (n &optional unmark)
8237   "Set the process mark on the next N articles.
8238 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8239 the process mark instead.  The difference between N and the actual
8240 number of articles marked is returned."
8241   (interactive "p")
8242   (let ((backward (< n 0))
8243         (n (abs n)))
8244     (while (and
8245             (> n 0)
8246             (if unmark
8247                 (gnus-summary-remove-process-mark
8248                  (gnus-summary-article-number))
8249               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8250             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8251       (setq n (1- n)))
8252     (when (/= 0 n)
8253       (gnus-message 7 "No more articles"))
8254     (gnus-summary-recenter)
8255     (gnus-summary-position-point)
8256     n))
8257
8258 (defun gnus-summary-unmark-as-processable (n)
8259   "Remove the process mark from the next N articles.
8260 If N is negative, unmark backward instead.  The difference between N and
8261 the actual number of articles unmarked is returned."
8262   (interactive "p")
8263   (gnus-summary-mark-as-processable n t))
8264
8265 (defun gnus-summary-unmark-all-processable ()
8266   "Remove the process mark from all articles."
8267   (interactive)
8268   (save-excursion
8269     (while gnus-newsgroup-processable
8270       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8271   (gnus-summary-position-point))
8272
8273 (defun gnus-summary-mark-as-expirable (n)
8274   "Mark N articles forward as expirable.
8275 If N is negative, mark backward instead.  The difference between N and
8276 the actual number of articles marked is returned."
8277   (interactive "p")
8278   (gnus-summary-mark-forward n gnus-expirable-mark))
8279
8280 (defun gnus-summary-mark-article-as-replied (article)
8281   "Mark ARTICLE replied and update the summary line."
8282   (push article gnus-newsgroup-replied)
8283   (let ((buffer-read-only nil))
8284     (when (gnus-summary-goto-subject article nil t)
8285       (gnus-summary-update-secondary-mark article))))
8286
8287 (defun gnus-summary-set-bookmark (article)
8288   "Set a bookmark in current article."
8289   (interactive (list (gnus-summary-article-number)))
8290   (when (or (not (get-buffer gnus-article-buffer))
8291             (not gnus-current-article)
8292             (not gnus-article-current)
8293             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8294     (error "No current article selected"))
8295   ;; Remove old bookmark, if one exists.
8296   (let ((old (assq article gnus-newsgroup-bookmarks)))
8297     (when old
8298       (setq gnus-newsgroup-bookmarks
8299             (delq old gnus-newsgroup-bookmarks))))
8300   ;; Set the new bookmark, which is on the form
8301   ;; (article-number . line-number-in-body).
8302   (push
8303    (cons article
8304          (save-excursion
8305            (set-buffer gnus-article-buffer)
8306            (count-lines
8307             (min (point)
8308                  (save-excursion
8309                    (goto-char (point-min))
8310                    (search-forward "\n\n" nil t)
8311                    (point)))
8312             (point))))
8313    gnus-newsgroup-bookmarks)
8314   (gnus-message 6 "A bookmark has been added to the current article."))
8315
8316 (defun gnus-summary-remove-bookmark (article)
8317   "Remove the bookmark from the current article."
8318   (interactive (list (gnus-summary-article-number)))
8319   ;; Remove old bookmark, if one exists.
8320   (let ((old (assq article gnus-newsgroup-bookmarks)))
8321     (if old
8322         (progn
8323           (setq gnus-newsgroup-bookmarks
8324                 (delq old gnus-newsgroup-bookmarks))
8325           (gnus-message 6 "Removed bookmark."))
8326       (gnus-message 6 "No bookmark in current article."))))
8327
8328 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8329 (defun gnus-summary-mark-as-dormant (n)
8330   "Mark N articles forward as dormant.
8331 If N is negative, mark backward instead.  The difference between N and
8332 the actual number of articles marked is returned."
8333   (interactive "p")
8334   (gnus-summary-mark-forward n gnus-dormant-mark))
8335
8336 (defun gnus-summary-set-process-mark (article)
8337   "Set the process mark on ARTICLE and update the summary line."
8338   (setq gnus-newsgroup-processable
8339         (cons article
8340               (delq article gnus-newsgroup-processable)))
8341   (when (gnus-summary-goto-subject article)
8342     (gnus-summary-show-thread)
8343     (gnus-summary-goto-subject article)
8344     (gnus-summary-update-secondary-mark article)))
8345
8346 (defun gnus-summary-remove-process-mark (article)
8347   "Remove the process mark from ARTICLE and update the summary line."
8348   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8349   (when (gnus-summary-goto-subject article)
8350     (gnus-summary-show-thread)
8351     (gnus-summary-goto-subject article)
8352     (gnus-summary-update-secondary-mark article)))
8353
8354 (defun gnus-summary-set-saved-mark (article)
8355   "Set the process mark on ARTICLE and update the summary line."
8356   (push article gnus-newsgroup-saved)
8357   (when (gnus-summary-goto-subject article)
8358     (gnus-summary-update-secondary-mark article)))
8359
8360 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8361   "Mark N articles as read forwards.
8362 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8363 The difference between N and the actual number of articles marked is
8364 returned.
8365 Iff NO-EXPIRE, auto-expiry will be inhibited."
8366   (interactive "p")
8367   (gnus-summary-show-thread)
8368   (let ((backward (< n 0))
8369         (gnus-summary-goto-unread
8370          (and gnus-summary-goto-unread
8371               (not (eq gnus-summary-goto-unread 'never))
8372               (not (memq mark (list gnus-unread-mark
8373                                     gnus-ticked-mark gnus-dormant-mark)))))
8374         (n (abs n))
8375         (mark (or mark gnus-del-mark)))
8376     (while (and (> n 0)
8377                 (gnus-summary-mark-article nil mark no-expire)
8378                 (zerop (gnus-summary-next-subject
8379                         (if backward -1 1)
8380                         (and gnus-summary-goto-unread
8381                              (not (eq gnus-summary-goto-unread 'never)))
8382                         t)))
8383       (setq n (1- n)))
8384     (when (/= 0 n)
8385       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8386     (gnus-summary-recenter)
8387     (gnus-summary-position-point)
8388     (gnus-set-mode-line 'summary)
8389     n))
8390
8391 (defun gnus-summary-mark-article-as-read (mark)
8392   "Mark the current article quickly as read with MARK."
8393   (let ((article (gnus-summary-article-number)))
8394     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8395     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8396     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8397     (push (cons article mark) gnus-newsgroup-reads)
8398     ;; Possibly remove from cache, if that is used.
8399     (when gnus-use-cache
8400       (gnus-cache-enter-remove-article article))
8401     ;; Allow the backend to change the mark.
8402     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8403     ;; Check for auto-expiry.
8404     (when (and gnus-newsgroup-auto-expire
8405                (memq mark gnus-auto-expirable-marks))
8406       (setq mark gnus-expirable-mark)
8407       ;; Let the backend know about the mark change.
8408       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8409       (push article gnus-newsgroup-expirable))
8410     ;; Set the mark in the buffer.
8411     (gnus-summary-update-mark mark 'unread)
8412     t))
8413
8414 (defun gnus-summary-mark-article-as-unread (mark)
8415   "Mark the current article quickly as unread with MARK."
8416   (let* ((article (gnus-summary-article-number))
8417          (old-mark (gnus-summary-article-mark article)))
8418     ;; Allow the backend to change the mark.
8419     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8420     (if (eq mark old-mark)
8421         t
8422       (if (<= article 0)
8423           (progn
8424             (gnus-error 1 "Can't mark negative article numbers")
8425             nil)
8426         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8427         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8428         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8429         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8430         (cond ((= mark gnus-ticked-mark)
8431                (push article gnus-newsgroup-marked))
8432               ((= mark gnus-dormant-mark)
8433                (push article gnus-newsgroup-dormant))
8434               (t
8435                (push article gnus-newsgroup-unreads)))
8436         (gnus-pull article gnus-newsgroup-reads)
8437
8438         ;; See whether the article is to be put in the cache.
8439         (and gnus-use-cache
8440              (vectorp (gnus-summary-article-header article))
8441              (save-excursion
8442                (gnus-cache-possibly-enter-article
8443                 gnus-newsgroup-name article
8444                 (= mark gnus-ticked-mark)
8445                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8446
8447         ;; Fix the mark.
8448         (gnus-summary-update-mark mark 'unread)
8449         t))))
8450
8451 (defun gnus-summary-mark-article (&optional article mark no-expire)
8452   "Mark ARTICLE with MARK.  MARK can be any character.
8453 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8454 `??' (dormant) and `?E' (expirable).
8455 If MARK is nil, then the default character `?r' is used.
8456 If ARTICLE is nil, then the article on the current line will be
8457 marked.
8458 Iff NO-EXPIRE, auto-expiry will be inhibited."
8459   ;; The mark might be a string.
8460   (when (stringp mark)
8461     (setq mark (aref mark 0)))
8462   ;; If no mark is given, then we check auto-expiring.
8463   (when (null mark)
8464     (setq mark gnus-del-mark))
8465   (when (and (not no-expire)
8466              gnus-newsgroup-auto-expire
8467              (memq mark gnus-auto-expirable-marks))
8468     (setq mark gnus-expirable-mark))
8469   (let ((article (or article (gnus-summary-article-number)))
8470         (old-mark (gnus-summary-article-mark article)))
8471     ;; Allow the backend to change the mark.
8472     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8473     (if (eq mark old-mark)
8474         t
8475       (unless article
8476         (error "No article on current line"))
8477       (if (not (if (or (= mark gnus-unread-mark)
8478                        (= mark gnus-ticked-mark)
8479                        (= mark gnus-dormant-mark))
8480                    (gnus-mark-article-as-unread article mark)
8481                  (gnus-mark-article-as-read article mark)))
8482           t
8483         ;; See whether the article is to be put in the cache.
8484         (and gnus-use-cache
8485              (not (= mark gnus-canceled-mark))
8486              (vectorp (gnus-summary-article-header article))
8487              (save-excursion
8488                (gnus-cache-possibly-enter-article
8489                 gnus-newsgroup-name article
8490                 (= mark gnus-ticked-mark)
8491                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8492
8493         (when (gnus-summary-goto-subject article nil t)
8494           (let ((buffer-read-only nil))
8495             (gnus-summary-show-thread)
8496             ;; Fix the mark.
8497             (gnus-summary-update-mark mark 'unread)
8498             t))))))
8499
8500 (defun gnus-summary-update-secondary-mark (article)
8501   "Update the secondary (read, process, cache) mark."
8502   (gnus-summary-update-mark
8503    (cond ((memq article gnus-newsgroup-processable)
8504           gnus-process-mark)
8505          ((memq article gnus-newsgroup-cached)
8506           gnus-cached-mark)
8507          ((memq article gnus-newsgroup-replied)
8508           gnus-replied-mark)
8509          ((memq article gnus-newsgroup-saved)
8510           gnus-saved-mark)
8511          (t gnus-unread-mark))
8512    'replied)
8513   (when (gnus-visual-p 'summary-highlight 'highlight)
8514     (gnus-run-hooks 'gnus-summary-update-hook))
8515   t)
8516
8517 (defun gnus-summary-update-mark (mark type)
8518   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8519         (buffer-read-only nil))
8520     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8521     (when forward
8522       (when (looking-at "\r")
8523         (incf forward))
8524       (when (<= (+ forward (point)) (point-max))
8525         ;; Go to the right position on the line.
8526         (goto-char (+ forward (point)))
8527         ;; Replace the old mark with the new mark.
8528         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8529         ;; Optionally update the marks by some user rule.
8530         (when (eq type 'unread)
8531           (gnus-data-set-mark
8532            (gnus-data-find (gnus-summary-article-number)) mark)
8533           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8534
8535 (defun gnus-mark-article-as-read (article &optional mark)
8536   "Enter ARTICLE in the pertinent lists and remove it from others."
8537   ;; Make the article expirable.
8538   (let ((mark (or mark gnus-del-mark)))
8539     (if (= mark gnus-expirable-mark)
8540         (push article gnus-newsgroup-expirable)
8541       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8542     ;; Remove from unread and marked lists.
8543     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8544     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8545     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8546     (push (cons article mark) gnus-newsgroup-reads)
8547     ;; Possibly remove from cache, if that is used.
8548     (when gnus-use-cache
8549       (gnus-cache-enter-remove-article article))
8550     t))
8551
8552 (defun gnus-mark-article-as-unread (article &optional mark)
8553   "Enter ARTICLE in the pertinent lists and remove it from others."
8554   (let ((mark (or mark gnus-ticked-mark)))
8555     (if (<= article 0)
8556         (progn
8557           (gnus-error 1 "Can't mark negative article numbers")
8558           nil)
8559       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8560             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8561             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8562             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8563
8564       ;; Unsuppress duplicates?
8565       (when gnus-suppress-duplicates
8566         (gnus-dup-unsuppress-article article))
8567
8568       (cond ((= mark gnus-ticked-mark)
8569              (push article gnus-newsgroup-marked))
8570             ((= mark gnus-dormant-mark)
8571              (push article gnus-newsgroup-dormant))
8572             (t
8573              (push article gnus-newsgroup-unreads)))
8574       (gnus-pull article gnus-newsgroup-reads)
8575       t)))
8576
8577 (defalias 'gnus-summary-mark-as-unread-forward
8578   'gnus-summary-tick-article-forward)
8579 (make-obsolete 'gnus-summary-mark-as-unread-forward
8580                'gnus-summary-tick-article-forward)
8581 (defun gnus-summary-tick-article-forward (n)
8582   "Tick N articles forwards.
8583 If N is negative, tick backwards instead.
8584 The difference between N and the number of articles ticked is returned."
8585   (interactive "p")
8586   (gnus-summary-mark-forward n gnus-ticked-mark))
8587
8588 (defalias 'gnus-summary-mark-as-unread-backward
8589   'gnus-summary-tick-article-backward)
8590 (make-obsolete 'gnus-summary-mark-as-unread-backward
8591                'gnus-summary-tick-article-backward)
8592 (defun gnus-summary-tick-article-backward (n)
8593   "Tick N articles backwards.
8594 The difference between N and the number of articles ticked is returned."
8595   (interactive "p")
8596   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8597
8598 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8599 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8600 (defun gnus-summary-tick-article (&optional article clear-mark)
8601   "Mark current article as unread.
8602 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8603 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8604   (interactive)
8605   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8606                                        gnus-ticked-mark)))
8607
8608 (defun gnus-summary-mark-as-read-forward (n)
8609   "Mark N articles as read forwards.
8610 If N is negative, mark backwards instead.
8611 The difference between N and the actual number of articles marked is
8612 returned."
8613   (interactive "p")
8614   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8615
8616 (defun gnus-summary-mark-as-read-backward (n)
8617   "Mark the N articles as read backwards.
8618 The difference between N and the actual number of articles marked is
8619 returned."
8620   (interactive "p")
8621   (gnus-summary-mark-forward
8622    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8623
8624 (defun gnus-summary-mark-as-read (&optional article mark)
8625   "Mark current article as read.
8626 ARTICLE specifies the article to be marked as read.
8627 MARK specifies a string to be inserted at the beginning of the line."
8628   (gnus-summary-mark-article article mark))
8629
8630 (defun gnus-summary-clear-mark-forward (n)
8631   "Clear marks from N articles forward.
8632 If N is negative, clear backward instead.
8633 The difference between N and the number of marks cleared is returned."
8634   (interactive "p")
8635   (gnus-summary-mark-forward n gnus-unread-mark))
8636
8637 (defun gnus-summary-clear-mark-backward (n)
8638   "Clear marks from N articles backward.
8639 The difference between N and the number of marks cleared is returned."
8640   (interactive "p")
8641   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8642
8643 (defun gnus-summary-mark-unread-as-read ()
8644   "Intended to be used by `gnus-summary-mark-article-hook'."
8645   (when (memq gnus-current-article gnus-newsgroup-unreads)
8646     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8647
8648 (defun gnus-summary-mark-read-and-unread-as-read ()
8649   "Intended to be used by `gnus-summary-mark-article-hook'."
8650   (let ((mark (gnus-summary-article-mark)))
8651     (when (or (gnus-unread-mark-p mark)
8652               (gnus-read-mark-p mark))
8653       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8654
8655 (defun gnus-summary-mark-region-as-read (point mark all)
8656   "Mark all unread articles between point and mark as read.
8657 If given a prefix, mark all articles between point and mark as read,
8658 even ticked and dormant ones."
8659   (interactive "r\nP")
8660   (save-excursion
8661     (let (article)
8662       (goto-char point)
8663       (beginning-of-line)
8664       (while (and
8665               (< (point) mark)
8666               (progn
8667                 (when (or all
8668                           (memq (setq article (gnus-summary-article-number))
8669                                 gnus-newsgroup-unreads))
8670                   (gnus-summary-mark-article article gnus-del-mark))
8671                 t)
8672               (gnus-summary-find-next))))))
8673
8674 (defun gnus-summary-mark-below (score mark)
8675   "Mark articles with score less than SCORE with MARK."
8676   (interactive "P\ncMark: ")
8677   (setq score (if score
8678                   (prefix-numeric-value score)
8679                 (or gnus-summary-default-score 0)))
8680   (save-excursion
8681     (set-buffer gnus-summary-buffer)
8682     (goto-char (point-min))
8683     (while
8684         (progn
8685           (and (< (gnus-summary-article-score) score)
8686                (gnus-summary-mark-article nil mark))
8687           (gnus-summary-find-next)))))
8688
8689 (defun gnus-summary-kill-below (&optional score)
8690   "Mark articles with score below SCORE as read."
8691   (interactive "P")
8692   (gnus-summary-mark-below score gnus-killed-mark))
8693
8694 (defun gnus-summary-clear-above (&optional score)
8695   "Clear all marks from articles with score above SCORE."
8696   (interactive "P")
8697   (gnus-summary-mark-above score gnus-unread-mark))
8698
8699 (defun gnus-summary-tick-above (&optional score)
8700   "Tick all articles with score above SCORE."
8701   (interactive "P")
8702   (gnus-summary-mark-above score gnus-ticked-mark))
8703
8704 (defun gnus-summary-mark-above (score mark)
8705   "Mark articles with score over SCORE with MARK."
8706   (interactive "P\ncMark: ")
8707   (setq score (if score
8708                   (prefix-numeric-value score)
8709                 (or gnus-summary-default-score 0)))
8710   (save-excursion
8711     (set-buffer gnus-summary-buffer)
8712     (goto-char (point-min))
8713     (while (and (progn
8714                   (when (> (gnus-summary-article-score) score)
8715                     (gnus-summary-mark-article nil mark))
8716                   t)
8717                 (gnus-summary-find-next)))))
8718
8719 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8720 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8721 (defun gnus-summary-limit-include-expunged (&optional no-error)
8722   "Display all the hidden articles that were expunged for low scores."
8723   (interactive)
8724   (let ((buffer-read-only nil))
8725     (let ((scored gnus-newsgroup-scored)
8726           headers h)
8727       (while scored
8728         (unless (gnus-summary-goto-subject (caar scored))
8729           (and (setq h (gnus-summary-article-header (caar scored)))
8730                (< (cdar scored) gnus-summary-expunge-below)
8731                (push h headers)))
8732         (setq scored (cdr scored)))
8733       (if (not headers)
8734           (when (not no-error)
8735             (error "No expunged articles hidden"))
8736         (goto-char (point-min))
8737         (gnus-summary-prepare-unthreaded (nreverse headers))
8738         (goto-char (point-min))
8739         (gnus-summary-position-point)
8740         t))))
8741
8742 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8743   "Mark all unread articles in this newsgroup as read.
8744 If prefix argument ALL is non-nil, ticked and dormant articles will
8745 also be marked as read.
8746 If QUIETLY is non-nil, no questions will be asked.
8747 If TO-HERE is non-nil, it should be a point in the buffer.  All
8748 articles before this point will be marked as read.
8749 Note that this function will only catch up the unread article
8750 in the current summary buffer limitation.
8751 The number of articles marked as read is returned."
8752   (interactive "P")
8753   (prog1
8754       (save-excursion
8755         (when (or quietly
8756                   (not gnus-interactive-catchup) ;Without confirmation?
8757                   gnus-expert-user
8758                   (gnus-y-or-n-p
8759                    (if all
8760                        "Mark absolutely all articles as read? "
8761                      "Mark all unread articles as read? ")))
8762           (if (and not-mark
8763                    (not gnus-newsgroup-adaptive)
8764                    (not gnus-newsgroup-auto-expire)
8765                    (not gnus-suppress-duplicates)
8766                    (or (not gnus-use-cache)
8767                        (eq gnus-use-cache 'passive)))
8768               (progn
8769                 (when all
8770                   (setq gnus-newsgroup-marked nil
8771                         gnus-newsgroup-dormant nil))
8772                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8773             ;; We actually mark all articles as canceled, which we
8774             ;; have to do when using auto-expiry or adaptive scoring.
8775             (gnus-summary-show-all-threads)
8776             (when (gnus-summary-first-subject (not all) t)
8777               (while (and
8778                       (if to-here (< (point) to-here) t)
8779                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8780                       (gnus-summary-find-next (not all) nil nil t))))
8781             (gnus-set-mode-line 'summary))
8782           t))
8783     (gnus-summary-position-point)))
8784
8785 (defun gnus-summary-catchup-to-here (&optional all)
8786   "Mark all unticked articles before the current one as read.
8787 If ALL is non-nil, also mark ticked and dormant articles as read."
8788   (interactive "P")
8789   (save-excursion
8790     (gnus-save-hidden-threads
8791       (let ((beg (point)))
8792         ;; We check that there are unread articles.
8793         (when (or all (gnus-summary-find-prev))
8794           (gnus-summary-catchup all t beg)))))
8795   (gnus-summary-position-point))
8796
8797 (defun gnus-summary-catchup-all (&optional quietly)
8798   "Mark all articles in this newsgroup as read."
8799   (interactive "P")
8800   (gnus-summary-catchup t quietly))
8801
8802 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8803   "Mark all unread articles in this group as read, then exit.
8804 If prefix argument ALL is non-nil, all articles are marked as read."
8805   (interactive "P")
8806   (when (gnus-summary-catchup all quietly nil 'fast)
8807     ;; Select next newsgroup or exit.
8808     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8809              (eq gnus-auto-select-next 'quietly))
8810         (gnus-summary-next-group nil)
8811       (gnus-summary-exit))))
8812
8813 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8814   "Mark all articles in this newsgroup as read, and then exit."
8815   (interactive "P")
8816   (gnus-summary-catchup-and-exit t quietly))
8817
8818 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8819   "Mark all articles in this group as read and select the next group.
8820 If given a prefix, mark all articles, unread as well as ticked, as
8821 read."
8822   (interactive "P")
8823   (save-excursion
8824     (gnus-summary-catchup all))
8825   (gnus-summary-next-group))
8826
8827 ;;;
8828 ;;; with article
8829 ;;;
8830
8831 (defmacro gnus-with-article (article &rest forms)
8832   "Select ARTICLE and perform FORMS in the original article buffer.
8833 Then replace the article with the result."
8834   `(progn
8835      ;; We don't want the article to be marked as read.
8836      (let (gnus-mark-article-hook)
8837        (gnus-summary-select-article t t nil ,article))
8838      (set-buffer gnus-original-article-buffer)
8839      ,@forms
8840      (if (not (gnus-check-backend-function
8841                'request-replace-article (car gnus-article-current)))
8842          (gnus-message 5 "Read-only group; not replacing")
8843        (unless (gnus-request-replace-article
8844                 ,article (car gnus-article-current)
8845                 (current-buffer) t)
8846          (error "Couldn't replace article")))
8847      ;; The cache and backlog have to be flushed somewhat.
8848      (when gnus-keep-backlog
8849        (gnus-backlog-remove-article
8850         (car gnus-article-current) (cdr gnus-article-current)))
8851      (when gnus-use-cache
8852        (gnus-cache-update-article
8853         (car gnus-article-current) (cdr gnus-article-current)))))
8854
8855 (put 'gnus-with-article 'lisp-indent-function 1)
8856 (put 'gnus-with-article 'edebug-form-spec '(form body))
8857
8858 ;; Thread-based commands.
8859
8860 (defun gnus-summary-articles-in-thread (&optional article)
8861   "Return a list of all articles in the current thread.
8862 If ARTICLE is non-nil, return all articles in the thread that starts
8863 with that article."
8864   (let* ((article (or article (gnus-summary-article-number)))
8865          (data (gnus-data-find-list article))
8866          (top-level (gnus-data-level (car data)))
8867          (top-subject
8868           (cond ((null gnus-thread-operation-ignore-subject)
8869                  (gnus-simplify-subject-re
8870                   (mail-header-subject (gnus-data-header (car data)))))
8871                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8872                  (gnus-simplify-subject-fuzzy
8873                   (mail-header-subject (gnus-data-header (car data)))))
8874                 (t nil)))
8875          (end-point (save-excursion
8876                       (if (gnus-summary-go-to-next-thread)
8877                           (point) (point-max))))
8878          articles)
8879     (while (and data
8880                 (< (gnus-data-pos (car data)) end-point))
8881       (when (or (not top-subject)
8882                 (string= top-subject
8883                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8884                              (gnus-simplify-subject-fuzzy
8885                               (mail-header-subject
8886                                (gnus-data-header (car data))))
8887                            (gnus-simplify-subject-re
8888                             (mail-header-subject
8889                              (gnus-data-header (car data)))))))
8890         (push (gnus-data-number (car data)) articles))
8891       (unless (and (setq data (cdr data))
8892                    (> (gnus-data-level (car data)) top-level))
8893         (setq data nil)))
8894     ;; Return the list of articles.
8895     (nreverse articles)))
8896
8897 (defun gnus-summary-rethread-current ()
8898   "Rethread the thread the current article is part of."
8899   (interactive)
8900   (let* ((gnus-show-threads t)
8901          (article (gnus-summary-article-number))
8902          (id (mail-header-id (gnus-summary-article-header)))
8903          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8904     (unless id
8905       (error "No article on the current line"))
8906     (gnus-rebuild-thread id)
8907     (gnus-summary-goto-subject article)))
8908
8909 (defun gnus-summary-reparent-thread ()
8910   "Make the current article child of the marked (or previous) article.
8911
8912 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8913 is non-nil or the Subject: of both articles are the same."
8914   (interactive)
8915   (unless (not (gnus-group-read-only-p))
8916     (error "The current newsgroup does not support article editing"))
8917   (unless (<= (length gnus-newsgroup-processable) 1)
8918     (error "No more than one article may be marked"))
8919   (save-window-excursion
8920     (let ((gnus-article-buffer " *reparent*")
8921           (current-article (gnus-summary-article-number))
8922           ;; First grab the marked article, otherwise one line up.
8923           (parent-article (if (not (null gnus-newsgroup-processable))
8924                               (car gnus-newsgroup-processable)
8925                             (save-excursion
8926                               (if (eq (forward-line -1) 0)
8927                                   (gnus-summary-article-number)
8928                                 (error "Beginning of summary buffer"))))))
8929       (unless (not (eq current-article parent-article))
8930         (error "An article may not be self-referential"))
8931       (let ((message-id (mail-header-id
8932                          (gnus-summary-article-header parent-article))))
8933         (unless (and message-id (not (equal message-id "")))
8934           (error "No message-id in desired parent"))
8935         (gnus-with-article current-article
8936           (save-restriction
8937             (goto-char (point-min))
8938             (message-narrow-to-head)
8939             (if (re-search-forward "^References: " nil t)
8940                 (progn
8941                   (re-search-forward "^[^ \t]" nil t)
8942                   (forward-line -1)
8943                   (end-of-line)
8944                   (insert " " message-id))
8945               (insert "References: " message-id "\n"))))
8946         (set-buffer gnus-summary-buffer)
8947         (gnus-summary-unmark-all-processable)
8948         (gnus-summary-update-article current-article)
8949         (gnus-summary-rethread-current)
8950         (gnus-message 3 "Article %d is now the child of article %d"
8951                       current-article parent-article)))))
8952
8953 (defun gnus-summary-toggle-threads (&optional arg)
8954   "Toggle showing conversation threads.
8955 If ARG is positive number, turn showing conversation threads on."
8956   (interactive "P")
8957   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8958     (setq gnus-show-threads
8959           (if (null arg) (not gnus-show-threads)
8960             (> (prefix-numeric-value arg) 0)))
8961     (gnus-summary-prepare)
8962     (gnus-summary-goto-subject current)
8963     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8964     (gnus-summary-position-point)))
8965
8966 (defun gnus-summary-show-all-threads ()
8967   "Show all threads."
8968   (interactive)
8969   (save-excursion
8970     (let ((buffer-read-only nil))
8971       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8972   (gnus-summary-position-point))
8973
8974 (defun gnus-summary-show-thread ()
8975   "Show thread subtrees.
8976 Returns nil if no thread was there to be shown."
8977   (interactive)
8978   (let ((buffer-read-only nil)
8979         (orig (point))
8980         ;; first goto end then to beg, to have point at beg after let
8981         (end (progn (end-of-line) (point)))
8982         (beg (progn (beginning-of-line) (point))))
8983     (prog1
8984         ;; Any hidden lines here?
8985         (search-forward "\r" end t)
8986       (subst-char-in-region beg end ?\^M ?\n t)
8987       (goto-char orig)
8988       (gnus-summary-position-point))))
8989
8990 (defun gnus-summary-hide-all-threads ()
8991   "Hide all thread subtrees."
8992   (interactive)
8993   (save-excursion
8994     (goto-char (point-min))
8995     (gnus-summary-hide-thread)
8996     (while (zerop (gnus-summary-next-thread 1 t))
8997       (gnus-summary-hide-thread)))
8998   (gnus-summary-position-point))
8999
9000 (defun gnus-summary-hide-thread ()
9001   "Hide thread subtrees.
9002 Returns nil if no threads were there to be hidden."
9003   (interactive)
9004   (let ((buffer-read-only nil)
9005         (start (point))
9006         (article (gnus-summary-article-number)))
9007     (goto-char start)
9008     ;; Go forward until either the buffer ends or the subthread
9009     ;; ends.
9010     (when (and (not (eobp))
9011                (or (zerop (gnus-summary-next-thread 1 t))
9012                    (goto-char (point-max))))
9013       (prog1
9014           (if (and (> (point) start)
9015                    (search-backward "\n" start t))
9016               (progn
9017                 (subst-char-in-region start (point) ?\n ?\^M)
9018                 (gnus-summary-goto-subject article))
9019             (goto-char start)
9020             nil)))))
9021
9022 (defun gnus-summary-go-to-next-thread (&optional previous)
9023   "Go to the same level (or less) next thread.
9024 If PREVIOUS is non-nil, go to previous thread instead.
9025 Return the article number moved to, or nil if moving was impossible."
9026   (let ((level (gnus-summary-thread-level))
9027         (way (if previous -1 1))
9028         (beg (point)))
9029     (forward-line way)
9030     (while (and (not (eobp))
9031                 (< level (gnus-summary-thread-level)))
9032       (forward-line way))
9033     (if (eobp)
9034         (progn
9035           (goto-char beg)
9036           nil)
9037       (setq beg (point))
9038       (prog1
9039           (gnus-summary-article-number)
9040         (goto-char beg)))))
9041
9042 (defun gnus-summary-next-thread (n &optional silent)
9043   "Go to the same level next N'th thread.
9044 If N is negative, search backward instead.
9045 Returns the difference between N and the number of skips actually
9046 done.
9047
9048 If SILENT, don't output messages."
9049   (interactive "p")
9050   (let ((backward (< n 0))
9051         (n (abs n)))
9052     (while (and (> n 0)
9053                 (gnus-summary-go-to-next-thread backward))
9054       (decf n))
9055     (unless silent
9056       (gnus-summary-position-point))
9057     (when (and (not silent) (/= 0 n))
9058       (gnus-message 7 "No more threads"))
9059     n))
9060
9061 (defun gnus-summary-prev-thread (n)
9062   "Go to the same level previous N'th thread.
9063 Returns the difference between N and the number of skips actually
9064 done."
9065   (interactive "p")
9066   (gnus-summary-next-thread (- n)))
9067
9068 (defun gnus-summary-go-down-thread ()
9069   "Go down one level in the current thread."
9070   (let ((children (gnus-summary-article-children)))
9071     (when children
9072       (gnus-summary-goto-subject (car children)))))
9073
9074 (defun gnus-summary-go-up-thread ()
9075   "Go up one level in the current thread."
9076   (let ((parent (gnus-summary-article-parent)))
9077     (when parent
9078       (gnus-summary-goto-subject parent))))
9079
9080 (defun gnus-summary-down-thread (n)
9081   "Go down thread N steps.
9082 If N is negative, go up instead.
9083 Returns the difference between N and how many steps down that were
9084 taken."
9085   (interactive "p")
9086   (let ((up (< n 0))
9087         (n (abs n)))
9088     (while (and (> n 0)
9089                 (if up (gnus-summary-go-up-thread)
9090                   (gnus-summary-go-down-thread)))
9091       (setq n (1- n)))
9092     (gnus-summary-position-point)
9093     (when (/= 0 n)
9094       (gnus-message 7 "Can't go further"))
9095     n))
9096
9097 (defun gnus-summary-up-thread (n)
9098   "Go up thread N steps.
9099 If N is negative, go up instead.
9100 Returns the difference between N and how many steps down that were
9101 taken."
9102   (interactive "p")
9103   (gnus-summary-down-thread (- n)))
9104
9105 (defun gnus-summary-top-thread ()
9106   "Go to the top of the thread."
9107   (interactive)
9108   (while (gnus-summary-go-up-thread))
9109   (gnus-summary-article-number))
9110
9111 (defun gnus-summary-kill-thread (&optional unmark)
9112   "Mark articles under current thread as read.
9113 If the prefix argument is positive, remove any kinds of marks.
9114 If the prefix argument is negative, tick articles instead."
9115   (interactive "P")
9116   (when unmark
9117     (setq unmark (prefix-numeric-value unmark)))
9118   (let ((articles (gnus-summary-articles-in-thread)))
9119     (save-excursion
9120       ;; Expand the thread.
9121       (gnus-summary-show-thread)
9122       ;; Mark all the articles.
9123       (while articles
9124         (gnus-summary-goto-subject (car articles))
9125         (cond ((null unmark)
9126                (gnus-summary-mark-article-as-read gnus-killed-mark))
9127               ((> unmark 0)
9128                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9129               (t
9130                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9131         (setq articles (cdr articles))))
9132     ;; Hide killed subtrees.
9133     (and (null unmark)
9134          gnus-thread-hide-killed
9135          (gnus-summary-hide-thread))
9136     ;; If marked as read, go to next unread subject.
9137     (when (null unmark)
9138       ;; Go to next unread subject.
9139       (gnus-summary-next-subject 1 t)))
9140   (gnus-set-mode-line 'summary))
9141
9142 ;; Summary sorting commands
9143
9144 (defun gnus-summary-sort-by-number (&optional reverse)
9145   "Sort the summary buffer by article number.
9146 Argument REVERSE means reverse order."
9147   (interactive "P")
9148   (gnus-summary-sort 'number reverse))
9149
9150 (defun gnus-summary-sort-by-author (&optional reverse)
9151   "Sort the summary buffer by author name alphabetically.
9152 If `case-fold-search' is non-nil, case of letters is ignored.
9153 Argument REVERSE means reverse order."
9154   (interactive "P")
9155   (gnus-summary-sort 'author reverse))
9156
9157 (defun gnus-summary-sort-by-subject (&optional reverse)
9158   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9159 If `case-fold-search' is non-nil, case of letters is ignored.
9160 Argument REVERSE means reverse order."
9161   (interactive "P")
9162   (gnus-summary-sort 'subject reverse))
9163
9164 (defun gnus-summary-sort-by-date (&optional reverse)
9165   "Sort the summary buffer by date.
9166 Argument REVERSE means reverse order."
9167   (interactive "P")
9168   (gnus-summary-sort 'date reverse))
9169
9170 (defun gnus-summary-sort-by-score (&optional reverse)
9171   "Sort the summary buffer by score.
9172 Argument REVERSE means reverse order."
9173   (interactive "P")
9174   (gnus-summary-sort 'score reverse))
9175
9176 (defun gnus-summary-sort-by-lines (&optional reverse)
9177   "Sort the summary buffer by the number of lines.
9178 Argument REVERSE means reverse order."
9179   (interactive "P")
9180   (gnus-summary-sort 'lines reverse))
9181
9182 (defun gnus-summary-sort-by-chars (&optional reverse)
9183   "Sort the summary buffer by article length.
9184 Argument REVERSE means reverse order."
9185   (interactive "P")
9186   (gnus-summary-sort 'chars reverse))
9187
9188 (defun gnus-summary-sort (predicate reverse)
9189   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9190   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9191          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9192          (gnus-thread-sort-functions
9193           (if (not reverse)
9194               thread
9195             `(lambda (t1 t2)
9196                (,thread t2 t1))))
9197          (gnus-sort-gathered-threads-function
9198           gnus-thread-sort-functions)
9199          (gnus-article-sort-functions
9200           (if (not reverse)
9201               article
9202             `(lambda (t1 t2)
9203                (,article t2 t1))))
9204          (buffer-read-only)
9205          (gnus-summary-prepare-hook nil))
9206     ;; We do the sorting by regenerating the threads.
9207     (gnus-summary-prepare)
9208     ;; Hide subthreads if needed.
9209     (when (and gnus-show-threads gnus-thread-hide-subtree)
9210       (gnus-summary-hide-all-threads))))
9211
9212 ;; Summary saving commands.
9213
9214 (defun gnus-summary-save-article (&optional n not-saved)
9215   "Save the current article using the default saver function.
9216 If N is a positive number, save the N next articles.
9217 If N is a negative number, save the N previous articles.
9218 If N is nil and any articles have been marked with the process mark,
9219 save those articles instead.
9220 The variable `gnus-default-article-saver' specifies the saver function."
9221   (interactive "P")
9222   (let* ((articles (gnus-summary-work-articles n))
9223          (save-buffer (save-excursion
9224                         (nnheader-set-temp-buffer " *Gnus Save*")))
9225          (num (length articles))
9226          header file)
9227     (dolist (article articles)
9228       (setq header (gnus-summary-article-header article))
9229       (if (not (vectorp header))
9230           ;; This is a pseudo-article.
9231           (if (assq 'name header)
9232               (gnus-copy-file (cdr (assq 'name header)))
9233             (gnus-message 1 "Article %d is unsaveable" article))
9234         ;; This is a real article.
9235         (save-window-excursion
9236           (gnus-summary-select-article t nil nil article))
9237         (save-excursion
9238           (set-buffer save-buffer)
9239           (erase-buffer)
9240           (insert-buffer-substring gnus-original-article-buffer))
9241         (setq file (gnus-article-save save-buffer file num))
9242         (gnus-summary-remove-process-mark article)
9243         (unless not-saved
9244           (gnus-summary-set-saved-mark article))))
9245     (gnus-kill-buffer save-buffer)
9246     (gnus-summary-position-point)
9247     (gnus-set-mode-line 'summary)
9248     n))
9249
9250 (defun gnus-summary-pipe-output (&optional arg)
9251   "Pipe the current article to a subprocess.
9252 If N is a positive number, pipe the N next articles.
9253 If N is a negative number, pipe the N previous articles.
9254 If N is nil and any articles have been marked with the process mark,
9255 pipe those articles instead."
9256   (interactive "P")
9257   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9258     (gnus-summary-save-article arg t))
9259   (gnus-configure-windows 'pipe))
9260
9261 (defun gnus-summary-save-article-mail (&optional arg)
9262   "Append the current article to an mail file.
9263 If N is a positive number, save the N next articles.
9264 If N is a negative number, save the N previous articles.
9265 If N is nil and any articles have been marked with the process mark,
9266 save those articles instead."
9267   (interactive "P")
9268   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9269     (gnus-summary-save-article arg)))
9270
9271 (defun gnus-summary-save-article-rmail (&optional arg)
9272   "Append the current article to an rmail file.
9273 If N is a positive number, save the N next articles.
9274 If N is a negative number, save the N previous articles.
9275 If N is nil and any articles have been marked with the process mark,
9276 save those articles instead."
9277   (interactive "P")
9278   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9279     (gnus-summary-save-article arg)))
9280
9281 (defun gnus-summary-save-article-file (&optional arg)
9282   "Append the current article to a file.
9283 If N is a positive number, save the N next articles.
9284 If N is a negative number, save the N previous articles.
9285 If N is nil and any articles have been marked with the process mark,
9286 save those articles instead."
9287   (interactive "P")
9288   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9289     (gnus-summary-save-article arg)))
9290
9291 (defun gnus-summary-write-article-file (&optional arg)
9292   "Write the current article to a file, deleting the previous file.
9293 If N is a positive number, save the N next articles.
9294 If N is a negative number, save the N previous articles.
9295 If N is nil and any articles have been marked with the process mark,
9296 save those articles instead."
9297   (interactive "P")
9298   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9299     (gnus-summary-save-article arg)))
9300
9301 (defun gnus-summary-save-article-body-file (&optional arg)
9302   "Append the current article body to a file.
9303 If N is a positive number, save the N next articles.
9304 If N is a negative number, save the N previous articles.
9305 If N is nil and any articles have been marked with the process mark,
9306 save those articles instead."
9307   (interactive "P")
9308   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9309     (gnus-summary-save-article arg)))
9310
9311 (defun gnus-summary-pipe-message (program)
9312   "Pipe the current article through PROGRAM."
9313   (interactive "sProgram: ")
9314   (gnus-summary-select-article)
9315   (let ((mail-header-separator ""))
9316     (gnus-eval-in-buffer-window gnus-article-buffer
9317       (save-restriction
9318         (widen)
9319         (let ((start (window-start))
9320               buffer-read-only)
9321           (message-pipe-buffer-body program)
9322           (set-window-start (get-buffer-window (current-buffer)) start))))))
9323
9324 (defun gnus-get-split-value (methods)
9325   "Return a value based on the split METHODS."
9326   (let (split-name method result match)
9327     (when methods
9328       (save-excursion
9329         (set-buffer gnus-original-article-buffer)
9330         (save-restriction
9331           (nnheader-narrow-to-headers)
9332           (while (and methods (not split-name))
9333             (goto-char (point-min))
9334             (setq method (pop methods))
9335             (setq match (car method))
9336             (when (cond
9337                    ((stringp match)
9338                     ;; Regular expression.
9339                     (ignore-errors
9340                       (re-search-forward match nil t)))
9341                    ((gnus-functionp match)
9342                     ;; Function.
9343                     (save-restriction
9344                       (widen)
9345                       (setq result (funcall match gnus-newsgroup-name))))
9346                    ((consp match)
9347                     ;; Form.
9348                     (save-restriction
9349                       (widen)
9350                       (setq result (eval match)))))
9351               (setq split-name (cdr method))
9352               (cond ((stringp result)
9353                      (push (expand-file-name
9354                             result gnus-article-save-directory)
9355                            split-name))
9356                     ((consp result)
9357                      (setq split-name (append result split-name)))))))))
9358     (nreverse split-name)))
9359
9360 (defun gnus-valid-move-group-p (group)
9361   (and (boundp group)
9362        (symbol-name group)
9363        (symbol-value group)
9364        (gnus-get-function (gnus-find-method-for-group
9365                            (symbol-name group)) 'request-accept-article t)))
9366
9367 (defun gnus-read-move-group-name (prompt default articles prefix)
9368   "Read a group name."
9369   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9370          (minibuffer-confirm-incomplete nil) ; XEmacs
9371          (prom
9372           (format "%s %s to:"
9373                   prompt
9374                   (if (> (length articles) 1)
9375                       (format "these %d articles" (length articles))
9376                     "this article")))
9377          (to-newsgroup
9378           (cond
9379            ((null split-name)
9380             (gnus-completing-read default prom
9381                                   gnus-active-hashtb
9382                                   'gnus-valid-move-group-p
9383                                   nil prefix
9384                                   'gnus-group-history))
9385            ((= 1 (length split-name))
9386             (gnus-completing-read (car split-name) prom
9387                                   gnus-active-hashtb
9388                                   'gnus-valid-move-group-p
9389                                   nil nil
9390                                   'gnus-group-history))
9391            (t
9392             (gnus-completing-read nil prom
9393                                   (mapcar (lambda (el) (list el))
9394                                           (nreverse split-name))
9395                                   nil nil nil
9396                                   'gnus-group-history))))
9397          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9398     (when to-newsgroup
9399       (if (or (string= to-newsgroup "")
9400               (string= to-newsgroup prefix))
9401           (setq to-newsgroup default))
9402       (unless to-newsgroup
9403         (error "No group name entered"))
9404       (or (gnus-active to-newsgroup)
9405           (gnus-activate-group to-newsgroup nil nil to-method)
9406           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9407                                      to-newsgroup))
9408               (or (and (gnus-request-create-group to-newsgroup to-method)
9409                        (gnus-activate-group
9410                         to-newsgroup nil nil to-method)
9411                        (gnus-subscribe-group to-newsgroup))
9412                   (error "Couldn't create group %s" to-newsgroup)))
9413           (error "No such group: %s" to-newsgroup)))
9414     to-newsgroup))
9415
9416 (defun gnus-summary-save-parts (type dir n &optional reverse)
9417   "Save parts matching TYPE to DIR.
9418 If REVERSE, save parts that do not match TYPE."
9419   (interactive
9420    (list (read-string "Save parts of type: " 
9421                       (or (car gnus-summary-save-parts-type-history)
9422                           gnus-summary-save-parts-default-mime)
9423                       'gnus-summary-save-parts-type-history)
9424          (setq gnus-summary-save-parts-last-directory
9425                (read-file-name "Save to directory: " 
9426                                gnus-summary-save-parts-last-directory
9427                                nil t))
9428          current-prefix-arg))
9429   (gnus-summary-iterate n
9430     (let ((gnus-display-mime-function nil)
9431           (gnus-inhibit-treatment t))
9432       (gnus-summary-select-article))
9433     (save-excursion
9434       (set-buffer gnus-article-buffer)
9435       (let ((handles (or gnus-article-mime-handles
9436                          (mm-dissect-buffer) (mm-uu-dissect))))
9437         (when handles
9438           (gnus-summary-save-parts-1 type dir handles reverse)
9439           (unless gnus-article-mime-handles ;; Don't destroy this case.
9440             (mm-destroy-parts handles)))))))
9441
9442 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9443   (if (stringp (car handle))
9444       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9445               (cdr handle))
9446     (when (if reverse
9447               (not (string-match type (mm-handle-media-type handle)))
9448             (string-match type (mm-handle-media-type handle)))
9449       (let ((file (expand-file-name
9450                    (file-name-nondirectory
9451                     (or
9452                      (mail-content-type-get
9453                       (mm-handle-disposition handle) 'filename)
9454                      (concat gnus-newsgroup-name
9455                              "." (number-to-string
9456                                   (cdr gnus-article-current)))))
9457                    dir)))
9458         (unless (file-exists-p file)
9459           (mm-save-part-to-file handle file))))))
9460
9461 ;; Summary extract commands
9462
9463 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9464   (let ((buffer-read-only nil)
9465         (article (gnus-summary-article-number))
9466         after-article b e)
9467     (unless (gnus-summary-goto-subject article)
9468       (error "No such article: %d" article))
9469     (gnus-summary-position-point)
9470     ;; If all commands are to be bunched up on one line, we collect
9471     ;; them here.
9472     (unless gnus-view-pseudos-separately
9473       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9474             files action)
9475         (while ps
9476           (setq action (cdr (assq 'action (car ps))))
9477           (setq files (list (cdr (assq 'name (car ps)))))
9478           (while (and ps (cdr ps)
9479                       (string= (or action "1")
9480                                (or (cdr (assq 'action (cadr ps))) "2")))
9481             (push (cdr (assq 'name (cadr ps))) files)
9482             (setcdr ps (cddr ps)))
9483           (when files
9484             (when (not (string-match "%s" action))
9485               (push " " files))
9486             (push " " files)
9487             (when (assq 'execute (car ps))
9488               (setcdr (assq 'execute (car ps))
9489                       (funcall (if (string-match "%s" action)
9490                                    'format 'concat)
9491                                action
9492                                (mapconcat
9493                                 (lambda (f)
9494                                   (if (equal f " ")
9495                                       f
9496                                     (mm-quote-arg f)))
9497                                 files " ")))))
9498           (setq ps (cdr ps)))))
9499     (if (and gnus-view-pseudos (not not-view))
9500         (while pslist
9501           (when (assq 'execute (car pslist))
9502             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9503                                   (eq gnus-view-pseudos 'not-confirm)))
9504           (setq pslist (cdr pslist)))
9505       (save-excursion
9506         (while pslist
9507           (setq after-article (or (cdr (assq 'article (car pslist)))
9508                                   (gnus-summary-article-number)))
9509           (gnus-summary-goto-subject after-article)
9510           (forward-line 1)
9511           (setq b (point))
9512           (insert "    " (file-name-nondirectory
9513                           (cdr (assq 'name (car pslist))))
9514                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9515           (setq e (point))
9516           (forward-line -1)             ; back to `b'
9517           (gnus-add-text-properties
9518            b (1- e) (list 'gnus-number gnus-reffed-article-number
9519                           gnus-mouse-face-prop gnus-mouse-face))
9520           (gnus-data-enter
9521            after-article gnus-reffed-article-number
9522            gnus-unread-mark b (car pslist) 0 (- e b))
9523           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9524           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9525           (setq pslist (cdr pslist)))))))
9526
9527 (defun gnus-pseudos< (p1 p2)
9528   (let ((c1 (cdr (assq 'action p1)))
9529         (c2 (cdr (assq 'action p2))))
9530     (and c1 c2 (string< c1 c2))))
9531
9532 (defun gnus-request-pseudo-article (props)
9533   (cond ((assq 'execute props)
9534          (gnus-execute-command (cdr (assq 'execute props)))))
9535   (let ((gnus-current-article (gnus-summary-article-number)))
9536     (gnus-run-hooks 'gnus-mark-article-hook)))
9537
9538 (defun gnus-execute-command (command &optional automatic)
9539   (save-excursion
9540     (gnus-article-setup-buffer)
9541     (set-buffer gnus-article-buffer)
9542     (setq buffer-read-only nil)
9543     (let ((command (if automatic command
9544                      (read-string "Command: " (cons command 0)))))
9545       (erase-buffer)
9546       (insert "$ " command "\n\n")
9547       (if gnus-view-pseudo-asynchronously
9548           (start-process "gnus-execute" (current-buffer) shell-file-name
9549                          shell-command-switch command)
9550         (call-process shell-file-name nil t nil
9551                       shell-command-switch command)))))
9552
9553 ;; Summary kill commands.
9554
9555 (defun gnus-summary-edit-global-kill (article)
9556   "Edit the \"global\" kill file."
9557   (interactive (list (gnus-summary-article-number)))
9558   (gnus-group-edit-global-kill article))
9559
9560 (defun gnus-summary-edit-local-kill ()
9561   "Edit a local kill file applied to the current newsgroup."
9562   (interactive)
9563   (setq gnus-current-headers (gnus-summary-article-header))
9564   (gnus-group-edit-local-kill
9565    (gnus-summary-article-number) gnus-newsgroup-name))
9566
9567 ;;; Header reading.
9568
9569 (defun gnus-read-header (id &optional header)
9570   "Read the headers of article ID and enter them into the Gnus system."
9571   (let ((group gnus-newsgroup-name)
9572         (gnus-override-method
9573          (or
9574           gnus-override-method
9575           (and (gnus-news-group-p gnus-newsgroup-name)
9576                (car (gnus-refer-article-methods)))))
9577         where)
9578     ;; First we check to see whether the header in question is already
9579     ;; fetched.
9580     (if (stringp id)
9581         ;; This is a Message-ID.
9582         (setq header (or header (gnus-id-to-header id)))
9583       ;; This is an article number.
9584       (setq header (or header (gnus-summary-article-header id))))
9585     (if (and header
9586              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9587         ;; We have found the header.
9588         header
9589       ;; If this is a sparse article, we have to nix out its
9590       ;; previous entry in the thread hashtb.
9591       (when (and header
9592                  (gnus-summary-article-sparse-p (mail-header-number header)))
9593         (let* ((parent (gnus-parent-id (mail-header-references header)))
9594                (thread (and parent (gnus-id-to-thread parent))))
9595           (when thread
9596             (delq (assq header thread) thread))))
9597       ;; We have to really fetch the header to this article.
9598       (save-excursion
9599         (set-buffer nntp-server-buffer)
9600         (when (setq where (gnus-request-head id group))
9601           (nnheader-fold-continuation-lines)
9602           (goto-char (point-max))
9603           (insert ".\n")
9604           (goto-char (point-min))
9605           (insert "211 ")
9606           (princ (cond
9607                   ((numberp id) id)
9608                   ((cdr where) (cdr where))
9609                   (header (mail-header-number header))
9610                   (t gnus-reffed-article-number))
9611                  (current-buffer))
9612           (insert " Article retrieved.\n"))
9613         (if (or (not where)
9614                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9615             ()                          ; Malformed head.
9616           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9617             (when (and (stringp id)
9618                        (not (string= (gnus-group-real-name group)
9619                                      (car where))))
9620               ;; If we fetched by Message-ID and the article came
9621               ;; from a different group, we fudge some bogus article
9622               ;; numbers for this article.
9623               (mail-header-set-number header gnus-reffed-article-number))
9624             (save-excursion
9625               (set-buffer gnus-summary-buffer)
9626               (decf gnus-reffed-article-number)
9627               (gnus-remove-header (mail-header-number header))
9628               (push header gnus-newsgroup-headers)
9629               (setq gnus-current-headers header)
9630               (push (mail-header-number header) gnus-newsgroup-limit)))
9631           header)))))
9632
9633 (defun gnus-remove-header (number)
9634   "Remove header NUMBER from `gnus-newsgroup-headers'."
9635   (if (and gnus-newsgroup-headers
9636            (= number (mail-header-number (car gnus-newsgroup-headers))))
9637       (pop gnus-newsgroup-headers)
9638     (let ((headers gnus-newsgroup-headers))
9639       (while (and (cdr headers)
9640                   (not (= number (mail-header-number (cadr headers)))))
9641         (pop headers))
9642       (when (cdr headers)
9643         (setcdr headers (cddr headers))))))
9644
9645 ;;;
9646 ;;; summary highlights
9647 ;;;
9648
9649 (defun gnus-highlight-selected-summary ()
9650   "Highlight selected article in summary buffer."
9651   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9652   (when gnus-summary-selected-face
9653     (save-excursion
9654       (let* ((beg (progn (beginning-of-line) (point)))
9655              (end (progn (end-of-line) (point)))
9656              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9657              (from (if (get-text-property beg gnus-mouse-face-prop)
9658                        beg
9659                      (or (next-single-property-change
9660                           beg gnus-mouse-face-prop nil end)
9661                          beg)))
9662              (to
9663               (if (= from end)
9664                   (- from 2)
9665                 (or (next-single-property-change
9666                      from gnus-mouse-face-prop nil end)
9667                     end))))
9668         ;; If no mouse-face prop on line we will have to = from = end,
9669         ;; so we highlight the entire line instead.
9670         (when (= (+ to 2) from)
9671           (setq from beg)
9672           (setq to end))
9673         (if gnus-newsgroup-selected-overlay
9674             ;; Move old overlay.
9675             (gnus-move-overlay
9676              gnus-newsgroup-selected-overlay from to (current-buffer))
9677           ;; Create new overlay.
9678           (gnus-overlay-put
9679            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9680            'face gnus-summary-selected-face))))))
9681
9682 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9683 (defun gnus-summary-highlight-line ()
9684   "Highlight current line according to `gnus-summary-highlight'."
9685   (let* ((list gnus-summary-highlight)
9686          (p (point))
9687          (end (progn (end-of-line) (point)))
9688          ;; now find out where the line starts and leave point there.
9689          (beg (progn (beginning-of-line) (point)))
9690          (article (gnus-summary-article-number))
9691          (score (or (cdr (assq (or article gnus-current-article)
9692                                gnus-newsgroup-scored))
9693                     gnus-summary-default-score 0))
9694          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9695          (inhibit-read-only t))
9696     ;; Eval the cars of the lists until we find a match.
9697     (let ((default gnus-summary-default-score))
9698       (while (and list
9699                   (not (eval (caar list))))
9700         (setq list (cdr list))))
9701     (let ((face (cdar list)))
9702       (unless (eq face (get-text-property beg 'face))
9703         (gnus-put-text-property-excluding-characters-with-faces
9704          beg end 'face
9705          (setq face (if (boundp face) (symbol-value face) face)))
9706         (when gnus-summary-highlight-line-function
9707           (funcall gnus-summary-highlight-line-function article face))))
9708     (goto-char p)))
9709
9710 (defun gnus-update-read-articles (group unread &optional compute)
9711   "Update the list of read articles in GROUP."
9712   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9713          (entry (gnus-gethash group gnus-newsrc-hashtb))
9714          (info (nth 2 entry))
9715          (prev 1)
9716          (unread (sort (copy-sequence unread) '<))
9717          read)
9718     (if (or (not info) (not active))
9719         ;; There is no info on this group if it was, in fact,
9720         ;; killed.  Gnus stores no information on killed groups, so
9721         ;; there's nothing to be done.
9722         ;; One could store the information somewhere temporarily,
9723         ;; perhaps...  Hmmm...
9724         ()
9725       ;; Remove any negative articles numbers.
9726       (while (and unread (< (car unread) 0))
9727         (setq unread (cdr unread)))
9728       ;; Remove any expired article numbers
9729       (while (and unread (< (car unread) (car active)))
9730         (setq unread (cdr unread)))
9731       ;; Compute the ranges of read articles by looking at the list of
9732       ;; unread articles.
9733       (while unread
9734         (when (/= (car unread) prev)
9735           (push (if (= prev (1- (car unread))) prev
9736                   (cons prev (1- (car unread))))
9737                 read))
9738         (setq prev (1+ (car unread)))
9739         (setq unread (cdr unread)))
9740       (when (<= prev (cdr active))
9741         (push (cons prev (cdr active)) read))
9742       (setq read (if (> (length read) 1) (nreverse read) read))
9743       (if compute
9744           read
9745         (save-excursion
9746           (let (setmarkundo)
9747             ;; Propagate the read marks to the backend.
9748             (when (gnus-check-backend-function 'request-set-mark group)
9749               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9750                     (add (gnus-remove-from-range read (gnus-info-read info))))
9751                 (when (or add del)
9752                   (unless (gnus-check-group group)
9753                     (error "Can't open server for %s" group))
9754                   (gnus-request-set-mark
9755                    group (delq nil (list (if add (list add 'add '(read)))
9756                                          (if del (list del 'del '(read))))))
9757                   (setq setmarkundo
9758                         `(gnus-request-set-mark
9759                           ,group
9760                           ',(delq nil (list
9761                                        (if del (list del 'add '(read)))
9762                                        (if add (list add 'del '(read))))))))))
9763             (set-buffer gnus-group-buffer)
9764             (gnus-undo-register
9765               `(progn
9766                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9767                  (gnus-info-set-read ',info ',(gnus-info-read info))
9768                  (gnus-get-unread-articles-in-group ',info 
9769                                                     (gnus-active ,group))
9770                  (gnus-group-update-group ,group t)
9771                  ,setmarkundo))))
9772         ;; Enter this list into the group info.
9773         (gnus-info-set-read info read)
9774         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9775         (gnus-get-unread-articles-in-group info (gnus-active group))
9776         t))))
9777
9778 (defun gnus-offer-save-summaries ()
9779   "Offer to save all active summary buffers."
9780   (save-excursion
9781     (let ((buflist (buffer-list))
9782           buffers bufname)
9783       ;; Go through all buffers and find all summaries.
9784       (while buflist
9785         (and (setq bufname (buffer-name (car buflist)))
9786              (string-match "Summary" bufname)
9787              (save-excursion
9788                (set-buffer bufname)
9789                ;; We check that this is, indeed, a summary buffer.
9790                (and (eq major-mode 'gnus-summary-mode)
9791                     ;; Also make sure this isn't bogus.
9792                     gnus-newsgroup-prepared
9793                     ;; Also make sure that this isn't a dead summary buffer.
9794                     (not gnus-dead-summary-mode)))
9795              (push bufname buffers))
9796         (setq buflist (cdr buflist)))
9797       ;; Go through all these summary buffers and offer to save them.
9798       (when buffers
9799         (map-y-or-n-p
9800          "Update summary buffer %s? "
9801          (lambda (buf)
9802            (switch-to-buffer buf)
9803            (gnus-summary-exit))
9804          buffers)))))
9805
9806 (defun gnus-summary-setup-default-charset ()
9807   "Setup newsgroup default charset."
9808   (if (equal gnus-newsgroup-name "nndraft:drafts")
9809       (setq gnus-newsgroup-charset nil)
9810     (let* ((name (and gnus-newsgroup-name
9811                       (gnus-group-real-name gnus-newsgroup-name)))
9812            (ignored-charsets
9813             (or gnus-newsgroup-ephemeral-ignored-charsets
9814                 (append
9815                  (and gnus-newsgroup-name
9816                       (or (gnus-group-find-parameter gnus-newsgroup-name
9817                                                      'ignored-charsets t)
9818                           (let ((alist gnus-group-ignored-charsets-alist)
9819                                 elem (charsets nil))
9820                             (while (setq elem (pop alist))
9821                               (when (and name
9822                                          (string-match (car elem) name))
9823                                 (setq alist nil
9824                                       charsets (cdr elem))))
9825                             charsets)))
9826                  gnus-newsgroup-ignored-charsets))))
9827       (setq gnus-newsgroup-charset
9828             (or gnus-newsgroup-ephemeral-charset
9829                 (and gnus-newsgroup-name
9830                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9831                          (let ((alist gnus-group-charset-alist)
9832                                elem charset)
9833                            (while (setq elem (pop alist))
9834                              (when (and name
9835                                         (string-match (car elem) name))
9836                                (setq alist nil
9837                                      charset (cadr elem))))
9838                            charset)))
9839                 gnus-default-charset))
9840       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9841            ignored-charsets))))
9842
9843 ;;;
9844 ;;; Mime Commands
9845 ;;;
9846
9847 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9848   "Display the current article buffer fully MIME-buttonized.
9849 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9850 treated as multipart/mixed."
9851   (interactive "P")
9852   (require 'gnus-art)
9853   (let ((gnus-unbuttonized-mime-types nil)
9854         (gnus-mime-display-multipart-as-mixed show-all-parts))
9855     (gnus-summary-show-article)))
9856
9857 (defun gnus-summary-repair-multipart (article)
9858   "Add a Content-Type header to a multipart article without one."
9859   (interactive (list (gnus-summary-article-number)))
9860   (gnus-with-article article
9861     (message-narrow-to-head)
9862     (message-remove-header "Mime-Version")
9863     (goto-char (point-max))
9864     (insert "Mime-Version: 1.0\n")
9865     (widen)
9866     (when (search-forward "\n--" nil t)
9867       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9868         (message-narrow-to-head)
9869         (message-remove-header "Content-Type")
9870         (goto-char (point-max))
9871         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9872                         separator))
9873         (widen))))
9874   (let (gnus-mark-article-hook)
9875     (gnus-summary-select-article t t nil article)))
9876
9877 (defun gnus-summary-toggle-display-buttonized ()
9878   "Toggle the buttonizing of the article buffer."
9879   (interactive)
9880   (require 'gnus-art)
9881   (if (setq gnus-inhibit-mime-unbuttonizing
9882             (not gnus-inhibit-mime-unbuttonizing))
9883       (let ((gnus-unbuttonized-mime-types nil))
9884         (gnus-summary-show-article))
9885     (gnus-summary-show-article)))
9886
9887 ;;;
9888 ;;; Generic summary marking commands
9889 ;;;
9890
9891 (defvar gnus-summary-marking-alist
9892   '((read gnus-del-mark "d")
9893     (unread gnus-unread-mark "u")
9894     (ticked gnus-ticked-mark "!")
9895     (dormant gnus-dormant-mark "?")
9896     (expirable gnus-expirable-mark "e"))
9897   "An alist of names/marks/keystrokes.")
9898
9899 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9900 (defvar gnus-summary-mark-map)
9901
9902 (defun gnus-summary-make-all-marking-commands ()
9903   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9904   (dolist (elem gnus-summary-marking-alist)
9905     (apply 'gnus-summary-make-marking-command elem)))
9906
9907 (defun gnus-summary-make-marking-command (name mark keystroke)
9908   (let ((map (make-sparse-keymap)))
9909     (define-key gnus-summary-generic-mark-map keystroke map)
9910     (dolist (lway `((next "next" next nil "n")
9911                     (next-unread "next unread" next t "N")
9912                     (prev "previous" prev nil "p")
9913                     (prev-unread "previous unread" prev t "P")
9914                     (nomove "" nil nil ,keystroke)))
9915       (let ((func (gnus-summary-make-marking-command-1
9916                    mark (car lway) lway name)))
9917         (setq func (eval func))
9918         (define-key map (nth 4 lway) func)))))
9919       
9920 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9921   `(defun ,(intern
9922             (format "gnus-summary-put-mark-as-%s%s"
9923                     name (if (eq way 'nomove)
9924                              ""
9925                            (concat "-" (symbol-name way)))))
9926      (n)
9927      ,(format
9928        "Mark the current article as %s%s.
9929 If N, the prefix, then repeat N times.
9930 If N is negative, move in reverse order.
9931 The difference between N and the actual number of articles marked is
9932 returned."
9933        name (cadr lway))
9934      (interactive "p")
9935      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9936     
9937 (defun gnus-summary-generic-mark (n mark move unread)
9938   "Mark N articles with MARK."
9939   (unless (eq major-mode 'gnus-summary-mode)
9940     (error "This command can only be used in the summary buffer"))
9941   (gnus-summary-show-thread)
9942   (let ((nummove
9943          (cond
9944           ((eq move 'next) 1)
9945           ((eq move 'prev) -1)
9946           (t 0))))
9947     (if (zerop nummove)
9948         (setq n 1)
9949       (when (< n 0)
9950         (setq n (abs n)
9951               nummove (* -1 nummove))))
9952     (while (and (> n 0)
9953                 (gnus-summary-mark-article nil mark)
9954                 (zerop (gnus-summary-next-subject nummove unread t)))
9955       (setq n (1- n)))
9956     (when (/= 0 n)
9957       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9958     (gnus-summary-recenter)
9959     (gnus-summary-position-point)
9960     (gnus-set-mode-line 'summary)
9961     n))
9962
9963 (gnus-summary-make-all-marking-commands)
9964
9965 (gnus-ems-redefine)
9966
9967 (provide 'gnus-sum)
9968
9969 (run-hooks 'gnus-sum-load-hook)
9970
9971 ;;; gnus-sum.el ends here