2000-11-01 07:39:24 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40 (autoload 'gnus-cache-write-active "gnus-cache")
41
42 (defcustom gnus-kill-summary-on-exit t
43   "*If non-nil, kill the summary buffer when you exit from it.
44 If nil, the summary will become a \"*Dead Summary*\" buffer, and
45 it will be killed sometime later."
46   :group 'gnus-summary-exit
47   :type 'boolean)
48
49 (defcustom gnus-fetch-old-headers nil
50   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
51 If an unread article in the group refers to an older, already read (or
52 just marked as read) article, the old article will not normally be
53 displayed in the Summary buffer.  If this variable is non-nil, Gnus
54 will attempt to grab the headers to the old articles, and thereby
55 build complete threads.  If it has the value `some', only enough
56 headers to connect otherwise loose threads will be displayed.  This
57 variable can also be a number.  In that case, no more than that number
58 of old headers will be fetched.  If it has the value `invisible', all
59 old headers will be fetched, but none will be displayed.
60
61 The server has to support NOV for any of this to work."
62   :group 'gnus-thread
63   :type '(choice (const :tag "off" nil)
64                  (const some)
65                  number
66                  (sexp :menu-tag "other" t)))
67
68 (defcustom gnus-refer-thread-limit 200
69   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
70 If t, fetch all the available old headers."
71   :group 'gnus-thread
72   :type '(choice number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-summary-make-false-root 'adopt
76   "*nil means that Gnus won't gather loose threads.
77 If the root of a thread has expired or been read in a previous
78 session, the information necessary to build a complete thread has been
79 lost.  Instead of having many small sub-threads from this original thread
80 scattered all over the summary buffer, Gnus can gather them.
81
82 If non-nil, Gnus will try to gather all loose sub-threads from an
83 original thread into one large thread.
84
85 If this variable is non-nil, it should be one of `none', `adopt',
86 `dummy' or `empty'.
87
88 If this variable is `none', Gnus will not make a false root, but just
89 present the sub-threads after another.
90 If this variable is `dummy', Gnus will create a dummy root that will
91 have all the sub-threads as children.
92 If this variable is `adopt', Gnus will make one of the \"children\"
93 the parent and mark all the step-children as such.
94 If this variable is `empty', the \"children\" are printed with empty
95 subject fields.  (Or rather, they will be printed with a string
96 given by the `gnus-summary-same-subject' variable.)"
97   :group 'gnus-thread
98   :type '(choice (const :tag "off" nil)
99                  (const none)
100                  (const dummy)
101                  (const adopt)
102                  (const empty)))
103
104 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
105   "*A regexp to match subjects to be excluded from loose thread gathering.
106 As loose thread gathering is done on subjects only, that means that
107 there can be many false gatherings performed.  By rooting out certain
108 common subjects, gathering might become saner."
109   :group 'gnus-thread
110   :type 'regexp)
111
112 (defcustom gnus-summary-gather-subject-limit nil
113   "*Maximum length of subject comparisons when gathering loose threads.
114 Use nil to compare full subjects.  Setting this variable to a low
115 number will help gather threads that have been corrupted by
116 newsreaders chopping off subject lines, but it might also mean that
117 unrelated articles that have subject that happen to begin with the
118 same few characters will be incorrectly gathered.
119
120 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
121 comparing subjects."
122   :group 'gnus-thread
123   :type '(choice (const :tag "off" nil)
124                  (const fuzzy)
125                  (sexp :menu-tag "on" t)))
126
127 (defcustom gnus-simplify-subject-functions nil
128   "List of functions taking a string argument that simplify subjects.
129 The functions are applied recursively.
130
131 Useful functions to put in this list include: `gnus-simplify-subject-re',
132 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
133   :group 'gnus-thread
134   :type '(repeat function))
135
136 (defcustom gnus-simplify-ignored-prefixes nil
137   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  regexp))
141
142 (defcustom gnus-build-sparse-threads nil
143   "*If non-nil, fill in the gaps in threads.
144 If `some', only fill in the gaps that are needed to tie loose threads
145 together.  If `more', fill in all leaf nodes that Gnus can find.  If
146 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  (const some)
150                  (const more)
151                  (sexp :menu-tag "all" t)))
152
153 (defcustom gnus-summary-thread-gathering-function
154   'gnus-gather-threads-by-subject
155   "*Function used for gathering loose threads.
156 There are two pre-defined functions: `gnus-gather-threads-by-subject',
157 which only takes Subjects into consideration; and
158 `gnus-gather-threads-by-references', which compared the References
159 headers of the articles to find matches."
160   :group 'gnus-thread
161   :type '(radio (function-item gnus-gather-threads-by-subject)
162                 (function-item gnus-gather-threads-by-references)
163                 (function :tag "other")))
164
165 (defcustom gnus-summary-same-subject ""
166   "*String indicating that the current article has the same subject as the previous.
167 This variable will only be used if the value of
168 `gnus-summary-make-false-root' is `empty'."
169   :group 'gnus-summary-format
170   :type 'string)
171
172 (defcustom gnus-summary-goto-unread t
173   "*If t, many commands will go to the next unread article.
174 This applies to marking commands as well as other commands that
175 \"naturally\" select the next article, like, for instance, `SPC' at
176 the end of an article.
177
178 If nil, the marking commands do NOT go to the next unread article
179 (they go to the next article instead).  If `never', commands that
180 usually go to the next unread article, will go to the next article,
181 whether it is read or not."
182   :group 'gnus-summary-marks
183   :link '(custom-manual "(gnus)Setting Marks")
184   :type '(choice (const :tag "off" nil)
185                  (const never)
186                  (sexp :menu-tag "on" t)))
187
188 (defcustom gnus-summary-default-score 0
189   "*Default article score level.
190 All scores generated by the score files will be added to this score.
191 If this variable is nil, scoring will be disabled."
192   :group 'gnus-score-default
193   :type '(choice (const :tag "disable")
194                  integer))
195
196 (defcustom gnus-summary-zcore-fuzz 0
197   "*Fuzziness factor for the zcore in the summary buffer.
198 Articles with scores closer than this to `gnus-summary-default-score'
199 will not be marked."
200   :group 'gnus-summary-format
201   :type 'integer)
202
203 (defcustom gnus-simplify-subject-fuzzy-regexp nil
204   "*Strings to be removed when doing fuzzy matches.
205 This can either be a regular expression or list of regular expressions
206 that will be removed from subject strings if fuzzy subject
207 simplification is selected."
208   :group 'gnus-thread
209   :type '(repeat regexp))
210
211 (defcustom gnus-show-threads t
212   "*If non-nil, display threads in summary mode."
213   :group 'gnus-thread
214   :type 'boolean)
215
216 (defcustom gnus-thread-hide-subtree nil
217   "*If non-nil, hide all threads initially.
218 If threads are hidden, you have to run the command
219 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
220 to expose hidden threads."
221   :group 'gnus-thread
222   :type 'boolean)
223
224 (defcustom gnus-thread-hide-killed t
225   "*If non-nil, hide killed threads automatically."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-ignore-subject t
230   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
231 If nil, articles that have different subjects from their parents will
232 start separate threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-operation-ignore-subject t
237   "*If non-nil, subjects will be ignored when doing thread commands.
238 This affects commands like `gnus-summary-kill-thread' and
239 `gnus-summary-lower-thread'.
240
241 If this variable is nil, articles in the same thread with different
242 subjects will not be included in the operation in question.  If this
243 variable is `fuzzy', only articles that have subjects that are fuzzily
244 equal will be included."
245   :group 'gnus-thread
246   :type '(choice (const :tag "off" nil)
247                  (const fuzzy)
248                  (sexp :tag "on" t)))
249
250 (defcustom gnus-thread-indent-level 4
251   "*Number that says how much each sub-thread should be indented."
252   :group 'gnus-thread
253   :type 'integer)
254
255 (defcustom gnus-auto-extend-newsgroup t
256   "*If non-nil, extend newsgroup forward and backward when requested."
257   :group 'gnus-summary-choose
258   :type 'boolean)
259
260 (defcustom gnus-auto-select-first t
261   "*If nil, don't select the first unread article when entering a group.
262 If this variable is `best', select the highest-scored unread article
263 in the group.  If t, select the first unread article.
264
265 This variable can also be a function to place point on a likely
266 subject line.  Useful values include `gnus-summary-first-unread-subject',
267 `gnus-summary-first-unread-article' and
268 `gnus-summary-best-unread-article'.
269
270 If you want to prevent automatic selection of the first unread article
271 in some newsgroups, set the variable to nil in
272 `gnus-select-group-hook'."
273   :group 'gnus-group-select
274   :type '(choice (const :tag "none" nil)
275                  (const best)
276                  (sexp :menu-tag "first" t)
277                  (function-item gnus-summary-first-unread-subject)
278                  (function-item gnus-summary-first-unread-article)
279                  (function-item gnus-summary-best-unread-article)))
280
281 (defcustom gnus-auto-select-next t
282   "*If non-nil, offer to go to the next group from the end of the previous.
283 If the value is t and the next newsgroup is empty, Gnus will exit
284 summary mode and go back to group mode.  If the value is neither nil
285 nor t, Gnus will select the following unread newsgroup.  In
286 particular, if the value is the symbol `quietly', the next unread
287 newsgroup will be selected without any confirmation, and if it is
288 `almost-quietly', the next group will be selected without any
289 confirmation if you are located on the last article in the group.
290 Finally, if this variable is `slightly-quietly', the `Z n' command
291 will go to the next group without confirmation."
292   :group 'gnus-summary-maneuvering
293   :type '(choice (const :tag "off" nil)
294                  (const quietly)
295                  (const almost-quietly)
296                  (const slightly-quietly)
297                  (sexp :menu-tag "on" t)))
298
299 (defcustom gnus-auto-select-same nil
300   "*If non-nil, select the next article with the same subject.
301 If there are no more articles with the same subject, go to
302 the first unread article."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-summary-check-current nil
307   "*If non-nil, consider the current article when moving.
308 The \"unread\" movement commands will stay on the same line if the
309 current article is unread."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-auto-center-summary t
314   "*If non-nil, always center the current summary buffer.
315 In particular, if `vertical' do only vertical recentering.  If non-nil
316 and non-`vertical', do both horizontal and vertical recentering."
317   :group 'gnus-summary-maneuvering
318   :type '(choice (const :tag "none" nil)
319                  (const vertical)
320                  (integer :tag "height")
321                  (sexp :menu-tag "both" t)))
322
323 (defcustom gnus-show-all-headers nil
324   "*If non-nil, don't hide any headers."
325   :group 'gnus-article-hiding
326   :group 'gnus-article-headers
327   :type 'boolean)
328
329 (defcustom gnus-summary-ignore-duplicates nil
330   "*If non-nil, ignore articles with identical Message-ID headers."
331   :group 'gnus-summary
332   :type 'boolean)
333
334 (defcustom gnus-single-article-buffer t
335   "*If non-nil, display all articles in the same buffer.
336 If nil, each group will get its own article buffer."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-break-pages t
341   "*If non-nil, do page breaking on articles.
342 The page delimiter is specified by the `gnus-page-delimiter'
343 variable."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-move-split-methods nil
348   "*Variable used to suggest where articles are to be moved to.
349 It uses the same syntax as the `gnus-split-methods' variable."
350   :group 'gnus-summary-mail
351   :type '(repeat (choice (list :value (fun) function)
352                          (cons :value ("" "") regexp (repeat string))
353                          (sexp :value nil))))
354
355 (defcustom gnus-unread-mark ?  ;Whitespace
356   "*Mark used for unread articles."
357   :group 'gnus-summary-marks
358   :type 'character)
359
360 (defcustom gnus-ticked-mark ?!
361   "*Mark used for ticked articles."
362   :group 'gnus-summary-marks
363   :type 'character)
364
365 (defcustom gnus-dormant-mark ??
366   "*Mark used for dormant articles."
367   :group 'gnus-summary-marks
368   :type 'character)
369
370 (defcustom gnus-del-mark ?r
371   "*Mark used for del'd articles."
372   :group 'gnus-summary-marks
373   :type 'character)
374
375 (defcustom gnus-read-mark ?R
376   "*Mark used for read articles."
377   :group 'gnus-summary-marks
378   :type 'character)
379
380 (defcustom gnus-expirable-mark ?E
381   "*Mark used for expirable articles."
382   :group 'gnus-summary-marks
383   :type 'character)
384
385 (defcustom gnus-killed-mark ?K
386   "*Mark used for killed articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-souped-mark ?F
391   "*Mark used for killed articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-kill-file-mark ?X
396   "*Mark used for articles killed by kill files."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-low-score-mark ?Y
401   "*Mark used for articles with a low score."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-catchup-mark ?C
406   "*Mark used for articles that are caught up."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-replied-mark ?A
411   "*Mark used for articles that have been replied to."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-cached-mark ?*
416   "*Mark used for articles that are in the cache."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-saved-mark ?S
421   "*Mark used for articles that have been saved to."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-ancient-mark ?O
426   "*Mark used for ancient articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-sparse-mark ?Q
431   "*Mark used for sparsely reffed articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-canceled-mark ?G
436   "*Mark used for canceled articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-duplicate-mark ?M
441   "*Mark used for duplicate articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-undownloaded-mark ?@
446   "*Mark used for articles that weren't downloaded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-downloadable-mark ?%
451   "*Mark used for articles that are to be downloaded."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-unsendable-mark ?=
456   "*Mark used for articles that won't be sent."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-score-over-mark ?+
461   "*Score mark used for articles with high scores."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-score-below-mark ?-
466   "*Score mark used for articles with low scores."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-empty-thread-mark ?  ;Whitespace
471   "*There is no thread under the article."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-not-empty-thread-mark ?=
476   "*There is a thread under the article."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-view-pseudo-asynchronously nil
481   "*If non-nil, Gnus will view pseudo-articles asynchronously."
482   :group 'gnus-extract-view
483   :type 'boolean)
484
485 (defcustom gnus-auto-expirable-marks
486   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
487         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
488         gnus-souped-mark gnus-duplicate-mark)
489   "*The list of marks converted into expiration if a group is auto-expirable."
490   :group 'gnus-summary
491   :type '(repeat character))
492
493 (defcustom gnus-inhibit-user-auto-expire t
494   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
495   :group 'gnus-summary
496   :type 'boolean)
497
498 (defcustom gnus-view-pseudos nil
499   "*If `automatic', pseudo-articles will be viewed automatically.
500 If `not-confirm', pseudos will be viewed automatically, and the user
501 will not be asked to confirm the command."
502   :group 'gnus-extract-view
503   :type '(choice (const :tag "off" nil)
504                  (const automatic)
505                  (const not-confirm)))
506
507 (defcustom gnus-view-pseudos-separately t
508   "*If non-nil, one pseudo-article will be created for each file to be viewed.
509 If nil, all files that use the same viewing command will be given as a
510 list of parameters to that command."
511   :group 'gnus-extract-view
512   :type 'boolean)
513
514 (defcustom gnus-insert-pseudo-articles t
515   "*If non-nil, insert pseudo-articles when decoding articles."
516   :group 'gnus-extract-view
517   :type 'boolean)
518
519 (defcustom gnus-summary-dummy-line-format
520   "  %(:                          :%) %S\n"
521   "*The format specification for the dummy roots in the summary buffer.
522 It works along the same lines as a normal formatting string,
523 with some simple extensions.
524
525 %S  The subject"
526   :group 'gnus-threading
527   :type 'string)
528
529 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
530   "*The format specification for the summary mode line.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions:
533
534 %G  Group name
535 %p  Unprefixed group name
536 %A  Current article number
537 %z  Current article score
538 %V  Gnus version
539 %U  Number of unread articles in the group
540 %e  Number of unselected articles in the group
541 %Z  A string with unread/unselected article counts
542 %g  Shortish group name
543 %S  Subject of the current article
544 %u  User-defined spec
545 %s  Current score file name
546 %d  Number of dormant articles
547 %r  Number of articles that have been marked as read in this session
548 %E  Number of articles expunged by the score files"
549   :group 'gnus-summary-format
550   :type 'string)
551
552 (defcustom gnus-list-identifiers nil
553   "Regexp that matches list identifiers to be removed from subject.
554 This can also be a list of regexps."
555   :group 'gnus-summary-format
556   :group 'gnus-article-hiding
557   :type '(choice (const :tag "none" nil)
558                  (regexp :value ".*")
559                  (repeat :value (".*") regexp)))
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-threading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook nil
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting summary mode.
704 This hook is not called from the non-updating exit commands like `Q'."
705   :group 'gnus-various
706   :type 'hook)
707
708 (defcustom gnus-summary-update-hook
709   (list 'gnus-summary-highlight-line)
710   "*A hook called when a summary line is changed.
711 The hook will not be called if `gnus-visual' is nil.
712
713 The default function `gnus-summary-highlight-line' will
714 highlight the line according to the `gnus-summary-highlight'
715 variable."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
720   "*A hook called when an article is selected for the first time.
721 The hook is intended to mark an article as read (or unread)
722 automatically when it is selected."
723   :group 'gnus-summary-choose
724   :type 'hook)
725
726 (defcustom gnus-group-no-more-groups-hook nil
727   "*A hook run when returning to group mode having no more (unread) groups."
728   :group 'gnus-group-select
729   :type 'hook)
730
731 (defcustom gnus-ps-print-hook nil
732   "*A hook run before ps-printing something from Gnus."
733   :group 'gnus-summary
734   :type 'hook)
735
736 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
737   "Face used for highlighting the current article in the summary buffer."
738   :group 'gnus-summary-visual
739   :type 'face)
740
741 (defcustom gnus-summary-highlight
742   '(((= mark gnus-canceled-mark)
743      . gnus-summary-cancelled-face)
744     ((and (> score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-high-ticked-face)
748     ((and (< score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-low-ticked-face)
752     ((or (= mark gnus-dormant-mark)
753          (= mark gnus-ticked-mark))
754      . gnus-summary-normal-ticked-face)
755     ((and (> score default) (= mark gnus-ancient-mark))
756      . gnus-summary-high-ancient-face)
757     ((and (< score default) (= mark gnus-ancient-mark))
758      . gnus-summary-low-ancient-face)
759     ((= mark gnus-ancient-mark)
760      . gnus-summary-normal-ancient-face)
761     ((and (> score default) (= mark gnus-unread-mark))
762      . gnus-summary-high-unread-face)
763     ((and (< score default) (= mark gnus-unread-mark))
764      . gnus-summary-low-unread-face)
765     ((= mark gnus-unread-mark)
766      . gnus-summary-normal-unread-face)
767     ((and (> score default) (memq mark (list gnus-downloadable-mark
768                                              gnus-undownloaded-mark)))
769      . gnus-summary-high-unread-face)
770     ((and (< score default) (memq mark (list gnus-downloadable-mark
771                                              gnus-undownloaded-mark)))
772      . gnus-summary-low-unread-face)
773     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
774      . gnus-summary-normal-unread-face)
775     ((> score default)
776      . gnus-summary-high-read-face)
777     ((< score default)
778      . gnus-summary-low-read-face)
779     (t
780      . gnus-summary-normal-read-face))
781   "*Controls the highlighting of summary buffer lines.
782
783 A list of (FORM . FACE) pairs.  When deciding how a a particular
784 summary line should be displayed, each form is evaluated.  The content
785 of the face field after the first true form is used.  You can change
786 how those summary lines are displayed, by editing the face field.
787
788 You can use the following variables in the FORM field.
789
790 score:   The articles score
791 default: The default article score.
792 below:   The score below which articles are automatically marked as read.
793 mark:    The articles mark."
794   :group 'gnus-summary-visual
795   :type '(repeat (cons (sexp :tag "Form" nil)
796                        face)))
797
798 (defcustom gnus-alter-header-function nil
799   "Function called to allow alteration of article header structures.
800 The function is called with one parameter, the article header vector,
801 which it may alter in any way.")
802
803 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
804   "Variable that says which function should be used to decode a string with encoded words.")
805
806 (defcustom gnus-extra-headers nil
807   "*Extra headers to parse."
808   :group 'gnus-summary
809   :type '(repeat symbol))
810
811 (defcustom gnus-ignored-from-addresses
812   (and user-mail-address (regexp-quote user-mail-address))
813   "*Regexp of From headers that may be suppressed in favor of To headers."
814   :group 'gnus-summary
815   :type 'regexp)
816
817 (defcustom gnus-group-charset-alist
818   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
819     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
820     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
821     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
822     ("^relcom\\>" koi8-r)
823     ("^fido7\\>" koi8-r)
824     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
825     ("^israel\\>" iso-8859-1)
826     ("^han\\>" euc-kr)
827     ("^alt.chinese.text.big5\\>" chinese-big5)
828     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
829     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
830     (".*" iso-8859-1))
831   "Alist of regexps (to match group names) and default charsets to be used when reading."
832   :type '(repeat (list (regexp :tag "Group")
833                        (symbol :tag "Charset")))
834   :group 'gnus-charset)
835
836 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
837   "List of charsets that should be ignored.
838 When these charsets are used in the \"charset\" parameter, the
839 default charset will be used instead."
840   :type '(repeat symbol)
841   :group 'gnus-charset)
842
843 (defcustom gnus-group-ignored-charsets-alist
844   '(("alt\\.chinese\\.text" iso-8859-1))
845   "Alist of regexps (to match group names) and charsets that should be ignored.
846 When these charsets are used in the \"charset\" parameter, the
847 default charset will be used instead."
848   :type '(repeat (cons (regexp :tag "Group")
849                        (repeat symbol)))
850   :group 'gnus-charset)
851
852 (defcustom gnus-group-highlight-words-alist nil
853   "Alist of group regexps and highlight regexps.
854 This variable uses the same syntax as `gnus-emphasis-alist'."
855   :type '(repeat (cons (regexp :tag "Group")
856                        (repeat (list (regexp :tag "Highlight regexp")
857                                      (number :tag "Group for entire word" 0)
858                                      (number :tag "Group for displayed part" 0)
859                                      (symbol :tag "Face"
860                                              gnus-emphasis-highlight-words)))))
861   :group 'gnus-summary-visual)
862
863 (defcustom gnus-summary-show-article-charset-alist
864   nil
865   "Alist of number and charset.
866 The article will be shown with the charset corresponding to the
867 numbered argument.
868 For example: ((1 . cn-gb-2312) (2 . big5))."
869   :type '(repeat (cons (number :tag "Argument" 1)
870                        (symbol :tag "Charset")))
871   :group 'gnus-charset)
872
873 (defcustom gnus-preserve-marks t
874   "Whether marks are preserved when moving, copying and respooling messages."
875   :type 'boolean
876   :group 'gnus-summary-marks)
877
878 (defcustom gnus-alter-articles-to-read-function nil
879   "Function to be called to alter the list of articles to be selected."
880   :type 'function
881   :group 'gnus-summary)
882
883 (defcustom gnus-orphan-score nil
884   "*All orphans get this score added.  Set in the score file."
885   :group 'gnus-score-default
886   :type '(choice (const nil)
887                  integer))
888
889 (defcustom gnus-summary-save-parts-default-mime "image/.*"
890   "*A regexp to match MIME parts when saving multiple parts of a message
891 with gnus-summary-save-parts (X m). This regexp will be used by default
892 when prompting the user for which type of files to save."
893   :group 'gnus-summary
894   :type 'regexp)
895
896
897 ;;; Internal variables
898
899 (defvar gnus-article-mime-handles nil)
900 (defvar gnus-article-decoded-p nil)
901 (defvar gnus-article-charset nil)
902 (defvar gnus-article-ignored-charsets nil)
903 (defvar gnus-scores-exclude-files nil)
904 (defvar gnus-page-broken nil)
905 (defvar gnus-inhibit-mime-unbuttonizing nil)
906
907 (defvar gnus-original-article nil)
908 (defvar gnus-article-internal-prepare-hook nil)
909 (defvar gnus-newsgroup-process-stack nil)
910
911 (defvar gnus-thread-indent-array nil)
912 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
913 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
914   "Function called to sort the articles within a thread after it has been gathered together.")
915
916 (defvar gnus-summary-save-parts-type-history nil)
917 (defvar gnus-summary-save-parts-last-directory nil)
918
919 ;; Avoid highlighting in kill files.
920 (defvar gnus-summary-inhibit-highlight nil)
921 (defvar gnus-newsgroup-selected-overlay nil)
922 (defvar gnus-inhibit-limiting nil)
923 (defvar gnus-newsgroup-adaptive-score-file nil)
924 (defvar gnus-current-score-file nil)
925 (defvar gnus-current-move-group nil)
926 (defvar gnus-current-copy-group nil)
927 (defvar gnus-current-crosspost-group nil)
928
929 (defvar gnus-newsgroup-dependencies nil)
930 (defvar gnus-newsgroup-adaptive nil)
931 (defvar gnus-summary-display-article-function nil)
932 (defvar gnus-summary-highlight-line-function nil
933   "Function called after highlighting a summary line.")
934
935 (defvar gnus-summary-line-format-alist
936   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
937     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
938     (?s gnus-tmp-subject-or-nil ?s)
939     (?n gnus-tmp-name ?s)
940     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
941         ?s)
942     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
943             gnus-tmp-from) ?s)
944     (?F gnus-tmp-from ?s)
945     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
946     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
947     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
948     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
949     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
950     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
951     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
952     (?L gnus-tmp-lines ?d)
953     (?I gnus-tmp-indentation ?s)
954     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
955     (?R gnus-tmp-replied ?c)
956     (?\[ gnus-tmp-opening-bracket ?c)
957     (?\] gnus-tmp-closing-bracket ?c)
958     (?\> (make-string gnus-tmp-level ? ) ?s)
959     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
960     (?i gnus-tmp-score ?d)
961     (?z gnus-tmp-score-char ?c)
962     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
963     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
964     (?U gnus-tmp-unread ?c)
965     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
966     (?t (gnus-summary-number-of-articles-in-thread
967          (and (boundp 'thread) (car thread)) gnus-tmp-level)
968         ?d)
969     (?e (gnus-summary-number-of-articles-in-thread
970          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
971         ?c)
972     (?u gnus-tmp-user-defined ?s)
973     (?P (gnus-pick-line-number) ?d))
974   "An alist of format specifications that can appear in summary lines.
975 These are paired with what variables they correspond with, along with
976 the type of the variable (string, integer, character, etc).")
977
978 (defvar gnus-summary-dummy-line-format-alist
979   `((?S gnus-tmp-subject ?s)
980     (?N gnus-tmp-number ?d)
981     (?u gnus-tmp-user-defined ?s)))
982
983 (defvar gnus-summary-mode-line-format-alist
984   `((?G gnus-tmp-group-name ?s)
985     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
986     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
987     (?A gnus-tmp-article-number ?d)
988     (?Z gnus-tmp-unread-and-unselected ?s)
989     (?V gnus-version ?s)
990     (?U gnus-tmp-unread-and-unticked ?d)
991     (?S gnus-tmp-subject ?s)
992     (?e gnus-tmp-unselected ?d)
993     (?u gnus-tmp-user-defined ?s)
994     (?d (length gnus-newsgroup-dormant) ?d)
995     (?t (length gnus-newsgroup-marked) ?d)
996     (?r (length gnus-newsgroup-reads) ?d)
997     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
998     (?E gnus-newsgroup-expunged-tally ?d)
999     (?s (gnus-current-score-file-nondirectory) ?s)))
1000
1001 (defvar gnus-last-search-regexp nil
1002   "Default regexp for article search command.")
1003
1004 (defvar gnus-last-shell-command nil
1005   "Default shell command on article.")
1006
1007 (defvar gnus-newsgroup-begin nil)
1008 (defvar gnus-newsgroup-end nil)
1009 (defvar gnus-newsgroup-last-rmail nil)
1010 (defvar gnus-newsgroup-last-mail nil)
1011 (defvar gnus-newsgroup-last-folder nil)
1012 (defvar gnus-newsgroup-last-file nil)
1013 (defvar gnus-newsgroup-auto-expire nil)
1014 (defvar gnus-newsgroup-active nil)
1015
1016 (defvar gnus-newsgroup-data nil)
1017 (defvar gnus-newsgroup-data-reverse nil)
1018 (defvar gnus-newsgroup-limit nil)
1019 (defvar gnus-newsgroup-limits nil)
1020
1021 (defvar gnus-newsgroup-unreads nil
1022   "List of unread articles in the current newsgroup.")
1023
1024 (defvar gnus-newsgroup-unselected nil
1025   "List of unselected unread articles in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-reads nil
1028   "Alist of read articles and article marks in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-expunged-tally nil)
1031
1032 (defvar gnus-newsgroup-marked nil
1033   "List of ticked articles in the current newsgroup (a subset of unread art).")
1034
1035 (defvar gnus-newsgroup-killed nil
1036   "List of ranges of articles that have been through the scoring process.")
1037
1038 (defvar gnus-newsgroup-cached nil
1039   "List of articles that come from the article cache.")
1040
1041 (defvar gnus-newsgroup-saved nil
1042   "List of articles that have been saved.")
1043
1044 (defvar gnus-newsgroup-kill-headers nil)
1045
1046 (defvar gnus-newsgroup-replied nil
1047   "List of articles that have been replied to in the current newsgroup.")
1048
1049 (defvar gnus-newsgroup-expirable nil
1050   "List of articles in the current newsgroup that can be expired.")
1051
1052 (defvar gnus-newsgroup-processable nil
1053   "List of articles in the current newsgroup that can be processed.")
1054
1055 (defvar gnus-newsgroup-downloadable nil
1056   "List of articles in the current newsgroup that can be processed.")
1057
1058 (defvar gnus-newsgroup-undownloaded nil
1059   "List of articles in the current newsgroup that haven't been downloaded..")
1060
1061 (defvar gnus-newsgroup-unsendable nil
1062   "List of articles in the current newsgroup that won't be sent.")
1063
1064 (defvar gnus-newsgroup-bookmarks nil
1065   "List of articles in the current newsgroup that have bookmarks.")
1066
1067 (defvar gnus-newsgroup-dormant nil
1068   "List of dormant articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-scored nil
1071   "List of scored articles in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-headers nil
1074   "List of article headers in the current newsgroup.")
1075
1076 (defvar gnus-newsgroup-threads nil)
1077
1078 (defvar gnus-newsgroup-prepared nil
1079   "Whether the current group has been prepared properly.")
1080
1081 (defvar gnus-newsgroup-ancient nil
1082   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-sparse nil)
1085
1086 (defvar gnus-current-article nil)
1087 (defvar gnus-article-current nil)
1088 (defvar gnus-current-headers nil)
1089 (defvar gnus-have-all-headers nil)
1090 (defvar gnus-last-article nil)
1091 (defvar gnus-newsgroup-history nil)
1092 (defvar gnus-newsgroup-charset nil)
1093 (defvar gnus-newsgroup-ephemeral-charset nil)
1094 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1095
1096 (defconst gnus-summary-local-variables
1097   '(gnus-newsgroup-name
1098     gnus-newsgroup-begin gnus-newsgroup-end
1099     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1100     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1101     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1102     gnus-newsgroup-unselected gnus-newsgroup-marked
1103     gnus-newsgroup-reads gnus-newsgroup-saved
1104     gnus-newsgroup-replied gnus-newsgroup-expirable
1105     gnus-newsgroup-processable gnus-newsgroup-killed
1106     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1107     gnus-newsgroup-unsendable
1108     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1109     gnus-newsgroup-headers gnus-newsgroup-threads
1110     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1111     gnus-current-article gnus-current-headers gnus-have-all-headers
1112     gnus-last-article gnus-article-internal-prepare-hook
1113     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1114     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1115     gnus-thread-expunge-below
1116     gnus-score-alist gnus-current-score-file
1117     (gnus-summary-expunge-below . global)
1118     (gnus-summary-mark-below . global)
1119     (gnus-orphan-score . global)
1120     gnus-newsgroup-active gnus-scores-exclude-files
1121     gnus-newsgroup-history gnus-newsgroup-ancient
1122     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1123     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1124     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1125     (gnus-newsgroup-expunged-tally . 0)
1126     gnus-cache-removable-articles gnus-newsgroup-cached
1127     gnus-newsgroup-data gnus-newsgroup-data-reverse
1128     gnus-newsgroup-limit gnus-newsgroup-limits
1129     gnus-newsgroup-charset)
1130   "Variables that are buffer-local to the summary buffers.")
1131
1132 ;; Byte-compiler warning.
1133 (defvar gnus-article-mode-map)
1134
1135 ;; MIME stuff.
1136
1137 (defvar gnus-decode-encoded-word-methods
1138   '(mail-decode-encoded-word-string)
1139   "List of methods used to decode encoded words.
1140
1141 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1142 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1143 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1144 whose names match REGEXP.
1145
1146 For example:
1147 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1148  mail-decode-encoded-word-string
1149  (\"chinese\" . rfc1843-decode-string))")
1150
1151 (defvar gnus-decode-encoded-word-methods-cache nil)
1152
1153 (defun gnus-multi-decode-encoded-word-string (string)
1154   "Apply the functions from `gnus-encoded-word-methods' that match."
1155   (unless (and gnus-decode-encoded-word-methods-cache
1156                (eq gnus-newsgroup-name
1157                    (car gnus-decode-encoded-word-methods-cache)))
1158     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1159     (mapcar (lambda (x)
1160               (if (symbolp x)
1161                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1162                 (if (and gnus-newsgroup-name
1163                          (string-match (car x) gnus-newsgroup-name))
1164                     (nconc gnus-decode-encoded-word-methods-cache
1165                            (list (cdr x))))))
1166           gnus-decode-encoded-word-methods))
1167   (let ((xlist gnus-decode-encoded-word-methods-cache))
1168     (pop xlist)
1169     (while xlist
1170       (setq string (funcall (pop xlist) string))))
1171   string)
1172
1173 ;; Subject simplification.
1174
1175 (defun gnus-simplify-whitespace (str)
1176   "Remove excessive whitespace from STR."
1177   (let ((mystr str))
1178     ;; Multiple spaces.
1179     (while (string-match "[ \t][ \t]+" mystr)
1180       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1181                           " "
1182                           (substring mystr (match-end 0)))))
1183     ;; Leading spaces.
1184     (when (string-match "^[ \t]+" mystr)
1185       (setq mystr (substring mystr (match-end 0))))
1186     ;; Trailing spaces.
1187     (when (string-match "[ \t]+$" mystr)
1188       (setq mystr (substring mystr 0 (match-beginning 0))))
1189     mystr))
1190
1191 (defsubst gnus-simplify-subject-re (subject)
1192   "Remove \"Re:\" from subject lines."
1193   (if (string-match "^[Rr][Ee]: *" subject)
1194       (substring subject (match-end 0))
1195     subject))
1196
1197 (defun gnus-simplify-subject (subject &optional re-only)
1198   "Remove `Re:' and words in parentheses.
1199 If RE-ONLY is non-nil, strip leading `Re:'s only."
1200   (let ((case-fold-search t))           ;Ignore case.
1201     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1202     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1203       (setq subject (substring subject (match-end 0))))
1204     ;; Remove uninteresting prefixes.
1205     (when (and (not re-only)
1206                gnus-simplify-ignored-prefixes
1207                (string-match gnus-simplify-ignored-prefixes subject))
1208       (setq subject (substring subject (match-end 0))))
1209     ;; Remove words in parentheses from end.
1210     (unless re-only
1211       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1212         (setq subject (substring subject 0 (match-beginning 0)))))
1213     ;; Return subject string.
1214     subject))
1215
1216 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1217 ;; all whitespace.
1218 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1219   (goto-char (point-min))
1220   (while (re-search-forward regexp nil t)
1221     (replace-match (or newtext ""))))
1222
1223 (defun gnus-simplify-buffer-fuzzy ()
1224   "Simplify string in the buffer fuzzily.
1225 The string in the accessible portion of the current buffer is simplified.
1226 It is assumed to be a single-line subject.
1227 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1228 matter is removed.  Additional things can be deleted by setting
1229 `gnus-simplify-subject-fuzzy-regexp'."
1230   (let ((case-fold-search t)
1231         (modified-tick))
1232     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1233
1234     (while (not (eq modified-tick (buffer-modified-tick)))
1235       (setq modified-tick (buffer-modified-tick))
1236       (cond
1237        ((listp gnus-simplify-subject-fuzzy-regexp)
1238         (mapcar 'gnus-simplify-buffer-fuzzy-step
1239                 gnus-simplify-subject-fuzzy-regexp))
1240        (gnus-simplify-subject-fuzzy-regexp
1241         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1242       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1243       (gnus-simplify-buffer-fuzzy-step
1244        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1245       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1246
1247     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1248     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1249     (gnus-simplify-buffer-fuzzy-step " $")
1250     (gnus-simplify-buffer-fuzzy-step "^ +")))
1251
1252 (defun gnus-simplify-subject-fuzzy (subject)
1253   "Simplify a subject string fuzzily.
1254 See `gnus-simplify-buffer-fuzzy' for details."
1255   (save-excursion
1256     (gnus-set-work-buffer)
1257     (let ((case-fold-search t))
1258       ;; Remove uninteresting prefixes.
1259       (when (and gnus-simplify-ignored-prefixes
1260                  (string-match gnus-simplify-ignored-prefixes subject))
1261         (setq subject (substring subject (match-end 0))))
1262       (insert subject)
1263       (inline (gnus-simplify-buffer-fuzzy))
1264       (buffer-string))))
1265
1266 (defsubst gnus-simplify-subject-fully (subject)
1267   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1268   (cond
1269    (gnus-simplify-subject-functions
1270     (gnus-map-function gnus-simplify-subject-functions subject))
1271    ((null gnus-summary-gather-subject-limit)
1272     (gnus-simplify-subject-re subject))
1273    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1274     (gnus-simplify-subject-fuzzy subject))
1275    ((numberp gnus-summary-gather-subject-limit)
1276     (gnus-limit-string (gnus-simplify-subject-re subject)
1277                        gnus-summary-gather-subject-limit))
1278    (t
1279     subject)))
1280
1281 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1282   "Check whether two subjects are equal.
1283 If optional argument simple-first is t, first argument is already
1284 simplified."
1285   (cond
1286    ((null simple-first)
1287     (equal (gnus-simplify-subject-fully s1)
1288            (gnus-simplify-subject-fully s2)))
1289    (t
1290     (equal s1
1291            (gnus-simplify-subject-fully s2)))))
1292
1293 (defun gnus-summary-bubble-group ()
1294   "Increase the score of the current group.
1295 This is a handy function to add to `gnus-summary-exit-hook' to
1296 increase the score of each group you read."
1297   (gnus-group-add-score gnus-newsgroup-name))
1298
1299 \f
1300 ;;;
1301 ;;; Gnus summary mode
1302 ;;;
1303
1304 (put 'gnus-summary-mode 'mode-class 'special)
1305
1306 (when t
1307   ;; Non-orthogonal keys
1308
1309   (gnus-define-keys gnus-summary-mode-map
1310     " " gnus-summary-next-page
1311     "\177" gnus-summary-prev-page
1312     [delete] gnus-summary-prev-page
1313     [backspace] gnus-summary-prev-page
1314     "\r" gnus-summary-scroll-up
1315     "\M-\r" gnus-summary-scroll-down
1316     "n" gnus-summary-next-unread-article
1317     "p" gnus-summary-prev-unread-article
1318     "N" gnus-summary-next-article
1319     "P" gnus-summary-prev-article
1320     "\M-\C-n" gnus-summary-next-same-subject
1321     "\M-\C-p" gnus-summary-prev-same-subject
1322     "\M-n" gnus-summary-next-unread-subject
1323     "\M-p" gnus-summary-prev-unread-subject
1324     "." gnus-summary-first-unread-article
1325     "," gnus-summary-best-unread-article
1326     "\M-s" gnus-summary-search-article-forward
1327     "\M-r" gnus-summary-search-article-backward
1328     "<" gnus-summary-beginning-of-article
1329     ">" gnus-summary-end-of-article
1330     "j" gnus-summary-goto-article
1331     "^" gnus-summary-refer-parent-article
1332     "\M-^" gnus-summary-refer-article
1333     "u" gnus-summary-tick-article-forward
1334     "!" gnus-summary-tick-article-forward
1335     "U" gnus-summary-tick-article-backward
1336     "d" gnus-summary-mark-as-read-forward
1337     "D" gnus-summary-mark-as-read-backward
1338     "E" gnus-summary-mark-as-expirable
1339     "\M-u" gnus-summary-clear-mark-forward
1340     "\M-U" gnus-summary-clear-mark-backward
1341     "k" gnus-summary-kill-same-subject-and-select
1342     "\C-k" gnus-summary-kill-same-subject
1343     "\M-\C-k" gnus-summary-kill-thread
1344     "\M-\C-l" gnus-summary-lower-thread
1345     "e" gnus-summary-edit-article
1346     "#" gnus-summary-mark-as-processable
1347     "\M-#" gnus-summary-unmark-as-processable
1348     "\M-\C-t" gnus-summary-toggle-threads
1349     "\M-\C-s" gnus-summary-show-thread
1350     "\M-\C-h" gnus-summary-hide-thread
1351     "\M-\C-f" gnus-summary-next-thread
1352     "\M-\C-b" gnus-summary-prev-thread
1353     [(meta down)] gnus-summary-next-thread
1354     [(meta up)] gnus-summary-prev-thread
1355     "\M-\C-u" gnus-summary-up-thread
1356     "\M-\C-d" gnus-summary-down-thread
1357     "&" gnus-summary-execute-command
1358     "c" gnus-summary-catchup-and-exit
1359     "\C-w" gnus-summary-mark-region-as-read
1360     "\C-t" gnus-summary-toggle-truncation
1361     "?" gnus-summary-mark-as-dormant
1362     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1363     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1364     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1365     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1366     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1367     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1368     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1369     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1370     "=" gnus-summary-expand-window
1371     "\C-x\C-s" gnus-summary-reselect-current-group
1372     "\M-g" gnus-summary-rescan-group
1373     "w" gnus-summary-stop-page-breaking
1374     "\C-c\C-r" gnus-summary-caesar-message
1375     "f" gnus-summary-followup
1376     "F" gnus-summary-followup-with-original
1377     "C" gnus-summary-cancel-article
1378     "r" gnus-summary-reply
1379     "R" gnus-summary-reply-with-original
1380     "\C-c\C-f" gnus-summary-mail-forward
1381     "o" gnus-summary-save-article
1382     "\C-o" gnus-summary-save-article-mail
1383     "|" gnus-summary-pipe-output
1384     "\M-k" gnus-summary-edit-local-kill
1385     "\M-K" gnus-summary-edit-global-kill
1386     ;; "V" gnus-version
1387     "\C-c\C-d" gnus-summary-describe-group
1388     "q" gnus-summary-exit
1389     "Q" gnus-summary-exit-no-update
1390     "\C-c\C-i" gnus-info-find-node
1391     gnus-mouse-2 gnus-mouse-pick-article
1392     "m" gnus-summary-mail-other-window
1393     "a" gnus-summary-post-news
1394     "x" gnus-summary-limit-to-unread
1395     "s" gnus-summary-isearch-article
1396     "t" gnus-summary-toggle-header
1397     "g" gnus-summary-show-article
1398     "l" gnus-summary-goto-last-article
1399     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1400     "\C-d" gnus-summary-enter-digest-group
1401     "\M-\C-d" gnus-summary-read-document
1402     "\M-\C-e" gnus-summary-edit-parameters
1403     "\M-\C-a" gnus-summary-customize-parameters
1404     "\C-c\C-b" gnus-bug
1405     "*" gnus-cache-enter-article
1406     "\M-*" gnus-cache-remove-article
1407     "\M-&" gnus-summary-universal-argument
1408     "\C-l" gnus-recenter
1409     "I" gnus-summary-increase-score
1410     "L" gnus-summary-lower-score
1411     "\M-i" gnus-symbolic-argument
1412     "h" gnus-summary-select-article-buffer
1413
1414     "b" gnus-article-view-part
1415     "\M-t" gnus-summary-toggle-display-buttonized
1416
1417     "V" gnus-summary-score-map
1418     "X" gnus-uu-extract-map
1419     "S" gnus-summary-send-map)
1420
1421   ;; Sort of orthogonal keymap
1422   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1423     "t" gnus-summary-tick-article-forward
1424     "!" gnus-summary-tick-article-forward
1425     "d" gnus-summary-mark-as-read-forward
1426     "r" gnus-summary-mark-as-read-forward
1427     "c" gnus-summary-clear-mark-forward
1428     " " gnus-summary-clear-mark-forward
1429     "e" gnus-summary-mark-as-expirable
1430     "x" gnus-summary-mark-as-expirable
1431     "?" gnus-summary-mark-as-dormant
1432     "b" gnus-summary-set-bookmark
1433     "B" gnus-summary-remove-bookmark
1434     "#" gnus-summary-mark-as-processable
1435     "\M-#" gnus-summary-unmark-as-processable
1436     "S" gnus-summary-limit-include-expunged
1437     "C" gnus-summary-catchup
1438     "H" gnus-summary-catchup-to-here
1439     "\C-c" gnus-summary-catchup-all
1440     "k" gnus-summary-kill-same-subject-and-select
1441     "K" gnus-summary-kill-same-subject
1442     "P" gnus-uu-mark-map)
1443
1444   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1445     "c" gnus-summary-clear-above
1446     "u" gnus-summary-tick-above
1447     "m" gnus-summary-mark-above
1448     "k" gnus-summary-kill-below)
1449
1450   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1451     "/" gnus-summary-limit-to-subject
1452     "n" gnus-summary-limit-to-articles
1453     "w" gnus-summary-pop-limit
1454     "s" gnus-summary-limit-to-subject
1455     "a" gnus-summary-limit-to-author
1456     "u" gnus-summary-limit-to-unread
1457     "m" gnus-summary-limit-to-marks
1458     "M" gnus-summary-limit-exclude-marks
1459     "v" gnus-summary-limit-to-score
1460     "*" gnus-summary-limit-include-cached
1461     "D" gnus-summary-limit-include-dormant
1462     "T" gnus-summary-limit-include-thread
1463     "d" gnus-summary-limit-exclude-dormant
1464     "t" gnus-summary-limit-to-age
1465     "x" gnus-summary-limit-to-extra
1466     "E" gnus-summary-limit-include-expunged
1467     "c" gnus-summary-limit-exclude-childless-dormant
1468     "C" gnus-summary-limit-mark-excluded-as-read)
1469
1470   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1471     "n" gnus-summary-next-unread-article
1472     "p" gnus-summary-prev-unread-article
1473     "N" gnus-summary-next-article
1474     "P" gnus-summary-prev-article
1475     "\C-n" gnus-summary-next-same-subject
1476     "\C-p" gnus-summary-prev-same-subject
1477     "\M-n" gnus-summary-next-unread-subject
1478     "\M-p" gnus-summary-prev-unread-subject
1479     "f" gnus-summary-first-unread-article
1480     "b" gnus-summary-best-unread-article
1481     "j" gnus-summary-goto-article
1482     "g" gnus-summary-goto-subject
1483     "l" gnus-summary-goto-last-article
1484     "o" gnus-summary-pop-article)
1485
1486   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1487     "k" gnus-summary-kill-thread
1488     "l" gnus-summary-lower-thread
1489     "i" gnus-summary-raise-thread
1490     "T" gnus-summary-toggle-threads
1491     "t" gnus-summary-rethread-current
1492     "^" gnus-summary-reparent-thread
1493     "s" gnus-summary-show-thread
1494     "S" gnus-summary-show-all-threads
1495     "h" gnus-summary-hide-thread
1496     "H" gnus-summary-hide-all-threads
1497     "n" gnus-summary-next-thread
1498     "p" gnus-summary-prev-thread
1499     "u" gnus-summary-up-thread
1500     "o" gnus-summary-top-thread
1501     "d" gnus-summary-down-thread
1502     "#" gnus-uu-mark-thread
1503     "\M-#" gnus-uu-unmark-thread)
1504
1505   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1506     "g" gnus-summary-prepare
1507     "c" gnus-summary-insert-cached-articles)
1508
1509   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1510     "c" gnus-summary-catchup-and-exit
1511     "C" gnus-summary-catchup-all-and-exit
1512     "E" gnus-summary-exit-no-update
1513     "Q" gnus-summary-exit
1514     "Z" gnus-summary-exit
1515     "n" gnus-summary-catchup-and-goto-next-group
1516     "R" gnus-summary-reselect-current-group
1517     "G" gnus-summary-rescan-group
1518     "N" gnus-summary-next-group
1519     "s" gnus-summary-save-newsrc
1520     "P" gnus-summary-prev-group)
1521
1522   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1523     " " gnus-summary-next-page
1524     "n" gnus-summary-next-page
1525     "\177" gnus-summary-prev-page
1526     [delete] gnus-summary-prev-page
1527     "p" gnus-summary-prev-page
1528     "\r" gnus-summary-scroll-up
1529     "\M-\r" gnus-summary-scroll-down
1530     "<" gnus-summary-beginning-of-article
1531     ">" gnus-summary-end-of-article
1532     "b" gnus-summary-beginning-of-article
1533     "e" gnus-summary-end-of-article
1534     "^" gnus-summary-refer-parent-article
1535     "r" gnus-summary-refer-parent-article
1536     "D" gnus-summary-enter-digest-group
1537     "R" gnus-summary-refer-references
1538     "T" gnus-summary-refer-thread
1539     "g" gnus-summary-show-article
1540     "s" gnus-summary-isearch-article
1541     "P" gnus-summary-print-article
1542     "t" gnus-article-babel)
1543
1544   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1545     "b" gnus-article-add-buttons
1546     "B" gnus-article-add-buttons-to-head
1547     "o" gnus-article-treat-overstrike
1548     "e" gnus-article-emphasize
1549     "w" gnus-article-fill-cited-article
1550     "Q" gnus-article-fill-long-lines
1551     "C" gnus-article-capitalize-sentences
1552     "c" gnus-article-remove-cr
1553     "q" gnus-article-de-quoted-unreadable
1554     "6" gnus-article-de-base64-unreadable
1555     "Z" gnus-article-decode-HZ
1556     "h" gnus-article-wash-html
1557     "s" gnus-summary-force-verify-and-decrypt
1558     "f" gnus-article-display-x-face
1559     "l" gnus-summary-stop-page-breaking
1560     "r" gnus-summary-caesar-message
1561     "t" gnus-summary-toggle-header
1562     "v" gnus-summary-verbose-headers
1563     "H" gnus-article-strip-headers-in-body
1564     "d" gnus-article-treat-dumbquotes)
1565
1566   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1567     "a" gnus-article-hide
1568     "h" gnus-article-hide-headers
1569     "b" gnus-article-hide-boring-headers
1570     "s" gnus-article-hide-signature
1571     "c" gnus-article-hide-citation
1572     "C" gnus-article-hide-citation-in-followups
1573     "l" gnus-article-hide-list-identifiers
1574     "p" gnus-article-hide-pgp
1575     "B" gnus-article-strip-banner
1576     "P" gnus-article-hide-pem
1577     "\C-c" gnus-article-hide-citation-maybe)
1578
1579   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1580     "a" gnus-article-highlight
1581     "h" gnus-article-highlight-headers
1582     "c" gnus-article-highlight-citation
1583     "s" gnus-article-highlight-signature)
1584
1585   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1586     "w" gnus-article-decode-mime-words
1587     "c" gnus-article-decode-charset
1588     "v" gnus-mime-view-all-parts
1589     "b" gnus-article-view-part)
1590
1591   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1592     "z" gnus-article-date-ut
1593     "u" gnus-article-date-ut
1594     "l" gnus-article-date-local
1595     "e" gnus-article-date-lapsed
1596     "o" gnus-article-date-original
1597     "i" gnus-article-date-iso8601
1598     "s" gnus-article-date-user)
1599
1600   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1601     "t" gnus-article-remove-trailing-blank-lines
1602     "l" gnus-article-strip-leading-blank-lines
1603     "m" gnus-article-strip-multiple-blank-lines
1604     "a" gnus-article-strip-blank-lines
1605     "A" gnus-article-strip-all-blank-lines
1606     "s" gnus-article-strip-leading-space
1607     "e" gnus-article-strip-trailing-space)
1608
1609   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1610     "v" gnus-version
1611     "f" gnus-summary-fetch-faq
1612     "d" gnus-summary-describe-group
1613     "h" gnus-summary-describe-briefly
1614     "i" gnus-info-find-node)
1615
1616   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1617     "e" gnus-summary-expire-articles
1618     "\M-\C-e" gnus-summary-expire-articles-now
1619     "\177" gnus-summary-delete-article
1620     [delete] gnus-summary-delete-article
1621     [backspace] gnus-summary-delete-article
1622     "m" gnus-summary-move-article
1623     "r" gnus-summary-respool-article
1624     "w" gnus-summary-edit-article
1625     "c" gnus-summary-copy-article
1626     "B" gnus-summary-crosspost-article
1627     "q" gnus-summary-respool-query
1628     "t" gnus-summary-respool-trace
1629     "i" gnus-summary-import-article
1630     "p" gnus-summary-article-posted-p)
1631
1632   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1633     "o" gnus-summary-save-article
1634     "m" gnus-summary-save-article-mail
1635     "F" gnus-summary-write-article-file
1636     "r" gnus-summary-save-article-rmail
1637     "f" gnus-summary-save-article-file
1638     "b" gnus-summary-save-article-body-file
1639     "h" gnus-summary-save-article-folder
1640     "v" gnus-summary-save-article-vm
1641     "p" gnus-summary-pipe-output
1642     "s" gnus-soup-add-article)
1643
1644   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1645     "b" gnus-summary-display-buttonized
1646     "m" gnus-summary-repair-multipart
1647     "v" gnus-article-view-part
1648     "o" gnus-article-save-part
1649     "c" gnus-article-copy-part
1650     "e" gnus-article-externalize-part
1651     "i" gnus-article-inline-part
1652     "|" gnus-article-pipe-part))
1653
1654 (defun gnus-summary-make-menu-bar ()
1655   (gnus-turn-off-edit-menu 'summary)
1656
1657   (unless (boundp 'gnus-summary-misc-menu)
1658
1659     (easy-menu-define
1660      gnus-summary-kill-menu gnus-summary-mode-map ""
1661      (cons
1662       "Score"
1663       (nconc
1664        (list
1665         ["Enter score..." gnus-summary-score-entry t]
1666         ["Customize" gnus-score-customize t])
1667        (gnus-make-score-map 'increase)
1668        (gnus-make-score-map 'lower)
1669        '(("Mark"
1670           ["Kill below" gnus-summary-kill-below t]
1671           ["Mark above" gnus-summary-mark-above t]
1672           ["Tick above" gnus-summary-tick-above t]
1673           ["Clear above" gnus-summary-clear-above t])
1674          ["Current score" gnus-summary-current-score t]
1675          ["Set score" gnus-summary-set-score t]
1676          ["Switch current score file..." gnus-score-change-score-file t]
1677          ["Set mark below..." gnus-score-set-mark-below t]
1678          ["Set expunge below..." gnus-score-set-expunge-below t]
1679          ["Edit current score file" gnus-score-edit-current-scores t]
1680          ["Edit score file" gnus-score-edit-file t]
1681          ["Trace score" gnus-score-find-trace t]
1682          ["Find words" gnus-score-find-favourite-words t]
1683          ["Rescore buffer" gnus-summary-rescore t]
1684          ["Increase score..." gnus-summary-increase-score t]
1685          ["Lower score..." gnus-summary-lower-score t]))))
1686
1687     ;; Define both the Article menu in the summary buffer and the equivalent
1688     ;; Commands menu in the article buffer here for consistency.
1689     (let ((innards
1690            '(("Hide"
1691               ["All" gnus-article-hide t]
1692               ["Headers" gnus-article-hide-headers t]
1693               ["Signature" gnus-article-hide-signature t]
1694               ["Citation" gnus-article-hide-citation t]
1695               ["List identifiers" gnus-article-hide-list-identifiers t]
1696               ["PGP" gnus-article-hide-pgp t]
1697               ["Banner" gnus-article-strip-banner t]
1698               ["Boring headers" gnus-article-hide-boring-headers t])
1699              ("Highlight"
1700               ["All" gnus-article-highlight t]
1701               ["Headers" gnus-article-highlight-headers t]
1702               ["Signature" gnus-article-highlight-signature t]
1703               ["Citation" gnus-article-highlight-citation t])
1704              ("MIME"
1705               ["Words" gnus-article-decode-mime-words t]
1706               ["Charset" gnus-article-decode-charset t]
1707               ["QP" gnus-article-de-quoted-unreadable t]
1708               ["Base64" gnus-article-de-base64-unreadable t]
1709               ["View all" gnus-mime-view-all-parts t]
1710               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt])
1711              ("Date"
1712               ["Local" gnus-article-date-local t]
1713               ["ISO8601" gnus-article-date-iso8601 t]
1714               ["UT" gnus-article-date-ut t]
1715               ["Original" gnus-article-date-original t]
1716               ["Lapsed" gnus-article-date-lapsed t]
1717               ["User-defined" gnus-article-date-user t])
1718              ("Washing"
1719               ("Remove Blanks"
1720                ["Leading" gnus-article-strip-leading-blank-lines t]
1721                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1722                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1723                ["All of the above" gnus-article-strip-blank-lines t]
1724                ["All" gnus-article-strip-all-blank-lines t]
1725                ["Leading space" gnus-article-strip-leading-space t]
1726                ["Trailing space" gnus-article-strip-trailing-space t])
1727               ["Overstrike" gnus-article-treat-overstrike t]
1728               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1729               ["Emphasis" gnus-article-emphasize t]
1730               ["Word wrap" gnus-article-fill-cited-article t]
1731               ["Fill long lines" gnus-article-fill-long-lines t]
1732               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1733               ["CR" gnus-article-remove-cr t]
1734               ["Show X-Face" gnus-article-display-x-face t]
1735               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1736               ["Base64" gnus-article-de-base64-unreadable t]
1737               ["Rot 13" gnus-summary-caesar-message t]
1738               ["Unix pipe" gnus-summary-pipe-message t]
1739               ["Add buttons" gnus-article-add-buttons t]
1740               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1741               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1742               ["Verbose header" gnus-summary-verbose-headers t]
1743               ["Toggle header" gnus-summary-toggle-header t]
1744               ["Html" gnus-article-wash-html t]
1745               ["HZ" gnus-article-decode-HZ t])
1746              ("Output"
1747               ["Save in default format" gnus-summary-save-article t]
1748               ["Save in file" gnus-summary-save-article-file t]
1749               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1750               ["Save in MH folder" gnus-summary-save-article-folder t]
1751               ["Save in VM folder" gnus-summary-save-article-vm t]
1752               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1753               ["Save body in file" gnus-summary-save-article-body-file t]
1754               ["Pipe through a filter" gnus-summary-pipe-output t]
1755               ["Add to SOUP packet" gnus-soup-add-article t]
1756               ["Print" gnus-summary-print-article t])
1757              ("Backend"
1758               ["Respool article..." gnus-summary-respool-article t]
1759               ["Move article..." gnus-summary-move-article
1760                (gnus-check-backend-function
1761                 'request-move-article gnus-newsgroup-name)]
1762               ["Copy article..." gnus-summary-copy-article t]
1763               ["Crosspost article..." gnus-summary-crosspost-article
1764                (gnus-check-backend-function
1765                 'request-replace-article gnus-newsgroup-name)]
1766               ["Import file..." gnus-summary-import-article t]
1767               ["Check if posted" gnus-summary-article-posted-p t]
1768               ["Edit article" gnus-summary-edit-article
1769                (not (gnus-group-read-only-p))]
1770               ["Delete article" gnus-summary-delete-article
1771                (gnus-check-backend-function
1772                 'request-expire-articles gnus-newsgroup-name)]
1773               ["Query respool" gnus-summary-respool-query t]
1774               ["Trace respool" gnus-summary-respool-trace t]
1775               ["Delete expirable articles" gnus-summary-expire-articles-now
1776                (gnus-check-backend-function
1777                 'request-expire-articles gnus-newsgroup-name)])
1778              ("Extract"
1779               ["Uudecode" gnus-uu-decode-uu t]
1780               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1781               ["Unshar" gnus-uu-decode-unshar t]
1782               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1783               ["Save" gnus-uu-decode-save t]
1784               ["Binhex" gnus-uu-decode-binhex t]
1785               ["Postscript" gnus-uu-decode-postscript t])
1786              ("Cache"
1787               ["Enter article" gnus-cache-enter-article t]
1788               ["Remove article" gnus-cache-remove-article t])
1789              ["Translate" gnus-article-babel t]
1790              ["Select article buffer" gnus-summary-select-article-buffer t]
1791              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1792              ["Isearch article..." gnus-summary-isearch-article t]
1793              ["Beginning of the article" gnus-summary-beginning-of-article t]
1794              ["End of the article" gnus-summary-end-of-article t]
1795              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1796              ["Fetch referenced articles" gnus-summary-refer-references t]
1797              ["Fetch current thread" gnus-summary-refer-thread t]
1798              ["Fetch article with id..." gnus-summary-refer-article t]
1799              ["Redisplay" gnus-summary-show-article t])))
1800       (easy-menu-define
1801        gnus-summary-article-menu gnus-summary-mode-map ""
1802        (cons "Article" innards))
1803
1804       (easy-menu-define
1805        gnus-article-commands-menu gnus-article-mode-map ""
1806        (cons "Commands" innards)))
1807
1808     (easy-menu-define
1809      gnus-summary-thread-menu gnus-summary-mode-map ""
1810      '("Threads"
1811        ["Toggle threading" gnus-summary-toggle-threads t]
1812        ["Hide threads" gnus-summary-hide-all-threads t]
1813        ["Show threads" gnus-summary-show-all-threads t]
1814        ["Hide thread" gnus-summary-hide-thread t]
1815        ["Show thread" gnus-summary-show-thread t]
1816        ["Go to next thread" gnus-summary-next-thread t]
1817        ["Go to previous thread" gnus-summary-prev-thread t]
1818        ["Go down thread" gnus-summary-down-thread t]
1819        ["Go up thread" gnus-summary-up-thread t]
1820        ["Top of thread" gnus-summary-top-thread t]
1821        ["Mark thread as read" gnus-summary-kill-thread t]
1822        ["Lower thread score" gnus-summary-lower-thread t]
1823        ["Raise thread score" gnus-summary-raise-thread t]
1824        ["Rethread current" gnus-summary-rethread-current t]))
1825
1826     (easy-menu-define
1827      gnus-summary-post-menu gnus-summary-mode-map ""
1828      '("Post"
1829        ["Post an article" gnus-summary-post-news t]
1830        ["Followup" gnus-summary-followup t]
1831        ["Followup and yank" gnus-summary-followup-with-original t]
1832        ["Supersede article" gnus-summary-supersede-article t]
1833        ["Cancel article" gnus-summary-cancel-article t]
1834        ["Reply" gnus-summary-reply t]
1835        ["Reply and yank" gnus-summary-reply-with-original t]
1836        ["Wide reply" gnus-summary-wide-reply t]
1837        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1838        ["Mail forward" gnus-summary-mail-forward t]
1839        ["Post forward" gnus-summary-post-forward t]
1840        ["Digest and mail" gnus-uu-digest-mail-forward t]
1841        ["Digest and post" gnus-uu-digest-post-forward t]
1842        ["Resend message" gnus-summary-resend-message t]
1843        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1844        ["Send a mail" gnus-summary-mail-other-window t]
1845        ["Uuencode and post" gnus-uu-post-news t]
1846        ["Followup via news" gnus-summary-followup-to-mail t]
1847        ["Followup via news and yank"
1848         gnus-summary-followup-to-mail-with-original t]
1849        ;;("Draft"
1850        ;;["Send" gnus-summary-send-draft t]
1851        ;;["Send bounced" gnus-resend-bounced-mail t])
1852        ))
1853
1854     (easy-menu-define
1855      gnus-summary-misc-menu gnus-summary-mode-map ""
1856      '("Misc"
1857        ("Mark Read"
1858         ["Mark as read" gnus-summary-mark-as-read-forward t]
1859         ["Mark same subject and select"
1860          gnus-summary-kill-same-subject-and-select t]
1861         ["Mark same subject" gnus-summary-kill-same-subject t]
1862         ["Catchup" gnus-summary-catchup t]
1863         ["Catchup all" gnus-summary-catchup-all t]
1864         ["Catchup to here" gnus-summary-catchup-to-here t]
1865         ["Catchup region" gnus-summary-mark-region-as-read t]
1866         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1867        ("Mark Various"
1868         ["Tick" gnus-summary-tick-article-forward t]
1869         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1870         ["Remove marks" gnus-summary-clear-mark-forward t]
1871         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1872         ["Set bookmark" gnus-summary-set-bookmark t]
1873         ["Remove bookmark" gnus-summary-remove-bookmark t])
1874        ("Mark Limit"
1875         ["Marks..." gnus-summary-limit-to-marks t]
1876         ["Subject..." gnus-summary-limit-to-subject t]
1877         ["Author..." gnus-summary-limit-to-author t]
1878         ["Age..." gnus-summary-limit-to-age t]
1879         ["Extra..." gnus-summary-limit-to-extra t]
1880         ["Score" gnus-summary-limit-to-score t]
1881         ["Unread" gnus-summary-limit-to-unread t]
1882         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1883         ["Articles" gnus-summary-limit-to-articles t]
1884         ["Pop limit" gnus-summary-pop-limit t]
1885         ["Show dormant" gnus-summary-limit-include-dormant t]
1886         ["Hide childless dormant"
1887          gnus-summary-limit-exclude-childless-dormant t]
1888         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1889         ["Hide marked" gnus-summary-limit-exclude-marks t]
1890         ["Show expunged" gnus-summary-show-all-expunged t])
1891        ("Process Mark"
1892         ["Set mark" gnus-summary-mark-as-processable t]
1893         ["Remove mark" gnus-summary-unmark-as-processable t]
1894         ["Remove all marks" gnus-summary-unmark-all-processable t]
1895         ["Mark above" gnus-uu-mark-over t]
1896         ["Mark series" gnus-uu-mark-series t]
1897         ["Mark region" gnus-uu-mark-region t]
1898         ["Unmark region" gnus-uu-unmark-region t]
1899         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1900         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1901         ["Mark all" gnus-uu-mark-all t]
1902         ["Mark buffer" gnus-uu-mark-buffer t]
1903         ["Mark sparse" gnus-uu-mark-sparse t]
1904         ["Mark thread" gnus-uu-mark-thread t]
1905         ["Unmark thread" gnus-uu-unmark-thread t]
1906         ("Process Mark Sets"
1907          ["Kill" gnus-summary-kill-process-mark t]
1908          ["Yank" gnus-summary-yank-process-mark
1909           gnus-newsgroup-process-stack]
1910          ["Save" gnus-summary-save-process-mark t]))
1911        ("Scroll article"
1912         ["Page forward" gnus-summary-next-page t]
1913         ["Page backward" gnus-summary-prev-page t]
1914         ["Line forward" gnus-summary-scroll-up t])
1915        ("Move"
1916         ["Next unread article" gnus-summary-next-unread-article t]
1917         ["Previous unread article" gnus-summary-prev-unread-article t]
1918         ["Next article" gnus-summary-next-article t]
1919         ["Previous article" gnus-summary-prev-article t]
1920         ["Next unread subject" gnus-summary-next-unread-subject t]
1921         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1922         ["Next article same subject" gnus-summary-next-same-subject t]
1923         ["Previous article same subject" gnus-summary-prev-same-subject t]
1924         ["First unread article" gnus-summary-first-unread-article t]
1925         ["Best unread article" gnus-summary-best-unread-article t]
1926         ["Go to subject number..." gnus-summary-goto-subject t]
1927         ["Go to article number..." gnus-summary-goto-article t]
1928         ["Go to the last article" gnus-summary-goto-last-article t]
1929         ["Pop article off history" gnus-summary-pop-article t])
1930        ("Sort"
1931         ["Sort by number" gnus-summary-sort-by-number t]
1932         ["Sort by author" gnus-summary-sort-by-author t]
1933         ["Sort by subject" gnus-summary-sort-by-subject t]
1934         ["Sort by date" gnus-summary-sort-by-date t]
1935         ["Sort by score" gnus-summary-sort-by-score t]
1936         ["Sort by lines" gnus-summary-sort-by-lines t]
1937         ["Sort by characters" gnus-summary-sort-by-chars t])
1938        ("Help"
1939         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1940         ["Describe group" gnus-summary-describe-group t]
1941         ["Read manual" gnus-info-find-node t])
1942        ("Modes"
1943         ["Pick and read" gnus-pick-mode t]
1944         ["Binary" gnus-binary-mode t])
1945        ("Regeneration"
1946         ["Regenerate" gnus-summary-prepare t]
1947         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1948         ["Toggle threading" gnus-summary-toggle-threads t])
1949        ["Filter articles..." gnus-summary-execute-command t]
1950        ["Run command on subjects..." gnus-summary-universal-argument t]
1951        ["Search articles forward..." gnus-summary-search-article-forward t]
1952        ["Search articles backward..." gnus-summary-search-article-backward t]
1953        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1954        ["Expand window" gnus-summary-expand-window t]
1955        ["Expire expirable articles" gnus-summary-expire-articles
1956         (gnus-check-backend-function
1957          'request-expire-articles gnus-newsgroup-name)]
1958        ["Edit local kill file" gnus-summary-edit-local-kill t]
1959        ["Edit main kill file" gnus-summary-edit-global-kill t]
1960        ["Edit group parameters" gnus-summary-edit-parameters t]
1961        ["Customize group parameters" gnus-summary-customize-parameters t]
1962        ["Send a bug report" gnus-bug t]
1963        ("Exit"
1964         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1965         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1966         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1967         ["Exit group" gnus-summary-exit t]
1968         ["Exit group without updating" gnus-summary-exit-no-update t]
1969         ["Exit and goto next group" gnus-summary-next-group t]
1970         ["Exit and goto prev group" gnus-summary-prev-group t]
1971         ["Reselect group" gnus-summary-reselect-current-group t]
1972         ["Rescan group" gnus-summary-rescan-group t]
1973         ["Update dribble" gnus-summary-save-newsrc t])))
1974
1975     (gnus-run-hooks 'gnus-summary-menu-hook)))
1976
1977 (defun gnus-score-set-default (var value)
1978   "A version of set that updates the GNU Emacs menu-bar."
1979   (set var value)
1980   ;; It is the message that forces the active status to be updated.
1981   (message ""))
1982
1983 (defun gnus-make-score-map (type)
1984   "Make a summary score map of type TYPE."
1985   (if t
1986       nil
1987     (let ((headers '(("author" "from" string)
1988                      ("subject" "subject" string)
1989                      ("article body" "body" string)
1990                      ("article head" "head" string)
1991                      ("xref" "xref" string)
1992                      ("extra header" "extra" string)
1993                      ("lines" "lines" number)
1994                      ("followups to author" "followup" string)))
1995           (types '((number ("less than" <)
1996                            ("greater than" >)
1997                            ("equal" =))
1998                    (string ("substring" s)
1999                            ("exact string" e)
2000                            ("fuzzy string" f)
2001                            ("regexp" r))))
2002           (perms '(("temporary" (current-time-string))
2003                    ("permanent" nil)
2004                    ("immediate" now)))
2005           header)
2006       (list
2007        (apply
2008         'nconc
2009         (list
2010          (if (eq type 'lower)
2011              "Lower score"
2012            "Increase score"))
2013         (let (outh)
2014           (while headers
2015             (setq header (car headers))
2016             (setq outh
2017                   (cons
2018                    (apply
2019                     'nconc
2020                     (list (car header))
2021                     (let ((ts (cdr (assoc (nth 2 header) types)))
2022                           outt)
2023                       (while ts
2024                         (setq outt
2025                               (cons
2026                                (apply
2027                                 'nconc
2028                                 (list (caar ts))
2029                                 (let ((ps perms)
2030                                       outp)
2031                                   (while ps
2032                                     (setq outp
2033                                           (cons
2034                                            (vector
2035                                             (caar ps)
2036                                             (list
2037                                              'gnus-summary-score-entry
2038                                              (nth 1 header)
2039                                              (if (or (string= (nth 1 header)
2040                                                               "head")
2041                                                      (string= (nth 1 header)
2042                                                               "body"))
2043                                                  ""
2044                                                (list 'gnus-summary-header
2045                                                      (nth 1 header)))
2046                                              (list 'quote (nth 1 (car ts)))
2047                                              (list 'gnus-score-delta-default
2048                                                    nil)
2049                                              (nth 1 (car ps))
2050                                              t)
2051                                             t)
2052                                            outp))
2053                                     (setq ps (cdr ps)))
2054                                   (list (nreverse outp))))
2055                                outt))
2056                         (setq ts (cdr ts)))
2057                       (list (nreverse outt))))
2058                    outh))
2059             (setq headers (cdr headers)))
2060           (list (nreverse outh))))))))
2061
2062 \f
2063
2064 (defun gnus-summary-mode (&optional group)
2065   "Major mode for reading articles.
2066
2067 All normal editing commands are switched off.
2068 \\<gnus-summary-mode-map>
2069 Each line in this buffer represents one article.  To read an
2070 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2071 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2072 respectively.
2073
2074 You can also post articles and send mail from this buffer.  To
2075 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2076 of an article, type `\\[gnus-summary-reply]'.
2077
2078 There are approx. one gazillion commands you can execute in this
2079 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2080
2081 The following commands are available:
2082
2083 \\{gnus-summary-mode-map}"
2084   (interactive)
2085   (when (gnus-visual-p 'summary-menu 'menu)
2086     (gnus-summary-make-menu-bar))
2087   (kill-all-local-variables)
2088   (gnus-summary-make-local-variables)
2089   (gnus-make-thread-indent-array)
2090   (gnus-simplify-mode-line)
2091   (setq major-mode 'gnus-summary-mode)
2092   (setq mode-name "Summary")
2093   (make-local-variable 'minor-mode-alist)
2094   (use-local-map gnus-summary-mode-map)
2095   (buffer-disable-undo)
2096   (setq buffer-read-only t)             ;Disable modification
2097   (setq truncate-lines t)
2098   (setq selective-display t)
2099   (setq selective-display-ellipses t)   ;Display `...'
2100   (gnus-summary-set-display-table)
2101   (gnus-set-default-directory)
2102   (setq gnus-newsgroup-name group)
2103   (make-local-variable 'gnus-summary-line-format)
2104   (make-local-variable 'gnus-summary-line-format-spec)
2105   (make-local-variable 'gnus-summary-dummy-line-format)
2106   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2107   (make-local-variable 'gnus-summary-mark-positions)
2108   (make-local-hook 'pre-command-hook)
2109   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2110   (gnus-run-hooks 'gnus-summary-mode-hook)
2111   (mm-enable-multibyte-mule4)
2112   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2113   (gnus-update-summary-mark-positions))
2114
2115 (defun gnus-summary-make-local-variables ()
2116   "Make all the local summary buffer variables."
2117   (let (global)
2118     (dolist (local gnus-summary-local-variables)
2119       (if (consp local)
2120           (progn
2121             (if (eq (cdr local) 'global)
2122                 ;; Copy the global value of the variable.
2123                 (setq global (symbol-value (car local)))
2124               ;; Use the value from the list.
2125               (setq global (eval (cdr local))))
2126             (set (make-local-variable (car local)) global))
2127         ;; Simple nil-valued local variable.
2128         (set (make-local-variable local) nil)))))
2129
2130 (defun gnus-summary-clear-local-variables ()
2131   (let ((locals gnus-summary-local-variables))
2132     (while locals
2133       (if (consp (car locals))
2134           (and (vectorp (caar locals))
2135                (set (caar locals) nil))
2136         (and (vectorp (car locals))
2137              (set (car locals) nil)))
2138       (setq locals (cdr locals)))))
2139
2140 ;; Summary data functions.
2141
2142 (defmacro gnus-data-number (data)
2143   `(car ,data))
2144
2145 (defmacro gnus-data-set-number (data number)
2146   `(setcar ,data ,number))
2147
2148 (defmacro gnus-data-mark (data)
2149   `(nth 1 ,data))
2150
2151 (defmacro gnus-data-set-mark (data mark)
2152   `(setcar (nthcdr 1 ,data) ,mark))
2153
2154 (defmacro gnus-data-pos (data)
2155   `(nth 2 ,data))
2156
2157 (defmacro gnus-data-set-pos (data pos)
2158   `(setcar (nthcdr 2 ,data) ,pos))
2159
2160 (defmacro gnus-data-header (data)
2161   `(nth 3 ,data))
2162
2163 (defmacro gnus-data-set-header (data header)
2164   `(setf (nth 3 ,data) ,header))
2165
2166 (defmacro gnus-data-level (data)
2167   `(nth 4 ,data))
2168
2169 (defmacro gnus-data-unread-p (data)
2170   `(= (nth 1 ,data) gnus-unread-mark))
2171
2172 (defmacro gnus-data-read-p (data)
2173   `(/= (nth 1 ,data) gnus-unread-mark))
2174
2175 (defmacro gnus-data-pseudo-p (data)
2176   `(consp (nth 3 ,data)))
2177
2178 (defmacro gnus-data-find (number)
2179   `(assq ,number gnus-newsgroup-data))
2180
2181 (defmacro gnus-data-find-list (number &optional data)
2182   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2183      (memq (assq ,number bdata)
2184            bdata)))
2185
2186 (defmacro gnus-data-make (number mark pos header level)
2187   `(list ,number ,mark ,pos ,header ,level))
2188
2189 (defun gnus-data-enter (after-article number mark pos header level offset)
2190   (let ((data (gnus-data-find-list after-article)))
2191     (unless data
2192       (error "No such article: %d" after-article))
2193     (setcdr data (cons (gnus-data-make number mark pos header level)
2194                        (cdr data)))
2195     (setq gnus-newsgroup-data-reverse nil)
2196     (gnus-data-update-list (cddr data) offset)))
2197
2198 (defun gnus-data-enter-list (after-article list &optional offset)
2199   (when list
2200     (let ((data (and after-article (gnus-data-find-list after-article)))
2201           (ilist list))
2202       (if (not (or data
2203                    after-article))
2204           (let ((odata gnus-newsgroup-data))
2205             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2206             (when offset
2207               (gnus-data-update-list odata offset)))
2208         ;; Find the last element in the list to be spliced into the main
2209         ;; list.
2210         (while (cdr list)
2211           (setq list (cdr list)))
2212         (if (not data)
2213             (progn
2214               (setcdr list gnus-newsgroup-data)
2215               (setq gnus-newsgroup-data ilist)
2216               (when offset
2217                 (gnus-data-update-list (cdr list) offset)))
2218           (setcdr list (cdr data))
2219           (setcdr data ilist)
2220           (when offset
2221             (gnus-data-update-list (cdr list) offset))))
2222       (setq gnus-newsgroup-data-reverse nil))))
2223
2224 (defun gnus-data-remove (article &optional offset)
2225   (let ((data gnus-newsgroup-data))
2226     (if (= (gnus-data-number (car data)) article)
2227         (progn
2228           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2229                 gnus-newsgroup-data-reverse nil)
2230           (when offset
2231             (gnus-data-update-list gnus-newsgroup-data offset)))
2232       (while (cdr data)
2233         (when (= (gnus-data-number (cadr data)) article)
2234           (setcdr data (cddr data))
2235           (when offset
2236             (gnus-data-update-list (cdr data) offset))
2237           (setq data nil
2238                 gnus-newsgroup-data-reverse nil))
2239         (setq data (cdr data))))))
2240
2241 (defmacro gnus-data-list (backward)
2242   `(if ,backward
2243        (or gnus-newsgroup-data-reverse
2244            (setq gnus-newsgroup-data-reverse
2245                  (reverse gnus-newsgroup-data)))
2246      gnus-newsgroup-data))
2247
2248 (defun gnus-data-update-list (data offset)
2249   "Add OFFSET to the POS of all data entries in DATA."
2250   (setq gnus-newsgroup-data-reverse nil)
2251   (while data
2252     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2253     (setq data (cdr data))))
2254
2255 (defun gnus-summary-article-pseudo-p (article)
2256   "Say whether this article is a pseudo article or not."
2257   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2258
2259 (defmacro gnus-summary-article-sparse-p (article)
2260   "Say whether this article is a sparse article or not."
2261   `(memq ,article gnus-newsgroup-sparse))
2262
2263 (defmacro gnus-summary-article-ancient-p (article)
2264   "Say whether this article is a sparse article or not."
2265   `(memq ,article gnus-newsgroup-ancient))
2266
2267 (defun gnus-article-parent-p (number)
2268   "Say whether this article is a parent or not."
2269   (let ((data (gnus-data-find-list number)))
2270     (and (cdr data)                     ; There has to be an article after...
2271          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2272             (gnus-data-level (nth 1 data))))))
2273
2274 (defun gnus-article-children (number)
2275   "Return a list of all children to NUMBER."
2276   (let* ((data (gnus-data-find-list number))
2277          (level (gnus-data-level (car data)))
2278          children)
2279     (setq data (cdr data))
2280     (while (and data
2281                 (= (gnus-data-level (car data)) (1+ level)))
2282       (push (gnus-data-number (car data)) children)
2283       (setq data (cdr data)))
2284     children))
2285
2286 (defmacro gnus-summary-skip-intangible ()
2287   "If the current article is intangible, then jump to a different article."
2288   '(let ((to (get-text-property (point) 'gnus-intangible)))
2289      (and to (gnus-summary-goto-subject to))))
2290
2291 (defmacro gnus-summary-article-intangible-p ()
2292   "Say whether this article is intangible or not."
2293   '(get-text-property (point) 'gnus-intangible))
2294
2295 (defun gnus-article-read-p (article)
2296   "Say whether ARTICLE is read or not."
2297   (not (or (memq article gnus-newsgroup-marked)
2298            (memq article gnus-newsgroup-unreads)
2299            (memq article gnus-newsgroup-unselected)
2300            (memq article gnus-newsgroup-dormant))))
2301
2302 ;; Some summary mode macros.
2303
2304 (defmacro gnus-summary-article-number ()
2305   "The article number of the article on the current line.
2306 If there isn's an article number here, then we return the current
2307 article number."
2308   '(progn
2309      (gnus-summary-skip-intangible)
2310      (or (get-text-property (point) 'gnus-number)
2311          (gnus-summary-last-subject))))
2312
2313 (defmacro gnus-summary-article-header (&optional number)
2314   "Return the header of article NUMBER."
2315   `(gnus-data-header (gnus-data-find
2316                       ,(or number '(gnus-summary-article-number)))))
2317
2318 (defmacro gnus-summary-thread-level (&optional number)
2319   "Return the level of thread that starts with article NUMBER."
2320   `(if (and (eq gnus-summary-make-false-root 'dummy)
2321             (get-text-property (point) 'gnus-intangible))
2322        0
2323      (gnus-data-level (gnus-data-find
2324                        ,(or number '(gnus-summary-article-number))))))
2325
2326 (defmacro gnus-summary-article-mark (&optional number)
2327   "Return the mark of article NUMBER."
2328   `(gnus-data-mark (gnus-data-find
2329                     ,(or number '(gnus-summary-article-number)))))
2330
2331 (defmacro gnus-summary-article-pos (&optional number)
2332   "Return the position of the line of article NUMBER."
2333   `(gnus-data-pos (gnus-data-find
2334                    ,(or number '(gnus-summary-article-number)))))
2335
2336 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2337 (defmacro gnus-summary-article-subject (&optional number)
2338   "Return current subject string or nil if nothing."
2339   `(let ((headers
2340           ,(if number
2341                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2342              '(gnus-data-header (assq (gnus-summary-article-number)
2343                                       gnus-newsgroup-data)))))
2344      (and headers
2345           (vectorp headers)
2346           (mail-header-subject headers))))
2347
2348 (defmacro gnus-summary-article-score (&optional number)
2349   "Return current article score."
2350   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2351                   gnus-newsgroup-scored))
2352        gnus-summary-default-score 0))
2353
2354 (defun gnus-summary-article-children (&optional number)
2355   "Return a list of article numbers that are children of article NUMBER."
2356   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2357          (level (gnus-data-level (car data)))
2358          l children)
2359     (while (and (setq data (cdr data))
2360                 (> (setq l (gnus-data-level (car data))) level))
2361       (and (= (1+ level) l)
2362            (push (gnus-data-number (car data))
2363                  children)))
2364     (nreverse children)))
2365
2366 (defun gnus-summary-article-parent (&optional number)
2367   "Return the article number of the parent of article NUMBER."
2368   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2369                                     (gnus-data-list t)))
2370          (level (gnus-data-level (car data))))
2371     (if (zerop level)
2372         ()                              ; This is a root.
2373       ;; We search until we find an article with a level less than
2374       ;; this one.  That function has to be the parent.
2375       (while (and (setq data (cdr data))
2376                   (not (< (gnus-data-level (car data)) level))))
2377       (and data (gnus-data-number (car data))))))
2378
2379 (defun gnus-unread-mark-p (mark)
2380   "Say whether MARK is the unread mark."
2381   (= mark gnus-unread-mark))
2382
2383 (defun gnus-read-mark-p (mark)
2384   "Say whether MARK is one of the marks that mark as read.
2385 This is all marks except unread, ticked, dormant, and expirable."
2386   (not (or (= mark gnus-unread-mark)
2387            (= mark gnus-ticked-mark)
2388            (= mark gnus-dormant-mark)
2389            (= mark gnus-expirable-mark))))
2390
2391 (defmacro gnus-article-mark (number)
2392   "Return the MARK of article NUMBER.
2393 This macro should only be used when computing the mark the \"first\"
2394 time; i.e., when generating the summary lines.  After that,
2395 `gnus-summary-article-mark' should be used to examine the
2396 marks of articles."
2397   `(cond
2398     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2399     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2400     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2401     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2402     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2403     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2404     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2405     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2406            gnus-ancient-mark))))
2407
2408 ;; Saving hidden threads.
2409
2410 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2411 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2412
2413 (defmacro gnus-save-hidden-threads (&rest forms)
2414   "Save hidden threads, eval FORMS, and restore the hidden threads."
2415   (let ((config (make-symbol "config")))
2416     `(let ((,config (gnus-hidden-threads-configuration)))
2417        (unwind-protect
2418            (save-excursion
2419              ,@forms)
2420          (gnus-restore-hidden-threads-configuration ,config)))))
2421
2422 (defun gnus-data-compute-positions ()
2423   "Compute the positions of all articles."
2424   (setq gnus-newsgroup-data-reverse nil)
2425   (let ((data gnus-newsgroup-data))
2426     (save-excursion
2427       (gnus-save-hidden-threads
2428         (gnus-summary-show-all-threads)
2429         (goto-char (point-min))
2430         (while data
2431           (while (get-text-property (point) 'gnus-intangible)
2432             (forward-line 1))
2433           (gnus-data-set-pos (car data) (+ (point) 3))
2434           (setq data (cdr data))
2435           (forward-line 1))))))
2436
2437 (defun gnus-hidden-threads-configuration ()
2438   "Return the current hidden threads configuration."
2439   (save-excursion
2440     (let (config)
2441       (goto-char (point-min))
2442       (while (search-forward "\r" nil t)
2443         (push (1- (point)) config))
2444       config)))
2445
2446 (defun gnus-restore-hidden-threads-configuration (config)
2447   "Restore hidden threads configuration from CONFIG."
2448   (save-excursion
2449     (let (point buffer-read-only)
2450       (while (setq point (pop config))
2451         (when (and (< point (point-max))
2452                    (goto-char point)
2453                    (eq (char-after) ?\n))
2454           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2455
2456 ;; Various summary mode internalish functions.
2457
2458 (defun gnus-mouse-pick-article (e)
2459   (interactive "e")
2460   (mouse-set-point e)
2461   (gnus-summary-next-page nil t))
2462
2463 (defun gnus-summary-set-display-table ()
2464   "Change the display table.
2465 Odd characters have a tendency to mess
2466 up nicely formatted displays - we make all possible glyphs
2467 display only a single character."
2468
2469   ;; We start from the standard display table, if any.
2470   (let ((table (or (copy-sequence standard-display-table)
2471                    (make-display-table)))
2472         (i 32))
2473     ;; Nix out all the control chars...
2474     (while (>= (setq i (1- i)) 0)
2475       (aset table i [??]))
2476     ;; ... but not newline and cr, of course.  (cr is necessary for the
2477     ;; selective display).
2478     (aset table ?\n nil)
2479     (aset table ?\r nil)
2480     ;; We keep TAB as well.
2481     (aset table ?\t nil)
2482     ;; We nix out any glyphs over 126 that are not set already.
2483     (let ((i 256))
2484       (while (>= (setq i (1- i)) 127)
2485         ;; Only modify if the entry is nil.
2486         (unless (aref table i)
2487           (aset table i [??]))))
2488     (setq buffer-display-table table)))
2489
2490 (defun gnus-summary-setup-buffer (group)
2491   "Initialize summary buffer."
2492   (let ((buffer (concat "*Summary " group "*")))
2493     (if (get-buffer buffer)
2494         (progn
2495           (set-buffer buffer)
2496           (setq gnus-summary-buffer (current-buffer))
2497           (not gnus-newsgroup-prepared))
2498       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2499       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2500       (gnus-summary-mode group)
2501       (when gnus-carpal
2502         (gnus-carpal-setup-buffer 'summary))
2503       (unless gnus-single-article-buffer
2504         (make-local-variable 'gnus-article-buffer)
2505         (make-local-variable 'gnus-article-current)
2506         (make-local-variable 'gnus-original-article-buffer))
2507       (setq gnus-newsgroup-name group)
2508       t)))
2509
2510 (defun gnus-set-global-variables ()
2511   "Set the global equivalents of the buffer-local variables.
2512 They are set to the latest values they had.  These reflect the summary
2513 buffer that was in action when the last article was fetched."
2514   (when (eq major-mode 'gnus-summary-mode)
2515     (setq gnus-summary-buffer (current-buffer))
2516     (let ((name gnus-newsgroup-name)
2517           (marked gnus-newsgroup-marked)
2518           (unread gnus-newsgroup-unreads)
2519           (headers gnus-current-headers)
2520           (data gnus-newsgroup-data)
2521           (summary gnus-summary-buffer)
2522           (article-buffer gnus-article-buffer)
2523           (original gnus-original-article-buffer)
2524           (gac gnus-article-current)
2525           (reffed gnus-reffed-article-number)
2526           (score-file gnus-current-score-file)
2527           (default-charset gnus-newsgroup-charset))
2528       (save-excursion
2529         (set-buffer gnus-group-buffer)
2530         (setq gnus-newsgroup-name name
2531               gnus-newsgroup-marked marked
2532               gnus-newsgroup-unreads unread
2533               gnus-current-headers headers
2534               gnus-newsgroup-data data
2535               gnus-article-current gac
2536               gnus-summary-buffer summary
2537               gnus-article-buffer article-buffer
2538               gnus-original-article-buffer original
2539               gnus-reffed-article-number reffed
2540               gnus-current-score-file score-file
2541               gnus-newsgroup-charset default-charset)
2542         ;; The article buffer also has local variables.
2543         (when (gnus-buffer-live-p gnus-article-buffer)
2544           (set-buffer gnus-article-buffer)
2545           (setq gnus-summary-buffer summary))))))
2546
2547 (defun gnus-summary-article-unread-p (article)
2548   "Say whether ARTICLE is unread or not."
2549   (memq article gnus-newsgroup-unreads))
2550
2551 (defun gnus-summary-first-article-p (&optional article)
2552   "Return whether ARTICLE is the first article in the buffer."
2553   (if (not (setq article (or article (gnus-summary-article-number))))
2554       nil
2555     (eq article (caar gnus-newsgroup-data))))
2556
2557 (defun gnus-summary-last-article-p (&optional article)
2558   "Return whether ARTICLE is the last article in the buffer."
2559   (if (not (setq article (or article (gnus-summary-article-number))))
2560       ;; All non-existent numbers are the last article.  :-)
2561       t
2562     (not (cdr (gnus-data-find-list article)))))
2563
2564 (defun gnus-make-thread-indent-array ()
2565   (let ((n 200))
2566     (unless (and gnus-thread-indent-array
2567                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2568       (setq gnus-thread-indent-array (make-vector 201 "")
2569             gnus-thread-indent-array-level gnus-thread-indent-level)
2570       (while (>= n 0)
2571         (aset gnus-thread-indent-array n
2572               (make-string (* n gnus-thread-indent-level) ? ))
2573         (setq n (1- n))))))
2574
2575 (defun gnus-update-summary-mark-positions ()
2576   "Compute where the summary marks are to go."
2577   (save-excursion
2578     (when (gnus-buffer-exists-p gnus-summary-buffer)
2579       (set-buffer gnus-summary-buffer))
2580     (let ((gnus-replied-mark 129)
2581           (gnus-score-below-mark 130)
2582           (gnus-score-over-mark 130)
2583           (gnus-download-mark 131)
2584           (spec gnus-summary-line-format-spec)
2585           gnus-visual pos)
2586       (save-excursion
2587         (gnus-set-work-buffer)
2588         (let ((gnus-summary-line-format-spec spec)
2589               (gnus-newsgroup-downloadable '((0 . t))))
2590           (gnus-summary-insert-line
2591            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2592           (goto-char (point-min))
2593           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2594                                              (- (point) 2)))))
2595           (goto-char (point-min))
2596           (push (cons 'replied (and (search-forward "\201" nil t)
2597                                     (- (point) 2)))
2598                 pos)
2599           (goto-char (point-min))
2600           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2601                 pos)
2602           (goto-char (point-min))
2603           (push (cons 'download
2604                       (and (search-forward "\203" nil t) (- (point) 2)))
2605                 pos)))
2606       (setq gnus-summary-mark-positions pos))))
2607
2608 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2609   "Insert a dummy root in the summary buffer."
2610   (beginning-of-line)
2611   (gnus-add-text-properties
2612    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2613    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2614
2615 (defun gnus-summary-from-or-to-or-newsgroups (header)
2616   (let ((to (cdr (assq 'To (mail-header-extra header))))
2617         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2618         (mail-parse-charset gnus-newsgroup-charset)
2619         (mail-parse-ignored-charsets
2620          (save-excursion (set-buffer gnus-summary-buffer)
2621                          gnus-newsgroup-ignored-charsets)))
2622     (cond
2623      ((and to
2624            gnus-ignored-from-addresses
2625            (string-match gnus-ignored-from-addresses
2626                          (mail-header-from header)))
2627       (concat "-> "
2628               (or (car (funcall gnus-extract-address-components
2629                                 (funcall
2630                                  gnus-decode-encoded-word-function to)))
2631                   (funcall gnus-decode-encoded-word-function to))))
2632      ((and newsgroups
2633            gnus-ignored-from-addresses
2634            (string-match gnus-ignored-from-addresses
2635                          (mail-header-from header)))
2636       (concat "=> " newsgroups))
2637      (t
2638       (or (car (funcall gnus-extract-address-components
2639                         (mail-header-from header)))
2640           (mail-header-from header))))))
2641
2642 (defun gnus-summary-insert-line (gnus-tmp-header
2643                                  gnus-tmp-level gnus-tmp-current
2644                                  gnus-tmp-unread gnus-tmp-replied
2645                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2646                                  &optional gnus-tmp-dummy gnus-tmp-score
2647                                  gnus-tmp-process)
2648   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2649          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2650          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2651          (gnus-tmp-score-char
2652           (if (or (null gnus-summary-default-score)
2653                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2654                       gnus-summary-zcore-fuzz))
2655               ?  ;Whitespace
2656             (if (< gnus-tmp-score gnus-summary-default-score)
2657                 gnus-score-below-mark gnus-score-over-mark)))
2658          (gnus-tmp-replied
2659           (cond (gnus-tmp-process gnus-process-mark)
2660                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2661                  gnus-cached-mark)
2662                 (gnus-tmp-replied gnus-replied-mark)
2663                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2664                  gnus-saved-mark)
2665                 (t gnus-unread-mark)))
2666          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2667          (gnus-tmp-name
2668           (cond
2669            ((string-match "<[^>]+> *$" gnus-tmp-from)
2670             (let ((beg (match-beginning 0)))
2671               (or (and (string-match "^\".+\"" gnus-tmp-from)
2672                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2673                   (substring gnus-tmp-from 0 beg))))
2674            ((string-match "(.+)" gnus-tmp-from)
2675             (substring gnus-tmp-from
2676                        (1+ (match-beginning 0)) (1- (match-end 0))))
2677            (t gnus-tmp-from)))
2678          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2679          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2680          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2681          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2682          (buffer-read-only nil))
2683     (when (string= gnus-tmp-name "")
2684       (setq gnus-tmp-name gnus-tmp-from))
2685     (unless (numberp gnus-tmp-lines)
2686       (setq gnus-tmp-lines 0))
2687     (gnus-put-text-property
2688      (point)
2689      (progn (eval gnus-summary-line-format-spec) (point))
2690      'gnus-number gnus-tmp-number)
2691     (when (gnus-visual-p 'summary-highlight 'highlight)
2692       (forward-line -1)
2693       (gnus-run-hooks 'gnus-summary-update-hook)
2694       (forward-line 1))))
2695
2696 (defun gnus-summary-update-line (&optional dont-update)
2697   "Update summary line after change."
2698   (when (and gnus-summary-default-score
2699              (not gnus-summary-inhibit-highlight))
2700     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2701            (article (gnus-summary-article-number))
2702            (score (gnus-summary-article-score article)))
2703       (unless dont-update
2704         (if (and gnus-summary-mark-below
2705                  (< (gnus-summary-article-score)
2706                     gnus-summary-mark-below))
2707             ;; This article has a low score, so we mark it as read.
2708             (when (memq article gnus-newsgroup-unreads)
2709               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2710           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2711             ;; This article was previously marked as read on account
2712             ;; of a low score, but now it has risen, so we mark it as
2713             ;; unread.
2714             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2715         (gnus-summary-update-mark
2716          (if (or (null gnus-summary-default-score)
2717                  (<= (abs (- score gnus-summary-default-score))
2718                      gnus-summary-zcore-fuzz))
2719              ?  ;Whitespace
2720            (if (< score gnus-summary-default-score)
2721                gnus-score-below-mark gnus-score-over-mark))
2722          'score))
2723       ;; Do visual highlighting.
2724       (when (gnus-visual-p 'summary-highlight 'highlight)
2725         (gnus-run-hooks 'gnus-summary-update-hook)))))
2726
2727 (defvar gnus-tmp-new-adopts nil)
2728
2729 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2730   "Return the number of articles in THREAD.
2731 This may be 0 in some cases -- if none of the articles in
2732 the thread are to be displayed."
2733   (let* ((number
2734           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2735           (cond
2736            ((not (listp thread))
2737             1)
2738            ((and (consp thread) (cdr thread))
2739             (apply
2740              '+ 1 (mapcar
2741                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2742            ((null thread)
2743             1)
2744            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2745             1)
2746            (t 0))))
2747     (when (and level (zerop level) gnus-tmp-new-adopts)
2748       (incf number
2749             (apply '+ (mapcar
2750                        'gnus-summary-number-of-articles-in-thread
2751                        gnus-tmp-new-adopts))))
2752     (if char
2753         (if (> number 1) gnus-not-empty-thread-mark
2754           gnus-empty-thread-mark)
2755       number)))
2756
2757 (defun gnus-summary-set-local-parameters (group)
2758   "Go through the local params of GROUP and set all variable specs in that list."
2759   (let ((params (gnus-group-find-parameter group))
2760         elem)
2761     (while params
2762       (setq elem (car params)
2763             params (cdr params))
2764       (and (consp elem)                 ; Has to be a cons.
2765            (consp (cdr elem))           ; The cdr has to be a list.
2766            (symbolp (car elem))         ; Has to be a symbol in there.
2767            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2768            (ignore-errors               ; So we set it.
2769              (make-local-variable (car elem))
2770              (set (car elem) (eval (nth 1 elem))))))))
2771
2772 (defun gnus-summary-read-group (group &optional show-all no-article
2773                                       kill-buffer no-display backward
2774                                       select-articles)
2775   "Start reading news in newsgroup GROUP.
2776 If SHOW-ALL is non-nil, already read articles are also listed.
2777 If NO-ARTICLE is non-nil, no article is selected initially.
2778 If NO-DISPLAY, don't generate a summary buffer."
2779   (let (result)
2780     (while (and group
2781                 (null (setq result
2782                             (let ((gnus-auto-select-next nil))
2783                               (or (gnus-summary-read-group-1
2784                                    group show-all no-article
2785                                    kill-buffer no-display
2786                                    select-articles)
2787                                   (setq show-all nil
2788                                         select-articles nil)))))
2789                 (eq gnus-auto-select-next 'quietly))
2790       (set-buffer gnus-group-buffer)
2791       ;; The entry function called above goes to the next
2792       ;; group automatically, so we go two groups back
2793       ;; if we are searching for the previous group.
2794       (when backward
2795         (gnus-group-prev-unread-group 2))
2796       (if (not (equal group (gnus-group-group-name)))
2797           (setq group (gnus-group-group-name))
2798         (setq group nil)))
2799     result))
2800
2801 (defun gnus-summary-read-group-1 (group show-all no-article
2802                                         kill-buffer no-display
2803                                         &optional select-articles)
2804   ;; Killed foreign groups can't be entered.
2805   (when (and (not (gnus-group-native-p group))
2806              (not (gnus-gethash group gnus-newsrc-hashtb)))
2807     (error "Dead non-native groups can't be entered"))
2808   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2809   (let* ((new-group (gnus-summary-setup-buffer group))
2810          (quit-config (gnus-group-quit-config group))
2811          (did-select (and new-group (gnus-select-newsgroup
2812                                      group show-all select-articles))))
2813     (cond
2814      ;; This summary buffer exists already, so we just select it.
2815      ((not new-group)
2816       (gnus-set-global-variables)
2817       (when kill-buffer
2818         (gnus-kill-or-deaden-summary kill-buffer))
2819       (gnus-configure-windows 'summary 'force)
2820       (gnus-set-mode-line 'summary)
2821       (gnus-summary-position-point)
2822       (message "")
2823       t)
2824      ;; We couldn't select this group.
2825      ((null did-select)
2826       (when (and (eq major-mode 'gnus-summary-mode)
2827                  (not (equal (current-buffer) kill-buffer)))
2828         (kill-buffer (current-buffer))
2829         (if (not quit-config)
2830             (progn
2831               ;; Update the info -- marks might need to be removed,
2832               ;; for instance.
2833               (gnus-summary-update-info)
2834               (set-buffer gnus-group-buffer)
2835               (gnus-group-jump-to-group group)
2836               (gnus-group-next-unread-group 1))
2837           (gnus-handle-ephemeral-exit quit-config)))
2838       (gnus-message 3 "Can't select group")
2839       nil)
2840      ;; The user did a `C-g' while prompting for number of articles,
2841      ;; so we exit this group.
2842      ((eq did-select 'quit)
2843       (and (eq major-mode 'gnus-summary-mode)
2844            (not (equal (current-buffer) kill-buffer))
2845            (kill-buffer (current-buffer)))
2846       (when kill-buffer
2847         (gnus-kill-or-deaden-summary kill-buffer))
2848       (if (not quit-config)
2849           (progn
2850             (set-buffer gnus-group-buffer)
2851             (gnus-group-jump-to-group group)
2852             (gnus-group-next-unread-group 1)
2853             (gnus-configure-windows 'group 'force))
2854         (gnus-handle-ephemeral-exit quit-config))
2855       ;; Finally signal the quit.
2856       (signal 'quit nil))
2857      ;; The group was successfully selected.
2858      (t
2859       (gnus-set-global-variables)
2860       ;; Save the active value in effect when the group was entered.
2861       (setq gnus-newsgroup-active
2862             (gnus-copy-sequence
2863              (gnus-active gnus-newsgroup-name)))
2864       ;; You can change the summary buffer in some way with this hook.
2865       (gnus-run-hooks 'gnus-select-group-hook)
2866       ;; Set any local variables in the group parameters.
2867       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2868       (gnus-update-format-specifications
2869        nil 'summary 'summary-mode 'summary-dummy)
2870       (gnus-update-summary-mark-positions)
2871       ;; Do score processing.
2872       (when gnus-use-scoring
2873         (gnus-possibly-score-headers))
2874       ;; Check whether to fill in the gaps in the threads.
2875       (when gnus-build-sparse-threads
2876         (gnus-build-sparse-threads))
2877       ;; Find the initial limit.
2878       (if gnus-show-threads
2879           (if show-all
2880               (let ((gnus-newsgroup-dormant nil))
2881                 (gnus-summary-initial-limit show-all))
2882             (gnus-summary-initial-limit show-all))
2883         ;; When untreaded, all articles are always shown.
2884         (setq gnus-newsgroup-limit
2885               (mapcar
2886                (lambda (header) (mail-header-number header))
2887                gnus-newsgroup-headers)))
2888       ;; Generate the summary buffer.
2889       (unless no-display
2890         (gnus-summary-prepare))
2891       (when gnus-use-trees
2892         (gnus-tree-open group)
2893         (setq gnus-summary-highlight-line-function
2894               'gnus-tree-highlight-article))
2895       ;; If the summary buffer is empty, but there are some low-scored
2896       ;; articles or some excluded dormants, we include these in the
2897       ;; buffer.
2898       (when (and (zerop (buffer-size))
2899                  (not no-display))
2900         (cond (gnus-newsgroup-dormant
2901                (gnus-summary-limit-include-dormant))
2902               ((and gnus-newsgroup-scored show-all)
2903                (gnus-summary-limit-include-expunged t))))
2904       ;; Function `gnus-apply-kill-file' must be called in this hook.
2905       (gnus-run-hooks 'gnus-apply-kill-hook)
2906       (if (and (zerop (buffer-size))
2907                (not no-display))
2908           (progn
2909             ;; This newsgroup is empty.
2910             (gnus-summary-catchup-and-exit nil t)
2911             (gnus-message 6 "No unread news")
2912             (when kill-buffer
2913               (gnus-kill-or-deaden-summary kill-buffer))
2914             ;; Return nil from this function.
2915             nil)
2916         ;; Hide conversation thread subtrees.  We cannot do this in
2917         ;; gnus-summary-prepare-hook since kill processing may not
2918         ;; work with hidden articles.
2919         (and gnus-show-threads
2920              gnus-thread-hide-subtree
2921              (gnus-summary-hide-all-threads))
2922         (when kill-buffer
2923           (gnus-kill-or-deaden-summary kill-buffer))
2924         ;; Show first unread article if requested.
2925         (if (and (not no-article)
2926                  (not no-display)
2927                  gnus-newsgroup-unreads
2928                  gnus-auto-select-first)
2929             (progn
2930               (gnus-configure-windows 'summary)
2931               (cond
2932                ((eq gnus-auto-select-first 'best)
2933                 (gnus-summary-best-unread-article))
2934                ((eq gnus-auto-select-first t)
2935                 (gnus-summary-first-unread-article))
2936                ((gnus-functionp gnus-auto-select-first)
2937                 (funcall gnus-auto-select-first))))
2938           ;; Don't select any articles, just move point to the first
2939           ;; article in the group.
2940           (goto-char (point-min))
2941           (gnus-summary-position-point)
2942           (gnus-configure-windows 'summary 'force)
2943           (gnus-set-mode-line 'summary))
2944         (when (get-buffer-window gnus-group-buffer t)
2945           ;; Gotta use windows, because recenter does weird stuff if
2946           ;; the current buffer ain't the displayed window.
2947           (let ((owin (selected-window)))
2948             (select-window (get-buffer-window gnus-group-buffer t))
2949             (when (gnus-group-goto-group group)
2950               (recenter))
2951             (select-window owin)))
2952         ;; Mark this buffer as "prepared".
2953         (setq gnus-newsgroup-prepared t)
2954         (gnus-run-hooks 'gnus-summary-prepared-hook)
2955         t)))))
2956
2957 (defun gnus-summary-prepare ()
2958   "Generate the summary buffer."
2959   (interactive)
2960   (let ((buffer-read-only nil))
2961     (erase-buffer)
2962     (setq gnus-newsgroup-data nil
2963           gnus-newsgroup-data-reverse nil)
2964     (gnus-run-hooks 'gnus-summary-generate-hook)
2965     ;; Generate the buffer, either with threads or without.
2966     (when gnus-newsgroup-headers
2967       (gnus-summary-prepare-threads
2968        (if gnus-show-threads
2969            (gnus-sort-gathered-threads
2970             (funcall gnus-summary-thread-gathering-function
2971                      (gnus-sort-threads
2972                       (gnus-cut-threads (gnus-make-threads)))))
2973          ;; Unthreaded display.
2974          (gnus-sort-articles gnus-newsgroup-headers))))
2975     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2976     ;; Call hooks for modifying summary buffer.
2977     (goto-char (point-min))
2978     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2979
2980 (defsubst gnus-general-simplify-subject (subject)
2981   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2982   (setq subject
2983         (cond
2984          ;; Truncate the subject.
2985          (gnus-simplify-subject-functions
2986           (gnus-map-function gnus-simplify-subject-functions subject))
2987          ((numberp gnus-summary-gather-subject-limit)
2988           (setq subject (gnus-simplify-subject-re subject))
2989           (if (> (length subject) gnus-summary-gather-subject-limit)
2990               (substring subject 0 gnus-summary-gather-subject-limit)
2991             subject))
2992          ;; Fuzzily simplify it.
2993          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2994           (gnus-simplify-subject-fuzzy subject))
2995          ;; Just remove the leading "Re:".
2996          (t
2997           (gnus-simplify-subject-re subject))))
2998
2999   (if (and gnus-summary-gather-exclude-subject
3000            (string-match gnus-summary-gather-exclude-subject subject))
3001       nil                               ; This article shouldn't be gathered
3002     subject))
3003
3004 (defun gnus-summary-simplify-subject-query ()
3005   "Query where the respool algorithm would put this article."
3006   (interactive)
3007   (gnus-summary-select-article)
3008   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3009
3010 (defun gnus-gather-threads-by-subject (threads)
3011   "Gather threads by looking at Subject headers."
3012   (if (not gnus-summary-make-false-root)
3013       threads
3014     (let ((hashtb (gnus-make-hashtable 1024))
3015           (prev threads)
3016           (result threads)
3017           subject hthread whole-subject)
3018       (while threads
3019         (setq subject (gnus-general-simplify-subject
3020                        (setq whole-subject (mail-header-subject
3021                                             (caar threads)))))
3022         (when subject
3023           (if (setq hthread (gnus-gethash subject hashtb))
3024               (progn
3025                 ;; We enter a dummy root into the thread, if we
3026                 ;; haven't done that already.
3027                 (unless (stringp (caar hthread))
3028                   (setcar hthread (list whole-subject (car hthread))))
3029                 ;; We add this new gathered thread to this gathered
3030                 ;; thread.
3031                 (setcdr (car hthread)
3032                         (nconc (cdar hthread) (list (car threads))))
3033                 ;; Remove it from the list of threads.
3034                 (setcdr prev (cdr threads))
3035                 (setq threads prev))
3036             ;; Enter this thread into the hash table.
3037             (gnus-sethash subject threads hashtb)))
3038         (setq prev threads)
3039         (setq threads (cdr threads)))
3040       result)))
3041
3042 (defun gnus-gather-threads-by-references (threads)
3043   "Gather threads by looking at References headers."
3044   (let ((idhashtb (gnus-make-hashtable 1024))
3045         (thhashtb (gnus-make-hashtable 1024))
3046         (prev threads)
3047         (result threads)
3048         ids references id gthread gid entered ref)
3049     (while threads
3050       (when (setq references (mail-header-references (caar threads)))
3051         (setq id (mail-header-id (caar threads))
3052               ids (gnus-split-references references)
3053               entered nil)
3054         (while (setq ref (pop ids))
3055           (setq ids (delete ref ids))
3056           (if (not (setq gid (gnus-gethash ref idhashtb)))
3057               (progn
3058                 (gnus-sethash ref id idhashtb)
3059                 (gnus-sethash id threads thhashtb))
3060             (setq gthread (gnus-gethash gid thhashtb))
3061             (unless entered
3062               ;; We enter a dummy root into the thread, if we
3063               ;; haven't done that already.
3064               (unless (stringp (caar gthread))
3065                 (setcar gthread (list (mail-header-subject (caar gthread))
3066                                       (car gthread))))
3067               ;; We add this new gathered thread to this gathered
3068               ;; thread.
3069               (setcdr (car gthread)
3070                       (nconc (cdar gthread) (list (car threads)))))
3071             ;; Add it into the thread hash table.
3072             (gnus-sethash id gthread thhashtb)
3073             (setq entered t)
3074             ;; Remove it from the list of threads.
3075             (setcdr prev (cdr threads))
3076             (setq threads prev))))
3077       (setq prev threads)
3078       (setq threads (cdr threads)))
3079     result))
3080
3081 (defun gnus-sort-gathered-threads (threads)
3082   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3083   (let ((result threads))
3084     (while threads
3085       (when (stringp (caar threads))
3086         (setcdr (car threads)
3087                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3088       (setq threads (cdr threads)))
3089     result))
3090
3091 (defun gnus-thread-loop-p (root thread)
3092   "Say whether ROOT is in THREAD."
3093   (let ((stack (list thread))
3094         (infloop 0)
3095         th)
3096     (while (setq thread (pop stack))
3097       (setq th (cdr thread))
3098       (while (and th
3099                   (not (eq (caar th) root)))
3100         (pop th))
3101       (if th
3102           ;; We have found a loop.
3103           (let (ref-dep)
3104             (setcdr thread (delq (car th) (cdr thread)))
3105             (if (boundp (setq ref-dep (intern "none"
3106                                               gnus-newsgroup-dependencies)))
3107                 (setcdr (symbol-value ref-dep)
3108                         (nconc (cdr (symbol-value ref-dep))
3109                                (list (car th))))
3110               (set ref-dep (list nil (car th))))
3111             (setq infloop 1
3112                   stack nil))
3113         ;; Push all the subthreads onto the stack.
3114         (push (cdr thread) stack)))
3115     infloop))
3116
3117 (defun gnus-make-threads ()
3118   "Go through the dependency hashtb and find the roots.  Return all threads."
3119   (let (threads)
3120     (while (catch 'infloop
3121              (mapatoms
3122               (lambda (refs)
3123                 ;; Deal with self-referencing References loops.
3124                 (when (and (car (symbol-value refs))
3125                            (not (zerop
3126                                  (apply
3127                                   '+
3128                                   (mapcar
3129                                    (lambda (thread)
3130                                      (gnus-thread-loop-p
3131                                       (car (symbol-value refs)) thread))
3132                                    (cdr (symbol-value refs)))))))
3133                   (setq threads nil)
3134                   (throw 'infloop t))
3135                 (unless (car (symbol-value refs))
3136                   ;; These threads do not refer back to any other articles,
3137                   ;; so they're roots.
3138                   (setq threads (append (cdr (symbol-value refs)) threads))))
3139               gnus-newsgroup-dependencies)))
3140     threads))
3141
3142 ;; Build the thread tree.
3143 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3144   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3145
3146 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3147 if it was already present.
3148
3149 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3150 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3151 Message-IDs will be renamed be renamed to a unique Message-ID before
3152 being entered.
3153
3154 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3155   (let* ((id (mail-header-id header))
3156          (id-dep (and id (intern id dependencies)))
3157          ref ref-dep ref-header)
3158     ;; Enter this `header' in the `dependencies' table.
3159     (cond
3160      ((not id-dep)
3161       (setq header nil))
3162      ;; The first two cases do the normal part: enter a new `header'
3163      ;; in the `dependencies' table.
3164      ((not (boundp id-dep))
3165       (set id-dep (list header)))
3166      ((null (car (symbol-value id-dep)))
3167       (setcar (symbol-value id-dep) header))
3168
3169      ;; From here the `header' was already present in the
3170      ;; `dependencies' table.
3171      (force-new
3172       ;; Overrides an existing entry;
3173       ;; just set the header part of the entry.
3174       (setcar (symbol-value id-dep) header))
3175
3176      ;; Renames the existing `header' to a unique Message-ID.
3177      ((not gnus-summary-ignore-duplicates)
3178       ;; An article with this Message-ID has already been seen.
3179       ;; We rename the Message-ID.
3180       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3181            (list header))
3182       (mail-header-set-id header id))
3183
3184      ;; The last case ignores an existing entry, except it adds any
3185      ;; additional Xrefs (in case the two articles came from different
3186      ;; servers.
3187      ;; Also sets `header' to `nil' meaning that the `dependencies'
3188      ;; table was *not* modified.
3189      (t
3190       (mail-header-set-xref
3191        (car (symbol-value id-dep))
3192        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3193                    "")
3194                (or (mail-header-xref header) "")))
3195       (setq header nil)))
3196
3197     (when header
3198       ;; First check if that we are not creating a References loop.
3199       (setq ref (gnus-parent-id (mail-header-references header)))
3200       (while (and ref
3201                   (setq ref-dep (intern-soft ref dependencies))
3202                   (boundp ref-dep)
3203                   (setq ref-header (car (symbol-value ref-dep))))
3204         (if (string= id ref)
3205             ;; Yuk!  This is a reference loop.  Make the article be a
3206             ;; root article.
3207             (progn
3208               (mail-header-set-references (car (symbol-value id-dep)) "none")
3209               (setq ref nil))
3210           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3211       (setq ref (gnus-parent-id (mail-header-references header)))
3212       (setq ref-dep (intern (or ref "none") dependencies))
3213       (if (boundp ref-dep)
3214           (setcdr (symbol-value ref-dep)
3215                   (nconc (cdr (symbol-value ref-dep))
3216                          (list (symbol-value id-dep))))
3217         (set ref-dep (list nil (symbol-value id-dep)))))
3218     header))
3219
3220 (defun gnus-build-sparse-threads ()
3221   (let ((headers gnus-newsgroup-headers)
3222         (mail-parse-charset gnus-newsgroup-charset)
3223         (gnus-summary-ignore-duplicates t)
3224         header references generation relations
3225         subject child end new-child date)
3226     ;; First we create an alist of generations/relations, where
3227     ;; generations is how much we trust the relation, and the relation
3228     ;; is parent/child.
3229     (gnus-message 7 "Making sparse threads...")
3230     (save-excursion
3231       (nnheader-set-temp-buffer " *gnus sparse threads*")
3232       (while (setq header (pop headers))
3233         (when (and (setq references (mail-header-references header))
3234                    (not (string= references "")))
3235           (insert references)
3236           (setq child (mail-header-id header)
3237                 subject (mail-header-subject header)
3238                 date (mail-header-date header)
3239                 generation 0)
3240           (while (search-backward ">" nil t)
3241             (setq end (1+ (point)))
3242             (when (search-backward "<" nil t)
3243               (setq new-child (buffer-substring (point) end))
3244               (push (list (incf generation)
3245                           child (setq child new-child)
3246                           subject date)
3247                     relations)))
3248           (when child
3249             (push (list (1+ generation) child nil subject) relations))
3250           (erase-buffer)))
3251       (kill-buffer (current-buffer)))
3252     ;; Sort over trustworthiness.
3253     (mapcar
3254      (lambda (relation)
3255        (when (gnus-dependencies-add-header
3256               (make-full-mail-header
3257                gnus-reffed-article-number
3258                (nth 3 relation) "" (or (nth 4 relation) "")
3259                (nth 1 relation)
3260                (or (nth 2 relation) "") 0 0 "")
3261               gnus-newsgroup-dependencies nil)
3262          (push gnus-reffed-article-number gnus-newsgroup-limit)
3263          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3264          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3265                gnus-newsgroup-reads)
3266          (decf gnus-reffed-article-number)))
3267      (sort relations 'car-less-than-car))
3268     (gnus-message 7 "Making sparse threads...done")))
3269
3270 (defun gnus-build-old-threads ()
3271   ;; Look at all the articles that refer back to old articles, and
3272   ;; fetch the headers for the articles that aren't there.  This will
3273   ;; build complete threads - if the roots haven't been expired by the
3274   ;; server, that is.
3275   (let ((mail-parse-charset gnus-newsgroup-charset)
3276         id heads)
3277     (mapatoms
3278      (lambda (refs)
3279        (when (not (car (symbol-value refs)))
3280          (setq heads (cdr (symbol-value refs)))
3281          (while heads
3282            (if (memq (mail-header-number (caar heads))
3283                      gnus-newsgroup-dormant)
3284                (setq heads (cdr heads))
3285              (setq id (symbol-name refs))
3286              (while (and (setq id (gnus-build-get-header id))
3287                          (not (car (gnus-id-to-thread id)))))
3288              (setq heads nil)))))
3289      gnus-newsgroup-dependencies)))
3290
3291 ;; This function has to be called with point after the article number
3292 ;; on the beginning of the line.
3293 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3294   (let ((eol (gnus-point-at-eol))
3295         (buffer (current-buffer))
3296         header)
3297
3298     ;; overview: [num subject from date id refs chars lines misc]
3299     (unwind-protect
3300         (progn
3301           (narrow-to-region (point) eol)
3302           (unless (eobp)
3303             (forward-char))
3304
3305           (setq header
3306                 (make-full-mail-header
3307                  number                 ; number
3308                  (funcall gnus-decode-encoded-word-function
3309                           (nnheader-nov-field)) ; subject
3310                  (funcall gnus-decode-encoded-word-function
3311                           (nnheader-nov-field)) ; from
3312                  (nnheader-nov-field)   ; date
3313                  (nnheader-nov-read-message-id) ; id
3314                  (nnheader-nov-field)   ; refs
3315                  (nnheader-nov-read-integer) ; chars
3316                  (nnheader-nov-read-integer) ; lines
3317                  (unless (eobp)
3318                    (if (looking-at "Xref: ")
3319                        (goto-char (match-end 0)))
3320                    (nnheader-nov-field)) ; Xref
3321                  (nnheader-nov-parse-extra)))) ; extra
3322
3323       (widen))
3324
3325     (when gnus-alter-header-function
3326       (funcall gnus-alter-header-function header))
3327     (gnus-dependencies-add-header header dependencies force-new)))
3328
3329 (defun gnus-build-get-header (id)
3330   "Look through the buffer of NOV lines and find the header to ID.
3331 Enter this line into the dependencies hash table, and return
3332 the id of the parent article (if any)."
3333   (let ((deps gnus-newsgroup-dependencies)
3334         found header)
3335     (prog1
3336         (save-excursion
3337           (set-buffer nntp-server-buffer)
3338           (let ((case-fold-search nil))
3339             (goto-char (point-min))
3340             (while (and (not found)
3341                         (search-forward id nil t))
3342               (beginning-of-line)
3343               (setq found (looking-at
3344                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3345                                    (regexp-quote id))))
3346               (or found (beginning-of-line 2)))
3347             (when found
3348               (beginning-of-line)
3349               (and
3350                (setq header (gnus-nov-parse-line
3351                              (read (current-buffer)) deps))
3352                (gnus-parent-id (mail-header-references header))))))
3353       (when header
3354         (let ((number (mail-header-number header)))
3355           (push number gnus-newsgroup-limit)
3356           (push header gnus-newsgroup-headers)
3357           (if (memq number gnus-newsgroup-unselected)
3358               (progn
3359                 (push number gnus-newsgroup-unreads)
3360                 (setq gnus-newsgroup-unselected
3361                       (delq number gnus-newsgroup-unselected)))
3362             (push number gnus-newsgroup-ancient)))))))
3363
3364 (defun gnus-build-all-threads ()
3365   "Read all the headers."
3366   (let ((gnus-summary-ignore-duplicates t)
3367         (mail-parse-charset gnus-newsgroup-charset)
3368         (dependencies gnus-newsgroup-dependencies)
3369         header article)
3370     (save-excursion
3371       (set-buffer nntp-server-buffer)
3372       (let ((case-fold-search nil))
3373         (goto-char (point-min))
3374         (while (not (eobp))
3375           (ignore-errors
3376             (setq article (read (current-buffer))
3377                   header (gnus-nov-parse-line article dependencies)))
3378           (when header
3379             (save-excursion
3380               (set-buffer gnus-summary-buffer)
3381               (push header gnus-newsgroup-headers)
3382               (if (memq (setq article (mail-header-number header))
3383                         gnus-newsgroup-unselected)
3384                   (progn
3385                     (push article gnus-newsgroup-unreads)
3386                     (setq gnus-newsgroup-unselected
3387                           (delq article gnus-newsgroup-unselected)))
3388                 (push article gnus-newsgroup-ancient)))
3389             (forward-line 1)))))))
3390
3391 (defun gnus-summary-update-article-line (article header)
3392   "Update the line for ARTICLE using HEADERS."
3393   (let* ((id (mail-header-id header))
3394          (thread (gnus-id-to-thread id)))
3395     (unless thread
3396       (error "Article in no thread"))
3397     ;; Update the thread.
3398     (setcar thread header)
3399     (gnus-summary-goto-subject article)
3400     (let* ((datal (gnus-data-find-list article))
3401            (data (car datal))
3402            (length (when (cdr datal)
3403                      (- (gnus-data-pos data)
3404                         (gnus-data-pos (cadr datal)))))
3405            (buffer-read-only nil)
3406            (level (gnus-summary-thread-level)))
3407       (gnus-delete-line)
3408       (gnus-summary-insert-line
3409        header level nil (gnus-article-mark article)
3410        (memq article gnus-newsgroup-replied)
3411        (memq article gnus-newsgroup-expirable)
3412        ;; Only insert the Subject string when it's different
3413        ;; from the previous Subject string.
3414        (if (and
3415             gnus-show-threads
3416             (gnus-subject-equal
3417              (condition-case ()
3418                  (mail-header-subject
3419                   (gnus-data-header
3420                    (cadr
3421                     (gnus-data-find-list
3422                      article
3423                      (gnus-data-list t)))))
3424                ;; Error on the side of excessive subjects.
3425                (error ""))
3426              (mail-header-subject header)))
3427            ""
3428          (mail-header-subject header))
3429        nil (cdr (assq article gnus-newsgroup-scored))
3430        (memq article gnus-newsgroup-processable))
3431       (when length
3432         (gnus-data-update-list
3433          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3434
3435 (defun gnus-summary-update-article (article &optional iheader)
3436   "Update ARTICLE in the summary buffer."
3437   (set-buffer gnus-summary-buffer)
3438   (let* ((header (gnus-summary-article-header article))
3439          (id (mail-header-id header))
3440          (data (gnus-data-find article))
3441          (thread (gnus-id-to-thread id))
3442          (references (mail-header-references header))
3443          (parent
3444           (gnus-id-to-thread
3445            (or (gnus-parent-id
3446                 (when (and references
3447                            (not (equal "" references)))
3448                   references))
3449                "none")))
3450          (buffer-read-only nil)
3451          (old (car thread)))
3452     (when thread
3453       (unless iheader
3454         (setcar thread nil)
3455         (when parent
3456           (delq thread parent)))
3457       (if (gnus-summary-insert-subject id header)
3458           ;; Set the (possibly) new article number in the data structure.
3459           (gnus-data-set-number data (gnus-id-to-article id))
3460         (setcar thread old)
3461         nil))))
3462
3463 (defun gnus-rebuild-thread (id &optional line)
3464   "Rebuild the thread containing ID.
3465 If LINE, insert the rebuilt thread starting on line LINE."
3466   (let ((buffer-read-only nil)
3467         old-pos current thread data)
3468     (if (not gnus-show-threads)
3469         (setq thread (list (car (gnus-id-to-thread id))))
3470       ;; Get the thread this article is part of.
3471       (setq thread (gnus-remove-thread id)))
3472     (setq old-pos (gnus-point-at-bol))
3473     (setq current (save-excursion
3474                     (and (zerop (forward-line -1))
3475                          (gnus-summary-article-number))))
3476     ;; If this is a gathered thread, we have to go some re-gathering.
3477     (when (stringp (car thread))
3478       (let ((subject (car thread))
3479             roots thr)
3480         (setq thread (cdr thread))
3481         (while thread
3482           (unless (memq (setq thr (gnus-id-to-thread
3483                                    (gnus-root-id
3484                                     (mail-header-id (caar thread)))))
3485                         roots)
3486             (push thr roots))
3487           (setq thread (cdr thread)))
3488         ;; We now have all (unique) roots.
3489         (if (= (length roots) 1)
3490             ;; All the loose roots are now one solid root.
3491             (setq thread (car roots))
3492           (setq thread (cons subject (gnus-sort-threads roots))))))
3493     (let (threads)
3494       ;; We then insert this thread into the summary buffer.
3495       (when line
3496         (goto-char (point-min))
3497         (forward-line (1- line)))
3498       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3499         (if gnus-show-threads
3500             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3501           (gnus-summary-prepare-unthreaded thread))
3502         (setq data (nreverse gnus-newsgroup-data))
3503         (setq threads gnus-newsgroup-threads))
3504       ;; We splice the new data into the data structure.
3505       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3506       ;;!!! then we want to insert at the beginning of the buffer.
3507       ;;!!! That happens to be true with Gnus now, but that may
3508       ;;!!! change in the future.  Perhaps.
3509       (gnus-data-enter-list
3510        (if line nil current) data (- (point) old-pos))
3511       (setq gnus-newsgroup-threads
3512             (nconc threads gnus-newsgroup-threads))
3513       (gnus-data-compute-positions))))
3514
3515 (defun gnus-number-to-header (number)
3516   "Return the header for article NUMBER."
3517   (let ((headers gnus-newsgroup-headers))
3518     (while (and headers
3519                 (not (= number (mail-header-number (car headers)))))
3520       (pop headers))
3521     (when headers
3522       (car headers))))
3523
3524 (defun gnus-parent-headers (in-headers &optional generation)
3525   "Return the headers of the GENERATIONeth parent of HEADERS."
3526   (unless generation
3527     (setq generation 1))
3528   (let ((parent t)
3529         (headers in-headers)
3530         references)
3531     (while (and parent
3532                 (not (zerop generation))
3533                 (setq references (mail-header-references headers)))
3534       (setq headers (if (and references
3535                              (setq parent (gnus-parent-id references)))
3536                         (car (gnus-id-to-thread parent))
3537                       nil))
3538       (decf generation))
3539     (and (not (eq headers in-headers))
3540          headers)))
3541
3542 (defun gnus-id-to-thread (id)
3543   "Return the (sub-)thread where ID appears."
3544   (gnus-gethash id gnus-newsgroup-dependencies))
3545
3546 (defun gnus-id-to-article (id)
3547   "Return the article number of ID."
3548   (let ((thread (gnus-id-to-thread id)))
3549     (when (and thread
3550                (car thread))
3551       (mail-header-number (car thread)))))
3552
3553 (defun gnus-id-to-header (id)
3554   "Return the article headers of ID."
3555   (car (gnus-id-to-thread id)))
3556
3557 (defun gnus-article-displayed-root-p (article)
3558   "Say whether ARTICLE is a root(ish) article."
3559   (let ((level (gnus-summary-thread-level article))
3560         (refs (mail-header-references  (gnus-summary-article-header article)))
3561         particle)
3562     (cond
3563      ((null level) nil)
3564      ((zerop level) t)
3565      ((null refs) t)
3566      ((null (gnus-parent-id refs)) t)
3567      ((and (= 1 level)
3568            (null (setq particle (gnus-id-to-article
3569                                  (gnus-parent-id refs))))
3570            (null (gnus-summary-thread-level particle)))))))
3571
3572 (defun gnus-root-id (id)
3573   "Return the id of the root of the thread where ID appears."
3574   (let (last-id prev)
3575     (while (and id (setq prev (car (gnus-id-to-thread id))))
3576       (setq last-id id
3577             id (gnus-parent-id (mail-header-references prev))))
3578     last-id))
3579
3580 (defun gnus-articles-in-thread (thread)
3581   "Return the list of articles in THREAD."
3582   (cons (mail-header-number (car thread))
3583         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3584
3585 (defun gnus-remove-thread (id &optional dont-remove)
3586   "Remove the thread that has ID in it."
3587   (let (headers thread last-id)
3588     ;; First go up in this thread until we find the root.
3589     (setq last-id (gnus-root-id id)
3590           headers (message-flatten-list (gnus-id-to-thread last-id)))
3591     ;; We have now found the real root of this thread.  It might have
3592     ;; been gathered into some loose thread, so we have to search
3593     ;; through the threads to find the thread we wanted.
3594     (let ((threads gnus-newsgroup-threads)
3595           sub)
3596       (while threads
3597         (setq sub (car threads))
3598         (if (stringp (car sub))
3599             ;; This is a gathered thread, so we look at the roots
3600             ;; below it to find whether this article is in this
3601             ;; gathered root.
3602             (progn
3603               (setq sub (cdr sub))
3604               (while sub
3605                 (when (member (caar sub) headers)
3606                   (setq thread (car threads)
3607                         threads nil
3608                         sub nil))
3609                 (setq sub (cdr sub))))
3610           ;; It's an ordinary thread, so we check it.
3611           (when (eq (car sub) (car headers))
3612             (setq thread sub
3613                   threads nil)))
3614         (setq threads (cdr threads)))
3615       ;; If this article is in no thread, then it's a root.
3616       (if thread
3617           (unless dont-remove
3618             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3619         (setq thread (gnus-id-to-thread last-id)))
3620       (when thread
3621         (prog1
3622             thread                      ; We return this thread.
3623           (unless dont-remove
3624             (if (stringp (car thread))
3625                 (progn
3626                   ;; If we use dummy roots, then we have to remove the
3627                   ;; dummy root as well.
3628                   (when (eq gnus-summary-make-false-root 'dummy)
3629                     ;; We go to the dummy root by going to
3630                     ;; the first sub-"thread", and then one line up.
3631                     (gnus-summary-goto-article
3632                      (mail-header-number (caadr thread)))
3633                     (forward-line -1)
3634                     (gnus-delete-line)
3635                     (gnus-data-compute-positions))
3636                   (setq thread (cdr thread))
3637                   (while thread
3638                     (gnus-remove-thread-1 (car thread))
3639                     (setq thread (cdr thread))))
3640               (gnus-remove-thread-1 thread))))))))
3641
3642 (defun gnus-remove-thread-1 (thread)
3643   "Remove the thread THREAD recursively."
3644   (let ((number (mail-header-number (pop thread)))
3645         d)
3646     (setq thread (reverse thread))
3647     (while thread
3648       (gnus-remove-thread-1 (pop thread)))
3649     (when (setq d (gnus-data-find number))
3650       (goto-char (gnus-data-pos d))
3651       (gnus-summary-show-thread)
3652       (gnus-data-remove
3653        number
3654        (- (gnus-point-at-bol)
3655           (prog1
3656               (1+ (gnus-point-at-eol))
3657             (gnus-delete-line)))))))
3658
3659 (defun gnus-sort-threads-1 (threads func)
3660   (sort (mapcar (lambda (thread)
3661                   (cons (car thread)
3662                         (and (cdr thread)
3663                              (gnus-sort-threads-1 (cdr thread) func))))
3664                 threads) func))
3665
3666 (defun gnus-sort-threads (threads)
3667   "Sort THREADS."
3668   (if (not gnus-thread-sort-functions)
3669       threads
3670     (gnus-message 8 "Sorting threads...")
3671     (prog1
3672         (gnus-sort-threads-1 
3673          threads 
3674          (gnus-make-sort-function gnus-thread-sort-functions))
3675       (gnus-message 8 "Sorting threads...done"))))
3676
3677 (defun gnus-sort-articles (articles)
3678   "Sort ARTICLES."
3679   (when gnus-article-sort-functions
3680     (gnus-message 7 "Sorting articles...")
3681     (prog1
3682         (setq gnus-newsgroup-headers
3683               (sort articles (gnus-make-sort-function
3684                               gnus-article-sort-functions)))
3685       (gnus-message 7 "Sorting articles...done"))))
3686
3687 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3688 (defmacro gnus-thread-header (thread)
3689   "Return header of first article in THREAD.
3690 Note that THREAD must never, ever be anything else than a variable -
3691 using some other form will lead to serious barfage."
3692   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3693   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3694   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3695         (vector thread) 2))
3696
3697 (defsubst gnus-article-sort-by-number (h1 h2)
3698   "Sort articles by article number."
3699   (< (mail-header-number h1)
3700      (mail-header-number h2)))
3701
3702 (defun gnus-thread-sort-by-number (h1 h2)
3703   "Sort threads by root article number."
3704   (gnus-article-sort-by-number
3705    (gnus-thread-header h1) (gnus-thread-header h2)))
3706
3707 (defsubst gnus-article-sort-by-lines (h1 h2)
3708   "Sort articles by article Lines header."
3709   (< (mail-header-lines h1)
3710      (mail-header-lines h2)))
3711
3712 (defun gnus-thread-sort-by-lines (h1 h2)
3713   "Sort threads by root article Lines header."
3714   (gnus-article-sort-by-lines
3715    (gnus-thread-header h1) (gnus-thread-header h2)))
3716
3717 (defsubst gnus-article-sort-by-chars (h1 h2)
3718   "Sort articles by octet length."
3719   (< (mail-header-chars h1)
3720      (mail-header-chars h2)))
3721
3722 (defun gnus-thread-sort-by-chars (h1 h2)
3723   "Sort threads by root article octet length."
3724   (gnus-article-sort-by-chars
3725    (gnus-thread-header h1) (gnus-thread-header h2)))
3726
3727 (defsubst gnus-article-sort-by-author (h1 h2)
3728   "Sort articles by root author."
3729   (string-lessp
3730    (let ((extract (funcall
3731                    gnus-extract-address-components
3732                    (mail-header-from h1))))
3733      (or (car extract) (cadr extract) ""))
3734    (let ((extract (funcall
3735                    gnus-extract-address-components
3736                    (mail-header-from h2))))
3737      (or (car extract) (cadr extract) ""))))
3738
3739 (defun gnus-thread-sort-by-author (h1 h2)
3740   "Sort threads by root author."
3741   (gnus-article-sort-by-author
3742    (gnus-thread-header h1)  (gnus-thread-header h2)))
3743
3744 (defsubst gnus-article-sort-by-subject (h1 h2)
3745   "Sort articles by root subject."
3746   (string-lessp
3747    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3748    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3749
3750 (defun gnus-thread-sort-by-subject (h1 h2)
3751   "Sort threads by root subject."
3752   (gnus-article-sort-by-subject
3753    (gnus-thread-header h1) (gnus-thread-header h2)))
3754
3755 (defsubst gnus-article-sort-by-date (h1 h2)
3756   "Sort articles by root article date."
3757   (time-less-p
3758    (gnus-date-get-time (mail-header-date h1))
3759    (gnus-date-get-time (mail-header-date h2))))
3760
3761 (defun gnus-thread-sort-by-date (h1 h2)
3762   "Sort threads by root article date."
3763   (gnus-article-sort-by-date
3764    (gnus-thread-header h1) (gnus-thread-header h2)))
3765
3766 (defsubst gnus-article-sort-by-score (h1 h2)
3767   "Sort articles by root article score.
3768 Unscored articles will be counted as having a score of zero."
3769   (> (or (cdr (assq (mail-header-number h1)
3770                     gnus-newsgroup-scored))
3771          gnus-summary-default-score 0)
3772      (or (cdr (assq (mail-header-number h2)
3773                     gnus-newsgroup-scored))
3774          gnus-summary-default-score 0)))
3775
3776 (defun gnus-thread-sort-by-score (h1 h2)
3777   "Sort threads by root article score."
3778   (gnus-article-sort-by-score
3779    (gnus-thread-header h1) (gnus-thread-header h2)))
3780
3781 (defun gnus-thread-sort-by-total-score (h1 h2)
3782   "Sort threads by the sum of all scores in the thread.
3783 Unscored articles will be counted as having a score of zero."
3784   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3785
3786 (defun gnus-thread-total-score (thread)
3787   ;; This function find the total score of THREAD.
3788   (cond ((null thread)
3789          0)
3790         ((consp thread)
3791          (if (stringp (car thread))
3792              (apply gnus-thread-score-function 0
3793                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3794            (gnus-thread-total-score-1 thread)))
3795         (t
3796          (gnus-thread-total-score-1 (list thread)))))
3797
3798 (defun gnus-thread-total-score-1 (root)
3799   ;; This function find the total score of the thread below ROOT.
3800   (setq root (car root))
3801   (apply gnus-thread-score-function
3802          (or (append
3803               (mapcar 'gnus-thread-total-score
3804                       (cdr (gnus-id-to-thread (mail-header-id root))))
3805               (when (> (mail-header-number root) 0)
3806                 (list (or (cdr (assq (mail-header-number root)
3807                                      gnus-newsgroup-scored))
3808                           gnus-summary-default-score 0))))
3809              (list gnus-summary-default-score)
3810              '(0))))
3811
3812 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3813 (defvar gnus-tmp-prev-subject nil)
3814 (defvar gnus-tmp-false-parent nil)
3815 (defvar gnus-tmp-root-expunged nil)
3816 (defvar gnus-tmp-dummy-line nil)
3817
3818 (defvar gnus-tmp-header)
3819 (defun gnus-extra-header (type &optional header)
3820   "Return the extra header of TYPE."
3821   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3822       ""))
3823
3824 (defun gnus-summary-prepare-threads (threads)
3825   "Prepare summary buffer from THREADS and indentation LEVEL.
3826 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3827 or a straight list of headers."
3828   (gnus-message 7 "Generating summary...")
3829
3830   (setq gnus-newsgroup-threads threads)
3831   (beginning-of-line)
3832
3833   (let ((gnus-tmp-level 0)
3834         (default-score (or gnus-summary-default-score 0))
3835         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3836         thread number subject stack state gnus-tmp-gathered beg-match
3837         new-roots gnus-tmp-new-adopts thread-end
3838         gnus-tmp-header gnus-tmp-unread
3839         gnus-tmp-replied gnus-tmp-subject-or-nil
3840         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3841         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3842         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3843
3844     (setq gnus-tmp-prev-subject nil)
3845
3846     (if (vectorp (car threads))
3847         ;; If this is a straight (sic) list of headers, then a
3848         ;; threaded summary display isn't required, so we just create
3849         ;; an unthreaded one.
3850         (gnus-summary-prepare-unthreaded threads)
3851
3852       ;; Do the threaded display.
3853
3854       (while (or threads stack gnus-tmp-new-adopts new-roots)
3855
3856         (if (and (= gnus-tmp-level 0)
3857                  (or (not stack)
3858                      (= (caar stack) 0))
3859                  (not gnus-tmp-false-parent)
3860                  (or gnus-tmp-new-adopts new-roots))
3861             (if gnus-tmp-new-adopts
3862                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3863                       thread (list (car gnus-tmp-new-adopts))
3864                       gnus-tmp-header (caar thread)
3865                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3866               (when new-roots
3867                 (setq thread (list (car new-roots))
3868                       gnus-tmp-header (caar thread)
3869                       new-roots (cdr new-roots))))
3870
3871           (if threads
3872               ;; If there are some threads, we do them before the
3873               ;; threads on the stack.
3874               (setq thread threads
3875                     gnus-tmp-header (caar thread))
3876             ;; There were no current threads, so we pop something off
3877             ;; the stack.
3878             (setq state (car stack)
3879                   gnus-tmp-level (car state)
3880                   thread (cdr state)
3881                   stack (cdr stack)
3882                   gnus-tmp-header (caar thread))))
3883
3884         (setq gnus-tmp-false-parent nil)
3885         (setq gnus-tmp-root-expunged nil)
3886         (setq thread-end nil)
3887
3888         (if (stringp gnus-tmp-header)
3889             ;; The header is a dummy root.
3890             (cond
3891              ((eq gnus-summary-make-false-root 'adopt)
3892               ;; We let the first article adopt the rest.
3893               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3894                                                (cddar thread)))
3895               (setq gnus-tmp-gathered
3896                     (nconc (mapcar
3897                             (lambda (h) (mail-header-number (car h)))
3898                             (cddar thread))
3899                            gnus-tmp-gathered))
3900               (setq thread (cons (list (caar thread)
3901                                        (cadar thread))
3902                                  (cdr thread)))
3903               (setq gnus-tmp-level -1
3904                     gnus-tmp-false-parent t))
3905              ((eq gnus-summary-make-false-root 'empty)
3906               ;; We print adopted articles with empty subject fields.
3907               (setq gnus-tmp-gathered
3908                     (nconc (mapcar
3909                             (lambda (h) (mail-header-number (car h)))
3910                             (cddar thread))
3911                            gnus-tmp-gathered))
3912               (setq gnus-tmp-level -1))
3913              ((eq gnus-summary-make-false-root 'dummy)
3914               ;; We remember that we probably want to output a dummy
3915               ;; root.
3916               (setq gnus-tmp-dummy-line gnus-tmp-header)
3917               (setq gnus-tmp-prev-subject gnus-tmp-header))
3918              (t
3919               ;; We do not make a root for the gathered
3920               ;; sub-threads at all.
3921               (setq gnus-tmp-level -1)))
3922
3923           (setq number (mail-header-number gnus-tmp-header)
3924                 subject (mail-header-subject gnus-tmp-header))
3925
3926           (cond
3927            ;; If the thread has changed subject, we might want to make
3928            ;; this subthread into a root.
3929            ((and (null gnus-thread-ignore-subject)
3930                  (not (zerop gnus-tmp-level))
3931                  gnus-tmp-prev-subject
3932                  (not (inline
3933                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3934             (setq new-roots (nconc new-roots (list (car thread)))
3935                   thread-end t
3936                   gnus-tmp-header nil))
3937            ;; If the article lies outside the current limit,
3938            ;; then we do not display it.
3939            ((not (memq number gnus-newsgroup-limit))
3940             (setq gnus-tmp-gathered
3941                   (nconc (mapcar
3942                           (lambda (h) (mail-header-number (car h)))
3943                           (cdar thread))
3944                          gnus-tmp-gathered))
3945             (setq gnus-tmp-new-adopts (if (cdar thread)
3946                                           (append gnus-tmp-new-adopts
3947                                                   (cdar thread))
3948                                         gnus-tmp-new-adopts)
3949                   thread-end t
3950                   gnus-tmp-header nil)
3951             (when (zerop gnus-tmp-level)
3952               (setq gnus-tmp-root-expunged t)))
3953            ;; Perhaps this article is to be marked as read?
3954            ((and gnus-summary-mark-below
3955                  (< (or (cdr (assq number gnus-newsgroup-scored))
3956                         default-score)
3957                     gnus-summary-mark-below)
3958                  ;; Don't touch sparse articles.
3959                  (not (gnus-summary-article-sparse-p number))
3960                  (not (gnus-summary-article-ancient-p number)))
3961             (setq gnus-newsgroup-unreads
3962                   (delq number gnus-newsgroup-unreads))
3963             (if gnus-newsgroup-auto-expire
3964                 (push number gnus-newsgroup-expirable)
3965               (push (cons number gnus-low-score-mark)
3966                     gnus-newsgroup-reads))))
3967
3968           (when gnus-tmp-header
3969             ;; We may have an old dummy line to output before this
3970             ;; article.
3971             (when (and gnus-tmp-dummy-line
3972                        (gnus-subject-equal
3973                         gnus-tmp-dummy-line
3974                         (mail-header-subject gnus-tmp-header)))
3975               (gnus-summary-insert-dummy-line
3976                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3977               (setq gnus-tmp-dummy-line nil))
3978
3979             ;; Compute the mark.
3980             (setq gnus-tmp-unread (gnus-article-mark number))
3981
3982             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3983                                   gnus-tmp-header gnus-tmp-level)
3984                   gnus-newsgroup-data)
3985
3986             ;; Actually insert the line.
3987             (setq
3988              gnus-tmp-subject-or-nil
3989              (cond
3990               ((and gnus-thread-ignore-subject
3991                     gnus-tmp-prev-subject
3992                     (not (inline (gnus-subject-equal
3993                                   gnus-tmp-prev-subject subject))))
3994                subject)
3995               ((zerop gnus-tmp-level)
3996                (if (and (eq gnus-summary-make-false-root 'empty)
3997                         (memq number gnus-tmp-gathered)
3998                         gnus-tmp-prev-subject
3999                         (inline (gnus-subject-equal
4000                                  gnus-tmp-prev-subject subject)))
4001                    gnus-summary-same-subject
4002                  subject))
4003               (t gnus-summary-same-subject)))
4004             (if (and (eq gnus-summary-make-false-root 'adopt)
4005                      (= gnus-tmp-level 1)
4006                      (memq number gnus-tmp-gathered))
4007                 (setq gnus-tmp-opening-bracket ?\<
4008                       gnus-tmp-closing-bracket ?\>)
4009               (setq gnus-tmp-opening-bracket ?\[
4010                     gnus-tmp-closing-bracket ?\]))
4011             (setq
4012              gnus-tmp-indentation
4013              (aref gnus-thread-indent-array gnus-tmp-level)
4014              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4015              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4016                                 gnus-summary-default-score 0)
4017              gnus-tmp-score-char
4018              (if (or (null gnus-summary-default-score)
4019                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4020                          gnus-summary-zcore-fuzz))
4021                  ?  ;Whitespace
4022                (if (< gnus-tmp-score gnus-summary-default-score)
4023                    gnus-score-below-mark gnus-score-over-mark))
4024              gnus-tmp-replied
4025              (cond ((memq number gnus-newsgroup-processable)
4026                     gnus-process-mark)
4027                    ((memq number gnus-newsgroup-cached)
4028                     gnus-cached-mark)
4029                    ((memq number gnus-newsgroup-replied)
4030                     gnus-replied-mark)
4031                    ((memq number gnus-newsgroup-saved)
4032                     gnus-saved-mark)
4033                    (t gnus-unread-mark))
4034              gnus-tmp-from (mail-header-from gnus-tmp-header)
4035              gnus-tmp-name
4036              (cond
4037               ((string-match "<[^>]+> *$" gnus-tmp-from)
4038                (setq beg-match (match-beginning 0))
4039                (or (and (string-match "^\".+\"" gnus-tmp-from)
4040                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4041                    (substring gnus-tmp-from 0 beg-match)))
4042               ((string-match "(.+)" gnus-tmp-from)
4043                (substring gnus-tmp-from
4044                           (1+ (match-beginning 0)) (1- (match-end 0))))
4045               (t gnus-tmp-from)))
4046             (when (string= gnus-tmp-name "")
4047               (setq gnus-tmp-name gnus-tmp-from))
4048             (unless (numberp gnus-tmp-lines)
4049               (setq gnus-tmp-lines 0))
4050             (gnus-put-text-property
4051              (point)
4052              (progn (eval gnus-summary-line-format-spec) (point))
4053              'gnus-number number)
4054             (when gnus-visual-p
4055               (forward-line -1)
4056               (gnus-run-hooks 'gnus-summary-update-hook)
4057               (forward-line 1))
4058
4059             (setq gnus-tmp-prev-subject subject)))
4060
4061         (when (nth 1 thread)
4062           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4063         (incf gnus-tmp-level)
4064         (setq threads (if thread-end nil (cdar thread)))
4065         (unless threads
4066           (setq gnus-tmp-level 0)))))
4067   (gnus-message 7 "Generating summary...done"))
4068
4069 (defun gnus-summary-prepare-unthreaded (headers)
4070   "Generate an unthreaded summary buffer based on HEADERS."
4071   (let (header number mark)
4072
4073     (beginning-of-line)
4074
4075     (while headers
4076       ;; We may have to root out some bad articles...
4077       (when (memq (setq number (mail-header-number
4078                                 (setq header (pop headers))))
4079                   gnus-newsgroup-limit)
4080         ;; Mark article as read when it has a low score.
4081         (when (and gnus-summary-mark-below
4082                    (< (or (cdr (assq number gnus-newsgroup-scored))
4083                           gnus-summary-default-score 0)
4084                       gnus-summary-mark-below)
4085                    (not (gnus-summary-article-ancient-p number)))
4086           (setq gnus-newsgroup-unreads
4087                 (delq number gnus-newsgroup-unreads))
4088           (if gnus-newsgroup-auto-expire
4089               (push number gnus-newsgroup-expirable)
4090             (push (cons number gnus-low-score-mark)
4091                   gnus-newsgroup-reads)))
4092
4093         (setq mark (gnus-article-mark number))
4094         (push (gnus-data-make number mark (1+ (point)) header 0)
4095               gnus-newsgroup-data)
4096         (gnus-summary-insert-line
4097          header 0 number
4098          mark (memq number gnus-newsgroup-replied)
4099          (memq number gnus-newsgroup-expirable)
4100          (mail-header-subject header) nil
4101          (cdr (assq number gnus-newsgroup-scored))
4102          (memq number gnus-newsgroup-processable))))))
4103
4104 (defun gnus-summary-remove-list-identifiers ()
4105   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4106   (let ((regexp (if (stringp gnus-list-identifiers)
4107                     gnus-list-identifiers
4108                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4109     (dolist (header gnus-newsgroup-headers)
4110       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4111                                   " *\\)\\)+\\(Re: +\\)?\\)")
4112                           (mail-header-subject header))
4113         (mail-header-set-subject
4114          header (concat (substring (mail-header-subject header)
4115                                    0 (match-beginning 1))
4116                         (or
4117                          (match-string 3 (mail-header-subject header))
4118                          (match-string 5 (mail-header-subject header)))
4119                         (substring (mail-header-subject header)
4120                                    (match-end 1))))))))
4121
4122 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4123   "Select newsgroup GROUP.
4124 If READ-ALL is non-nil, all articles in the group are selected.
4125 If SELECT-ARTICLES, only select those articles from GROUP."
4126   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4127          ;;!!! Dirty hack; should be removed.
4128          (gnus-summary-ignore-duplicates
4129           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4130               t
4131             gnus-summary-ignore-duplicates))
4132          (info (nth 2 entry))
4133          articles fetched-articles cached)
4134
4135     (unless (gnus-check-server
4136              (setq gnus-current-select-method
4137                    (gnus-find-method-for-group group)))
4138       (error "Couldn't open server"))
4139
4140     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4141         (gnus-activate-group group)     ; Or we can activate it...
4142         (progn                          ; Or we bug out.
4143           (when (equal major-mode 'gnus-summary-mode)
4144             (kill-buffer (current-buffer)))
4145           (error "Couldn't request group %s: %s"
4146                  group (gnus-status-message group))))
4147
4148     (unless (gnus-request-group group t)
4149       (when (equal major-mode 'gnus-summary-mode)
4150         (kill-buffer (current-buffer)))
4151       (error "Couldn't request group %s: %s"
4152              group (gnus-status-message group)))
4153
4154     (setq gnus-newsgroup-name group)
4155     (setq gnus-newsgroup-unselected nil)
4156     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4157     (gnus-summary-setup-default-charset)
4158
4159     ;; Adjust and set lists of article marks.
4160     (when info
4161       (gnus-adjust-marked-articles info))
4162
4163     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4164     (when (gnus-virtual-group-p group)
4165       (setq cached gnus-newsgroup-cached))
4166
4167     (setq gnus-newsgroup-unreads
4168           (gnus-set-difference
4169            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4170            gnus-newsgroup-dormant))
4171
4172     (setq gnus-newsgroup-processable nil)
4173
4174     (gnus-update-read-articles group gnus-newsgroup-unreads)
4175
4176     (if (setq articles select-articles)
4177         (setq gnus-newsgroup-unselected
4178               (gnus-sorted-intersection
4179                gnus-newsgroup-unreads
4180                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4181       (setq articles (gnus-articles-to-read group read-all)))
4182
4183     (cond
4184      ((null articles)
4185       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4186       'quit)
4187      ((eq articles 0) nil)
4188      (t
4189       ;; Init the dependencies hash table.
4190       (setq gnus-newsgroup-dependencies
4191             (gnus-make-hashtable (length articles)))
4192       (gnus-set-global-variables)
4193       ;; Retrieve the headers and read them in.
4194       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4195       (setq gnus-newsgroup-headers
4196             (if (eq 'nov
4197                     (setq gnus-headers-retrieved-by
4198                           (gnus-retrieve-headers
4199                            articles gnus-newsgroup-name
4200                            ;; We might want to fetch old headers, but
4201                            ;; not if there is only 1 article.
4202                            (and (or (and
4203                                      (not (eq gnus-fetch-old-headers 'some))
4204                                      (not (numberp gnus-fetch-old-headers)))
4205                                     (> (length articles) 1))
4206                                 gnus-fetch-old-headers))))
4207                 (gnus-get-newsgroup-headers-xover
4208                  articles nil nil gnus-newsgroup-name t)
4209               (gnus-get-newsgroup-headers)))
4210       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4211
4212       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4213       (when cached
4214         (setq gnus-newsgroup-cached cached))
4215
4216       ;; Suppress duplicates?
4217       (when gnus-suppress-duplicates
4218         (gnus-dup-suppress-articles))
4219
4220       ;; Set the initial limit.
4221       (setq gnus-newsgroup-limit (copy-sequence articles))
4222       ;; Remove canceled articles from the list of unread articles.
4223       (setq gnus-newsgroup-unreads
4224             (gnus-set-sorted-intersection
4225              gnus-newsgroup-unreads
4226              (setq fetched-articles
4227                    (mapcar (lambda (headers) (mail-header-number headers))
4228                            gnus-newsgroup-headers))))
4229       ;; Removed marked articles that do not exist.
4230       (gnus-update-missing-marks
4231        (gnus-sorted-complement fetched-articles articles))
4232       ;; We might want to build some more threads first.
4233       (when (and gnus-fetch-old-headers
4234                  (eq gnus-headers-retrieved-by 'nov))
4235         (if (eq gnus-fetch-old-headers 'invisible)
4236             (gnus-build-all-threads)
4237           (gnus-build-old-threads)))
4238       ;; Let the Gnus agent mark articles as read.
4239       (when gnus-agent
4240         (gnus-agent-get-undownloaded-list))
4241       ;; Remove list identifiers from subject
4242       (when gnus-list-identifiers
4243         (gnus-summary-remove-list-identifiers))
4244       ;; Check whether auto-expire is to be done in this group.
4245       (setq gnus-newsgroup-auto-expire
4246             (gnus-group-auto-expirable-p group))
4247       ;; Set up the article buffer now, if necessary.
4248       (unless gnus-single-article-buffer
4249         (gnus-article-setup-buffer))
4250       ;; First and last article in this newsgroup.
4251       (when gnus-newsgroup-headers
4252         (setq gnus-newsgroup-begin
4253               (mail-header-number (car gnus-newsgroup-headers))
4254               gnus-newsgroup-end
4255               (mail-header-number
4256                (gnus-last-element gnus-newsgroup-headers))))
4257       ;; GROUP is successfully selected.
4258       (or gnus-newsgroup-headers t)))))
4259
4260 (defun gnus-articles-to-read (group &optional read-all)
4261   "Find out what articles the user wants to read."
4262   (let* ((articles
4263           ;; Select all articles if `read-all' is non-nil, or if there
4264           ;; are no unread articles.
4265           (if (or read-all
4266                   (and (zerop (length gnus-newsgroup-marked))
4267                        (zerop (length gnus-newsgroup-unreads)))
4268                   (eq (gnus-group-find-parameter group 'display)
4269                       'all))
4270               (or
4271                (gnus-uncompress-range (gnus-active group))
4272                (gnus-cache-articles-in-group group))
4273             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4274                           (copy-sequence gnus-newsgroup-unreads))
4275                   '<)))
4276          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4277          (scored (length scored-list))
4278          (number (length articles))
4279          (marked (+ (length gnus-newsgroup-marked)
4280                     (length gnus-newsgroup-dormant)))
4281          (select
4282           (cond
4283            ((numberp read-all)
4284             read-all)
4285            (t
4286             (condition-case ()
4287                 (cond
4288                  ((and (or (<= scored marked) (= scored number))
4289                        (numberp gnus-large-newsgroup)
4290                        (> number gnus-large-newsgroup))
4291                   (let ((input
4292                          (read-string
4293                           (format
4294                            "How many articles from %s (default %d): "
4295                            (gnus-limit-string gnus-newsgroup-name 35)
4296                            number))))
4297                     (if (string-match "^[ \t]*$" input) number input)))
4298                  ((and (> scored marked) (< scored number)
4299                        (> (- scored number) 20))
4300                   (let ((input
4301                          (read-string
4302                           (format "%s %s (%d scored, %d total): "
4303                                   "How many articles from"
4304                                   group scored number))))
4305                     (if (string-match "^[ \t]*$" input)
4306                         number input)))
4307                  (t number))
4308               (quit
4309                (message "Quit getting the articles to read")
4310                nil))))))
4311     (setq select (if (stringp select) (string-to-number select) select))
4312     (if (or (null select) (zerop select))
4313         select
4314       (if (and (not (zerop scored)) (<= (abs select) scored))
4315           (progn
4316             (setq articles (sort scored-list '<))
4317             (setq number (length articles)))
4318         (setq articles (copy-sequence articles)))
4319
4320       (when (< (abs select) number)
4321         (if (< select 0)
4322             ;; Select the N oldest articles.
4323             (setcdr (nthcdr (1- (abs select)) articles) nil)
4324           ;; Select the N most recent articles.
4325           (setq articles (nthcdr (- number select) articles))))
4326       (setq gnus-newsgroup-unselected
4327             (gnus-sorted-intersection
4328              gnus-newsgroup-unreads
4329              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4330       (when gnus-alter-articles-to-read-function
4331         (setq gnus-newsgroup-unreads
4332               (sort 
4333                (funcall gnus-alter-articles-to-read-function
4334                         gnus-newsgroup-name gnus-newsgroup-unreads)
4335                '<)))
4336       articles)))
4337
4338 (defun gnus-killed-articles (killed articles)
4339   (let (out)
4340     (while articles
4341       (when (inline (gnus-member-of-range (car articles) killed))
4342         (push (car articles) out))
4343       (setq articles (cdr articles)))
4344     out))
4345
4346 (defun gnus-uncompress-marks (marks)
4347   "Uncompress the mark ranges in MARKS."
4348   (let ((uncompressed '(score bookmark))
4349         out)
4350     (while marks
4351       (if (memq (caar marks) uncompressed)
4352           (push (car marks) out)
4353         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4354       (setq marks (cdr marks)))
4355     out))
4356
4357 (defun gnus-adjust-marked-articles (info)
4358   "Set all article lists and remove all marks that are no longer valid."
4359   (let* ((marked-lists (gnus-info-marks info))
4360          (active (gnus-active (gnus-info-group info)))
4361          (min (car active))
4362          (max (cdr active))
4363          (types gnus-article-mark-lists)
4364          (uncompressed '(score bookmark killed))
4365          marks var articles article mark)
4366
4367     (while marked-lists
4368       (setq marks (pop marked-lists))
4369       (set (setq var (intern (format "gnus-newsgroup-%s"
4370                                      (car (rassq (setq mark (car marks))
4371                                                  types)))))
4372            (if (memq (car marks) uncompressed) (cdr marks)
4373              (gnus-uncompress-range (cdr marks))))
4374
4375       (setq articles (symbol-value var))
4376
4377       ;; All articles have to be subsets of the active articles.
4378       (cond
4379        ;; Adjust "simple" lists.
4380        ((memq mark '(tick dormant expire reply save))
4381         (while articles
4382           (when (or (< (setq article (pop articles)) min) (> article max))
4383             (set var (delq article (symbol-value var))))))
4384        ;; Adjust assocs.
4385        ((memq mark uncompressed)
4386         (when (not (listp (cdr (symbol-value var))))
4387           (set var (list (symbol-value var))))
4388         (when (not (listp (cdr articles)))
4389           (setq articles (list articles)))
4390         (while articles
4391           (when (or (not (consp (setq article (pop articles))))
4392                     (< (car article) min)
4393                     (> (car article) max))
4394             (set var (delq article (symbol-value var))))))))))
4395
4396 (defun gnus-update-missing-marks (missing)
4397   "Go through the list of MISSING articles and remove them from the mark lists."
4398   (when missing
4399     (let ((types gnus-article-mark-lists)
4400           var m)
4401       ;; Go through all types.
4402       (while types
4403         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4404         (when (symbol-value var)
4405           ;; This list has articles.  So we delete all missing articles
4406           ;; from it.
4407           (setq m missing)
4408           (while m
4409             (set var (delq (pop m) (symbol-value var)))))))))
4410
4411 (defun gnus-update-marks ()
4412   "Enter the various lists of marked articles into the newsgroup info list."
4413   (let ((types gnus-article-mark-lists)
4414         (info (gnus-get-info gnus-newsgroup-name))
4415         (uncompressed '(score bookmark killed))
4416         type list newmarked symbol delta-marks)
4417     (when info
4418       ;; Add all marks lists to the list of marks lists.
4419       (while (setq type (pop types))
4420         (setq list (symbol-value
4421                     (setq symbol
4422                           (intern (format "gnus-newsgroup-%s"
4423                                           (car type))))))
4424
4425         (when list
4426           ;; Get rid of the entries of the articles that have the
4427           ;; default score.
4428           (when (and (eq (cdr type) 'score)
4429                      gnus-save-score
4430                      list)
4431             (let* ((arts list)
4432                    (prev (cons nil list))
4433                    (all prev))
4434               (while arts
4435                 (if (or (not (consp (car arts)))
4436                         (= (cdar arts) gnus-summary-default-score))
4437                     (setcdr prev (cdr arts))
4438                   (setq prev arts))
4439                 (setq arts (cdr arts)))
4440               (setq list (cdr all)))))
4441
4442         (unless (memq (cdr type) uncompressed)
4443           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4444        
4445         (when (gnus-check-backend-function
4446                'request-set-mark gnus-newsgroup-name)
4447           ;; propagate flags to server, with the following exceptions:
4448           ;; uncompressed:s are not proper flags (they are cons cells)
4449           ;; cache is a internal gnus flag
4450           ;; download are local to one gnus installation (well)
4451           ;; unsend are for nndraft groups only
4452           ;; xxx: generality of this?  this suits nnimap anyway
4453           (unless (memq (cdr type) (append '(cache download unsend)
4454                                            uncompressed))
4455             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4456                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4457                    (add (gnus-remove-from-range
4458                          (gnus-copy-sequence list) old)))
4459               (when add
4460                 (push (list add 'add (list (cdr type))) delta-marks))
4461               (when del
4462                 (push (list del 'del (list (cdr type))) delta-marks)))))
4463           
4464         (when list
4465           (push (cons (cdr type) list) newmarked)))
4466
4467       (when delta-marks
4468         (unless (gnus-check-group gnus-newsgroup-name)
4469           (error "Can't open server for %s" gnus-newsgroup-name))
4470         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4471           
4472       ;; Enter these new marks into the info of the group.
4473       (if (nthcdr 3 info)
4474           (setcar (nthcdr 3 info) newmarked)
4475         ;; Add the marks lists to the end of the info.
4476         (when newmarked
4477           (setcdr (nthcdr 2 info) (list newmarked))))
4478
4479       ;; Cut off the end of the info if there's nothing else there.
4480       (let ((i 5))
4481         (while (and (> i 2)
4482                     (not (nth i info)))
4483           (when (nthcdr (decf i) info)
4484             (setcdr (nthcdr i info) nil)))))))
4485
4486 (defun gnus-set-mode-line (where)
4487   "Set the mode line of the article or summary buffers.
4488 If WHERE is `summary', the summary mode line format will be used."
4489   ;; Is this mode line one we keep updated?
4490   (when (and (memq where gnus-updated-mode-lines)
4491              (symbol-value
4492               (intern (format "gnus-%s-mode-line-format-spec" where))))
4493     (let (mode-string)
4494       (save-excursion
4495         ;; We evaluate this in the summary buffer since these
4496         ;; variables are buffer-local to that buffer.
4497         (set-buffer gnus-summary-buffer)
4498         ;; We bind all these variables that are used in the `eval' form
4499         ;; below.
4500         (let* ((mformat (symbol-value
4501                          (intern
4502                           (format "gnus-%s-mode-line-format-spec" where))))
4503                (gnus-tmp-group-name (gnus-group-name-decode 
4504                                      gnus-newsgroup-name
4505                                      (gnus-group-name-charset 
4506                                       nil
4507                                       gnus-newsgroup-name)))
4508                (gnus-tmp-article-number (or gnus-current-article 0))
4509                (gnus-tmp-unread gnus-newsgroup-unreads)
4510                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4511                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4512                (gnus-tmp-unread-and-unselected
4513                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4514                             (zerop gnus-tmp-unselected))
4515                        "")
4516                       ((zerop gnus-tmp-unselected)
4517                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4518                       (t (format "{%d(+%d) more}"
4519                                  gnus-tmp-unread-and-unticked
4520                                  gnus-tmp-unselected))))
4521                (gnus-tmp-subject
4522                 (if (and gnus-current-headers
4523                          (vectorp gnus-current-headers))
4524                     (gnus-mode-string-quote
4525                      (mail-header-subject gnus-current-headers))
4526                   ""))
4527                bufname-length max-len
4528                gnus-tmp-header);; passed as argument to any user-format-funcs
4529           (setq mode-string (eval mformat))
4530           (setq bufname-length (if (string-match "%b" mode-string)
4531                                    (- (length
4532                                        (buffer-name
4533                                         (if (eq where 'summary)
4534                                             nil
4535                                           (get-buffer gnus-article-buffer))))
4536                                       2)
4537                                  0))
4538           (setq max-len (max 4 (if gnus-mode-non-string-length
4539                                    (- (window-width)
4540                                       gnus-mode-non-string-length
4541                                       bufname-length)
4542                                  (length mode-string))))
4543           ;; We might have to chop a bit of the string off...
4544           (when (> (length mode-string) max-len)
4545             (setq mode-string
4546                   (concat (truncate-string-to-width mode-string (- max-len 3))
4547                           "...")))
4548           ;; Pad the mode string a bit.
4549           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4550       ;; Update the mode line.
4551       (setq mode-line-buffer-identification
4552             (gnus-mode-line-buffer-identification (list mode-string)))
4553       (set-buffer-modified-p t))))
4554
4555 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4556   "Go through the HEADERS list and add all Xrefs to a hash table.
4557 The resulting hash table is returned, or nil if no Xrefs were found."
4558   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4559          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4560          (xref-hashtb (gnus-make-hashtable))
4561          start group entry number xrefs header)
4562     (while headers
4563       (setq header (pop headers))
4564       (when (and (setq xrefs (mail-header-xref header))
4565                  (not (memq (setq number (mail-header-number header))
4566                             unreads)))
4567         (setq start 0)
4568         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4569           (setq start (match-end 0))
4570           (setq group (if prefix
4571                           (concat prefix (substring xrefs (match-beginning 1)
4572                                                     (match-end 1)))
4573                         (substring xrefs (match-beginning 1) (match-end 1))))
4574           (setq number
4575                 (string-to-int (substring xrefs (match-beginning 2)
4576                                           (match-end 2))))
4577           (if (setq entry (gnus-gethash group xref-hashtb))
4578               (setcdr entry (cons number (cdr entry)))
4579             (gnus-sethash group (cons number nil) xref-hashtb)))))
4580     (and start xref-hashtb)))
4581
4582 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4583   "Look through all the headers and mark the Xrefs as read."
4584   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4585         name entry info xref-hashtb idlist method nth4)
4586     (save-excursion
4587       (set-buffer gnus-group-buffer)
4588       (when (setq xref-hashtb
4589                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4590         (mapatoms
4591          (lambda (group)
4592            (unless (string= from-newsgroup (setq name (symbol-name group)))
4593              (setq idlist (symbol-value group))
4594              ;; Dead groups are not updated.
4595              (and (prog1
4596                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4597                             info (nth 2 entry))
4598                     (when (stringp (setq nth4 (gnus-info-method info)))
4599                       (setq nth4 (gnus-server-to-method nth4))))
4600                   ;; Only do the xrefs if the group has the same
4601                   ;; select method as the group we have just read.
4602                   (or (gnus-methods-equal-p
4603                        nth4 (gnus-find-method-for-group from-newsgroup))
4604                       virtual
4605                       (equal nth4 (setq method (gnus-find-method-for-group
4606                                                 from-newsgroup)))
4607                       (and (equal (car nth4) (car method))
4608                            (equal (nth 1 nth4) (nth 1 method))))
4609                   gnus-use-cross-reference
4610                   (or (not (eq gnus-use-cross-reference t))
4611                       virtual
4612                       ;; Only do cross-references on subscribed
4613                       ;; groups, if that is what is wanted.
4614                       (<= (gnus-info-level info) gnus-level-subscribed))
4615                   (gnus-group-make-articles-read name idlist))))
4616          xref-hashtb)))))
4617
4618 (defun gnus-compute-read-articles (group articles)
4619   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4620          (info (nth 2 entry))
4621          (active (gnus-active group))
4622          ninfo)
4623     (when entry
4624       ;; First peel off all invalid article numbers.
4625       (when active
4626         (let ((ids articles)
4627               id first)
4628           (while (setq id (pop ids))
4629             (when (and first (> id (cdr active)))
4630               ;; We'll end up in this situation in one particular
4631               ;; obscure situation.  If you re-scan a group and get
4632               ;; a new article that is cross-posted to a different
4633               ;; group that has not been re-scanned, you might get
4634               ;; crossposted article that has a higher number than
4635               ;; Gnus believes possible.  So we re-activate this
4636               ;; group as well.  This might mean doing the
4637               ;; crossposting thingy will *increase* the number
4638               ;; of articles in some groups.  Tsk, tsk.
4639               (setq active (or (gnus-activate-group group) active)))
4640             (when (or (> id (cdr active))
4641                       (< id (car active)))
4642               (setq articles (delq id articles))))))
4643       ;; If the read list is nil, we init it.
4644       (if (and active
4645                (null (gnus-info-read info))
4646                (> (car active) 1))
4647           (setq ninfo (cons 1 (1- (car active))))
4648         (setq ninfo (gnus-info-read info)))
4649       ;; Then we add the read articles to the range.
4650       (gnus-add-to-range
4651        ninfo (setq articles (sort articles '<))))))
4652
4653 (defun gnus-group-make-articles-read (group articles)
4654   "Update the info of GROUP to say that ARTICLES are read."
4655   (let* ((num 0)
4656          (entry (gnus-gethash group gnus-newsrc-hashtb))
4657          (info (nth 2 entry))
4658          (active (gnus-active group))
4659          range)
4660     (when entry
4661       (setq range (gnus-compute-read-articles group articles))
4662       (save-excursion
4663         (set-buffer gnus-group-buffer)
4664         (gnus-undo-register
4665           `(progn
4666              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4667              (gnus-info-set-read ',info ',(gnus-info-read info))
4668              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4669              (gnus-group-update-group ,group t))))
4670       ;; Add the read articles to the range.
4671       (gnus-info-set-read info range)
4672       ;; Then we have to re-compute how many unread
4673       ;; articles there are in this group.
4674       (when active
4675         (cond
4676          ((not range)
4677           (setq num (- (1+ (cdr active)) (car active))))
4678          ((not (listp (cdr range)))
4679           (setq num (- (cdr active) (- (1+ (cdr range))
4680                                        (car range)))))
4681          (t
4682           (while range
4683             (if (numberp (car range))
4684                 (setq num (1+ num))
4685               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4686             (setq range (cdr range)))
4687           (setq num (- (cdr active) num))))
4688         ;; Update the number of unread articles.
4689         (setcar entry num)
4690         ;; Update the group buffer.
4691         (gnus-group-update-group group t)))))
4692
4693 (defvar gnus-newsgroup-none-id 0)
4694
4695 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4696   (let ((cur nntp-server-buffer)
4697         (dependencies
4698          (or dependencies
4699              (save-excursion (set-buffer gnus-summary-buffer)
4700                              gnus-newsgroup-dependencies)))
4701         headers id end ref
4702         (mail-parse-charset gnus-newsgroup-charset)
4703         (mail-parse-ignored-charsets
4704          (save-excursion (condition-case nil
4705                              (set-buffer gnus-summary-buffer)
4706                            (error))
4707                          gnus-newsgroup-ignored-charsets)))
4708     (save-excursion
4709       (set-buffer nntp-server-buffer)
4710       ;; Translate all TAB characters into SPACE characters.
4711       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4712       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4713       (gnus-run-hooks 'gnus-parse-headers-hook)
4714       (let ((case-fold-search t)
4715             in-reply-to header p lines chars)
4716         (goto-char (point-min))
4717         ;; Search to the beginning of the next header.  Error messages
4718         ;; do not begin with 2 or 3.
4719         (while (re-search-forward "^[23][0-9]+ " nil t)
4720           (setq id nil
4721                 ref nil)
4722           ;; This implementation of this function, with nine
4723           ;; search-forwards instead of the one re-search-forward and
4724           ;; a case (which basically was the old function) is actually
4725           ;; about twice as fast, even though it looks messier.  You
4726           ;; can't have everything, I guess.  Speed and elegance
4727           ;; doesn't always go hand in hand.
4728           (setq
4729            header
4730            (vector
4731             ;; Number.
4732             (prog1
4733                 (read cur)
4734               (end-of-line)
4735               (setq p (point))
4736               (narrow-to-region (point)
4737                                 (or (and (search-forward "\n.\n" nil t)
4738                                          (- (point) 2))
4739                                     (point))))
4740             ;; Subject.
4741             (progn
4742               (goto-char p)
4743               (if (search-forward "\nsubject: " nil t)
4744                   (funcall gnus-decode-encoded-word-function
4745                            (nnheader-header-value))
4746                 "(none)"))
4747             ;; From.
4748             (progn
4749               (goto-char p)
4750               (if (or (search-forward "\nfrom: " nil t)
4751                       (search-forward "\nfrom:" nil t))
4752                   (funcall gnus-decode-encoded-word-function
4753                            (nnheader-header-value))
4754                 "(nobody)"))
4755             ;; Date.
4756             (progn
4757               (goto-char p)
4758               (if (search-forward "\ndate: " nil t)
4759                   (nnheader-header-value) ""))
4760             ;; Message-ID.
4761             (progn
4762               (goto-char p)
4763               (setq id (if (re-search-forward
4764                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4765                            ;; We do it this way to make sure the Message-ID
4766                            ;; is (somewhat) syntactically valid.
4767                            (buffer-substring (match-beginning 1)
4768                                              (match-end 1))
4769                          ;; If there was no message-id, we just fake one
4770                          ;; to make subsequent routines simpler.
4771                          (nnheader-generate-fake-message-id))))
4772             ;; References.
4773             (progn
4774               (goto-char p)
4775               (if (search-forward "\nreferences: " nil t)
4776                   (progn
4777                     (setq end (point))
4778                     (prog1
4779                         (nnheader-header-value)
4780                       (setq ref
4781                             (buffer-substring
4782                              (progn
4783                                (end-of-line)
4784                                (search-backward ">" end t)
4785                                (1+ (point)))
4786                              (progn
4787                                (search-backward "<" end t)
4788                                (point))))))
4789                 ;; Get the references from the in-reply-to header if there
4790                 ;; were no references and the in-reply-to header looks
4791                 ;; promising.
4792                 (if (and (search-forward "\nin-reply-to: " nil t)
4793                          (setq in-reply-to (nnheader-header-value))
4794                          (string-match "<[^>]+>" in-reply-to))
4795                     (let (ref2)
4796                       (setq ref (substring in-reply-to (match-beginning 0)
4797                                            (match-end 0)))
4798                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4799                         (setq ref2 (substring in-reply-to (match-beginning 0)
4800                                               (match-end 0)))
4801                         (when (> (length ref2) (length ref))
4802                           (setq ref ref2)))
4803                       ref)
4804                   (setq ref nil))))
4805             ;; Chars.
4806             (progn
4807               (goto-char p)
4808               (if (search-forward "\nchars: " nil t)
4809                   (if (numberp (setq chars (ignore-errors (read cur))))
4810                       chars 0)
4811                 0))
4812             ;; Lines.
4813             (progn
4814               (goto-char p)
4815               (if (search-forward "\nlines: " nil t)
4816                   (if (numberp (setq lines (ignore-errors (read cur))))
4817                       lines 0)
4818                 0))
4819             ;; Xref.
4820             (progn
4821               (goto-char p)
4822               (and (search-forward "\nxref: " nil t)
4823                    (nnheader-header-value)))
4824             ;; Extra.
4825             (when gnus-extra-headers
4826               (let ((extra gnus-extra-headers)
4827                     out)
4828                 (while extra
4829                   (goto-char p)
4830                   (when (search-forward
4831                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4832                     (push (cons (car extra) (nnheader-header-value))
4833                           out))
4834                   (pop extra))
4835                 out))))
4836           (when (equal id ref)
4837             (setq ref nil))
4838
4839           (when gnus-alter-header-function
4840             (funcall gnus-alter-header-function header)
4841             (setq id (mail-header-id header)
4842                   ref (gnus-parent-id (mail-header-references header))))
4843
4844           (when (setq header
4845                       (gnus-dependencies-add-header
4846                        header dependencies force-new))
4847             (push header headers))
4848           (goto-char (point-max))
4849           (widen))
4850         (nreverse headers)))))
4851
4852 ;; Goes through the xover lines and returns a list of vectors
4853 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4854                                                   force-new dependencies
4855                                                   group also-fetch-heads)
4856   "Parse the news overview data in the server buffer.
4857 Return a list of headers that match SEQUENCE (see
4858 `nntp-retrieve-headers')."
4859   ;; Get the Xref when the users reads the articles since most/some
4860   ;; NNTP servers do not include Xrefs when using XOVER.
4861   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4862   (let ((mail-parse-charset gnus-newsgroup-charset)
4863         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4864         (cur nntp-server-buffer)
4865         (dependencies (or dependencies gnus-newsgroup-dependencies))
4866         number headers header)
4867     (save-excursion
4868       (set-buffer nntp-server-buffer)
4869       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4870       ;; Allow the user to mangle the headers before parsing them.
4871       (gnus-run-hooks 'gnus-parse-headers-hook)
4872       (goto-char (point-min))
4873       (while (not (eobp))
4874         (condition-case ()
4875             (while (and sequence (not (eobp)))
4876               (setq number (read cur))
4877               (while (and sequence
4878                           (< (car sequence) number))
4879                 (setq sequence (cdr sequence)))
4880               (and sequence
4881                    (eq number (car sequence))
4882                    (progn
4883                      (setq sequence (cdr sequence))
4884                      (setq header (inline
4885                                     (gnus-nov-parse-line
4886                                      number dependencies force-new))))
4887                    (push header headers))
4888               (forward-line 1))
4889           (error
4890            (gnus-error 4 "Strange nov line (%d)"
4891                        (count-lines (point-min) (point)))))
4892         (forward-line 1))
4893       ;; A common bug in inn is that if you have posted an article and
4894       ;; then retrieves the active file, it will answer correctly --
4895       ;; the new article is included.  However, a NOV entry for the
4896       ;; article may not have been generated yet, so this may fail.
4897       ;; We work around this problem by retrieving the last few
4898       ;; headers using HEAD.
4899       (if (or (not also-fetch-heads)
4900               (not sequence))
4901           ;; We (probably) got all the headers.
4902           (nreverse headers)
4903         (let ((gnus-nov-is-evil t))
4904           (nconc
4905            (nreverse headers)
4906            (when (gnus-retrieve-headers sequence group)
4907              (gnus-get-newsgroup-headers))))))))
4908
4909 (defun gnus-article-get-xrefs ()
4910   "Fill in the Xref value in `gnus-current-headers', if necessary.
4911 This is meant to be called in `gnus-article-internal-prepare-hook'."
4912   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4913                                  gnus-current-headers)))
4914     (or (not gnus-use-cross-reference)
4915         (not headers)
4916         (and (mail-header-xref headers)
4917              (not (string= (mail-header-xref headers) "")))
4918         (let ((case-fold-search t)
4919               xref)
4920           (save-restriction
4921             (nnheader-narrow-to-headers)
4922             (goto-char (point-min))
4923             (when (or (and (not (eobp))
4924                            (eq (downcase (char-after)) ?x)
4925                            (looking-at "Xref:"))
4926                       (search-forward "\nXref:" nil t))
4927               (goto-char (1+ (match-end 0)))
4928               (setq xref (buffer-substring (point)
4929                                            (progn (end-of-line) (point))))
4930               (mail-header-set-xref headers xref)))))))
4931
4932 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4933   "Find article ID and insert the summary line for that article.
4934 OLD-HEADER can either be a header or a line number to insert
4935 the subject line on."
4936   (let* ((line (and (numberp old-header) old-header))
4937          (old-header (and (vectorp old-header) old-header))
4938          (header (cond ((and old-header use-old-header)
4939                         old-header)
4940                        ((and (numberp id)
4941                              (gnus-number-to-header id))
4942                         (gnus-number-to-header id))
4943                        (t
4944                         (gnus-read-header id))))
4945          (number (and (numberp id) id))
4946          d)
4947     (when header
4948       ;; Rebuild the thread that this article is part of and go to the
4949       ;; article we have fetched.
4950       (when (and (not gnus-show-threads)
4951                  old-header)
4952         (when (and number
4953                    (setq d (gnus-data-find (mail-header-number old-header))))
4954           (goto-char (gnus-data-pos d))
4955           (gnus-data-remove
4956            number
4957            (- (gnus-point-at-bol)
4958               (prog1
4959                   (1+ (gnus-point-at-eol))
4960                 (gnus-delete-line))))))
4961       (when old-header
4962         (mail-header-set-number header (mail-header-number old-header)))
4963       (setq gnus-newsgroup-sparse
4964             (delq (setq number (mail-header-number header))
4965                   gnus-newsgroup-sparse))
4966       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4967       (push number gnus-newsgroup-limit)
4968       (gnus-rebuild-thread (mail-header-id header) line)
4969       (gnus-summary-goto-subject number nil t))
4970     (when (and (numberp number)
4971                (> number 0))
4972       ;; We have to update the boundaries even if we can't fetch the
4973       ;; article if ID is a number -- so that the next `P' or `N'
4974       ;; command will fetch the previous (or next) article even
4975       ;; if the one we tried to fetch this time has been canceled.
4976       (when (> number gnus-newsgroup-end)
4977         (setq gnus-newsgroup-end number))
4978       (when (< number gnus-newsgroup-begin)
4979         (setq gnus-newsgroup-begin number))
4980       (setq gnus-newsgroup-unselected
4981             (delq number gnus-newsgroup-unselected)))
4982     ;; Report back a success?
4983     (and header (mail-header-number header))))
4984
4985 ;;; Process/prefix in the summary buffer
4986
4987 (defun gnus-summary-work-articles (n)
4988   "Return a list of articles to be worked upon.
4989 The prefix argument, the list of process marked articles, and the
4990 current article will be taken into consideration."
4991   (save-excursion
4992     (set-buffer gnus-summary-buffer)
4993     (cond
4994      (n
4995       ;; A numerical prefix has been given.
4996       (setq n (prefix-numeric-value n))
4997       (let ((backward (< n 0))
4998             (n (abs (prefix-numeric-value n)))
4999             articles article)
5000         (save-excursion
5001           (while
5002               (and (> n 0)
5003                    (push (setq article (gnus-summary-article-number))
5004                          articles)
5005                    (if backward
5006                        (gnus-summary-find-prev nil article)
5007                      (gnus-summary-find-next nil article)))
5008             (decf n)))
5009         (nreverse articles)))
5010      ((and (gnus-region-active-p) (mark))
5011       (message "region active")
5012       ;; Work on the region between point and mark.
5013       (let ((max (max (point) (mark)))
5014             articles article)
5015         (save-excursion
5016           (goto-char (min (min (point) (mark))))
5017           (while
5018               (and
5019                (push (setq article (gnus-summary-article-number)) articles)
5020                (gnus-summary-find-next nil article)
5021                (< (point) max)))
5022           (nreverse articles))))
5023      (gnus-newsgroup-processable
5024       ;; There are process-marked articles present.
5025       ;; Save current state.
5026       (gnus-summary-save-process-mark)
5027       ;; Return the list.
5028       (reverse gnus-newsgroup-processable))
5029      (t
5030       ;; Just return the current article.
5031       (list (gnus-summary-article-number))))))
5032
5033 (defmacro gnus-summary-iterate (arg &rest forms)
5034   "Iterate over the process/prefixed articles and do FORMS.
5035 ARG is the interactive prefix given to the command.  FORMS will be
5036 executed with point over the summary line of the articles."
5037   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5038     `(let ((,articles (gnus-summary-work-articles ,arg)))
5039        (while ,articles
5040          (gnus-summary-goto-subject (car ,articles))
5041          ,@forms
5042          (pop ,articles)))))
5043
5044 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5045 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5046
5047 (defun gnus-summary-save-process-mark ()
5048   "Push the current set of process marked articles on the stack."
5049   (interactive)
5050   (push (copy-sequence gnus-newsgroup-processable)
5051         gnus-newsgroup-process-stack))
5052
5053 (defun gnus-summary-kill-process-mark ()
5054   "Push the current set of process marked articles on the stack and unmark."
5055   (interactive)
5056   (gnus-summary-save-process-mark)
5057   (gnus-summary-unmark-all-processable))
5058
5059 (defun gnus-summary-yank-process-mark ()
5060   "Pop the last process mark state off the stack and restore it."
5061   (interactive)
5062   (unless gnus-newsgroup-process-stack
5063     (error "Empty mark stack"))
5064   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5065
5066 (defun gnus-summary-process-mark-set (set)
5067   "Make SET into the current process marked articles."
5068   (gnus-summary-unmark-all-processable)
5069   (while set
5070     (gnus-summary-set-process-mark (pop set))))
5071
5072 ;;; Searching and stuff
5073
5074 (defun gnus-summary-search-group (&optional backward use-level)
5075   "Search for next unread newsgroup.
5076 If optional argument BACKWARD is non-nil, search backward instead."
5077   (save-excursion
5078     (set-buffer gnus-group-buffer)
5079     (when (gnus-group-search-forward
5080            backward nil (if use-level (gnus-group-group-level) nil))
5081       (gnus-group-group-name))))
5082
5083 (defun gnus-summary-best-group (&optional exclude-group)
5084   "Find the name of the best unread group.
5085 If EXCLUDE-GROUP, do not go to this group."
5086   (save-excursion
5087     (set-buffer gnus-group-buffer)
5088     (save-excursion
5089       (gnus-group-best-unread-group exclude-group))))
5090
5091 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5092   (if backward (gnus-summary-find-prev)
5093     (let* ((dummy (gnus-summary-article-intangible-p))
5094            (article (or article (gnus-summary-article-number)))
5095            (arts (gnus-data-find-list article))
5096            result)
5097       (when (and (not dummy)
5098                  (or (not gnus-summary-check-current)
5099                      (not unread)
5100                      (not (gnus-data-unread-p (car arts)))))
5101         (setq arts (cdr arts)))
5102       (when (setq result
5103                   (if unread
5104                       (progn
5105                         (while arts
5106                           (when (or (and undownloaded
5107                                          (eq gnus-undownloaded-mark
5108                                              (gnus-data-mark (car arts))))
5109                                     (gnus-data-unread-p (car arts)))
5110                             (setq result (car arts)
5111                                   arts nil))
5112                           (setq arts (cdr arts)))
5113                         result)
5114                     (car arts)))
5115         (goto-char (gnus-data-pos result))
5116         (gnus-data-number result)))))
5117
5118 (defun gnus-summary-find-prev (&optional unread article)
5119   (let* ((eobp (eobp))
5120          (article (or article (gnus-summary-article-number)))
5121          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5122          result)
5123     (when (and (not eobp)
5124                (or (not gnus-summary-check-current)
5125                    (not unread)
5126                    (not (gnus-data-unread-p (car arts)))))
5127       (setq arts (cdr arts)))
5128     (when (setq result
5129                 (if unread
5130                     (progn
5131                       (while arts
5132                         (when (gnus-data-unread-p (car arts))
5133                           (setq result (car arts)
5134                                 arts nil))
5135                         (setq arts (cdr arts)))
5136                       result)
5137                   (car arts)))
5138       (goto-char (gnus-data-pos result))
5139       (gnus-data-number result))))
5140
5141 (defun gnus-summary-find-subject (subject &optional unread backward article)
5142   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5143          (article (or article (gnus-summary-article-number)))
5144          (articles (gnus-data-list backward))
5145          (arts (gnus-data-find-list article articles))
5146          result)
5147     (when (or (not gnus-summary-check-current)
5148               (not unread)
5149               (not (gnus-data-unread-p (car arts))))
5150       (setq arts (cdr arts)))
5151     (while arts
5152       (and (or (not unread)
5153                (gnus-data-unread-p (car arts)))
5154            (vectorp (gnus-data-header (car arts)))
5155            (gnus-subject-equal
5156             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5157            (setq result (car arts)
5158                  arts nil))
5159       (setq arts (cdr arts)))
5160     (and result
5161          (goto-char (gnus-data-pos result))
5162          (gnus-data-number result))))
5163
5164 (defun gnus-summary-search-forward (&optional unread subject backward)
5165   "Search forward for an article.
5166 If UNREAD, look for unread articles.  If SUBJECT, look for
5167 articles with that subject.  If BACKWARD, search backward instead."
5168   (cond (subject (gnus-summary-find-subject subject unread backward))
5169         (backward (gnus-summary-find-prev unread))
5170         (t (gnus-summary-find-next unread))))
5171
5172 (defun gnus-recenter (&optional n)
5173   "Center point in window and redisplay frame.
5174 Also do horizontal recentering."
5175   (interactive "P")
5176   (when (and gnus-auto-center-summary
5177              (not (eq gnus-auto-center-summary 'vertical)))
5178     (gnus-horizontal-recenter))
5179   (recenter n))
5180
5181 (defun gnus-summary-recenter ()
5182   "Center point in the summary window.
5183 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5184 displayed, no centering will be performed."
5185   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5186   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5187   (interactive)
5188   (let* ((top (cond ((< (window-height) 4) 0)
5189                     ((< (window-height) 7) 1)
5190                     (t (if (numberp gnus-auto-center-summary)
5191                            gnus-auto-center-summary
5192                          2))))
5193          (height (1- (window-height)))
5194          (bottom (save-excursion (goto-char (point-max))
5195                                  (forward-line (- height))
5196                                  (point)))
5197          (window (get-buffer-window (current-buffer))))
5198     ;; The user has to want it.
5199     (when gnus-auto-center-summary
5200       (when (get-buffer-window gnus-article-buffer)
5201         ;; Only do recentering when the article buffer is displayed,
5202         ;; Set the window start to either `bottom', which is the biggest
5203         ;; possible valid number, or the second line from the top,
5204         ;; whichever is the least.
5205         (set-window-start
5206          window (min bottom (save-excursion
5207                               (forward-line (- top)) (point)))
5208          t))
5209       ;; Do horizontal recentering while we're at it.
5210       (when (and (get-buffer-window (current-buffer) t)
5211                  (not (eq gnus-auto-center-summary 'vertical)))
5212         (let ((selected (selected-window)))
5213           (select-window (get-buffer-window (current-buffer) t))
5214           (gnus-summary-position-point)
5215           (gnus-horizontal-recenter)
5216           (select-window selected))))))
5217
5218 (defun gnus-summary-jump-to-group (newsgroup)
5219   "Move point to NEWSGROUP in group mode buffer."
5220   ;; Keep update point of group mode buffer if visible.
5221   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5222       (save-window-excursion
5223         ;; Take care of tree window mode.
5224         (when (get-buffer-window gnus-group-buffer)
5225           (pop-to-buffer gnus-group-buffer))
5226         (gnus-group-jump-to-group newsgroup))
5227     (save-excursion
5228       ;; Take care of tree window mode.
5229       (if (get-buffer-window gnus-group-buffer)
5230           (pop-to-buffer gnus-group-buffer)
5231         (set-buffer gnus-group-buffer))
5232       (gnus-group-jump-to-group newsgroup))))
5233
5234 ;; This function returns a list of article numbers based on the
5235 ;; difference between the ranges of read articles in this group and
5236 ;; the range of active articles.
5237 (defun gnus-list-of-unread-articles (group)
5238   (let* ((read (gnus-info-read (gnus-get-info group)))
5239          (active (or (gnus-active group) (gnus-activate-group group)))
5240          (last (cdr active))
5241          first nlast unread)
5242     ;; If none are read, then all are unread.
5243     (if (not read)
5244         (setq first (car active))
5245       ;; If the range of read articles is a single range, then the
5246       ;; first unread article is the article after the last read
5247       ;; article.  Sounds logical, doesn't it?
5248       (if (and (not (listp (cdr read)))
5249                (or (< (car read) (car active))
5250                    (progn (setq read (list read))
5251                           nil)))
5252           (setq first (max (car active) (1+ (cdr read))))
5253         ;; `read' is a list of ranges.
5254         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5255                                   (caar read)))
5256                   1)
5257           (setq first (car active)))
5258         (while read
5259           (when first
5260             (while (< first nlast)
5261               (push first unread)
5262               (setq first (1+ first))))
5263           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5264           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5265           (setq read (cdr read)))))
5266     ;; And add the last unread articles.
5267     (while (<= first last)
5268       (push first unread)
5269       (setq first (1+ first)))
5270     ;; Return the list of unread articles.
5271     (delq 0 (nreverse unread))))
5272
5273 (defun gnus-list-of-read-articles (group)
5274   "Return a list of unread, unticked and non-dormant articles."
5275   (let* ((info (gnus-get-info group))
5276          (marked (gnus-info-marks info))
5277          (active (gnus-active group)))
5278     (and info active
5279          (gnus-set-difference
5280           (gnus-sorted-complement
5281            (gnus-uncompress-range active)
5282            (gnus-list-of-unread-articles group))
5283           (append
5284            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5285            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5286
5287 ;; Various summary commands
5288
5289 (defun gnus-summary-select-article-buffer ()
5290   "Reconfigure windows to show article buffer."
5291   (interactive)
5292   (if (not (gnus-buffer-live-p gnus-article-buffer))
5293       (error "There is no article buffer for this summary buffer")
5294     (gnus-configure-windows 'article)
5295     (select-window (get-buffer-window gnus-article-buffer))))
5296
5297 (defun gnus-summary-universal-argument (arg)
5298   "Perform any operation on all articles that are process/prefixed."
5299   (interactive "P")
5300   (let ((articles (gnus-summary-work-articles arg))
5301         func article)
5302     (if (eq
5303          (setq
5304           func
5305           (key-binding
5306            (read-key-sequence
5307             (substitute-command-keys
5308              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5309          'undefined)
5310         (gnus-error 1 "Undefined key")
5311       (save-excursion
5312         (while articles
5313           (gnus-summary-goto-subject (setq article (pop articles)))
5314           (let (gnus-newsgroup-processable)
5315             (command-execute func))
5316           (gnus-summary-remove-process-mark article)))))
5317   (gnus-summary-position-point))
5318
5319 (defun gnus-summary-toggle-truncation (&optional arg)
5320   "Toggle truncation of summary lines.
5321 With arg, turn line truncation on iff arg is positive."
5322   (interactive "P")
5323   (setq truncate-lines
5324         (if (null arg) (not truncate-lines)
5325           (> (prefix-numeric-value arg) 0)))
5326   (redraw-display))
5327
5328 (defun gnus-summary-reselect-current-group (&optional all rescan)
5329   "Exit and then reselect the current newsgroup.
5330 The prefix argument ALL means to select all articles."
5331   (interactive "P")
5332   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5333     (error "Ephemeral groups can't be reselected"))
5334   (let ((current-subject (gnus-summary-article-number))
5335         (group gnus-newsgroup-name))
5336     (setq gnus-newsgroup-begin nil)
5337     (gnus-summary-exit)
5338     ;; We have to adjust the point of group mode buffer because
5339     ;; point was moved to the next unread newsgroup by exiting.
5340     (gnus-summary-jump-to-group group)
5341     (when rescan
5342       (save-excursion
5343         (gnus-group-get-new-news-this-group 1)))
5344     (gnus-group-read-group all t)
5345     (gnus-summary-goto-subject current-subject nil t)))
5346
5347 (defun gnus-summary-rescan-group (&optional all)
5348   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5349   (interactive "P")
5350   (gnus-summary-reselect-current-group all t))
5351
5352 (defun gnus-summary-update-info (&optional non-destructive)
5353   (save-excursion
5354     (let ((group gnus-newsgroup-name))
5355       (when group
5356         (when gnus-newsgroup-kill-headers
5357           (setq gnus-newsgroup-killed
5358                 (gnus-compress-sequence
5359                  (nconc
5360                   (gnus-set-sorted-intersection
5361                    (gnus-uncompress-range gnus-newsgroup-killed)
5362                    (setq gnus-newsgroup-unselected
5363                          (sort gnus-newsgroup-unselected '<)))
5364                   (setq gnus-newsgroup-unreads
5365                         (sort gnus-newsgroup-unreads '<)))
5366                  t)))
5367         (unless (listp (cdr gnus-newsgroup-killed))
5368           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5369         (let ((headers gnus-newsgroup-headers))
5370           ;; Set the new ranges of read articles.
5371           (save-excursion
5372             (set-buffer gnus-group-buffer)
5373             (gnus-undo-force-boundary))
5374           (gnus-update-read-articles
5375            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5376           ;; Set the current article marks.
5377           (let ((gnus-newsgroup-scored
5378                  (if (and (not gnus-save-score)
5379                           (not non-destructive))
5380                      nil
5381                    gnus-newsgroup-scored)))
5382             (save-excursion
5383               (gnus-update-marks)))
5384           ;; Do the cross-ref thing.
5385           (when gnus-use-cross-reference
5386             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5387           ;; Do not switch windows but change the buffer to work.
5388           (set-buffer gnus-group-buffer)
5389           (unless (gnus-ephemeral-group-p group)
5390             (gnus-group-update-group group)))))))
5391
5392 (defun gnus-summary-save-newsrc (&optional force)
5393   "Save the current number of read/marked articles in the dribble buffer.
5394 The dribble buffer will then be saved.
5395 If FORCE (the prefix), also save the .newsrc file(s)."
5396   (interactive "P")
5397   (gnus-summary-update-info t)
5398   (if force
5399       (gnus-save-newsrc-file)
5400     (gnus-dribble-save)))
5401
5402 (defun gnus-summary-exit (&optional temporary)
5403   "Exit reading current newsgroup, and then return to group selection mode.
5404 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5405   (interactive)
5406   (gnus-set-global-variables)
5407   (when (gnus-buffer-live-p gnus-article-buffer)
5408     (save-excursion
5409       (set-buffer gnus-article-buffer)
5410       (mm-destroy-parts gnus-article-mime-handles)
5411       ;; Set it to nil for safety reason.
5412       (setq gnus-article-mime-handle-alist nil)
5413       (setq gnus-article-mime-handles nil)))
5414   (gnus-kill-save-kill-buffer)
5415   (gnus-async-halt-prefetch)
5416   (let* ((group gnus-newsgroup-name)
5417          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5418          (mode major-mode)
5419          (group-point nil)
5420          (buf (current-buffer)))
5421     (unless quit-config
5422       ;; Do adaptive scoring, and possibly save score files.
5423       (when gnus-newsgroup-adaptive
5424         (gnus-score-adaptive))
5425       (when gnus-use-scoring
5426         (gnus-score-save)))
5427     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5428     ;; If we have several article buffers, we kill them at exit.
5429     (unless gnus-single-article-buffer
5430       (gnus-kill-buffer gnus-original-article-buffer)
5431       (setq gnus-article-current nil))
5432     (when gnus-use-cache
5433       (gnus-cache-possibly-remove-articles)
5434       (gnus-cache-save-buffers))
5435     (gnus-async-prefetch-remove-group group)
5436     (when gnus-suppress-duplicates
5437       (gnus-dup-enter-articles))
5438     (when gnus-use-trees
5439       (gnus-tree-close group))
5440     (when gnus-use-cache
5441       (gnus-cache-write-active))
5442     ;; Remove entries for this group.
5443     (nnmail-purge-split-history (gnus-group-real-name group))
5444     ;; Make all changes in this group permanent.
5445     (unless quit-config
5446       (gnus-run-hooks 'gnus-exit-group-hook)
5447       (gnus-summary-update-info))
5448     (gnus-close-group group)
5449     ;; Make sure where we were, and go to next newsgroup.
5450     (set-buffer gnus-group-buffer)
5451     (unless quit-config
5452       (gnus-group-jump-to-group group))
5453     (gnus-run-hooks 'gnus-summary-exit-hook)
5454     (unless (or quit-config
5455                 ;; If this group has disappeared from the summary
5456                 ;; buffer, don't skip forwards.
5457                 (not (string= group (gnus-group-group-name))))
5458       (gnus-group-next-unread-group 1))
5459     (setq group-point (point))
5460     (if temporary
5461         nil                             ;Nothing to do.
5462       ;; If we have several article buffers, we kill them at exit.
5463       (unless gnus-single-article-buffer
5464         (gnus-kill-buffer gnus-article-buffer)
5465         (gnus-kill-buffer gnus-original-article-buffer)
5466         (setq gnus-article-current nil))
5467       (set-buffer buf)
5468       (if (not gnus-kill-summary-on-exit)
5469           (gnus-deaden-summary)
5470         ;; We set all buffer-local variables to nil.  It is unclear why
5471         ;; this is needed, but if we don't, buffer-local variables are
5472         ;; not garbage-collected, it seems.  This would the lead to en
5473         ;; ever-growing Emacs.
5474         (gnus-summary-clear-local-variables)
5475         (when (get-buffer gnus-article-buffer)
5476           (bury-buffer gnus-article-buffer))
5477         ;; We clear the global counterparts of the buffer-local
5478         ;; variables as well, just to be on the safe side.
5479         (set-buffer gnus-group-buffer)
5480         (gnus-summary-clear-local-variables)
5481         ;; Return to group mode buffer.
5482         (when (eq mode 'gnus-summary-mode)
5483           (gnus-kill-buffer buf)))
5484       (setq gnus-current-select-method gnus-select-method)
5485       (pop-to-buffer gnus-group-buffer)
5486       (if (not quit-config)
5487           (progn
5488             (goto-char group-point)
5489             (gnus-configure-windows 'group 'force))
5490         (gnus-handle-ephemeral-exit quit-config))
5491       ;; Clear the current group name.
5492       (unless quit-config
5493         (setq gnus-newsgroup-name nil)))))
5494
5495 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5496 (defun gnus-summary-exit-no-update (&optional no-questions)
5497   "Quit reading current newsgroup without updating read article info."
5498   (interactive)
5499   (let* ((group gnus-newsgroup-name)
5500          (quit-config (gnus-group-quit-config group)))
5501     (when (or no-questions
5502               gnus-expert-user
5503               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5504       (gnus-async-halt-prefetch)
5505       (mapcar 'funcall
5506               (delq 'gnus-summary-expire-articles
5507                     (copy-sequence gnus-summary-prepare-exit-hook)))
5508       (when (gnus-buffer-live-p gnus-article-buffer)
5509         (save-excursion
5510           (set-buffer gnus-article-buffer)
5511           (mm-destroy-parts gnus-article-mime-handles)
5512           ;; Set it to nil for safety reason.
5513           (setq gnus-article-mime-handle-alist nil)
5514           (setq gnus-article-mime-handles nil)))
5515       ;; If we have several article buffers, we kill them at exit.
5516       (unless gnus-single-article-buffer
5517         (gnus-kill-buffer gnus-article-buffer)
5518         (gnus-kill-buffer gnus-original-article-buffer)
5519         (setq gnus-article-current nil))
5520       (if (not gnus-kill-summary-on-exit)
5521           (gnus-deaden-summary)
5522         (gnus-close-group group)
5523         (gnus-summary-clear-local-variables)
5524         (set-buffer gnus-group-buffer)
5525         (gnus-summary-clear-local-variables)
5526         (when (get-buffer gnus-summary-buffer)
5527           (kill-buffer gnus-summary-buffer)))
5528       (unless gnus-single-article-buffer
5529         (setq gnus-article-current nil))
5530       (when gnus-use-trees
5531         (gnus-tree-close group))
5532       (gnus-async-prefetch-remove-group group)
5533       (when (get-buffer gnus-article-buffer)
5534         (bury-buffer gnus-article-buffer))
5535       ;; Return to the group buffer.
5536       (gnus-configure-windows 'group 'force)
5537       ;; Clear the current group name.
5538       (setq gnus-newsgroup-name nil)
5539       (when (equal (gnus-group-group-name) group)
5540         (gnus-group-next-unread-group 1))
5541       (when quit-config
5542         (gnus-handle-ephemeral-exit quit-config)))))
5543
5544 (defun gnus-handle-ephemeral-exit (quit-config)
5545   "Handle movement when leaving an ephemeral group.
5546 The state which existed when entering the ephemeral is reset."
5547   (if (not (buffer-name (car quit-config)))
5548       (gnus-configure-windows 'group 'force)
5549     (set-buffer (car quit-config))
5550     (cond ((eq major-mode 'gnus-summary-mode)
5551            (gnus-set-global-variables))
5552           ((eq major-mode 'gnus-article-mode)
5553            (save-excursion
5554              ;; The `gnus-summary-buffer' variable may point
5555              ;; to the old summary buffer when using a single
5556              ;; article buffer.
5557              (unless (gnus-buffer-live-p gnus-summary-buffer)
5558                (set-buffer gnus-group-buffer))
5559              (set-buffer gnus-summary-buffer)
5560              (gnus-set-global-variables))))
5561     (if (or (eq (cdr quit-config) 'article)
5562             (eq (cdr quit-config) 'pick))
5563         (progn
5564           ;; The current article may be from the ephemeral group
5565           ;; thus it is best that we reload this article
5566           (gnus-summary-show-article)
5567           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5568               (gnus-configure-windows 'pick 'force)
5569             (gnus-configure-windows (cdr quit-config) 'force)))
5570       (gnus-configure-windows (cdr quit-config) 'force))
5571     (when (eq major-mode 'gnus-summary-mode)
5572       (gnus-summary-next-subject 1 nil t)
5573       (gnus-summary-recenter)
5574       (gnus-summary-position-point))))
5575
5576 ;;; Dead summaries.
5577
5578 (defvar gnus-dead-summary-mode-map nil)
5579
5580 (unless gnus-dead-summary-mode-map
5581   (setq gnus-dead-summary-mode-map (make-keymap))
5582   (suppress-keymap gnus-dead-summary-mode-map)
5583   (substitute-key-definition
5584    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5585   (let ((keys '("\C-d" "\r" "\177" [delete])))
5586     (while keys
5587       (define-key gnus-dead-summary-mode-map
5588         (pop keys) 'gnus-summary-wake-up-the-dead))))
5589
5590 (defvar gnus-dead-summary-mode nil
5591   "Minor mode for Gnus summary buffers.")
5592
5593 (defun gnus-dead-summary-mode (&optional arg)
5594   "Minor mode for Gnus summary buffers."
5595   (interactive "P")
5596   (when (eq major-mode 'gnus-summary-mode)
5597     (make-local-variable 'gnus-dead-summary-mode)
5598     (setq gnus-dead-summary-mode
5599           (if (null arg) (not gnus-dead-summary-mode)
5600             (> (prefix-numeric-value arg) 0)))
5601     (when gnus-dead-summary-mode
5602       (gnus-add-minor-mode
5603        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5604
5605 (defun gnus-deaden-summary ()
5606   "Make the current summary buffer into a dead summary buffer."
5607   ;; Kill any previous dead summary buffer.
5608   (when (and gnus-dead-summary
5609              (buffer-name gnus-dead-summary))
5610     (save-excursion
5611       (set-buffer gnus-dead-summary)
5612       (when gnus-dead-summary-mode
5613         (kill-buffer (current-buffer)))))
5614   ;; Make this the current dead summary.
5615   (setq gnus-dead-summary (current-buffer))
5616   (gnus-dead-summary-mode 1)
5617   (let ((name (buffer-name)))
5618     (when (string-match "Summary" name)
5619       (rename-buffer
5620        (concat (substring name 0 (match-beginning 0)) "Dead "
5621                (substring name (match-beginning 0)))
5622        t)
5623       (bury-buffer))))
5624
5625 (defun gnus-kill-or-deaden-summary (buffer)
5626   "Kill or deaden the summary BUFFER."
5627   (save-excursion
5628     (when (and (buffer-name buffer)
5629                (not gnus-single-article-buffer))
5630       (save-excursion
5631         (set-buffer buffer)
5632         (gnus-kill-buffer gnus-article-buffer)
5633         (gnus-kill-buffer gnus-original-article-buffer)))
5634     (cond (gnus-kill-summary-on-exit
5635            (when (and gnus-use-trees
5636                       (gnus-buffer-exists-p buffer))
5637              (save-excursion
5638                (set-buffer buffer)
5639                (gnus-tree-close gnus-newsgroup-name)))
5640            (gnus-kill-buffer buffer))
5641           ((gnus-buffer-exists-p buffer)
5642            (save-excursion
5643              (set-buffer buffer)
5644              (gnus-deaden-summary))))))
5645
5646 (defun gnus-summary-wake-up-the-dead (&rest args)
5647   "Wake up the dead summary buffer."
5648   (interactive)
5649   (gnus-dead-summary-mode -1)
5650   (let ((name (buffer-name)))
5651     (when (string-match "Dead " name)
5652       (rename-buffer
5653        (concat (substring name 0 (match-beginning 0))
5654                (substring name (match-end 0)))
5655        t)))
5656   (gnus-message 3 "This dead summary is now alive again"))
5657
5658 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5659 (defun gnus-summary-fetch-faq (&optional faq-dir)
5660   "Fetch the FAQ for the current group.
5661 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5662 in."
5663   (interactive
5664    (list
5665     (when current-prefix-arg
5666       (completing-read
5667        "Faq dir: " (and (listp gnus-group-faq-directory)
5668                         (mapcar (lambda (file) (list file))
5669                                 gnus-group-faq-directory))))))
5670   (let (gnus-faq-buffer)
5671     (when (setq gnus-faq-buffer
5672                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5673       (gnus-configure-windows 'summary-faq))))
5674
5675 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5676 (defun gnus-summary-describe-group (&optional force)
5677   "Describe the current newsgroup."
5678   (interactive "P")
5679   (gnus-group-describe-group force gnus-newsgroup-name))
5680
5681 (defun gnus-summary-describe-briefly ()
5682   "Describe summary mode commands briefly."
5683   (interactive)
5684   (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")))
5685
5686 ;; Walking around group mode buffer from summary mode.
5687
5688 (defun gnus-summary-next-group (&optional no-article target-group backward)
5689   "Exit current newsgroup and then select next unread newsgroup.
5690 If prefix argument NO-ARTICLE is non-nil, no article is selected
5691 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5692 previous group instead."
5693   (interactive "P")
5694   ;; Stop pre-fetching.
5695   (gnus-async-halt-prefetch)
5696   (let ((current-group gnus-newsgroup-name)
5697         (current-buffer (current-buffer))
5698         entered)
5699     ;; First we semi-exit this group to update Xrefs and all variables.
5700     ;; We can't do a real exit, because the window conf must remain
5701     ;; the same in case the user is prompted for info, and we don't
5702     ;; want the window conf to change before that...
5703     (gnus-summary-exit t)
5704     (while (not entered)
5705       ;; Then we find what group we are supposed to enter.
5706       (set-buffer gnus-group-buffer)
5707       (gnus-group-jump-to-group current-group)
5708       (setq target-group
5709             (or target-group
5710                 (if (eq gnus-keep-same-level 'best)
5711                     (gnus-summary-best-group gnus-newsgroup-name)
5712                   (gnus-summary-search-group backward gnus-keep-same-level))))
5713       (if (not target-group)
5714           ;; There are no further groups, so we return to the group
5715           ;; buffer.
5716           (progn
5717             (gnus-message 5 "Returning to the group buffer")
5718             (setq entered t)
5719             (when (gnus-buffer-live-p current-buffer)
5720               (set-buffer current-buffer)
5721               (gnus-summary-exit))
5722             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5723         ;; We try to enter the target group.
5724         (gnus-group-jump-to-group target-group)
5725         (let ((unreads (gnus-group-group-unread)))
5726           (if (and (or (eq t unreads)
5727                        (and unreads (not (zerop unreads))))
5728                    (gnus-summary-read-group
5729                     target-group nil no-article
5730                     (and (buffer-name current-buffer) current-buffer)
5731                     nil backward))
5732               (setq entered t)
5733             (setq current-group target-group
5734                   target-group nil)))))))
5735
5736 (defun gnus-summary-prev-group (&optional no-article)
5737   "Exit current newsgroup and then select previous unread newsgroup.
5738 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5739   (interactive "P")
5740   (gnus-summary-next-group no-article nil t))
5741
5742 ;; Walking around summary lines.
5743
5744 (defun gnus-summary-first-subject (&optional unread undownloaded)
5745   "Go to the first unread subject.
5746 If UNREAD is non-nil, go to the first unread article.
5747 Returns the article selected or nil if there are no unread articles."
5748   (interactive "P")
5749   (prog1
5750       (cond
5751        ;; Empty summary.
5752        ((null gnus-newsgroup-data)
5753         (gnus-message 3 "No articles in the group")
5754         nil)
5755        ;; Pick the first article.
5756        ((not unread)
5757         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5758         (gnus-data-number (car gnus-newsgroup-data)))
5759        ;; No unread articles.
5760        ((null gnus-newsgroup-unreads)
5761         (gnus-message 3 "No more unread articles")
5762         nil)
5763        ;; Find the first unread article.
5764        (t
5765         (let ((data gnus-newsgroup-data))
5766           (while (and data
5767                       (and (not (and undownloaded
5768                                      (eq gnus-undownloaded-mark
5769                                          (gnus-data-mark (car data)))))
5770                            (not (gnus-data-unread-p (car data)))))
5771             (setq data (cdr data)))
5772           (when data
5773             (goto-char (gnus-data-pos (car data)))
5774             (gnus-data-number (car data))))))
5775     (gnus-summary-position-point)))
5776
5777 (defun gnus-summary-next-subject (n &optional unread dont-display)
5778   "Go to next N'th summary line.
5779 If N is negative, go to the previous N'th subject line.
5780 If UNREAD is non-nil, only unread articles are selected.
5781 The difference between N and the actual number of steps taken is
5782 returned."
5783   (interactive "p")
5784   (let ((backward (< n 0))
5785         (n (abs n)))
5786     (while (and (> n 0)
5787                 (if backward
5788                     (gnus-summary-find-prev unread)
5789                   (gnus-summary-find-next unread)))
5790       (unless (zerop (setq n (1- n)))
5791         (gnus-summary-show-thread)))
5792     (when (/= 0 n)
5793       (gnus-message 7 "No more%s articles"
5794                     (if unread " unread" "")))
5795     (unless dont-display
5796       (gnus-summary-recenter)
5797       (gnus-summary-position-point))
5798     n))
5799
5800 (defun gnus-summary-next-unread-subject (n)
5801   "Go to next N'th unread summary line."
5802   (interactive "p")
5803   (gnus-summary-next-subject n t))
5804
5805 (defun gnus-summary-prev-subject (n &optional unread)
5806   "Go to previous N'th summary line.
5807 If optional argument UNREAD is non-nil, only unread article is selected."
5808   (interactive "p")
5809   (gnus-summary-next-subject (- n) unread))
5810
5811 (defun gnus-summary-prev-unread-subject (n)
5812   "Go to previous N'th unread summary line."
5813   (interactive "p")
5814   (gnus-summary-next-subject (- n) t))
5815
5816 (defun gnus-summary-goto-subject (article &optional force silent)
5817   "Go the subject line of ARTICLE.
5818 If FORCE, also allow jumping to articles not currently shown."
5819   (interactive "nArticle number: ")
5820   (let ((b (point))
5821         (data (gnus-data-find article)))
5822     ;; We read in the article if we have to.
5823     (and (not data)
5824          force
5825          (gnus-summary-insert-subject
5826           article
5827           (if (or (numberp force) (vectorp force)) force)
5828           t)
5829          (setq data (gnus-data-find article)))
5830     (goto-char b)
5831     (if (not data)
5832         (progn
5833           (unless silent
5834             (gnus-message 3 "Can't find article %d" article))
5835           nil)
5836       (goto-char (gnus-data-pos data))
5837       (gnus-summary-position-point)
5838       article)))
5839
5840 ;; Walking around summary lines with displaying articles.
5841
5842 (defun gnus-summary-expand-window (&optional arg)
5843   "Make the summary buffer take up the entire Emacs frame.
5844 Given a prefix, will force an `article' buffer configuration."
5845   (interactive "P")
5846   (if arg
5847       (gnus-configure-windows 'article 'force)
5848     (gnus-configure-windows 'summary 'force)))
5849
5850 (defun gnus-summary-display-article (article &optional all-header)
5851   "Display ARTICLE in article buffer."
5852   (gnus-set-global-variables)
5853   (when (gnus-buffer-live-p gnus-article-buffer)
5854     (with-current-buffer gnus-article-buffer
5855       (setq gnus-article-charset gnus-newsgroup-charset)
5856       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
5857       (mm-enable-multibyte-mule4)))
5858   (if (null article)
5859       nil
5860     (prog1
5861         (if gnus-summary-display-article-function
5862             (funcall gnus-summary-display-article-function article all-header)
5863           (gnus-article-prepare article all-header))
5864       (gnus-run-hooks 'gnus-select-article-hook)
5865       (when (and gnus-current-article
5866                  (not (zerop gnus-current-article)))
5867         (gnus-summary-goto-subject gnus-current-article))
5868       (gnus-summary-recenter)
5869       (when (and gnus-use-trees gnus-show-threads)
5870         (gnus-possibly-generate-tree article)
5871         (gnus-highlight-selected-tree article))
5872       ;; Successfully display article.
5873       (gnus-article-set-window-start
5874        (cdr (assq article gnus-newsgroup-bookmarks))))))
5875
5876 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5877   "Select the current article.
5878 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5879 non-nil, the article will be re-fetched even if it already present in
5880 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5881 be displayed."
5882   ;; Make sure we are in the summary buffer to work around bbdb bug.
5883   (unless (eq major-mode 'gnus-summary-mode)
5884     (set-buffer gnus-summary-buffer))
5885   (let ((article (or article (gnus-summary-article-number)))
5886         (all-headers (not (not all-headers))) ;Must be T or NIL.
5887         gnus-summary-display-article-function)
5888     (and (not pseudo)
5889          (gnus-summary-article-pseudo-p article)
5890          (error "This is a pseudo-article"))
5891     (save-excursion
5892       (set-buffer gnus-summary-buffer)
5893       (if (or (and gnus-single-article-buffer
5894                    (or (null gnus-current-article)
5895                        (null gnus-article-current)
5896                        (null (get-buffer gnus-article-buffer))
5897                        (not (eq article (cdr gnus-article-current)))
5898                        (not (equal (car gnus-article-current)
5899                                    gnus-newsgroup-name))))
5900               (and (not gnus-single-article-buffer)
5901                    (or (null gnus-current-article)
5902                        (not (eq gnus-current-article article))))
5903               force)
5904           ;; The requested article is different from the current article.
5905           (progn
5906             (gnus-summary-display-article article all-headers)
5907             (when (gnus-buffer-live-p gnus-article-buffer)
5908                (with-current-buffer gnus-article-buffer
5909                 (if (not gnus-article-decoded-p) ;; a local variable
5910                     (mm-disable-multibyte-mule4))))
5911             (when (or all-headers gnus-show-all-headers)
5912               (gnus-article-show-all-headers))
5913             (gnus-article-set-window-start
5914              (cdr (assq article gnus-newsgroup-bookmarks)))
5915             article)
5916         (when (or all-headers gnus-show-all-headers)
5917           (gnus-article-show-all-headers))
5918         'old))))
5919
5920 (defun gnus-summary-force-verify-and-decrypt ()
5921   (interactive)
5922   (let ((mm-verify-option 'known)
5923         (mm-decrypt-option 'known))
5924     (gnus-summary-select-article nil 'force)))
5925
5926 (defun gnus-summary-set-current-mark (&optional current-mark)
5927   "Obsolete function."
5928   nil)
5929
5930 (defun gnus-summary-next-article (&optional unread subject backward push)
5931   "Select the next article.
5932 If UNREAD, only unread articles are selected.
5933 If SUBJECT, only articles with SUBJECT are selected.
5934 If BACKWARD, the previous article is selected instead of the next."
5935   (interactive "P")
5936   (cond
5937    ;; Is there such an article?
5938    ((and (gnus-summary-search-forward unread subject backward)
5939          (or (gnus-summary-display-article (gnus-summary-article-number))
5940              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5941     (gnus-summary-position-point))
5942    ;; If not, we try the first unread, if that is wanted.
5943    ((and subject
5944          gnus-auto-select-same
5945          (gnus-summary-first-unread-article))
5946     (gnus-summary-position-point)
5947     (gnus-message 6 "Wrapped"))
5948    ;; Try to get next/previous article not displayed in this group.
5949    ((and gnus-auto-extend-newsgroup
5950          (not unread) (not subject))
5951     (gnus-summary-goto-article
5952      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5953      nil (count-lines (point-min) (point))))
5954    ;; Go to next/previous group.
5955    (t
5956     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5957       (gnus-summary-jump-to-group gnus-newsgroup-name))
5958     (let ((cmd last-command-char)
5959           (point
5960            (save-excursion
5961              (set-buffer gnus-group-buffer)
5962              (point)))
5963           (group
5964            (if (eq gnus-keep-same-level 'best)
5965                (gnus-summary-best-group gnus-newsgroup-name)
5966              (gnus-summary-search-group backward gnus-keep-same-level))))
5967       ;; For some reason, the group window gets selected.  We change
5968       ;; it back.
5969       (select-window (get-buffer-window (current-buffer)))
5970       ;; Select next unread newsgroup automagically.
5971       (cond
5972        ((or (not gnus-auto-select-next)
5973             (not cmd))
5974         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5975        ((or (eq gnus-auto-select-next 'quietly)
5976             (and (eq gnus-auto-select-next 'slightly-quietly)
5977                  push)
5978             (and (eq gnus-auto-select-next 'almost-quietly)
5979                  (gnus-summary-last-article-p)))
5980         ;; Select quietly.
5981         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5982             (gnus-summary-exit)
5983           (gnus-message 7 "No more%s articles (%s)..."
5984                         (if unread " unread" "")
5985                         (if group (concat "selecting " group)
5986                           "exiting"))
5987           (gnus-summary-next-group nil group backward)))
5988        (t
5989         (when (gnus-key-press-event-p last-input-event)
5990           (gnus-summary-walk-group-buffer
5991            gnus-newsgroup-name cmd unread backward point))))))))
5992
5993 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5994   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5995                       (?\C-p (gnus-group-prev-unread-group 1))))
5996         (cursor-in-echo-area t)
5997         keve key group ended)
5998     (save-excursion
5999       (set-buffer gnus-group-buffer)
6000       (goto-char start)
6001       (setq group
6002             (if (eq gnus-keep-same-level 'best)
6003                 (gnus-summary-best-group gnus-newsgroup-name)
6004               (gnus-summary-search-group backward gnus-keep-same-level))))
6005     (while (not ended)
6006       (gnus-message
6007        5 "No more%s articles%s" (if unread " unread" "")
6008        (if (and group
6009                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6010            (format " (Type %s for %s [%s])"
6011                    (single-key-description cmd) group
6012                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6013          (format " (Type %s to exit %s)"
6014                  (single-key-description cmd)
6015                  gnus-newsgroup-name)))
6016       ;; Confirm auto selection.
6017       (setq key (car (setq keve (gnus-read-event-char))))
6018       (setq ended t)
6019       (cond
6020        ((assq key keystrokes)
6021         (let ((obuf (current-buffer)))
6022           (switch-to-buffer gnus-group-buffer)
6023           (when group
6024             (gnus-group-jump-to-group group))
6025           (eval (cadr (assq key keystrokes)))
6026           (setq group (gnus-group-group-name))
6027           (switch-to-buffer obuf))
6028         (setq ended nil))
6029        ((equal key cmd)
6030         (if (or (not group)
6031                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6032             (gnus-summary-exit)
6033           (gnus-summary-next-group nil group backward)))
6034        (t
6035         (push (cdr keve) unread-command-events))))))
6036
6037 (defun gnus-summary-next-unread-article ()
6038   "Select unread article after current one."
6039   (interactive)
6040   (gnus-summary-next-article
6041    (or (not (eq gnus-summary-goto-unread 'never))
6042        (gnus-summary-last-article-p (gnus-summary-article-number)))
6043    (and gnus-auto-select-same
6044         (gnus-summary-article-subject))))
6045
6046 (defun gnus-summary-prev-article (&optional unread subject)
6047   "Select the article after the current one.
6048 If UNREAD is non-nil, only unread articles are selected."
6049   (interactive "P")
6050   (gnus-summary-next-article unread subject t))
6051
6052 (defun gnus-summary-prev-unread-article ()
6053   "Select unread article before current one."
6054   (interactive)
6055   (gnus-summary-prev-article
6056    (or (not (eq gnus-summary-goto-unread 'never))
6057        (gnus-summary-first-article-p (gnus-summary-article-number)))
6058    (and gnus-auto-select-same
6059         (gnus-summary-article-subject))))
6060
6061 (defun gnus-summary-next-page (&optional lines circular)
6062   "Show next page of the selected article.
6063 If at the end of the current article, select the next article.
6064 LINES says how many lines should be scrolled up.
6065
6066 If CIRCULAR is non-nil, go to the start of the article instead of
6067 selecting the next article when reaching the end of the current
6068 article."
6069   (interactive "P")
6070   (setq gnus-summary-buffer (current-buffer))
6071   (gnus-set-global-variables)
6072   (let ((article (gnus-summary-article-number))
6073         (article-window (get-buffer-window gnus-article-buffer t))
6074         endp)
6075     ;; If the buffer is empty, we have no article.
6076     (unless article
6077       (error "No article to select"))
6078     (gnus-configure-windows 'article)
6079     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6080         (if (and (eq gnus-summary-goto-unread 'never)
6081                  (not (gnus-summary-last-article-p article)))
6082             (gnus-summary-next-article)
6083           (gnus-summary-next-unread-article))
6084       (if (or (null gnus-current-article)
6085               (null gnus-article-current)
6086               (/= article (cdr gnus-article-current))
6087               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6088           ;; Selected subject is different from current article's.
6089           (gnus-summary-display-article article)
6090         (when article-window
6091           (gnus-eval-in-buffer-window gnus-article-buffer
6092             (setq endp (gnus-article-next-page lines)))
6093           (when endp
6094             (cond (circular
6095                    (gnus-summary-beginning-of-article))
6096                   (lines
6097                    (gnus-message 3 "End of message"))
6098                   ((null lines)
6099                    (if (and (eq gnus-summary-goto-unread 'never)
6100                             (not (gnus-summary-last-article-p article)))
6101                        (gnus-summary-next-article)
6102                      (gnus-summary-next-unread-article))))))))
6103     (gnus-summary-recenter)
6104     (gnus-summary-position-point)))
6105
6106 (defun gnus-summary-prev-page (&optional lines move)
6107   "Show previous page of selected article.
6108 Argument LINES specifies lines to be scrolled down.
6109 If MOVE, move to the previous unread article if point is at
6110 the beginning of the buffer."
6111   (interactive "P")
6112   (let ((article (gnus-summary-article-number))
6113         (article-window (get-buffer-window gnus-article-buffer t))
6114         endp)
6115     (gnus-configure-windows 'article)
6116     (if (or (null gnus-current-article)
6117             (null gnus-article-current)
6118             (/= article (cdr gnus-article-current))
6119             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6120         ;; Selected subject is different from current article's.
6121         (gnus-summary-display-article article)
6122       (gnus-summary-recenter)
6123       (when article-window
6124         (gnus-eval-in-buffer-window gnus-article-buffer
6125           (setq endp (gnus-article-prev-page lines)))
6126         (when (and move endp)
6127           (cond (lines
6128                  (gnus-message 3 "Beginning of message"))
6129                 ((null lines)
6130                  (if (and (eq gnus-summary-goto-unread 'never)
6131                           (not (gnus-summary-first-article-p article)))
6132                      (gnus-summary-prev-article)
6133                    (gnus-summary-prev-unread-article))))))))
6134   (gnus-summary-position-point))
6135
6136 (defun gnus-summary-prev-page-or-article (&optional lines)
6137   "Show previous page of selected article.
6138 Argument LINES specifies lines to be scrolled down.
6139 If at the beginning of the article, go to the next article."
6140   (interactive "P")
6141   (gnus-summary-prev-page lines t))
6142
6143 (defun gnus-summary-scroll-up (lines)
6144   "Scroll up (or down) one line current article.
6145 Argument LINES specifies lines to be scrolled up (or down if negative)."
6146   (interactive "p")
6147   (gnus-configure-windows 'article)
6148   (gnus-summary-show-thread)
6149   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6150     (gnus-eval-in-buffer-window gnus-article-buffer
6151       (cond ((> lines 0)
6152              (when (gnus-article-next-page lines)
6153                (gnus-message 3 "End of message")))
6154             ((< lines 0)
6155              (gnus-article-prev-page (- lines))))))
6156   (gnus-summary-recenter)
6157   (gnus-summary-position-point))
6158
6159 (defun gnus-summary-scroll-down (lines)
6160   "Scroll down (or up) one line current article.
6161 Argument LINES specifies lines to be scrolled down (or up if negative)."
6162   (interactive "p")
6163   (gnus-summary-scroll-up (- lines)))
6164
6165 (defun gnus-summary-next-same-subject ()
6166   "Select next article which has the same subject as current one."
6167   (interactive)
6168   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6169
6170 (defun gnus-summary-prev-same-subject ()
6171   "Select previous article which has the same subject as current one."
6172   (interactive)
6173   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6174
6175 (defun gnus-summary-next-unread-same-subject ()
6176   "Select next unread article which has the same subject as current one."
6177   (interactive)
6178   (gnus-summary-next-article t (gnus-summary-article-subject)))
6179
6180 (defun gnus-summary-prev-unread-same-subject ()
6181   "Select previous unread article which has the same subject as current one."
6182   (interactive)
6183   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6184
6185 (defun gnus-summary-first-unread-article ()
6186   "Select the first unread article.
6187 Return nil if there are no unread articles."
6188   (interactive)
6189   (prog1
6190       (when (gnus-summary-first-subject t)
6191         (gnus-summary-show-thread)
6192         (gnus-summary-first-subject t)
6193         (gnus-summary-display-article (gnus-summary-article-number)))
6194     (gnus-summary-position-point)))
6195
6196 (defun gnus-summary-first-unread-subject ()
6197   "Place the point on the subject line of the first unread article.
6198 Return nil if there are no unread articles."
6199   (interactive)
6200   (prog1
6201       (when (gnus-summary-first-subject t)
6202         (gnus-summary-show-thread)
6203         (gnus-summary-first-subject t))
6204     (gnus-summary-position-point)))
6205
6206 (defun gnus-summary-first-article ()
6207   "Select the first article.
6208 Return nil if there are no articles."
6209   (interactive)
6210   (prog1
6211       (when (gnus-summary-first-subject)
6212         (gnus-summary-show-thread)
6213         (gnus-summary-first-subject)
6214         (gnus-summary-display-article (gnus-summary-article-number)))
6215     (gnus-summary-position-point)))
6216
6217 (defun gnus-summary-best-unread-article ()
6218   "Select the unread article with the highest score."
6219   (interactive)
6220   (let ((best -1000000)
6221         (data gnus-newsgroup-data)
6222         article score)
6223     (while data
6224       (and (gnus-data-unread-p (car data))
6225            (> (setq score
6226                     (gnus-summary-article-score (gnus-data-number (car data))))
6227               best)
6228            (setq best score
6229                  article (gnus-data-number (car data))))
6230       (setq data (cdr data)))
6231     (prog1
6232         (if article
6233             (gnus-summary-goto-article article)
6234           (error "No unread articles"))
6235       (gnus-summary-position-point))))
6236
6237 (defun gnus-summary-last-subject ()
6238   "Go to the last displayed subject line in the group."
6239   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6240     (when article
6241       (gnus-summary-goto-subject article))))
6242
6243 (defun gnus-summary-goto-article (article &optional all-headers force)
6244   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6245 If ALL-HEADERS is non-nil, no header lines are hidden.
6246 If FORCE, go to the article even if it isn't displayed.  If FORCE
6247 is a number, it is the line the article is to be displayed on."
6248   (interactive
6249    (list
6250     (completing-read
6251      "Article number or Message-ID: "
6252      (mapcar (lambda (number) (list (int-to-string number)))
6253              gnus-newsgroup-limit))
6254     current-prefix-arg
6255     t))
6256   (prog1
6257       (if (and (stringp article)
6258                (string-match "@" article))
6259           (gnus-summary-refer-article article)
6260         (when (stringp article)
6261           (setq article (string-to-number article)))
6262         (if (gnus-summary-goto-subject article force)
6263             (gnus-summary-display-article article all-headers)
6264           (gnus-message 4 "Couldn't go to article %s" article) nil))
6265     (gnus-summary-position-point)))
6266
6267 (defun gnus-summary-goto-last-article ()
6268   "Go to the previously read article."
6269   (interactive)
6270   (prog1
6271       (when gnus-last-article
6272         (gnus-summary-goto-article gnus-last-article nil t))
6273     (gnus-summary-position-point)))
6274
6275 (defun gnus-summary-pop-article (number)
6276   "Pop one article off the history and go to the previous.
6277 NUMBER articles will be popped off."
6278   (interactive "p")
6279   (let (to)
6280     (setq gnus-newsgroup-history
6281           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6282     (if to
6283         (gnus-summary-goto-article (car to) nil t)
6284       (error "Article history empty")))
6285   (gnus-summary-position-point))
6286
6287 ;; Summary commands and functions for limiting the summary buffer.
6288
6289 (defun gnus-summary-limit-to-articles (n)
6290   "Limit the summary buffer to the next N articles.
6291 If not given a prefix, use the process marked articles instead."
6292   (interactive "P")
6293   (prog1
6294       (let ((articles (gnus-summary-work-articles n)))
6295         (setq gnus-newsgroup-processable nil)
6296         (gnus-summary-limit articles))
6297     (gnus-summary-position-point)))
6298
6299 (defun gnus-summary-pop-limit (&optional total)
6300   "Restore the previous limit.
6301 If given a prefix, remove all limits."
6302   (interactive "P")
6303   (when total
6304     (setq gnus-newsgroup-limits
6305           (list (mapcar (lambda (h) (mail-header-number h))
6306                         gnus-newsgroup-headers))))
6307   (unless gnus-newsgroup-limits
6308     (error "No limit to pop"))
6309   (prog1
6310       (gnus-summary-limit nil 'pop)
6311     (gnus-summary-position-point)))
6312
6313 (defun gnus-summary-limit-to-subject (subject &optional header)
6314   "Limit the summary buffer to articles that have subjects that match a regexp."
6315   (interactive "sLimit to subject (regexp): ")
6316   (unless header
6317     (setq header "subject"))
6318   (when (not (equal "" subject))
6319     (prog1
6320         (let ((articles (gnus-summary-find-matching
6321                          (or header "subject") subject 'all)))
6322           (unless articles
6323             (error "Found no matches for \"%s\"" subject))
6324           (gnus-summary-limit articles))
6325       (gnus-summary-position-point))))
6326
6327 (defun gnus-summary-limit-to-author (from)
6328   "Limit the summary buffer to articles that have authors that match a regexp."
6329   (interactive "sLimit to author (regexp): ")
6330   (gnus-summary-limit-to-subject from "from"))
6331
6332 (defun gnus-summary-limit-to-age (age &optional younger-p)
6333   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6334 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6335 articles that are younger than AGE days."
6336   (interactive
6337    (let ((younger current-prefix-arg)
6338          (days-got nil)
6339          days)
6340      (while (not days-got)
6341        (setq days (if younger
6342                       (read-string "Limit to articles within (in days): ")
6343                     (read-string "Limit to articles old than (in days): ")))
6344        (when (> (length days) 0)
6345          (setq days (read days)))
6346        (if (numberp days)
6347            (setq days-got t)
6348          (message "Please enter a number.")
6349          (sleep-for 1)))
6350      (list days younger)))
6351   (prog1
6352       (let ((data gnus-newsgroup-data)
6353             (cutoff (days-to-time age))
6354             articles d date is-younger)
6355         (while (setq d (pop data))
6356           (when (and (vectorp (gnus-data-header d))
6357                      (setq date (mail-header-date (gnus-data-header d))))
6358             (setq is-younger (time-less-p
6359                               (time-since (condition-case ()
6360                                               (date-to-time date)
6361                                             (error '(0 0))))
6362                               cutoff))
6363             (when (if younger-p
6364                       is-younger
6365                     (not is-younger))
6366               (push (gnus-data-number d) articles))))
6367         (gnus-summary-limit (nreverse articles)))
6368     (gnus-summary-position-point)))
6369
6370 (defun gnus-summary-limit-to-extra (header regexp)
6371   "Limit the summary buffer to articles that match an 'extra' header."
6372   (interactive
6373    (let ((header
6374           (intern
6375            (gnus-completing-read
6376             (symbol-name (car gnus-extra-headers))
6377             "Limit extra header:"
6378             (mapcar (lambda (x)
6379                       (cons (symbol-name x) x))
6380                     gnus-extra-headers)
6381             nil
6382             t))))
6383      (list header
6384            (read-string (format "Limit to header %s (regexp): " header)))))
6385   (when (not (equal "" regexp))
6386     (prog1
6387         (let ((articles (gnus-summary-find-matching
6388                          (cons 'extra header) regexp 'all)))
6389           (unless articles
6390             (error "Found no matches for \"%s\"" regexp))
6391           (gnus-summary-limit articles))
6392       (gnus-summary-position-point))))
6393
6394 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6395 (make-obsolete
6396  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6397
6398 (defun gnus-summary-limit-to-unread (&optional all)
6399   "Limit the summary buffer to articles that are not marked as read.
6400 If ALL is non-nil, limit strictly to unread articles."
6401   (interactive "P")
6402   (if all
6403       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6404     (gnus-summary-limit-to-marks
6405      ;; Concat all the marks that say that an article is read and have
6406      ;; those removed.
6407      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6408            gnus-killed-mark gnus-kill-file-mark
6409            gnus-low-score-mark gnus-expirable-mark
6410            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6411            gnus-duplicate-mark gnus-souped-mark)
6412      'reverse)))
6413
6414 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6415 (make-obsolete 'gnus-summary-delete-marked-with
6416                'gnus-summary-limit-exlude-marks)
6417
6418 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6419   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6420 If REVERSE, limit the summary buffer to articles that are marked
6421 with MARKS.  MARKS can either be a string of marks or a list of marks.
6422 Returns how many articles were removed."
6423   (interactive "sMarks: ")
6424   (gnus-summary-limit-to-marks marks t))
6425
6426 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6427   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6428 If REVERSE (the prefix), limit the summary buffer to articles that are
6429 not marked with MARKS.  MARKS can either be a string of marks or a
6430 list of marks.
6431 Returns how many articles were removed."
6432   (interactive "sMarks: \nP")
6433   (prog1
6434       (let ((data gnus-newsgroup-data)
6435             (marks (if (listp marks) marks
6436                      (append marks nil))) ; Transform to list.
6437             articles)
6438         (while data
6439           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6440                   (memq (gnus-data-mark (car data)) marks))
6441             (push (gnus-data-number (car data)) articles))
6442           (setq data (cdr data)))
6443         (gnus-summary-limit articles))
6444     (gnus-summary-position-point)))
6445
6446 (defun gnus-summary-limit-to-score (&optional score)
6447   "Limit to articles with score at or above SCORE."
6448   (interactive "P")
6449   (setq score (if score
6450                   (prefix-numeric-value score)
6451                 (or gnus-summary-default-score 0)))
6452   (let ((data gnus-newsgroup-data)
6453         articles)
6454     (while data
6455       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6456                 score)
6457         (push (gnus-data-number (car data)) articles))
6458       (setq data (cdr data)))
6459     (prog1
6460         (gnus-summary-limit articles)
6461       (gnus-summary-position-point))))
6462
6463 (defun gnus-summary-limit-include-thread (id)
6464   "Display all the hidden articles that in the current thread."
6465   (interactive (list (mail-header-id (gnus-summary-article-header))))
6466   (let ((articles (gnus-articles-in-thread
6467                    (gnus-id-to-thread (gnus-root-id id)))))
6468     (prog1
6469         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6470       (gnus-summary-position-point))))
6471
6472 (defun gnus-summary-limit-include-dormant ()
6473   "Display all the hidden articles that are marked as dormant.
6474 Note that this command only works on a subset of the articles currently
6475 fetched for this group."
6476   (interactive)
6477   (unless gnus-newsgroup-dormant
6478     (error "There are no dormant articles in this group"))
6479   (prog1
6480       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6481     (gnus-summary-position-point)))
6482
6483 (defun gnus-summary-limit-exclude-dormant ()
6484   "Hide all dormant articles."
6485   (interactive)
6486   (prog1
6487       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6488     (gnus-summary-position-point)))
6489
6490 (defun gnus-summary-limit-exclude-childless-dormant ()
6491   "Hide all dormant articles that have no children."
6492   (interactive)
6493   (let ((data (gnus-data-list t))
6494         articles d children)
6495     ;; Find all articles that are either not dormant or have
6496     ;; children.
6497     (while (setq d (pop data))
6498       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6499                 (and (setq children
6500                            (gnus-article-children (gnus-data-number d)))
6501                      (let (found)
6502                        (while children
6503                          (when (memq (car children) articles)
6504                            (setq children nil
6505                                  found t))
6506                          (pop children))
6507                        found)))
6508         (push (gnus-data-number d) articles)))
6509     ;; Do the limiting.
6510     (prog1
6511         (gnus-summary-limit articles)
6512       (gnus-summary-position-point))))
6513
6514 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6515   "Mark all unread excluded articles as read.
6516 If ALL, mark even excluded ticked and dormants as read."
6517   (interactive "P")
6518   (let ((articles (gnus-sorted-complement
6519                    (sort
6520                     (mapcar (lambda (h) (mail-header-number h))
6521                             gnus-newsgroup-headers)
6522                     '<)
6523                    (sort gnus-newsgroup-limit '<)))
6524         article)
6525     (setq gnus-newsgroup-unreads
6526           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6527     (if all
6528         (setq gnus-newsgroup-dormant nil
6529               gnus-newsgroup-marked nil
6530               gnus-newsgroup-reads
6531               (nconc
6532                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6533                gnus-newsgroup-reads))
6534       (while (setq article (pop articles))
6535         (unless (or (memq article gnus-newsgroup-dormant)
6536                     (memq article gnus-newsgroup-marked))
6537           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6538
6539 (defun gnus-summary-limit (articles &optional pop)
6540   (if pop
6541       ;; We pop the previous limit off the stack and use that.
6542       (setq articles (car gnus-newsgroup-limits)
6543             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6544     ;; We use the new limit, so we push the old limit on the stack.
6545     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6546   ;; Set the limit.
6547   (setq gnus-newsgroup-limit articles)
6548   (let ((total (length gnus-newsgroup-data))
6549         (data (gnus-data-find-list (gnus-summary-article-number)))
6550         (gnus-summary-mark-below nil)   ; Inhibit this.
6551         found)
6552     ;; This will do all the work of generating the new summary buffer
6553     ;; according to the new limit.
6554     (gnus-summary-prepare)
6555     ;; Hide any threads, possibly.
6556     (and gnus-show-threads
6557          gnus-thread-hide-subtree
6558          (gnus-summary-hide-all-threads))
6559     ;; Try to return to the article you were at, or one in the
6560     ;; neighborhood.
6561     (when data
6562       ;; We try to find some article after the current one.
6563       (while data
6564         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6565           (setq data nil
6566                 found t))
6567         (setq data (cdr data))))
6568     (unless found
6569       ;; If there is no data, that means that we were after the last
6570       ;; article.  The same goes when we can't find any articles
6571       ;; after the current one.
6572       (goto-char (point-max))
6573       (gnus-summary-find-prev))
6574     (gnus-set-mode-line 'summary)
6575     ;; We return how many articles were removed from the summary
6576     ;; buffer as a result of the new limit.
6577     (- total (length gnus-newsgroup-data))))
6578
6579 (defsubst gnus-invisible-cut-children (threads)
6580   (let ((num 0))
6581     (while threads
6582       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6583         (incf num))
6584       (pop threads))
6585     (< num 2)))
6586
6587 (defsubst gnus-cut-thread (thread)
6588   "Go forwards in the thread until we find an article that we want to display."
6589   (when (or (eq gnus-fetch-old-headers 'some)
6590             (eq gnus-fetch-old-headers 'invisible)
6591             (numberp gnus-fetch-old-headers)
6592             (eq gnus-build-sparse-threads 'some)
6593             (eq gnus-build-sparse-threads 'more))
6594     ;; Deal with old-fetched headers and sparse threads.
6595     (while (and
6596             thread
6597             (or
6598              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6599              (gnus-summary-article-ancient-p
6600               (mail-header-number (car thread))))
6601             (if (or (<= (length (cdr thread)) 1)
6602                     (eq gnus-fetch-old-headers 'invisible))
6603                 (setq gnus-newsgroup-limit
6604                       (delq (mail-header-number (car thread))
6605                             gnus-newsgroup-limit)
6606                       thread (cadr thread))
6607               (when (gnus-invisible-cut-children (cdr thread))
6608                 (let ((th (cdr thread)))
6609                   (while th
6610                     (if (memq (mail-header-number (caar th))
6611                               gnus-newsgroup-limit)
6612                         (setq thread (car th)
6613                               th nil)
6614                       (setq th (cdr th))))))))))
6615   thread)
6616
6617 (defun gnus-cut-threads (threads)
6618   "Cut off all uninteresting articles from the beginning of threads."
6619   (when (or (eq gnus-fetch-old-headers 'some)
6620             (eq gnus-fetch-old-headers 'invisible)
6621             (numberp gnus-fetch-old-headers)
6622             (eq gnus-build-sparse-threads 'some)
6623             (eq gnus-build-sparse-threads 'more))
6624     (let ((th threads))
6625       (while th
6626         (setcar th (gnus-cut-thread (car th)))
6627         (setq th (cdr th)))))
6628   ;; Remove nixed out threads.
6629   (delq nil threads))
6630
6631 (defun gnus-summary-initial-limit (&optional show-if-empty)
6632   "Figure out what the initial limit is supposed to be on group entry.
6633 This entails weeding out unwanted dormants, low-scored articles,
6634 fetch-old-headers verbiage, and so on."
6635   ;; Most groups have nothing to remove.
6636   (if (or gnus-inhibit-limiting
6637           (and (null gnus-newsgroup-dormant)
6638                (not (eq gnus-fetch-old-headers 'some))
6639                (not (numberp gnus-fetch-old-headers))
6640                (not (eq gnus-fetch-old-headers 'invisible))
6641                (null gnus-summary-expunge-below)
6642                (not (eq gnus-build-sparse-threads 'some))
6643                (not (eq gnus-build-sparse-threads 'more))
6644                (null gnus-thread-expunge-below)
6645                (not gnus-use-nocem)))
6646       ()                                ; Do nothing.
6647     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6648     (setq gnus-newsgroup-limit nil)
6649     (mapatoms
6650      (lambda (node)
6651        (unless (car (symbol-value node))
6652          ;; These threads have no parents -- they are roots.
6653          (let ((nodes (cdr (symbol-value node)))
6654                thread)
6655            (while nodes
6656              (if (and gnus-thread-expunge-below
6657                       (< (gnus-thread-total-score (car nodes))
6658                          gnus-thread-expunge-below))
6659                  (gnus-expunge-thread (pop nodes))
6660                (setq thread (pop nodes))
6661                (gnus-summary-limit-children thread))))))
6662      gnus-newsgroup-dependencies)
6663     ;; If this limitation resulted in an empty group, we might
6664     ;; pop the previous limit and use it instead.
6665     (when (and (not gnus-newsgroup-limit)
6666                show-if-empty)
6667       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6668     gnus-newsgroup-limit))
6669
6670 (defun gnus-summary-limit-children (thread)
6671   "Return 1 if this subthread is visible and 0 if it is not."
6672   ;; First we get the number of visible children to this thread.  This
6673   ;; is done by recursing down the thread using this function, so this
6674   ;; will really go down to a leaf article first, before slowly
6675   ;; working its way up towards the root.
6676   (when thread
6677     (let ((children
6678            (if (cdr thread)
6679                (apply '+ (mapcar 'gnus-summary-limit-children
6680                                  (cdr thread)))
6681              0))
6682           (number (mail-header-number (car thread)))
6683           score)
6684       (if (and
6685            (not (memq number gnus-newsgroup-marked))
6686            (or
6687             ;; If this article is dormant and has absolutely no visible
6688             ;; children, then this article isn't visible.
6689             (and (memq number gnus-newsgroup-dormant)
6690                  (zerop children))
6691             ;; If this is "fetch-old-headered" and there is no
6692             ;; visible children, then we don't want this article.
6693             (and (or (eq gnus-fetch-old-headers 'some)
6694                      (numberp gnus-fetch-old-headers))
6695                  (gnus-summary-article-ancient-p number)
6696                  (zerop children))
6697             ;; If this is "fetch-old-headered" and `invisible', then
6698             ;; we don't want this article.
6699             (and (eq gnus-fetch-old-headers 'invisible)
6700                  (gnus-summary-article-ancient-p number))
6701             ;; If this is a sparsely inserted article with no children,
6702             ;; we don't want it.
6703             (and (eq gnus-build-sparse-threads 'some)
6704                  (gnus-summary-article-sparse-p number)
6705                  (zerop children))
6706             ;; If we use expunging, and this article is really
6707             ;; low-scored, then we don't want this article.
6708             (when (and gnus-summary-expunge-below
6709                        (< (setq score
6710                                 (or (cdr (assq number gnus-newsgroup-scored))
6711                                     gnus-summary-default-score))
6712                           gnus-summary-expunge-below))
6713               ;; We increase the expunge-tally here, but that has
6714               ;; nothing to do with the limits, really.
6715               (incf gnus-newsgroup-expunged-tally)
6716               ;; We also mark as read here, if that's wanted.
6717               (when (and gnus-summary-mark-below
6718                          (< score gnus-summary-mark-below))
6719                 (setq gnus-newsgroup-unreads
6720                       (delq number gnus-newsgroup-unreads))
6721                 (if gnus-newsgroup-auto-expire
6722                     (push number gnus-newsgroup-expirable)
6723                   (push (cons number gnus-low-score-mark)
6724                         gnus-newsgroup-reads)))
6725               t)
6726             ;; Check NoCeM things.
6727             (if (and gnus-use-nocem
6728                      (gnus-nocem-unwanted-article-p
6729                       (mail-header-id (car thread))))
6730                 (progn
6731                   (setq gnus-newsgroup-unreads
6732                         (delq number gnus-newsgroup-unreads))
6733                   t))))
6734           ;; Nope, invisible article.
6735           0
6736         ;; Ok, this article is to be visible, so we add it to the limit
6737         ;; and return 1.
6738         (push number gnus-newsgroup-limit)
6739         1))))
6740
6741 (defun gnus-expunge-thread (thread)
6742   "Mark all articles in THREAD as read."
6743   (let* ((number (mail-header-number (car thread))))
6744     (incf gnus-newsgroup-expunged-tally)
6745     ;; We also mark as read here, if that's wanted.
6746     (setq gnus-newsgroup-unreads
6747           (delq number gnus-newsgroup-unreads))
6748     (if gnus-newsgroup-auto-expire
6749         (push number gnus-newsgroup-expirable)
6750       (push (cons number gnus-low-score-mark)
6751             gnus-newsgroup-reads)))
6752   ;; Go recursively through all subthreads.
6753   (mapcar 'gnus-expunge-thread (cdr thread)))
6754
6755 ;; Summary article oriented commands
6756
6757 (defun gnus-summary-refer-parent-article (n)
6758   "Refer parent article N times.
6759 If N is negative, go to ancestor -N instead.
6760 The difference between N and the number of articles fetched is returned."
6761   (interactive "p")
6762   (let ((skip 1)
6763         error header ref)
6764     (when (not (natnump n))
6765       (setq skip (abs n)
6766             n 1))
6767     (while (and (> n 0)
6768                 (not error))
6769       (setq header (gnus-summary-article-header))
6770       (if (and (eq (mail-header-number header)
6771                    (cdr gnus-article-current))
6772                (equal gnus-newsgroup-name
6773                       (car gnus-article-current)))
6774           ;; If we try to find the parent of the currently
6775           ;; displayed article, then we take a look at the actual
6776           ;; References header, since this is slightly more
6777           ;; reliable than the References field we got from the
6778           ;; server.
6779           (save-excursion
6780             (set-buffer gnus-original-article-buffer)
6781             (nnheader-narrow-to-headers)
6782             (unless (setq ref (message-fetch-field "references"))
6783               (setq ref (message-fetch-field "in-reply-to")))
6784             (widen))
6785         (setq ref
6786               ;; It's not the current article, so we take a bet on
6787               ;; the value we got from the server.
6788               (mail-header-references header)))
6789       (if (and ref
6790                (not (equal ref "")))
6791           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6792             (gnus-message 1 "Couldn't find parent"))
6793         (gnus-message 1 "No references in article %d"
6794                       (gnus-summary-article-number))
6795         (setq error t))
6796       (decf n))
6797     (gnus-summary-position-point)
6798     n))
6799
6800 (defun gnus-summary-refer-references ()
6801   "Fetch all articles mentioned in the References header.
6802 Return the number of articles fetched."
6803   (interactive)
6804   (let ((ref (mail-header-references (gnus-summary-article-header)))
6805         (current (gnus-summary-article-number))
6806         (n 0))
6807     (if (or (not ref)
6808             (equal ref ""))
6809         (error "No References in the current article")
6810       ;; For each Message-ID in the References header...
6811       (while (string-match "<[^>]*>" ref)
6812         (incf n)
6813         ;; ... fetch that article.
6814         (gnus-summary-refer-article
6815          (prog1 (match-string 0 ref)
6816            (setq ref (substring ref (match-end 0))))))
6817       (gnus-summary-goto-subject current)
6818       (gnus-summary-position-point)
6819       n)))
6820
6821 (defun gnus-summary-refer-thread (&optional limit)
6822   "Fetch all articles in the current thread.
6823 If LIMIT (the numerical prefix), fetch that many old headers instead
6824 of what's specified by the `gnus-refer-thread-limit' variable."
6825   (interactive "P")
6826   (let ((id (mail-header-id (gnus-summary-article-header)))
6827         (limit (if limit (prefix-numeric-value limit)
6828                  gnus-refer-thread-limit)))
6829     ;; We want to fetch LIMIT *old* headers, but we also have to
6830     ;; re-fetch all the headers in the current buffer, because many of
6831     ;; them may be undisplayed.  So we adjust LIMIT.
6832     (when (numberp limit)
6833       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6834     (unless (eq gnus-fetch-old-headers 'invisible)
6835       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6836       ;; Retrieve the headers and read them in.
6837       (if (eq (gnus-retrieve-headers
6838                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6839               'nov)
6840           (gnus-build-all-threads)
6841         (error "Can't fetch thread from backends that don't support NOV"))
6842       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6843     (gnus-summary-limit-include-thread id)))
6844
6845 (defun gnus-summary-refer-article (message-id)
6846   "Fetch an article specified by MESSAGE-ID."
6847   (interactive "sMessage-ID: ")
6848   (when (and (stringp message-id)
6849              (not (zerop (length message-id))))
6850     ;; Construct the correct Message-ID if necessary.
6851     ;; Suggested by tale@pawl.rpi.edu.
6852     (unless (string-match "^<" message-id)
6853       (setq message-id (concat "<" message-id)))
6854     (unless (string-match ">$" message-id)
6855       (setq message-id (concat message-id ">")))
6856     (let* ((header (gnus-id-to-header message-id))
6857            (sparse (and header
6858                         (gnus-summary-article-sparse-p
6859                          (mail-header-number header))
6860                         (memq (mail-header-number header)
6861                               gnus-newsgroup-limit)))
6862            number)
6863       (cond
6864        ;; If the article is present in the buffer we just go to it.
6865        ((and header
6866              (or (not (gnus-summary-article-sparse-p
6867                        (mail-header-number header)))
6868                  sparse))
6869         (prog1
6870             (gnus-summary-goto-article
6871              (mail-header-number header) nil t)
6872           (when sparse
6873             (gnus-summary-update-article (mail-header-number header)))))
6874        (t
6875         ;; We fetch the article.
6876         (catch 'found
6877           (dolist (gnus-override-method (gnus-refer-article-methods))
6878             (gnus-check-server gnus-override-method)
6879             ;; Fetch the header, and display the article.
6880             (when (setq number (gnus-summary-insert-subject message-id))
6881               (gnus-summary-select-article nil nil nil number)
6882               (throw 'found t)))
6883           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6884
6885 (defun gnus-refer-article-methods ()
6886   "Return a list of referrable methods."
6887   (cond
6888    ;; No method, so we default to current and native.
6889    ((null gnus-refer-article-method)
6890     (list gnus-current-select-method gnus-select-method))
6891    ;; Current.
6892    ((eq 'current gnus-refer-article-method)
6893     (list gnus-current-select-method))
6894    ;; List of select methods.
6895    ((not (and (symbolp (car gnus-refer-article-method))
6896               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6897     (let (out)
6898       (dolist (method gnus-refer-article-method)
6899         (push (if (eq 'current method)
6900                   gnus-current-select-method
6901                 method)
6902               out))
6903       (nreverse out)))
6904    ;; One single select method.
6905    (t
6906     (list gnus-refer-article-method))))
6907
6908 (defun gnus-summary-edit-parameters ()
6909   "Edit the group parameters of the current group."
6910   (interactive)
6911   (gnus-group-edit-group gnus-newsgroup-name 'params))
6912
6913 (defun gnus-summary-customize-parameters ()
6914   "Customize the group parameters of the current group."
6915   (interactive)
6916   (gnus-group-customize gnus-newsgroup-name))
6917
6918 (defun gnus-summary-enter-digest-group (&optional force)
6919   "Enter an nndoc group based on the current article.
6920 If FORCE, force a digest interpretation.  If not, try
6921 to guess what the document format is."
6922   (interactive "P")
6923   (let ((conf gnus-current-window-configuration))
6924     (save-excursion
6925       (gnus-summary-select-article))
6926     (setq gnus-current-window-configuration conf)
6927     (let* ((name (format "%s-%d"
6928                          (gnus-group-prefixed-name
6929                           gnus-newsgroup-name (list 'nndoc ""))
6930                          (save-excursion
6931                            (set-buffer gnus-summary-buffer)
6932                            gnus-current-article)))
6933            (ogroup gnus-newsgroup-name)
6934            (params (append (gnus-info-params (gnus-get-info ogroup))
6935                            (list (cons 'to-group ogroup))
6936                            (list (cons 'save-article-group ogroup))))
6937            (case-fold-search t)
6938            (buf (current-buffer))
6939            dig to-address)
6940       (save-excursion
6941         (set-buffer gnus-original-article-buffer)
6942         ;; Have the digest group inherit the main mail address of
6943         ;; the parent article.
6944         (when (setq to-address (or (message-fetch-field "reply-to")
6945                                    (message-fetch-field "from")))
6946           (setq params (append 
6947                         (list (cons 'to-address 
6948                                     (funcall gnus-decode-encoded-word-function
6949                                              to-address))))))
6950         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6951         (insert-buffer-substring gnus-original-article-buffer)
6952         ;; Remove lines that may lead nndoc to misinterpret the
6953         ;; document type.
6954         (narrow-to-region
6955          (goto-char (point-min))
6956          (or (search-forward "\n\n" nil t) (point)))
6957         (goto-char (point-min))
6958         (delete-matching-lines "^Path:\\|^From ")
6959         (widen))
6960       (unwind-protect
6961           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6962                     (gnus-newsgroup-ephemeral-ignored-charsets
6963                      gnus-newsgroup-ignored-charsets))
6964                 (gnus-group-read-ephemeral-group
6965                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6966                               (nndoc-article-type
6967                                ,(if force 'mbox 'guess))) t))
6968               ;; Make all postings to this group go to the parent group.
6969               (nconc (gnus-info-params (gnus-get-info name))
6970                      params)
6971             ;; Couldn't select this doc group.
6972             (switch-to-buffer buf)
6973             (gnus-set-global-variables)
6974             (gnus-configure-windows 'summary)
6975             (gnus-message 3 "Article couldn't be entered?"))
6976         (kill-buffer dig)))))
6977
6978 (defun gnus-summary-read-document (n)
6979   "Open a new group based on the current article(s).
6980 This will allow you to read digests and other similar
6981 documents as newsgroups.
6982 Obeys the standard process/prefix convention."
6983   (interactive "P")
6984   (let* ((articles (gnus-summary-work-articles n))
6985          (ogroup gnus-newsgroup-name)
6986          (params (append (gnus-info-params (gnus-get-info ogroup))
6987                          (list (cons 'to-group ogroup))))
6988          article group egroup groups vgroup)
6989     (while (setq article (pop articles))
6990       (setq group (format "%s-%d" gnus-newsgroup-name article))
6991       (gnus-summary-remove-process-mark article)
6992       (when (gnus-summary-display-article article)
6993         (save-excursion
6994           (with-temp-buffer
6995             (insert-buffer-substring gnus-original-article-buffer)
6996             ;; Remove some headers that may lead nndoc to make
6997             ;; the wrong guess.
6998             (message-narrow-to-head)
6999             (goto-char (point-min))
7000             (delete-matching-lines "^\\(Path\\):\\|^From ")
7001             (widen)
7002             (if (setq egroup
7003                       (gnus-group-read-ephemeral-group
7004                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7005                                      (nndoc-article-type guess))
7006                        t nil t))
7007                 (progn
7008                   ;; Make all postings to this group go to the parent group.
7009                   (nconc (gnus-info-params (gnus-get-info egroup))
7010                          params)
7011                   (push egroup groups))
7012               ;; Couldn't select this doc group.
7013               (gnus-error 3 "Article couldn't be entered"))))))
7014     ;; Now we have selected all the documents.
7015     (cond
7016      ((not groups)
7017       (error "None of the articles could be interpreted as documents"))
7018      ((gnus-group-read-ephemeral-group
7019        (setq vgroup (format
7020                      "nnvirtual:%s-%s" gnus-newsgroup-name
7021                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7022        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7023        t
7024        (cons (current-buffer) 'summary)))
7025      (t
7026       (error "Couldn't select virtual nndoc group")))))
7027
7028 (defun gnus-summary-isearch-article (&optional regexp-p)
7029   "Do incremental search forward on the current article.
7030 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7031   (interactive "P")
7032   (gnus-summary-select-article)
7033   (gnus-configure-windows 'article)
7034   (gnus-eval-in-buffer-window gnus-article-buffer
7035     (save-restriction
7036       (widen)
7037       (isearch-forward regexp-p))))
7038
7039 (defun gnus-summary-search-article-forward (regexp &optional backward)
7040   "Search for an article containing REGEXP forward.
7041 If BACKWARD, search backward instead."
7042   (interactive
7043    (list (read-string
7044           (format "Search article %s (regexp%s): "
7045                   (if current-prefix-arg "backward" "forward")
7046                   (if gnus-last-search-regexp
7047                       (concat ", default " gnus-last-search-regexp)
7048                     "")))
7049          current-prefix-arg))
7050   (if (string-equal regexp "")
7051       (setq regexp (or gnus-last-search-regexp ""))
7052     (setq gnus-last-search-regexp regexp))
7053   (if (gnus-summary-search-article regexp backward)
7054       (gnus-summary-show-thread)
7055     (error "Search failed: \"%s\"" regexp)))
7056
7057 (defun gnus-summary-search-article-backward (regexp)
7058   "Search for an article containing REGEXP backward."
7059   (interactive
7060    (list (read-string
7061           (format "Search article backward (regexp%s): "
7062                   (if gnus-last-search-regexp
7063                       (concat ", default " gnus-last-search-regexp)
7064                     "")))))
7065   (gnus-summary-search-article-forward regexp 'backward))
7066
7067 (defun gnus-summary-search-article (regexp &optional backward)
7068   "Search for an article containing REGEXP.
7069 Optional argument BACKWARD means do search for backward.
7070 `gnus-select-article-hook' is not called during the search."
7071   ;; We have to require this here to make sure that the following
7072   ;; dynamic binding isn't shadowed by autoloading.
7073   (require 'gnus-async)
7074   (require 'gnus-art)
7075   (let ((gnus-select-article-hook nil)  ;Disable hook.
7076         (gnus-article-prepare-hook nil)
7077         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7078         (gnus-use-article-prefetch nil)
7079         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7080         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7081         (sum (current-buffer))
7082         (gnus-display-mime-function nil)
7083         (found nil)
7084         point)
7085     (gnus-save-hidden-threads
7086       (gnus-summary-select-article)
7087       (set-buffer gnus-article-buffer)
7088       (goto-char (window-point (get-buffer-window (current-buffer))))
7089       (when backward
7090         (forward-line -1))
7091       (while (not found)
7092         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7093         (if (if backward
7094                 (re-search-backward regexp nil t)
7095               (re-search-forward regexp nil t))
7096             ;; We found the regexp.
7097             (progn
7098               (setq found 'found)
7099               (beginning-of-line)
7100               (set-window-start
7101                (get-buffer-window (current-buffer))
7102                (point))
7103               (forward-line 1)
7104               (set-window-point
7105                (get-buffer-window (current-buffer))
7106                (point))
7107               (set-buffer sum)
7108               (setq point (point)))
7109           ;; We didn't find it, so we go to the next article.
7110           (set-buffer sum)
7111           (setq found 'not)
7112           (while (eq found 'not)
7113             (if (not (if backward (gnus-summary-find-prev)
7114                        (gnus-summary-find-next)))
7115                 ;; No more articles.
7116                 (setq found t)
7117               ;; Select the next article and adjust point.
7118               (unless (gnus-summary-article-sparse-p
7119                        (gnus-summary-article-number))
7120                 (setq found nil)
7121                 (gnus-summary-select-article)
7122                 (set-buffer gnus-article-buffer)
7123                 (widen)
7124                 (goto-char (if backward (point-max) (point-min))))))))
7125       (gnus-message 7 ""))
7126     ;; Return whether we found the regexp.
7127     (when (eq found 'found)
7128       (goto-char point)
7129       (gnus-summary-show-thread)
7130       (gnus-summary-goto-subject gnus-current-article)
7131       (gnus-summary-position-point)
7132       t)))
7133
7134 (defun gnus-summary-find-matching (header regexp &optional backward unread
7135                                           not-case-fold)
7136   "Return a list of all articles that match REGEXP on HEADER.
7137 The search stars on the current article and goes forwards unless
7138 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7139 If UNREAD is non-nil, only unread articles will
7140 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7141 in the comparisons."
7142   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7143                 (gnus-data-find-list
7144                  (gnus-summary-article-number) (gnus-data-list backward))))
7145         (case-fold-search (not not-case-fold))
7146         articles d func)
7147     (if (consp header)
7148         (if (eq (car header) 'extra)
7149             (setq func
7150                   `(lambda (h)
7151                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7152                          "")))
7153           (error "%s is an invalid header" header))
7154       (unless (fboundp (intern (concat "mail-header-" header)))
7155         (error "%s is not a valid header" header))
7156       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7157     (while data
7158       (setq d (car data))
7159       (and (or (not unread)             ; We want all articles...
7160                (gnus-data-unread-p d))  ; Or just unreads.
7161            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7162            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7163            (push (gnus-data-number d) articles)) ; Success!
7164       (setq data (cdr data)))
7165     (nreverse articles)))
7166
7167 (defun gnus-summary-execute-command (header regexp command &optional backward)
7168   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7169 If HEADER is an empty string (or nil), the match is done on the entire
7170 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7171   (interactive
7172    (list (let ((completion-ignore-case t))
7173            (completing-read
7174             "Header name: "
7175             (mapcar (lambda (string) (list string))
7176                     '("Number" "Subject" "From" "Lines" "Date"
7177                       "Message-ID" "Xref" "References" "Body"))
7178             nil 'require-match))
7179          (read-string "Regexp: ")
7180          (read-key-sequence "Command: ")
7181          current-prefix-arg))
7182   (when (equal header "Body")
7183     (setq header ""))
7184   ;; Hidden thread subtrees must be searched as well.
7185   (gnus-summary-show-all-threads)
7186   ;; We don't want to change current point nor window configuration.
7187   (save-excursion
7188     (save-window-excursion
7189       (gnus-message 6 "Executing %s..." (key-description command))
7190       ;; We'd like to execute COMMAND interactively so as to give arguments.
7191       (gnus-execute header regexp
7192                     `(call-interactively ',(key-binding command))
7193                     backward)
7194       (gnus-message 6 "Executing %s...done" (key-description command)))))
7195
7196 (defun gnus-summary-beginning-of-article ()
7197   "Scroll the article back to the beginning."
7198   (interactive)
7199   (gnus-summary-select-article)
7200   (gnus-configure-windows 'article)
7201   (gnus-eval-in-buffer-window gnus-article-buffer
7202     (widen)
7203     (goto-char (point-min))
7204     (when gnus-page-broken
7205       (gnus-narrow-to-page))))
7206
7207 (defun gnus-summary-end-of-article ()
7208   "Scroll to the end of the article."
7209   (interactive)
7210   (gnus-summary-select-article)
7211   (gnus-configure-windows 'article)
7212   (gnus-eval-in-buffer-window gnus-article-buffer
7213     (widen)
7214     (goto-char (point-max))
7215     (recenter -3)
7216     (when gnus-page-broken
7217       (gnus-narrow-to-page))))
7218
7219 (defun gnus-summary-print-article (&optional filename n)
7220   "Generate and print a PostScript image of the N next (mail) articles.
7221
7222 If N is negative, print the N previous articles.  If N is nil and articles
7223 have been marked with the process mark, print these instead.
7224
7225 If the optional first argument FILENAME is nil, send the image to the
7226 printer.  If FILENAME is a string, save the PostScript image in a file with
7227 that name.  If FILENAME is a number, prompt the user for the name of the file
7228 to save in."
7229   (interactive (list (ps-print-preprint current-prefix-arg)
7230                      current-prefix-arg))
7231   (dolist (article (gnus-summary-work-articles n))
7232     (gnus-summary-select-article nil nil 'pseudo article)
7233     (gnus-eval-in-buffer-window gnus-article-buffer
7234       (let ((buffer (generate-new-buffer " *print*")))
7235         (unwind-protect
7236             (progn
7237               (copy-to-buffer buffer (point-min) (point-max))
7238               (set-buffer buffer)
7239               (gnus-article-delete-invisible-text)
7240               (let ((ps-left-header
7241                      (list
7242                       (concat "("
7243                               (mail-header-subject gnus-current-headers) ")")
7244                       (concat "("
7245                               (mail-header-from gnus-current-headers) ")")))
7246                     (ps-right-header
7247                      (list
7248                       "/pagenumberstring load"
7249                       (concat "("
7250                               (mail-header-date gnus-current-headers) ")"))))
7251                 (gnus-run-hooks 'gnus-ps-print-hook)
7252                 (save-excursion
7253                   (ps-print-buffer-with-faces filename))))
7254           (kill-buffer buffer))))))
7255
7256 (defun gnus-summary-show-article (&optional arg)
7257   "Force re-fetching of the current article.
7258 If ARG (the prefix) is a number, show the article with the charset
7259 defined in `gnus-summary-show-article-charset-alist', or the charset
7260 inputed.
7261 If ARG (the prefix) is non-nil and not a number, show the raw article
7262 without any article massaging functions being run."
7263   (interactive "P")
7264   (cond
7265    ((numberp arg)
7266     (let ((gnus-newsgroup-charset
7267            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7268                (read-coding-system "Charset: ")))
7269           (gnus-newsgroup-ignored-charsets 'gnus-all))
7270       (gnus-summary-select-article nil 'force)
7271       (let ((deps gnus-newsgroup-dependencies)
7272             head header)
7273         (save-excursion
7274           (set-buffer gnus-original-article-buffer)
7275           (save-restriction
7276             (message-narrow-to-head)
7277             (setq head (buffer-string)))
7278           (with-temp-buffer
7279             (insert (format "211 %d Article retrieved.\n"
7280                             (cdr gnus-article-current)))
7281             (insert head)
7282             (insert ".\n")
7283             (let ((nntp-server-buffer (current-buffer)))
7284               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7285         (gnus-data-set-header
7286          (gnus-data-find (cdr gnus-article-current))
7287          header)
7288         (gnus-summary-update-article-line
7289          (cdr gnus-article-current) header))))
7290    ((not arg)
7291     ;; Select the article the normal way.
7292     (gnus-summary-select-article nil 'force))
7293    (t
7294     ;; We have to require this here to make sure that the following
7295     ;; dynamic binding isn't shadowed by autoloading.
7296     (require 'gnus-async)
7297     (require 'gnus-art)
7298     ;; Bind the article treatment functions to nil.
7299     (let ((gnus-have-all-headers t)
7300           gnus-article-prepare-hook
7301           gnus-article-decode-hook
7302           gnus-display-mime-function
7303           gnus-break-pages)
7304       ;; Destroy any MIME parts.
7305       (when (gnus-buffer-live-p gnus-article-buffer)
7306         (save-excursion
7307           (set-buffer gnus-article-buffer)
7308           (mm-destroy-parts gnus-article-mime-handles)
7309           ;; Set it to nil for safety reason.
7310           (setq gnus-article-mime-handle-alist nil)
7311           (setq gnus-article-mime-handles nil)))
7312       (gnus-summary-select-article nil 'force))))
7313   (gnus-summary-goto-subject gnus-current-article)
7314   (gnus-summary-position-point))
7315
7316 (defun gnus-summary-verbose-headers (&optional arg)
7317   "Toggle permanent full header display.
7318 If ARG is a positive number, turn header display on.
7319 If ARG is a negative number, turn header display off."
7320   (interactive "P")
7321   (setq gnus-show-all-headers
7322         (cond ((or (not (numberp arg))
7323                    (zerop arg))
7324                (not gnus-show-all-headers))
7325               ((natnump arg)
7326                t)))
7327   (gnus-summary-show-article))
7328
7329 (defun gnus-summary-toggle-header (&optional arg)
7330   "Show the headers if they are hidden, or hide them if they are shown.
7331 If ARG is a positive number, show the entire header.
7332 If ARG is a negative number, hide the unwanted header lines."
7333   (interactive "P")
7334   (save-excursion
7335     (set-buffer gnus-article-buffer)
7336     (save-restriction
7337       (let* ((buffer-read-only nil)
7338              (inhibit-point-motion-hooks t)
7339              hidden e)
7340         (setq hidden
7341               (if (numberp arg)
7342                   (>= arg 0)
7343                 (save-restriction
7344                   (article-narrow-to-head)
7345                   (gnus-article-hidden-text-p 'headers))))
7346         (goto-char (point-min))
7347         (when (search-forward "\n\n" nil t)
7348           (delete-region (point-min) (1- (point))))
7349         (goto-char (point-min))
7350         (save-excursion
7351           (set-buffer gnus-original-article-buffer)
7352           (goto-char (point-min))
7353           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7354         (insert-buffer-substring gnus-original-article-buffer 1 e)
7355         (save-restriction
7356           (narrow-to-region (point-min) (point))
7357           (article-decode-encoded-words)
7358           (if  hidden
7359               (let ((gnus-treat-hide-headers nil)
7360                     (gnus-treat-hide-boring-headers nil))
7361                 (setq gnus-article-wash-types
7362                       (delq 'headers gnus-article-wash-types))
7363                 (gnus-treat-article 'head))
7364             (gnus-treat-article 'head)))
7365         (gnus-set-mode-line 'article)))))
7366
7367 (defun gnus-summary-show-all-headers ()
7368   "Make all header lines visible."
7369   (interactive)
7370   (gnus-article-show-all-headers))
7371
7372 (defun gnus-summary-caesar-message (&optional arg)
7373   "Caesar rotate the current article by 13.
7374 The numerical prefix specifies how many places to rotate each letter
7375 forward."
7376   (interactive "P")
7377   (gnus-summary-select-article)
7378   (let ((mail-header-separator ""))
7379     (gnus-eval-in-buffer-window gnus-article-buffer
7380       (save-restriction
7381         (widen)
7382         (let ((start (window-start))
7383               buffer-read-only)
7384           (message-caesar-buffer-body arg)
7385           (set-window-start (get-buffer-window (current-buffer)) start))))))
7386
7387 (defun gnus-summary-stop-page-breaking ()
7388   "Stop page breaking in the current article."
7389   (interactive)
7390   (gnus-summary-select-article)
7391   (gnus-eval-in-buffer-window gnus-article-buffer
7392     (widen)
7393     (when (gnus-visual-p 'page-marker)
7394       (let ((buffer-read-only nil))
7395         (gnus-remove-text-with-property 'gnus-prev)
7396         (gnus-remove-text-with-property 'gnus-next))
7397       (setq gnus-page-broken nil))))
7398
7399 (defun gnus-summary-move-article (&optional n to-newsgroup
7400                                             select-method action)
7401   "Move the current article to a different newsgroup.
7402 If N is a positive number, move the N next articles.
7403 If N is a negative number, move the N previous articles.
7404 If N is nil and any articles have been marked with the process mark,
7405 move those articles instead.
7406 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7407 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7408 re-spool using this method.
7409
7410 For this function to work, both the current newsgroup and the
7411 newsgroup that you want to move to have to support the `request-move'
7412 and `request-accept' functions.
7413
7414 ACTION can be either `move' (the default), `crosspost' or `copy'."
7415   (interactive "P")
7416   (unless action
7417     (setq action 'move))
7418   ;; Disable marking as read.
7419   (let (gnus-mark-article-hook)
7420     (save-window-excursion
7421       (gnus-summary-select-article)))
7422   ;; Check whether the source group supports the required functions.
7423   (cond ((and (eq action 'move)
7424               (not (gnus-check-backend-function
7425                     'request-move-article gnus-newsgroup-name)))
7426          (error "The current group does not support article moving"))
7427         ((and (eq action 'crosspost)
7428               (not (gnus-check-backend-function
7429                     'request-replace-article gnus-newsgroup-name)))
7430          (error "The current group does not support article editing")))
7431   (let ((articles (gnus-summary-work-articles n))
7432         (prefix (if (gnus-check-backend-function
7433                     'request-move-article gnus-newsgroup-name)
7434                     (gnus-group-real-prefix gnus-newsgroup-name)
7435                   ""))
7436         (names '((move "Move" "Moving")
7437                  (copy "Copy" "Copying")
7438                  (crosspost "Crosspost" "Crossposting")))
7439         (copy-buf (save-excursion
7440                     (nnheader-set-temp-buffer " *copy article*")))
7441         art-group to-method new-xref article to-groups)
7442     (unless (assq action names)
7443       (error "Unknown action %s" action))
7444     ;; Read the newsgroup name.
7445     (when (and (not to-newsgroup)
7446                (not select-method))
7447       (setq to-newsgroup
7448             (gnus-read-move-group-name
7449              (cadr (assq action names))
7450              (symbol-value (intern (format "gnus-current-%s-group" action)))
7451              articles prefix))
7452       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7453     (setq to-method (or select-method
7454                         (gnus-server-to-method
7455                          (gnus-group-method to-newsgroup))))
7456     ;; Check the method we are to move this article to...
7457     (unless (gnus-check-backend-function
7458              'request-accept-article (car to-method))
7459       (error "%s does not support article copying" (car to-method)))
7460     (unless (gnus-check-server to-method)
7461       (error "Can't open server %s" (car to-method)))
7462     (gnus-message 6 "%s to %s: %s..."
7463                   (caddr (assq action names))
7464                   (or (car select-method) to-newsgroup) articles)
7465     (while articles
7466       (setq article (pop articles))
7467       (setq
7468        art-group
7469        (cond
7470         ;; Move the article.
7471         ((eq action 'move)
7472          ;; Remove this article from future suppression.
7473          (gnus-dup-unsuppress-article article)
7474          (gnus-request-move-article
7475           article                       ; Article to move
7476           gnus-newsgroup-name           ; From newsgroup
7477           (nth 1 (gnus-find-method-for-group
7478                   gnus-newsgroup-name)) ; Server
7479           (list 'gnus-request-accept-article
7480                 to-newsgroup (list 'quote select-method)
7481                 (not articles) t)       ; Accept form
7482           (not articles)))              ; Only save nov last time
7483         ;; Copy the article.
7484         ((eq action 'copy)
7485          (save-excursion
7486            (set-buffer copy-buf)
7487            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7488              (gnus-request-accept-article
7489               to-newsgroup select-method (not articles) t))))
7490         ;; Crosspost the article.
7491         ((eq action 'crosspost)
7492          (let ((xref (message-tokenize-header
7493                       (mail-header-xref (gnus-summary-article-header article))
7494                       " ")))
7495            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7496                                   ":" article))
7497            (unless xref
7498              (setq xref (list (system-name))))
7499            (setq new-xref
7500                  (concat
7501                   (mapconcat 'identity
7502                              (delete "Xref:" (delete new-xref xref))
7503                              " ")
7504                   " " new-xref))
7505            (save-excursion
7506              (set-buffer copy-buf)
7507              ;; First put the article in the destination group.
7508              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7509              (when (consp (setq art-group
7510                                 (gnus-request-accept-article
7511                                  to-newsgroup select-method (not articles))))
7512                (setq new-xref (concat new-xref " " (car art-group)
7513                                       ":" (cdr art-group)))
7514                ;; Now we have the new Xrefs header, so we insert
7515                ;; it and replace the new article.
7516                (nnheader-replace-header "Xref" new-xref)
7517                (gnus-request-replace-article
7518                 (cdr art-group) to-newsgroup (current-buffer))
7519                art-group))))))
7520       (cond
7521        ((not art-group)
7522         (gnus-message 1 "Couldn't %s article %s: %s"
7523                       (cadr (assq action names)) article
7524                       (nnheader-get-report (car to-method))))
7525        ((eq art-group 'junk)
7526         (when (eq action 'move)
7527           (gnus-summary-mark-article article gnus-canceled-mark)
7528           (gnus-message 4 "Deleted article %s" article)))
7529        (t
7530         (let* ((pto-group (gnus-group-prefixed-name
7531                            (car art-group) to-method))
7532                (entry
7533                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7534                (info (nth 2 entry))
7535                (to-group (gnus-info-group info))
7536                to-marks)
7537           ;; Update the group that has been moved to.
7538           (when (and info
7539                      (memq action '(move copy)))
7540             (unless (member to-group to-groups)
7541               (push to-group to-groups))
7542
7543             (unless (memq article gnus-newsgroup-unreads)
7544               (push 'read to-marks)
7545               (gnus-info-set-read
7546                info (gnus-add-to-range (gnus-info-read info)
7547                                        (list (cdr art-group)))))
7548
7549             ;; See whether the article is to be put in the cache.
7550             (let ((marks gnus-article-mark-lists)
7551                   (to-article (cdr art-group)))
7552
7553               ;; Enter the article into the cache in the new group,
7554               ;; if that is required.
7555               (when gnus-use-cache
7556                 (gnus-cache-possibly-enter-article
7557                  to-group to-article
7558                  (memq article gnus-newsgroup-marked)
7559                  (memq article gnus-newsgroup-dormant)
7560                  (memq article gnus-newsgroup-unreads)))
7561
7562               (when gnus-preserve-marks
7563                 ;; Copy any marks over to the new group.
7564                 (when (and (equal to-group gnus-newsgroup-name)
7565                            (not (memq article gnus-newsgroup-unreads)))
7566                   ;; Mark this article as read in this group.
7567                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7568                   (setcdr (gnus-active to-group) to-article)
7569                   (setcdr gnus-newsgroup-active to-article))
7570
7571                 (while marks
7572                   (when (memq article (symbol-value
7573                                        (intern (format "gnus-newsgroup-%s"
7574                                                        (caar marks)))))
7575                     (push (cdar marks) to-marks)
7576                     ;; If the other group is the same as this group,
7577                     ;; then we have to add the mark to the list.
7578                     (when (equal to-group gnus-newsgroup-name)
7579                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7580                            (cons to-article
7581                                  (symbol-value
7582                                   (intern (format "gnus-newsgroup-%s"
7583                                                   (caar marks)))))))
7584                     ;; Copy the marks to other group.
7585                     (gnus-add-marked-articles
7586                      to-group (cdar marks) (list to-article) info))
7587                   (setq marks (cdr marks)))
7588
7589                 (gnus-request-set-mark to-group (list (list (list to-article)
7590                                                             'set
7591                                                             to-marks))))
7592
7593               (gnus-dribble-enter
7594                (concat "(gnus-group-set-info '"
7595                        (gnus-prin1-to-string (gnus-get-info to-group))
7596                        ")"))))
7597
7598           ;; Update the Xref header in this article to point to
7599           ;; the new crossposted article we have just created.
7600           (when (eq action 'crosspost)
7601             (save-excursion
7602               (set-buffer copy-buf)
7603               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7604               (nnheader-replace-header "Xref" new-xref)
7605               (gnus-request-replace-article
7606                article gnus-newsgroup-name (current-buffer)))))
7607
7608         ;;;!!!Why is this necessary?
7609         (set-buffer gnus-summary-buffer)
7610
7611         (gnus-summary-goto-subject article)
7612         (when (eq action 'move)
7613           (gnus-summary-mark-article article gnus-canceled-mark))))
7614       (gnus-summary-remove-process-mark article))
7615     ;; Re-activate all groups that have been moved to.
7616     (while to-groups
7617       (save-excursion
7618         (set-buffer gnus-group-buffer)
7619         (when (gnus-group-goto-group (car to-groups) t)
7620           (gnus-group-get-new-news-this-group 1 t))
7621         (pop to-groups)))
7622
7623     (gnus-kill-buffer copy-buf)
7624     (gnus-summary-position-point)
7625     (gnus-set-mode-line 'summary)))
7626
7627 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7628   "Move the current article to a different newsgroup.
7629 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7630 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7631 re-spool using this method."
7632   (interactive "P")
7633   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7634
7635 (defun gnus-summary-crosspost-article (&optional n)
7636   "Crosspost the current article to some other group."
7637   (interactive "P")
7638   (gnus-summary-move-article n nil nil 'crosspost))
7639
7640 (defcustom gnus-summary-respool-default-method nil
7641   "Default method for respooling an article.
7642 If nil, use to the current newsgroup method."
7643   :type '(choice (gnus-select-method :value (nnml ""))
7644                  (const nil))
7645   :group 'gnus-summary-mail)
7646
7647 (defun gnus-summary-respool-article (&optional n method)
7648   "Respool the current article.
7649 The article will be squeezed through the mail spooling process again,
7650 which means that it will be put in some mail newsgroup or other
7651 depending on `nnmail-split-methods'.
7652 If N is a positive number, respool the N next articles.
7653 If N is a negative number, respool the N previous articles.
7654 If N is nil and any articles have been marked with the process mark,
7655 respool those articles instead.
7656
7657 Respooling can be done both from mail groups and \"real\" newsgroups.
7658 In the former case, the articles in question will be moved from the
7659 current group into whatever groups they are destined to.  In the
7660 latter case, they will be copied into the relevant groups."
7661   (interactive
7662    (list current-prefix-arg
7663          (let* ((methods (gnus-methods-using 'respool))
7664                 (methname
7665                  (symbol-name (or gnus-summary-respool-default-method
7666                                   (car (gnus-find-method-for-group
7667                                         gnus-newsgroup-name)))))
7668                 (method
7669                  (gnus-completing-read
7670                   methname "What backend do you want to use when respooling?"
7671                   methods nil t nil 'gnus-mail-method-history))
7672                 ms)
7673            (cond
7674             ((zerop (length (setq ms (gnus-servers-using-backend
7675                                       (intern method)))))
7676              (list (intern method) ""))
7677             ((= 1 (length ms))
7678              (car ms))
7679             (t
7680              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7681                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7682                            ms-alist))))))))
7683   (unless method
7684     (error "No method given for respooling"))
7685   (if (assoc (symbol-name
7686               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7687              (gnus-methods-using 'respool))
7688       (gnus-summary-move-article n nil method)
7689     (gnus-summary-copy-article n nil method)))
7690
7691 (defun gnus-summary-import-article (file)
7692   "Import an arbitrary file into a mail newsgroup."
7693   (interactive "fImport file: ")
7694   (let ((group gnus-newsgroup-name)
7695         (now (current-time))
7696         atts lines)
7697     (unless (gnus-check-backend-function 'request-accept-article group)
7698       (error "%s does not support article importing" group))
7699     (or (file-readable-p file)
7700         (not (file-regular-p file))
7701         (error "Can't read %s" file))
7702     (save-excursion
7703       (set-buffer (gnus-get-buffer-create " *import file*"))
7704       (erase-buffer)
7705       (nnheader-insert-file-contents file)
7706       (goto-char (point-min))
7707       (unless (nnheader-article-p)
7708         ;; This doesn't look like an article, so we fudge some headers.
7709         (setq atts (file-attributes file)
7710               lines (count-lines (point-min) (point-max)))
7711         (insert "From: " (read-string "From: ") "\n"
7712                 "Subject: " (read-string "Subject: ") "\n"
7713                 "Date: " (message-make-date (nth 5 atts))
7714                 "\n"
7715                 "Message-ID: " (message-make-message-id) "\n"
7716                 "Lines: " (int-to-string lines) "\n"
7717                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7718       (gnus-request-accept-article group nil t)
7719       (kill-buffer (current-buffer)))))
7720
7721 (defun gnus-summary-article-posted-p ()
7722   "Say whether the current (mail) article is available from news as well.
7723 This will be the case if the article has both been mailed and posted."
7724   (interactive)
7725   (let ((id (mail-header-references (gnus-summary-article-header)))
7726         (gnus-override-method (car (gnus-refer-article-methods))))
7727     (if (gnus-request-head id "")
7728         (gnus-message 2 "The current message was found on %s"
7729                       gnus-override-method)
7730       (gnus-message 2 "The current message couldn't be found on %s"
7731                     gnus-override-method)
7732       nil)))
7733
7734 (defun gnus-summary-expire-articles (&optional now)
7735   "Expire all articles that are marked as expirable in the current group."
7736   (interactive)
7737   (when (gnus-check-backend-function
7738          'request-expire-articles gnus-newsgroup-name)
7739     ;; This backend supports expiry.
7740     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7741            (expirable (if total
7742                           (progn
7743                             ;; We need to update the info for
7744                             ;; this group for `gnus-list-of-read-articles'
7745                             ;; to give us the right answer.
7746                             (gnus-run-hooks 'gnus-exit-group-hook)
7747                             (gnus-summary-update-info)
7748                             (gnus-list-of-read-articles gnus-newsgroup-name))
7749                         (setq gnus-newsgroup-expirable
7750                               (sort gnus-newsgroup-expirable '<))))
7751            (expiry-wait (if now 'immediate
7752                           (gnus-group-find-parameter
7753                            gnus-newsgroup-name 'expiry-wait)))
7754            (nnmail-expiry-target
7755             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7756                 nnmail-expiry-target))
7757            es)
7758       (when expirable
7759         ;; There are expirable articles in this group, so we run them
7760         ;; through the expiry process.
7761         (gnus-message 6 "Expiring articles...")
7762         (unless (gnus-check-group gnus-newsgroup-name)
7763           (error "Can't open server for %s" gnus-newsgroup-name))
7764         ;; The list of articles that weren't expired is returned.
7765         (save-excursion
7766           (if expiry-wait
7767               (let ((nnmail-expiry-wait-function nil)
7768                     (nnmail-expiry-wait expiry-wait))
7769                 (setq es (gnus-request-expire-articles
7770                           expirable gnus-newsgroup-name)))
7771             (setq es (gnus-request-expire-articles
7772                       expirable gnus-newsgroup-name)))
7773           (unless total
7774             (setq gnus-newsgroup-expirable es))
7775           ;; We go through the old list of expirable, and mark all
7776           ;; really expired articles as nonexistent.
7777           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7778             (let ((gnus-use-cache nil))
7779               (while expirable
7780                 (unless (memq (car expirable) es)
7781                   (when (gnus-data-find (car expirable))
7782                     (gnus-summary-mark-article
7783                      (car expirable) gnus-canceled-mark)))
7784                 (setq expirable (cdr expirable))))))
7785         (gnus-message 6 "Expiring articles...done")))))
7786
7787 (defun gnus-summary-expire-articles-now ()
7788   "Expunge all expirable articles in the current group.
7789 This means that *all* articles that are marked as expirable will be
7790 deleted forever, right now."
7791   (interactive)
7792   (or gnus-expert-user
7793       (gnus-yes-or-no-p
7794        "Are you really, really, really sure you want to delete all these messages? ")
7795       (error "Phew!"))
7796   (gnus-summary-expire-articles t))
7797
7798 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7799 (defun gnus-summary-delete-article (&optional n)
7800   "Delete the N next (mail) articles.
7801 This command actually deletes articles.  This is not a marking
7802 command.  The article will disappear forever from your life, never to
7803 return.
7804 If N is negative, delete backwards.
7805 If N is nil and articles have been marked with the process mark,
7806 delete these instead."
7807   (interactive "P")
7808   (unless (gnus-check-backend-function 'request-expire-articles
7809                                        gnus-newsgroup-name)
7810     (error "The current newsgroup does not support article deletion"))
7811   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7812     (error "Couldn't open server"))
7813   ;; Compute the list of articles to delete.
7814   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7815         not-deleted)
7816     (if (and gnus-novice-user
7817              (not (gnus-yes-or-no-p
7818                    (format "Do you really want to delete %s forever? "
7819                            (if (> (length articles) 1)
7820                                (format "these %s articles" (length articles))
7821                              "this article")))))
7822         ()
7823       ;; Delete the articles.
7824       (setq not-deleted (gnus-request-expire-articles
7825                          articles gnus-newsgroup-name 'force))
7826       (while articles
7827         (gnus-summary-remove-process-mark (car articles))
7828         ;; The backend might not have been able to delete the article
7829         ;; after all.
7830         (unless (memq (car articles) not-deleted)
7831           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7832         (setq articles (cdr articles)))
7833       (when not-deleted
7834         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7835     (gnus-summary-position-point)
7836     (gnus-set-mode-line 'summary)
7837     not-deleted))
7838
7839 (defun gnus-summary-edit-article (&optional arg)
7840   "Edit the current article.
7841 This will have permanent effect only in mail groups.
7842 If ARG is nil, edit the decoded articles.
7843 If ARG is 1, edit the raw articles. 
7844 If ARG is 2, edit the raw articles even in read-only groups.
7845 Otherwise, allow editing of articles even in read-only
7846 groups."
7847   (interactive "P")
7848   (let (force raw)
7849     (cond 
7850      ((null arg))
7851      ((eq arg 1) (setq raw t))
7852      ((eq arg 2) (setq raw t
7853                        force t))
7854      (t (setq force t)))
7855     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7856         (error "Can't edit the raw article in group nndraft:drafts."))
7857     (save-excursion
7858       (set-buffer gnus-summary-buffer)
7859       (let ((mail-parse-charset gnus-newsgroup-charset)
7860             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7861         (gnus-set-global-variables)
7862         (when (and (not force)
7863                    (gnus-group-read-only-p))
7864           (error "The current newsgroup does not support article editing"))
7865         (gnus-summary-show-article t)
7866         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7867           (with-current-buffer gnus-article-buffer
7868             (mm-enable-multibyte-mule4)))
7869         (if (equal gnus-newsgroup-name "nndraft:drafts")
7870             (setq raw t))
7871         (gnus-article-edit-article
7872          (if raw 'ignore 
7873            #'(lambda () 
7874                (let ((mbl mml-buffer-list))
7875                  (setq mml-buffer-list nil)
7876                  (mime-to-mml)
7877                  (make-local-hook 'kill-buffer-hook)
7878                  (let ((mml-buffer-list mml-buffer-list))
7879                    (setq mml-buffer-list mbl)
7880                    (make-local-variable 'mml-buffer-list))
7881                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7882          `(lambda (no-highlight)
7883             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7884                   (message-options message-options)
7885                   (message-options-set-recipient)
7886                   (mail-parse-ignored-charsets 
7887                    ',gnus-newsgroup-ignored-charsets))
7888               ,(if (not raw) '(progn 
7889                                 (mml-to-mime)
7890                                 (mml-destroy-buffers)
7891                                 (remove-hook 'kill-buffer-hook 
7892                                              'mml-destroy-buffers t)
7893                                 (kill-local-variable 'mml-buffer-list)))
7894               (gnus-summary-edit-article-done
7895                ,(or (mail-header-references gnus-current-headers) "")
7896                ,(gnus-group-read-only-p) 
7897                ,gnus-summary-buffer no-highlight))))))))
7898
7899 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7900
7901 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7902                                                  no-highlight)
7903   "Make edits to the current article permanent."
7904   (interactive)
7905   ;; Replace the article.
7906   (let ((buf (current-buffer)))
7907     (with-temp-buffer
7908       (insert-buffer-substring buf)
7909       (if (and (not read-only)
7910                (not (gnus-request-replace-article
7911                      (cdr gnus-article-current) (car gnus-article-current)
7912                      (current-buffer) t)))
7913           (error "Couldn't replace article")
7914         ;; Update the summary buffer.
7915         (if (and references
7916                  (equal (message-tokenize-header references " ")
7917                         (message-tokenize-header
7918                          (or (message-fetch-field "references") "") " ")))
7919             ;; We only have to update this line.
7920             (save-excursion
7921               (save-restriction
7922                 (message-narrow-to-head)
7923                 (let ((head (buffer-string))
7924                       header)
7925                   (with-temp-buffer
7926                     (insert (format "211 %d Article retrieved.\n"
7927                                     (cdr gnus-article-current)))
7928                     (insert head)
7929                     (insert ".\n")
7930                     (let ((nntp-server-buffer (current-buffer)))
7931                       (setq header (car (gnus-get-newsgroup-headers
7932                                          (save-excursion
7933                                            (set-buffer gnus-summary-buffer)
7934                                            gnus-newsgroup-dependencies)
7935                                          t))))
7936                     (save-excursion
7937                       (set-buffer gnus-summary-buffer)
7938                       (gnus-data-set-header
7939                        (gnus-data-find (cdr gnus-article-current))
7940                        header)
7941                       (gnus-summary-update-article-line
7942                        (cdr gnus-article-current) header))))))
7943           ;; Update threads.
7944           (set-buffer (or buffer gnus-summary-buffer))
7945           (gnus-summary-update-article (cdr gnus-article-current)))
7946         ;; Prettify the article buffer again.
7947         (unless no-highlight
7948           (save-excursion
7949             (set-buffer gnus-article-buffer)
7950             ;;;!!! Fix this -- article should be rehighlighted.
7951             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7952             (set-buffer gnus-original-article-buffer)
7953             (gnus-request-article
7954              (cdr gnus-article-current)
7955              (car gnus-article-current) (current-buffer))))
7956         ;; Prettify the summary buffer line.
7957         (when (gnus-visual-p 'summary-highlight 'highlight)
7958           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7959
7960 (defun gnus-summary-edit-wash (key)
7961   "Perform editing command KEY in the article buffer."
7962   (interactive
7963    (list
7964     (progn
7965       (message "%s" (concat (this-command-keys) "- "))
7966       (read-char))))
7967   (message "")
7968   (gnus-summary-edit-article)
7969   (execute-kbd-macro (concat (this-command-keys) key))
7970   (gnus-article-edit-done))
7971
7972 ;;; Respooling
7973
7974 (defun gnus-summary-respool-query (&optional silent trace)
7975   "Query where the respool algorithm would put this article."
7976   (interactive)
7977   (let (gnus-mark-article-hook)
7978     (gnus-summary-select-article)
7979     (save-excursion
7980       (set-buffer gnus-original-article-buffer)
7981       (save-restriction
7982         (message-narrow-to-head)
7983         (let ((groups (nnmail-article-group 'identity trace)))
7984           (unless silent
7985             (if groups
7986                 (message "This message would go to %s"
7987                          (mapconcat 'car groups ", "))
7988               (message "This message would go to no groups"))
7989             groups))))))
7990
7991 (defun gnus-summary-respool-trace ()
7992   "Trace where the respool algorithm would put this article.
7993 Display a buffer showing all fancy splitting patterns which matched."
7994   (interactive)
7995   (gnus-summary-respool-query nil t))
7996
7997 ;; Summary marking commands.
7998
7999 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8000   "Mark articles which has the same subject as read, and then select the next.
8001 If UNMARK is positive, remove any kind of mark.
8002 If UNMARK is negative, tick articles."
8003   (interactive "P")
8004   (when unmark
8005     (setq unmark (prefix-numeric-value unmark)))
8006   (let ((count
8007          (gnus-summary-mark-same-subject
8008           (gnus-summary-article-subject) unmark)))
8009     ;; Select next unread article.  If auto-select-same mode, should
8010     ;; select the first unread article.
8011     (gnus-summary-next-article t (and gnus-auto-select-same
8012                                       (gnus-summary-article-subject)))
8013     (gnus-message 7 "%d article%s marked as %s"
8014                   count (if (= count 1) " is" "s are")
8015                   (if unmark "unread" "read"))))
8016
8017 (defun gnus-summary-kill-same-subject (&optional unmark)
8018   "Mark articles which has the same subject as read.
8019 If UNMARK is positive, remove any kind of mark.
8020 If UNMARK is negative, tick articles."
8021   (interactive "P")
8022   (when unmark
8023     (setq unmark (prefix-numeric-value unmark)))
8024   (let ((count
8025          (gnus-summary-mark-same-subject
8026           (gnus-summary-article-subject) unmark)))
8027     ;; If marked as read, go to next unread subject.
8028     (when (null unmark)
8029       ;; Go to next unread subject.
8030       (gnus-summary-next-subject 1 t))
8031     (gnus-message 7 "%d articles are marked as %s"
8032                   count (if unmark "unread" "read"))))
8033
8034 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8035   "Mark articles with same SUBJECT as read, and return marked number.
8036 If optional argument UNMARK is positive, remove any kinds of marks.
8037 If optional argument UNMARK is negative, mark articles as unread instead."
8038   (let ((count 1))
8039     (save-excursion
8040       (cond
8041        ((null unmark)                   ; Mark as read.
8042         (while (and
8043                 (progn
8044                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8045                   (gnus-summary-show-thread) t)
8046                 (gnus-summary-find-subject subject))
8047           (setq count (1+ count))))
8048        ((> unmark 0)                    ; Tick.
8049         (while (and
8050                 (progn
8051                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8052                   (gnus-summary-show-thread) t)
8053                 (gnus-summary-find-subject subject))
8054           (setq count (1+ count))))
8055        (t                               ; Mark as unread.
8056         (while (and
8057                 (progn
8058                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8059                   (gnus-summary-show-thread) t)
8060                 (gnus-summary-find-subject subject))
8061           (setq count (1+ count)))))
8062       (gnus-set-mode-line 'summary)
8063       ;; Return the number of marked articles.
8064       count)))
8065
8066 (defun gnus-summary-mark-as-processable (n &optional unmark)
8067   "Set the process mark on the next N articles.
8068 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8069 the process mark instead.  The difference between N and the actual
8070 number of articles marked is returned."
8071   (interactive "p")
8072   (let ((backward (< n 0))
8073         (n (abs n)))
8074     (while (and
8075             (> n 0)
8076             (if unmark
8077                 (gnus-summary-remove-process-mark
8078                  (gnus-summary-article-number))
8079               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8080             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8081       (setq n (1- n)))
8082     (when (/= 0 n)
8083       (gnus-message 7 "No more articles"))
8084     (gnus-summary-recenter)
8085     (gnus-summary-position-point)
8086     n))
8087
8088 (defun gnus-summary-unmark-as-processable (n)
8089   "Remove the process mark from the next N articles.
8090 If N is negative, unmark backward instead.  The difference between N and
8091 the actual number of articles unmarked is returned."
8092   (interactive "p")
8093   (gnus-summary-mark-as-processable n t))
8094
8095 (defun gnus-summary-unmark-all-processable ()
8096   "Remove the process mark from all articles."
8097   (interactive)
8098   (save-excursion
8099     (while gnus-newsgroup-processable
8100       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8101   (gnus-summary-position-point))
8102
8103 (defun gnus-summary-mark-as-expirable (n)
8104   "Mark N articles forward as expirable.
8105 If N is negative, mark backward instead.  The difference between N and
8106 the actual number of articles marked is returned."
8107   (interactive "p")
8108   (gnus-summary-mark-forward n gnus-expirable-mark))
8109
8110 (defun gnus-summary-mark-article-as-replied (article)
8111   "Mark ARTICLE replied and update the summary line."
8112   (push article gnus-newsgroup-replied)
8113   (let ((buffer-read-only nil))
8114     (when (gnus-summary-goto-subject article nil t)
8115       (gnus-summary-update-secondary-mark article))))
8116
8117 (defun gnus-summary-set-bookmark (article)
8118   "Set a bookmark in current article."
8119   (interactive (list (gnus-summary-article-number)))
8120   (when (or (not (get-buffer gnus-article-buffer))
8121             (not gnus-current-article)
8122             (not gnus-article-current)
8123             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8124     (error "No current article selected"))
8125   ;; Remove old bookmark, if one exists.
8126   (let ((old (assq article gnus-newsgroup-bookmarks)))
8127     (when old
8128       (setq gnus-newsgroup-bookmarks
8129             (delq old gnus-newsgroup-bookmarks))))
8130   ;; Set the new bookmark, which is on the form
8131   ;; (article-number . line-number-in-body).
8132   (push
8133    (cons article
8134          (save-excursion
8135            (set-buffer gnus-article-buffer)
8136            (count-lines
8137             (min (point)
8138                  (save-excursion
8139                    (goto-char (point-min))
8140                    (search-forward "\n\n" nil t)
8141                    (point)))
8142             (point))))
8143    gnus-newsgroup-bookmarks)
8144   (gnus-message 6 "A bookmark has been added to the current article."))
8145
8146 (defun gnus-summary-remove-bookmark (article)
8147   "Remove the bookmark from the current article."
8148   (interactive (list (gnus-summary-article-number)))
8149   ;; Remove old bookmark, if one exists.
8150   (let ((old (assq article gnus-newsgroup-bookmarks)))
8151     (if old
8152         (progn
8153           (setq gnus-newsgroup-bookmarks
8154                 (delq old gnus-newsgroup-bookmarks))
8155           (gnus-message 6 "Removed bookmark."))
8156       (gnus-message 6 "No bookmark in current article."))))
8157
8158 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8159 (defun gnus-summary-mark-as-dormant (n)
8160   "Mark N articles forward as dormant.
8161 If N is negative, mark backward instead.  The difference between N and
8162 the actual number of articles marked is returned."
8163   (interactive "p")
8164   (gnus-summary-mark-forward n gnus-dormant-mark))
8165
8166 (defun gnus-summary-set-process-mark (article)
8167   "Set the process mark on ARTICLE and update the summary line."
8168   (setq gnus-newsgroup-processable
8169         (cons article
8170               (delq article gnus-newsgroup-processable)))
8171   (when (gnus-summary-goto-subject article)
8172     (gnus-summary-show-thread)
8173     (gnus-summary-goto-subject article)
8174     (gnus-summary-update-secondary-mark article)))
8175
8176 (defun gnus-summary-remove-process-mark (article)
8177   "Remove the process mark from ARTICLE and update the summary line."
8178   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8179   (when (gnus-summary-goto-subject article)
8180     (gnus-summary-show-thread)
8181     (gnus-summary-goto-subject article)
8182     (gnus-summary-update-secondary-mark article)))
8183
8184 (defun gnus-summary-set-saved-mark (article)
8185   "Set the process mark on ARTICLE and update the summary line."
8186   (push article gnus-newsgroup-saved)
8187   (when (gnus-summary-goto-subject article)
8188     (gnus-summary-update-secondary-mark article)))
8189
8190 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8191   "Mark N articles as read forwards.
8192 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8193 The difference between N and the actual number of articles marked is
8194 returned.
8195 Iff NO-EXPIRE, auto-expiry will be inhibited."
8196   (interactive "p")
8197   (gnus-summary-show-thread)
8198   (let ((backward (< n 0))
8199         (gnus-summary-goto-unread
8200          (and gnus-summary-goto-unread
8201               (not (eq gnus-summary-goto-unread 'never))
8202               (not (memq mark (list gnus-unread-mark
8203                                     gnus-ticked-mark gnus-dormant-mark)))))
8204         (n (abs n))
8205         (mark (or mark gnus-del-mark)))
8206     (while (and (> n 0)
8207                 (gnus-summary-mark-article nil mark no-expire)
8208                 (zerop (gnus-summary-next-subject
8209                         (if backward -1 1)
8210                         (and gnus-summary-goto-unread
8211                              (not (eq gnus-summary-goto-unread 'never)))
8212                         t)))
8213       (setq n (1- n)))
8214     (when (/= 0 n)
8215       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8216     (gnus-summary-recenter)
8217     (gnus-summary-position-point)
8218     (gnus-set-mode-line 'summary)
8219     n))
8220
8221 (defun gnus-summary-mark-article-as-read (mark)
8222   "Mark the current article quickly as read with MARK."
8223   (let ((article (gnus-summary-article-number)))
8224     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8225     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8226     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8227     (push (cons article mark) gnus-newsgroup-reads)
8228     ;; Possibly remove from cache, if that is used.
8229     (when gnus-use-cache
8230       (gnus-cache-enter-remove-article article))
8231     ;; Allow the backend to change the mark.
8232     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8233     ;; Check for auto-expiry.
8234     (when (and gnus-newsgroup-auto-expire
8235                (memq mark gnus-auto-expirable-marks))
8236       (setq mark gnus-expirable-mark)
8237       ;; Let the backend know about the mark change.
8238       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8239       (push article gnus-newsgroup-expirable))
8240     ;; Set the mark in the buffer.
8241     (gnus-summary-update-mark mark 'unread)
8242     t))
8243
8244 (defun gnus-summary-mark-article-as-unread (mark)
8245   "Mark the current article quickly as unread with MARK."
8246   (let* ((article (gnus-summary-article-number))
8247          (old-mark (gnus-summary-article-mark article)))
8248     ;; Allow the backend to change the mark.
8249     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8250     (if (eq mark old-mark)
8251         t
8252       (if (<= article 0)
8253           (progn
8254             (gnus-error 1 "Can't mark negative article numbers")
8255             nil)
8256         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8257         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8258         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8259         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8260         (cond ((= mark gnus-ticked-mark)
8261                (push article gnus-newsgroup-marked))
8262               ((= mark gnus-dormant-mark)
8263                (push article gnus-newsgroup-dormant))
8264               (t
8265                (push article gnus-newsgroup-unreads)))
8266         (gnus-pull article gnus-newsgroup-reads)
8267
8268         ;; See whether the article is to be put in the cache.
8269         (and gnus-use-cache
8270              (vectorp (gnus-summary-article-header article))
8271              (save-excursion
8272                (gnus-cache-possibly-enter-article
8273                 gnus-newsgroup-name article
8274                 (= mark gnus-ticked-mark)
8275                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8276
8277         ;; Fix the mark.
8278         (gnus-summary-update-mark mark 'unread)
8279         t))))
8280
8281 (defun gnus-summary-mark-article (&optional article mark no-expire)
8282   "Mark ARTICLE with MARK.  MARK can be any character.
8283 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8284 `??' (dormant) and `?E' (expirable).
8285 If MARK is nil, then the default character `?r' is used.
8286 If ARTICLE is nil, then the article on the current line will be
8287 marked.
8288 Iff NO-EXPIRE, auto-expiry will be inhibited."
8289   ;; The mark might be a string.
8290   (when (stringp mark)
8291     (setq mark (aref mark 0)))
8292   ;; If no mark is given, then we check auto-expiring.
8293   (when (null mark)
8294     (setq mark gnus-del-mark))
8295   (when (and (not no-expire)
8296              gnus-newsgroup-auto-expire
8297              (memq mark gnus-auto-expirable-marks))
8298     (setq mark gnus-expirable-mark))
8299   (let ((article (or article (gnus-summary-article-number)))
8300         (old-mark (gnus-summary-article-mark article)))
8301     ;; Allow the backend to change the mark.
8302     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8303     (if (eq mark old-mark)
8304         t
8305       (unless article
8306         (error "No article on current line"))
8307       (if (not (if (or (= mark gnus-unread-mark)
8308                        (= mark gnus-ticked-mark)
8309                        (= mark gnus-dormant-mark))
8310                    (gnus-mark-article-as-unread article mark)
8311                  (gnus-mark-article-as-read article mark)))
8312           t
8313         ;; See whether the article is to be put in the cache.
8314         (and gnus-use-cache
8315              (not (= mark gnus-canceled-mark))
8316              (vectorp (gnus-summary-article-header article))
8317              (save-excursion
8318                (gnus-cache-possibly-enter-article
8319                 gnus-newsgroup-name article
8320                 (= mark gnus-ticked-mark)
8321                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8322
8323         (when (gnus-summary-goto-subject article nil t)
8324           (let ((buffer-read-only nil))
8325             (gnus-summary-show-thread)
8326             ;; Fix the mark.
8327             (gnus-summary-update-mark mark 'unread)
8328             t))))))
8329
8330 (defun gnus-summary-update-secondary-mark (article)
8331   "Update the secondary (read, process, cache) mark."
8332   (gnus-summary-update-mark
8333    (cond ((memq article gnus-newsgroup-processable)
8334           gnus-process-mark)
8335          ((memq article gnus-newsgroup-cached)
8336           gnus-cached-mark)
8337          ((memq article gnus-newsgroup-replied)
8338           gnus-replied-mark)
8339          ((memq article gnus-newsgroup-saved)
8340           gnus-saved-mark)
8341          (t gnus-unread-mark))
8342    'replied)
8343   (when (gnus-visual-p 'summary-highlight 'highlight)
8344     (gnus-run-hooks 'gnus-summary-update-hook))
8345   t)
8346
8347 (defun gnus-summary-update-mark (mark type)
8348   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8349         (buffer-read-only nil))
8350     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8351     (when forward
8352       (when (looking-at "\r")
8353         (incf forward))
8354       (when (<= (+ forward (point)) (point-max))
8355         ;; Go to the right position on the line.
8356         (goto-char (+ forward (point)))
8357         ;; Replace the old mark with the new mark.
8358         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8359         ;; Optionally update the marks by some user rule.
8360         (when (eq type 'unread)
8361           (gnus-data-set-mark
8362            (gnus-data-find (gnus-summary-article-number)) mark)
8363           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8364
8365 (defun gnus-mark-article-as-read (article &optional mark)
8366   "Enter ARTICLE in the pertinent lists and remove it from others."
8367   ;; Make the article expirable.
8368   (let ((mark (or mark gnus-del-mark)))
8369     (if (= mark gnus-expirable-mark)
8370         (push article gnus-newsgroup-expirable)
8371       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8372     ;; Remove from unread and marked lists.
8373     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8374     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8375     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8376     (push (cons article mark) gnus-newsgroup-reads)
8377     ;; Possibly remove from cache, if that is used.
8378     (when gnus-use-cache
8379       (gnus-cache-enter-remove-article article))
8380     t))
8381
8382 (defun gnus-mark-article-as-unread (article &optional mark)
8383   "Enter ARTICLE in the pertinent lists and remove it from others."
8384   (let ((mark (or mark gnus-ticked-mark)))
8385     (if (<= article 0)
8386         (progn
8387           (gnus-error 1 "Can't mark negative article numbers")
8388           nil)
8389       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8390             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8391             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8392             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8393
8394       ;; Unsuppress duplicates?
8395       (when gnus-suppress-duplicates
8396         (gnus-dup-unsuppress-article article))
8397
8398       (cond ((= mark gnus-ticked-mark)
8399              (push article gnus-newsgroup-marked))
8400             ((= mark gnus-dormant-mark)
8401              (push article gnus-newsgroup-dormant))
8402             (t
8403              (push article gnus-newsgroup-unreads)))
8404       (gnus-pull article gnus-newsgroup-reads)
8405       t)))
8406
8407 (defalias 'gnus-summary-mark-as-unread-forward
8408   'gnus-summary-tick-article-forward)
8409 (make-obsolete 'gnus-summary-mark-as-unread-forward
8410                'gnus-summary-tick-article-forward)
8411 (defun gnus-summary-tick-article-forward (n)
8412   "Tick N articles forwards.
8413 If N is negative, tick backwards instead.
8414 The difference between N and the number of articles ticked is returned."
8415   (interactive "p")
8416   (gnus-summary-mark-forward n gnus-ticked-mark))
8417
8418 (defalias 'gnus-summary-mark-as-unread-backward
8419   'gnus-summary-tick-article-backward)
8420 (make-obsolete 'gnus-summary-mark-as-unread-backward
8421                'gnus-summary-tick-article-backward)
8422 (defun gnus-summary-tick-article-backward (n)
8423   "Tick N articles backwards.
8424 The difference between N and the number of articles ticked is returned."
8425   (interactive "p")
8426   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8427
8428 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8429 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8430 (defun gnus-summary-tick-article (&optional article clear-mark)
8431   "Mark current article as unread.
8432 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8433 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8434   (interactive)
8435   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8436                                        gnus-ticked-mark)))
8437
8438 (defun gnus-summary-mark-as-read-forward (n)
8439   "Mark N articles as read forwards.
8440 If N is negative, mark backwards instead.
8441 The difference between N and the actual number of articles marked is
8442 returned."
8443   (interactive "p")
8444   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8445
8446 (defun gnus-summary-mark-as-read-backward (n)
8447   "Mark the N articles as read backwards.
8448 The difference between N and the actual number of articles marked is
8449 returned."
8450   (interactive "p")
8451   (gnus-summary-mark-forward
8452    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8453
8454 (defun gnus-summary-mark-as-read (&optional article mark)
8455   "Mark current article as read.
8456 ARTICLE specifies the article to be marked as read.
8457 MARK specifies a string to be inserted at the beginning of the line."
8458   (gnus-summary-mark-article article mark))
8459
8460 (defun gnus-summary-clear-mark-forward (n)
8461   "Clear marks from N articles forward.
8462 If N is negative, clear backward instead.
8463 The difference between N and the number of marks cleared is returned."
8464   (interactive "p")
8465   (gnus-summary-mark-forward n gnus-unread-mark))
8466
8467 (defun gnus-summary-clear-mark-backward (n)
8468   "Clear marks from N articles backward.
8469 The difference between N and the number of marks cleared is returned."
8470   (interactive "p")
8471   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8472
8473 (defun gnus-summary-mark-unread-as-read ()
8474   "Intended to be used by `gnus-summary-mark-article-hook'."
8475   (when (memq gnus-current-article gnus-newsgroup-unreads)
8476     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8477
8478 (defun gnus-summary-mark-read-and-unread-as-read ()
8479   "Intended to be used by `gnus-summary-mark-article-hook'."
8480   (let ((mark (gnus-summary-article-mark)))
8481     (when (or (gnus-unread-mark-p mark)
8482               (gnus-read-mark-p mark))
8483       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8484
8485 (defun gnus-summary-mark-region-as-read (point mark all)
8486   "Mark all unread articles between point and mark as read.
8487 If given a prefix, mark all articles between point and mark as read,
8488 even ticked and dormant ones."
8489   (interactive "r\nP")
8490   (save-excursion
8491     (let (article)
8492       (goto-char point)
8493       (beginning-of-line)
8494       (while (and
8495               (< (point) mark)
8496               (progn
8497                 (when (or all
8498                           (memq (setq article (gnus-summary-article-number))
8499                                 gnus-newsgroup-unreads))
8500                   (gnus-summary-mark-article article gnus-del-mark))
8501                 t)
8502               (gnus-summary-find-next))))))
8503
8504 (defun gnus-summary-mark-below (score mark)
8505   "Mark articles with score less than SCORE with MARK."
8506   (interactive "P\ncMark: ")
8507   (setq score (if score
8508                   (prefix-numeric-value score)
8509                 (or gnus-summary-default-score 0)))
8510   (save-excursion
8511     (set-buffer gnus-summary-buffer)
8512     (goto-char (point-min))
8513     (while
8514         (progn
8515           (and (< (gnus-summary-article-score) score)
8516                (gnus-summary-mark-article nil mark))
8517           (gnus-summary-find-next)))))
8518
8519 (defun gnus-summary-kill-below (&optional score)
8520   "Mark articles with score below SCORE as read."
8521   (interactive "P")
8522   (gnus-summary-mark-below score gnus-killed-mark))
8523
8524 (defun gnus-summary-clear-above (&optional score)
8525   "Clear all marks from articles with score above SCORE."
8526   (interactive "P")
8527   (gnus-summary-mark-above score gnus-unread-mark))
8528
8529 (defun gnus-summary-tick-above (&optional score)
8530   "Tick all articles with score above SCORE."
8531   (interactive "P")
8532   (gnus-summary-mark-above score gnus-ticked-mark))
8533
8534 (defun gnus-summary-mark-above (score mark)
8535   "Mark articles with score over SCORE with MARK."
8536   (interactive "P\ncMark: ")
8537   (setq score (if score
8538                   (prefix-numeric-value score)
8539                 (or gnus-summary-default-score 0)))
8540   (save-excursion
8541     (set-buffer gnus-summary-buffer)
8542     (goto-char (point-min))
8543     (while (and (progn
8544                   (when (> (gnus-summary-article-score) score)
8545                     (gnus-summary-mark-article nil mark))
8546                   t)
8547                 (gnus-summary-find-next)))))
8548
8549 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8550 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8551 (defun gnus-summary-limit-include-expunged (&optional no-error)
8552   "Display all the hidden articles that were expunged for low scores."
8553   (interactive)
8554   (let ((buffer-read-only nil))
8555     (let ((scored gnus-newsgroup-scored)
8556           headers h)
8557       (while scored
8558         (unless (gnus-summary-goto-subject (caar scored))
8559           (and (setq h (gnus-summary-article-header (caar scored)))
8560                (< (cdar scored) gnus-summary-expunge-below)
8561                (push h headers)))
8562         (setq scored (cdr scored)))
8563       (if (not headers)
8564           (when (not no-error)
8565             (error "No expunged articles hidden"))
8566         (goto-char (point-min))
8567         (gnus-summary-prepare-unthreaded (nreverse headers))
8568         (goto-char (point-min))
8569         (gnus-summary-position-point)
8570         t))))
8571
8572 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8573   "Mark all unread articles in this newsgroup as read.
8574 If prefix argument ALL is non-nil, ticked and dormant articles will
8575 also be marked as read.
8576 If QUIETLY is non-nil, no questions will be asked.
8577 If TO-HERE is non-nil, it should be a point in the buffer.  All
8578 articles before this point will be marked as read.
8579 Note that this function will only catch up the unread article
8580 in the current summary buffer limitation.
8581 The number of articles marked as read is returned."
8582   (interactive "P")
8583   (prog1
8584       (save-excursion
8585         (when (or quietly
8586                   (not gnus-interactive-catchup) ;Without confirmation?
8587                   gnus-expert-user
8588                   (gnus-y-or-n-p
8589                    (if all
8590                        "Mark absolutely all articles as read? "
8591                      "Mark all unread articles as read? ")))
8592           (if (and not-mark
8593                    (not gnus-newsgroup-adaptive)
8594                    (not gnus-newsgroup-auto-expire)
8595                    (not gnus-suppress-duplicates)
8596                    (or (not gnus-use-cache)
8597                        (eq gnus-use-cache 'passive)))
8598               (progn
8599                 (when all
8600                   (setq gnus-newsgroup-marked nil
8601                         gnus-newsgroup-dormant nil))
8602                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8603             ;; We actually mark all articles as canceled, which we
8604             ;; have to do when using auto-expiry or adaptive scoring.
8605             (gnus-summary-show-all-threads)
8606             (when (gnus-summary-first-subject (not all) t)
8607               (while (and
8608                       (if to-here (< (point) to-here) t)
8609                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8610                       (gnus-summary-find-next (not all) nil nil t))))
8611             (gnus-set-mode-line 'summary))
8612           t))
8613     (gnus-summary-position-point)))
8614
8615 (defun gnus-summary-catchup-to-here (&optional all)
8616   "Mark all unticked articles before the current one as read.
8617 If ALL is non-nil, also mark ticked and dormant articles as read."
8618   (interactive "P")
8619   (save-excursion
8620     (gnus-save-hidden-threads
8621       (let ((beg (point)))
8622         ;; We check that there are unread articles.
8623         (when (or all (gnus-summary-find-prev))
8624           (gnus-summary-catchup all t beg)))))
8625   (gnus-summary-position-point))
8626
8627 (defun gnus-summary-catchup-all (&optional quietly)
8628   "Mark all articles in this newsgroup as read."
8629   (interactive "P")
8630   (gnus-summary-catchup t quietly))
8631
8632 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8633   "Mark all unread articles in this group as read, then exit.
8634 If prefix argument ALL is non-nil, all articles are marked as read."
8635   (interactive "P")
8636   (when (gnus-summary-catchup all quietly nil 'fast)
8637     ;; Select next newsgroup or exit.
8638     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8639              (eq gnus-auto-select-next 'quietly))
8640         (gnus-summary-next-group nil)
8641       (gnus-summary-exit))))
8642
8643 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8644   "Mark all articles in this newsgroup as read, and then exit."
8645   (interactive "P")
8646   (gnus-summary-catchup-and-exit t quietly))
8647
8648 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8649   "Mark all articles in this group as read and select the next group.
8650 If given a prefix, mark all articles, unread as well as ticked, as
8651 read."
8652   (interactive "P")
8653   (save-excursion
8654     (gnus-summary-catchup all))
8655   (gnus-summary-next-group))
8656
8657 ;;;
8658 ;;; with article
8659 ;;;
8660
8661 (defmacro gnus-with-article (article &rest forms)
8662   "Select ARTICLE and perform FORMS in the original article buffer.
8663 Then replace the article with the result."
8664   `(progn
8665      ;; We don't want the article to be marked as read.
8666      (let (gnus-mark-article-hook)
8667        (gnus-summary-select-article t t nil ,article))
8668      (set-buffer gnus-original-article-buffer)
8669      ,@forms
8670      (if (not (gnus-check-backend-function
8671                'request-replace-article (car gnus-article-current)))
8672          (gnus-message 5 "Read-only group; not replacing")
8673        (unless (gnus-request-replace-article
8674                 ,article (car gnus-article-current)
8675                 (current-buffer) t)
8676          (error "Couldn't replace article")))
8677      ;; The cache and backlog have to be flushed somewhat.
8678      (when gnus-keep-backlog
8679        (gnus-backlog-remove-article
8680         (car gnus-article-current) (cdr gnus-article-current)))
8681      (when gnus-use-cache
8682        (gnus-cache-update-article
8683         (car gnus-article-current) (cdr gnus-article-current)))))
8684
8685 (put 'gnus-with-article 'lisp-indent-function 1)
8686 (put 'gnus-with-article 'edebug-form-spec '(form body))
8687
8688 ;; Thread-based commands.
8689
8690 (defun gnus-summary-articles-in-thread (&optional article)
8691   "Return a list of all articles in the current thread.
8692 If ARTICLE is non-nil, return all articles in the thread that starts
8693 with that article."
8694   (let* ((article (or article (gnus-summary-article-number)))
8695          (data (gnus-data-find-list article))
8696          (top-level (gnus-data-level (car data)))
8697          (top-subject
8698           (cond ((null gnus-thread-operation-ignore-subject)
8699                  (gnus-simplify-subject-re
8700                   (mail-header-subject (gnus-data-header (car data)))))
8701                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8702                  (gnus-simplify-subject-fuzzy
8703                   (mail-header-subject (gnus-data-header (car data)))))
8704                 (t nil)))
8705          (end-point (save-excursion
8706                       (if (gnus-summary-go-to-next-thread)
8707                           (point) (point-max))))
8708          articles)
8709     (while (and data
8710                 (< (gnus-data-pos (car data)) end-point))
8711       (when (or (not top-subject)
8712                 (string= top-subject
8713                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8714                              (gnus-simplify-subject-fuzzy
8715                               (mail-header-subject
8716                                (gnus-data-header (car data))))
8717                            (gnus-simplify-subject-re
8718                             (mail-header-subject
8719                              (gnus-data-header (car data)))))))
8720         (push (gnus-data-number (car data)) articles))
8721       (unless (and (setq data (cdr data))
8722                    (> (gnus-data-level (car data)) top-level))
8723         (setq data nil)))
8724     ;; Return the list of articles.
8725     (nreverse articles)))
8726
8727 (defun gnus-summary-rethread-current ()
8728   "Rethread the thread the current article is part of."
8729   (interactive)
8730   (let* ((gnus-show-threads t)
8731          (article (gnus-summary-article-number))
8732          (id (mail-header-id (gnus-summary-article-header)))
8733          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8734     (unless id
8735       (error "No article on the current line"))
8736     (gnus-rebuild-thread id)
8737     (gnus-summary-goto-subject article)))
8738
8739 (defun gnus-summary-reparent-thread ()
8740   "Make the current article child of the marked (or previous) article.
8741
8742 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8743 is non-nil or the Subject: of both articles are the same."
8744   (interactive)
8745   (unless (not (gnus-group-read-only-p))
8746     (error "The current newsgroup does not support article editing"))
8747   (unless (<= (length gnus-newsgroup-processable) 1)
8748     (error "No more than one article may be marked"))
8749   (save-window-excursion
8750     (let ((gnus-article-buffer " *reparent*")
8751           (current-article (gnus-summary-article-number))
8752           ;; First grab the marked article, otherwise one line up.
8753           (parent-article (if (not (null gnus-newsgroup-processable))
8754                               (car gnus-newsgroup-processable)
8755                             (save-excursion
8756                               (if (eq (forward-line -1) 0)
8757                                   (gnus-summary-article-number)
8758                                 (error "Beginning of summary buffer"))))))
8759       (unless (not (eq current-article parent-article))
8760         (error "An article may not be self-referential"))
8761       (let ((message-id (mail-header-id
8762                          (gnus-summary-article-header parent-article))))
8763         (unless (and message-id (not (equal message-id "")))
8764           (error "No message-id in desired parent"))
8765         (gnus-with-article current-article
8766           (save-restriction
8767             (goto-char (point-min))
8768             (message-narrow-to-head)
8769             (if (re-search-forward "^References: " nil t)
8770                 (progn
8771                   (re-search-forward "^[^ \t]" nil t)
8772                   (forward-line -1)
8773                   (end-of-line)
8774                   (insert " " message-id))
8775               (insert "References: " message-id "\n"))))
8776         (set-buffer gnus-summary-buffer)
8777         (gnus-summary-unmark-all-processable)
8778         (gnus-summary-update-article current-article)
8779         (gnus-summary-rethread-current)
8780         (gnus-message 3 "Article %d is now the child of article %d"
8781                       current-article parent-article)))))
8782
8783 (defun gnus-summary-toggle-threads (&optional arg)
8784   "Toggle showing conversation threads.
8785 If ARG is positive number, turn showing conversation threads on."
8786   (interactive "P")
8787   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8788     (setq gnus-show-threads
8789           (if (null arg) (not gnus-show-threads)
8790             (> (prefix-numeric-value arg) 0)))
8791     (gnus-summary-prepare)
8792     (gnus-summary-goto-subject current)
8793     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8794     (gnus-summary-position-point)))
8795
8796 (defun gnus-summary-show-all-threads ()
8797   "Show all threads."
8798   (interactive)
8799   (save-excursion
8800     (let ((buffer-read-only nil))
8801       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8802   (gnus-summary-position-point))
8803
8804 (defun gnus-summary-show-thread ()
8805   "Show thread subtrees.
8806 Returns nil if no thread was there to be shown."
8807   (interactive)
8808   (let ((buffer-read-only nil)
8809         (orig (point))
8810         ;; first goto end then to beg, to have point at beg after let
8811         (end (progn (end-of-line) (point)))
8812         (beg (progn (beginning-of-line) (point))))
8813     (prog1
8814         ;; Any hidden lines here?
8815         (search-forward "\r" end t)
8816       (subst-char-in-region beg end ?\^M ?\n t)
8817       (goto-char orig)
8818       (gnus-summary-position-point))))
8819
8820 (defun gnus-summary-hide-all-threads ()
8821   "Hide all thread subtrees."
8822   (interactive)
8823   (save-excursion
8824     (goto-char (point-min))
8825     (gnus-summary-hide-thread)
8826     (while (zerop (gnus-summary-next-thread 1 t))
8827       (gnus-summary-hide-thread)))
8828   (gnus-summary-position-point))
8829
8830 (defun gnus-summary-hide-thread ()
8831   "Hide thread subtrees.
8832 Returns nil if no threads were there to be hidden."
8833   (interactive)
8834   (let ((buffer-read-only nil)
8835         (start (point))
8836         (article (gnus-summary-article-number)))
8837     (goto-char start)
8838     ;; Go forward until either the buffer ends or the subthread
8839     ;; ends.
8840     (when (and (not (eobp))
8841                (or (zerop (gnus-summary-next-thread 1 t))
8842                    (goto-char (point-max))))
8843       (prog1
8844           (if (and (> (point) start)
8845                    (search-backward "\n" start t))
8846               (progn
8847                 (subst-char-in-region start (point) ?\n ?\^M)
8848                 (gnus-summary-goto-subject article))
8849             (goto-char start)
8850             nil)))))
8851
8852 (defun gnus-summary-go-to-next-thread (&optional previous)
8853   "Go to the same level (or less) next thread.
8854 If PREVIOUS is non-nil, go to previous thread instead.
8855 Return the article number moved to, or nil if moving was impossible."
8856   (let ((level (gnus-summary-thread-level))
8857         (way (if previous -1 1))
8858         (beg (point)))
8859     (forward-line way)
8860     (while (and (not (eobp))
8861                 (< level (gnus-summary-thread-level)))
8862       (forward-line way))
8863     (if (eobp)
8864         (progn
8865           (goto-char beg)
8866           nil)
8867       (setq beg (point))
8868       (prog1
8869           (gnus-summary-article-number)
8870         (goto-char beg)))))
8871
8872 (defun gnus-summary-next-thread (n &optional silent)
8873   "Go to the same level next N'th thread.
8874 If N is negative, search backward instead.
8875 Returns the difference between N and the number of skips actually
8876 done.
8877
8878 If SILENT, don't output messages."
8879   (interactive "p")
8880   (let ((backward (< n 0))
8881         (n (abs n)))
8882     (while (and (> n 0)
8883                 (gnus-summary-go-to-next-thread backward))
8884       (decf n))
8885     (unless silent
8886       (gnus-summary-position-point))
8887     (when (and (not silent) (/= 0 n))
8888       (gnus-message 7 "No more threads"))
8889     n))
8890
8891 (defun gnus-summary-prev-thread (n)
8892   "Go to the same level previous N'th thread.
8893 Returns the difference between N and the number of skips actually
8894 done."
8895   (interactive "p")
8896   (gnus-summary-next-thread (- n)))
8897
8898 (defun gnus-summary-go-down-thread ()
8899   "Go down one level in the current thread."
8900   (let ((children (gnus-summary-article-children)))
8901     (when children
8902       (gnus-summary-goto-subject (car children)))))
8903
8904 (defun gnus-summary-go-up-thread ()
8905   "Go up one level in the current thread."
8906   (let ((parent (gnus-summary-article-parent)))
8907     (when parent
8908       (gnus-summary-goto-subject parent))))
8909
8910 (defun gnus-summary-down-thread (n)
8911   "Go down thread N steps.
8912 If N is negative, go up instead.
8913 Returns the difference between N and how many steps down that were
8914 taken."
8915   (interactive "p")
8916   (let ((up (< n 0))
8917         (n (abs n)))
8918     (while (and (> n 0)
8919                 (if up (gnus-summary-go-up-thread)
8920                   (gnus-summary-go-down-thread)))
8921       (setq n (1- n)))
8922     (gnus-summary-position-point)
8923     (when (/= 0 n)
8924       (gnus-message 7 "Can't go further"))
8925     n))
8926
8927 (defun gnus-summary-up-thread (n)
8928   "Go up thread N steps.
8929 If N is negative, go up instead.
8930 Returns the difference between N and how many steps down that were
8931 taken."
8932   (interactive "p")
8933   (gnus-summary-down-thread (- n)))
8934
8935 (defun gnus-summary-top-thread ()
8936   "Go to the top of the thread."
8937   (interactive)
8938   (while (gnus-summary-go-up-thread))
8939   (gnus-summary-article-number))
8940
8941 (defun gnus-summary-kill-thread (&optional unmark)
8942   "Mark articles under current thread as read.
8943 If the prefix argument is positive, remove any kinds of marks.
8944 If the prefix argument is negative, tick articles instead."
8945   (interactive "P")
8946   (when unmark
8947     (setq unmark (prefix-numeric-value unmark)))
8948   (let ((articles (gnus-summary-articles-in-thread)))
8949     (save-excursion
8950       ;; Expand the thread.
8951       (gnus-summary-show-thread)
8952       ;; Mark all the articles.
8953       (while articles
8954         (gnus-summary-goto-subject (car articles))
8955         (cond ((null unmark)
8956                (gnus-summary-mark-article-as-read gnus-killed-mark))
8957               ((> unmark 0)
8958                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8959               (t
8960                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8961         (setq articles (cdr articles))))
8962     ;; Hide killed subtrees.
8963     (and (null unmark)
8964          gnus-thread-hide-killed
8965          (gnus-summary-hide-thread))
8966     ;; If marked as read, go to next unread subject.
8967     (when (null unmark)
8968       ;; Go to next unread subject.
8969       (gnus-summary-next-subject 1 t)))
8970   (gnus-set-mode-line 'summary))
8971
8972 ;; Summary sorting commands
8973
8974 (defun gnus-summary-sort-by-number (&optional reverse)
8975   "Sort the summary buffer by article number.
8976 Argument REVERSE means reverse order."
8977   (interactive "P")
8978   (gnus-summary-sort 'number reverse))
8979
8980 (defun gnus-summary-sort-by-author (&optional reverse)
8981   "Sort the summary buffer by author name alphabetically.
8982 If `case-fold-search' is non-nil, case of letters is ignored.
8983 Argument REVERSE means reverse order."
8984   (interactive "P")
8985   (gnus-summary-sort 'author reverse))
8986
8987 (defun gnus-summary-sort-by-subject (&optional reverse)
8988   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8989 If `case-fold-search' is non-nil, case of letters is ignored.
8990 Argument REVERSE means reverse order."
8991   (interactive "P")
8992   (gnus-summary-sort 'subject reverse))
8993
8994 (defun gnus-summary-sort-by-date (&optional reverse)
8995   "Sort the summary buffer by date.
8996 Argument REVERSE means reverse order."
8997   (interactive "P")
8998   (gnus-summary-sort 'date reverse))
8999
9000 (defun gnus-summary-sort-by-score (&optional reverse)
9001   "Sort the summary buffer by score.
9002 Argument REVERSE means reverse order."
9003   (interactive "P")
9004   (gnus-summary-sort 'score reverse))
9005
9006 (defun gnus-summary-sort-by-lines (&optional reverse)
9007   "Sort the summary buffer by the number of lines.
9008 Argument REVERSE means reverse order."
9009   (interactive "P")
9010   (gnus-summary-sort 'lines reverse))
9011
9012 (defun gnus-summary-sort-by-chars (&optional reverse)
9013   "Sort the summary buffer by article length.
9014 Argument REVERSE means reverse order."
9015   (interactive "P")
9016   (gnus-summary-sort 'chars reverse))
9017
9018 (defun gnus-summary-sort (predicate reverse)
9019   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9020   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9021          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9022          (gnus-thread-sort-functions
9023           (if (not reverse)
9024               thread
9025             `(lambda (t1 t2)
9026                (,thread t2 t1))))
9027          (gnus-sort-gathered-threads-function
9028           gnus-thread-sort-functions)
9029          (gnus-article-sort-functions
9030           (if (not reverse)
9031               article
9032             `(lambda (t1 t2)
9033                (,article t2 t1))))
9034          (buffer-read-only)
9035          (gnus-summary-prepare-hook nil))
9036     ;; We do the sorting by regenerating the threads.
9037     (gnus-summary-prepare)
9038     ;; Hide subthreads if needed.
9039     (when (and gnus-show-threads gnus-thread-hide-subtree)
9040       (gnus-summary-hide-all-threads))))
9041
9042 ;; Summary saving commands.
9043
9044 (defun gnus-summary-save-article (&optional n not-saved)
9045   "Save the current article using the default saver function.
9046 If N is a positive number, save the N next articles.
9047 If N is a negative number, save the N previous articles.
9048 If N is nil and any articles have been marked with the process mark,
9049 save those articles instead.
9050 The variable `gnus-default-article-saver' specifies the saver function."
9051   (interactive "P")
9052   (let* ((articles (gnus-summary-work-articles n))
9053          (save-buffer (save-excursion
9054                         (nnheader-set-temp-buffer " *Gnus Save*")))
9055          (num (length articles))
9056          header file)
9057     (dolist (article articles)
9058       (setq header (gnus-summary-article-header article))
9059       (if (not (vectorp header))
9060           ;; This is a pseudo-article.
9061           (if (assq 'name header)
9062               (gnus-copy-file (cdr (assq 'name header)))
9063             (gnus-message 1 "Article %d is unsaveable" article))
9064         ;; This is a real article.
9065         (save-window-excursion
9066           (gnus-summary-select-article t nil nil article))
9067         (save-excursion
9068           (set-buffer save-buffer)
9069           (erase-buffer)
9070           (insert-buffer-substring gnus-original-article-buffer))
9071         (setq file (gnus-article-save save-buffer file num))
9072         (gnus-summary-remove-process-mark article)
9073         (unless not-saved
9074           (gnus-summary-set-saved-mark article))))
9075     (gnus-kill-buffer save-buffer)
9076     (gnus-summary-position-point)
9077     (gnus-set-mode-line 'summary)
9078     n))
9079
9080 (defun gnus-summary-pipe-output (&optional arg)
9081   "Pipe the current article to a subprocess.
9082 If N is a positive number, pipe the N next articles.
9083 If N is a negative number, pipe the N previous articles.
9084 If N is nil and any articles have been marked with the process mark,
9085 pipe those articles instead."
9086   (interactive "P")
9087   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9088     (gnus-summary-save-article arg t))
9089   (gnus-configure-windows 'pipe))
9090
9091 (defun gnus-summary-save-article-mail (&optional arg)
9092   "Append the current article to an mail file.
9093 If N is a positive number, save the N next articles.
9094 If N is a negative number, save the N previous articles.
9095 If N is nil and any articles have been marked with the process mark,
9096 save those articles instead."
9097   (interactive "P")
9098   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9099     (gnus-summary-save-article arg)))
9100
9101 (defun gnus-summary-save-article-rmail (&optional arg)
9102   "Append the current article to an rmail file.
9103 If N is a positive number, save the N next articles.
9104 If N is a negative number, save the N previous articles.
9105 If N is nil and any articles have been marked with the process mark,
9106 save those articles instead."
9107   (interactive "P")
9108   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9109     (gnus-summary-save-article arg)))
9110
9111 (defun gnus-summary-save-article-file (&optional arg)
9112   "Append the current article to a file.
9113 If N is a positive number, save the N next articles.
9114 If N is a negative number, save the N previous articles.
9115 If N is nil and any articles have been marked with the process mark,
9116 save those articles instead."
9117   (interactive "P")
9118   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9119     (gnus-summary-save-article arg)))
9120
9121 (defun gnus-summary-write-article-file (&optional arg)
9122   "Write the current article to a file, deleting the previous file.
9123 If N is a positive number, save the N next articles.
9124 If N is a negative number, save the N previous articles.
9125 If N is nil and any articles have been marked with the process mark,
9126 save those articles instead."
9127   (interactive "P")
9128   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9129     (gnus-summary-save-article arg)))
9130
9131 (defun gnus-summary-save-article-body-file (&optional arg)
9132   "Append the current article body to a file.
9133 If N is a positive number, save the N next articles.
9134 If N is a negative number, save the N previous articles.
9135 If N is nil and any articles have been marked with the process mark,
9136 save those articles instead."
9137   (interactive "P")
9138   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9139     (gnus-summary-save-article arg)))
9140
9141 (defun gnus-summary-pipe-message (program)
9142   "Pipe the current article through PROGRAM."
9143   (interactive "sProgram: ")
9144   (gnus-summary-select-article)
9145   (let ((mail-header-separator ""))
9146     (gnus-eval-in-buffer-window gnus-article-buffer
9147       (save-restriction
9148         (widen)
9149         (let ((start (window-start))
9150               buffer-read-only)
9151           (message-pipe-buffer-body program)
9152           (set-window-start (get-buffer-window (current-buffer)) start))))))
9153
9154 (defun gnus-get-split-value (methods)
9155   "Return a value based on the split METHODS."
9156   (let (split-name method result match)
9157     (when methods
9158       (save-excursion
9159         (set-buffer gnus-original-article-buffer)
9160         (save-restriction
9161           (nnheader-narrow-to-headers)
9162           (while (and methods (not split-name))
9163             (goto-char (point-min))
9164             (setq method (pop methods))
9165             (setq match (car method))
9166             (when (cond
9167                    ((stringp match)
9168                     ;; Regular expression.
9169                     (ignore-errors
9170                       (re-search-forward match nil t)))
9171                    ((gnus-functionp match)
9172                     ;; Function.
9173                     (save-restriction
9174                       (widen)
9175                       (setq result (funcall match gnus-newsgroup-name))))
9176                    ((consp match)
9177                     ;; Form.
9178                     (save-restriction
9179                       (widen)
9180                       (setq result (eval match)))))
9181               (setq split-name (cdr method))
9182               (cond ((stringp result)
9183                      (push (expand-file-name
9184                             result gnus-article-save-directory)
9185                            split-name))
9186                     ((consp result)
9187                      (setq split-name (append result split-name)))))))))
9188     (nreverse split-name)))
9189
9190 (defun gnus-valid-move-group-p (group)
9191   (and (boundp group)
9192        (symbol-name group)
9193        (symbol-value group)
9194        (gnus-get-function (gnus-find-method-for-group
9195                            (symbol-name group)) 'request-accept-article t)))
9196
9197 (defun gnus-read-move-group-name (prompt default articles prefix)
9198   "Read a group name."
9199   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9200          (minibuffer-confirm-incomplete nil) ; XEmacs
9201          (prom
9202           (format "%s %s to:"
9203                   prompt
9204                   (if (> (length articles) 1)
9205                       (format "these %d articles" (length articles))
9206                     "this article")))
9207          (to-newsgroup
9208           (cond
9209            ((null split-name)
9210             (gnus-completing-read default prom
9211                                   gnus-active-hashtb
9212                                   'gnus-valid-move-group-p
9213                                   nil prefix
9214                                   'gnus-group-history))
9215            ((= 1 (length split-name))
9216             (gnus-completing-read (car split-name) prom
9217                                   gnus-active-hashtb
9218                                   'gnus-valid-move-group-p
9219                                   nil nil
9220                                   'gnus-group-history))
9221            (t
9222             (gnus-completing-read nil prom
9223                                   (mapcar (lambda (el) (list el))
9224                                           (nreverse split-name))
9225                                   nil nil nil
9226                                   'gnus-group-history))))
9227          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9228     (when to-newsgroup
9229       (if (or (string= to-newsgroup "")
9230               (string= to-newsgroup prefix))
9231           (setq to-newsgroup default))
9232       (unless to-newsgroup
9233         (error "No group name entered"))
9234       (or (gnus-active to-newsgroup)
9235           (gnus-activate-group to-newsgroup nil nil to-method)
9236           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9237                                      to-newsgroup))
9238               (or (and (gnus-request-create-group to-newsgroup to-method)
9239                        (gnus-activate-group
9240                         to-newsgroup nil nil to-method)
9241                        (gnus-subscribe-group to-newsgroup))
9242                   (error "Couldn't create group %s" to-newsgroup)))
9243           (error "No such group: %s" to-newsgroup)))
9244     to-newsgroup))
9245
9246 (defun gnus-summary-save-parts (type dir n &optional reverse)
9247   "Save parts matching TYPE to DIR.
9248 If REVERSE, save parts that do not match TYPE."
9249   (interactive
9250    (list (read-string "Save parts of type: " 
9251                       (or (car gnus-summary-save-parts-type-history)
9252                           gnus-summary-save-parts-default-mime)
9253                       'gnus-summary-save-parts-type-history)
9254          (setq gnus-summary-save-parts-last-directory
9255                (read-file-name "Save to directory: " 
9256                                gnus-summary-save-parts-last-directory
9257                                nil t))
9258          current-prefix-arg))
9259   (gnus-summary-iterate n
9260     (let ((gnus-display-mime-function nil)
9261           (gnus-inhibit-treatment t))
9262       (gnus-summary-select-article))
9263     (save-excursion
9264       (set-buffer gnus-article-buffer)
9265       (let ((handles (or gnus-article-mime-handles
9266                          (mm-dissect-buffer) (mm-uu-dissect))))
9267         (when handles
9268           (gnus-summary-save-parts-1 type dir handles reverse)
9269           (unless gnus-article-mime-handles ;; Don't destroy this case.
9270             (mm-destroy-parts handles)))))))
9271
9272 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9273   (if (stringp (car handle))
9274       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9275               (cdr handle))
9276     (when (if reverse
9277               (not (string-match type (mm-handle-media-type handle)))
9278             (string-match type (mm-handle-media-type handle)))
9279       (let ((file (expand-file-name
9280                    (file-name-nondirectory
9281                     (or
9282                      (mail-content-type-get
9283                       (mm-handle-disposition handle) 'filename)
9284                      (concat gnus-newsgroup-name
9285                              "." (number-to-string
9286                                   (cdr gnus-article-current)))))
9287                    dir)))
9288         (unless (file-exists-p file)
9289           (mm-save-part-to-file handle file))))))
9290
9291 ;; Summary extract commands
9292
9293 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9294   (let ((buffer-read-only nil)
9295         (article (gnus-summary-article-number))
9296         after-article b e)
9297     (unless (gnus-summary-goto-subject article)
9298       (error "No such article: %d" article))
9299     (gnus-summary-position-point)
9300     ;; If all commands are to be bunched up on one line, we collect
9301     ;; them here.
9302     (unless gnus-view-pseudos-separately
9303       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9304             files action)
9305         (while ps
9306           (setq action (cdr (assq 'action (car ps))))
9307           (setq files (list (cdr (assq 'name (car ps)))))
9308           (while (and ps (cdr ps)
9309                       (string= (or action "1")
9310                                (or (cdr (assq 'action (cadr ps))) "2")))
9311             (push (cdr (assq 'name (cadr ps))) files)
9312             (setcdr ps (cddr ps)))
9313           (when files
9314             (when (not (string-match "%s" action))
9315               (push " " files))
9316             (push " " files)
9317             (when (assq 'execute (car ps))
9318               (setcdr (assq 'execute (car ps))
9319                       (funcall (if (string-match "%s" action)
9320                                    'format 'concat)
9321                                action
9322                                (mapconcat
9323                                 (lambda (f)
9324                                   (if (equal f " ")
9325                                       f
9326                                     (mm-quote-arg f)))
9327                                 files " ")))))
9328           (setq ps (cdr ps)))))
9329     (if (and gnus-view-pseudos (not not-view))
9330         (while pslist
9331           (when (assq 'execute (car pslist))
9332             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9333                                   (eq gnus-view-pseudos 'not-confirm)))
9334           (setq pslist (cdr pslist)))
9335       (save-excursion
9336         (while pslist
9337           (setq after-article (or (cdr (assq 'article (car pslist)))
9338                                   (gnus-summary-article-number)))
9339           (gnus-summary-goto-subject after-article)
9340           (forward-line 1)
9341           (setq b (point))
9342           (insert "    " (file-name-nondirectory
9343                           (cdr (assq 'name (car pslist))))
9344                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9345           (setq e (point))
9346           (forward-line -1)             ; back to `b'
9347           (gnus-add-text-properties
9348            b (1- e) (list 'gnus-number gnus-reffed-article-number
9349                           gnus-mouse-face-prop gnus-mouse-face))
9350           (gnus-data-enter
9351            after-article gnus-reffed-article-number
9352            gnus-unread-mark b (car pslist) 0 (- e b))
9353           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9354           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9355           (setq pslist (cdr pslist)))))))
9356
9357 (defun gnus-pseudos< (p1 p2)
9358   (let ((c1 (cdr (assq 'action p1)))
9359         (c2 (cdr (assq 'action p2))))
9360     (and c1 c2 (string< c1 c2))))
9361
9362 (defun gnus-request-pseudo-article (props)
9363   (cond ((assq 'execute props)
9364          (gnus-execute-command (cdr (assq 'execute props)))))
9365   (let ((gnus-current-article (gnus-summary-article-number)))
9366     (gnus-run-hooks 'gnus-mark-article-hook)))
9367
9368 (defun gnus-execute-command (command &optional automatic)
9369   (save-excursion
9370     (gnus-article-setup-buffer)
9371     (set-buffer gnus-article-buffer)
9372     (setq buffer-read-only nil)
9373     (let ((command (if automatic command
9374                      (read-string "Command: " (cons command 0)))))
9375       (erase-buffer)
9376       (insert "$ " command "\n\n")
9377       (if gnus-view-pseudo-asynchronously
9378           (start-process "gnus-execute" (current-buffer) shell-file-name
9379                          shell-command-switch command)
9380         (call-process shell-file-name nil t nil
9381                       shell-command-switch command)))))
9382
9383 ;; Summary kill commands.
9384
9385 (defun gnus-summary-edit-global-kill (article)
9386   "Edit the \"global\" kill file."
9387   (interactive (list (gnus-summary-article-number)))
9388   (gnus-group-edit-global-kill article))
9389
9390 (defun gnus-summary-edit-local-kill ()
9391   "Edit a local kill file applied to the current newsgroup."
9392   (interactive)
9393   (setq gnus-current-headers (gnus-summary-article-header))
9394   (gnus-group-edit-local-kill
9395    (gnus-summary-article-number) gnus-newsgroup-name))
9396
9397 ;;; Header reading.
9398
9399 (defun gnus-read-header (id &optional header)
9400   "Read the headers of article ID and enter them into the Gnus system."
9401   (let ((group gnus-newsgroup-name)
9402         (gnus-override-method
9403          (or
9404           gnus-override-method
9405           (and (gnus-news-group-p gnus-newsgroup-name)
9406                (car (gnus-refer-article-methods)))))
9407         where)
9408     ;; First we check to see whether the header in question is already
9409     ;; fetched.
9410     (if (stringp id)
9411         ;; This is a Message-ID.
9412         (setq header (or header (gnus-id-to-header id)))
9413       ;; This is an article number.
9414       (setq header (or header (gnus-summary-article-header id))))
9415     (if (and header
9416              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9417         ;; We have found the header.
9418         header
9419       ;; If this is a sparse article, we have to nix out its
9420       ;; previous entry in the thread hashtb.
9421       (when (and header
9422                  (gnus-summary-article-sparse-p (mail-header-number header)))
9423         (let* ((parent (gnus-parent-id (mail-header-references header)))
9424                (thread (and parent (gnus-id-to-thread parent))))
9425           (when thread
9426             (delq (assq header thread) thread))))
9427       ;; We have to really fetch the header to this article.
9428       (save-excursion
9429         (set-buffer nntp-server-buffer)
9430         (when (setq where (gnus-request-head id group))
9431           (nnheader-fold-continuation-lines)
9432           (goto-char (point-max))
9433           (insert ".\n")
9434           (goto-char (point-min))
9435           (insert "211 ")
9436           (princ (cond
9437                   ((numberp id) id)
9438                   ((cdr where) (cdr where))
9439                   (header (mail-header-number header))
9440                   (t gnus-reffed-article-number))
9441                  (current-buffer))
9442           (insert " Article retrieved.\n"))
9443         (if (or (not where)
9444                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9445             ()                          ; Malformed head.
9446           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9447             (when (and (stringp id)
9448                        (not (string= (gnus-group-real-name group)
9449                                      (car where))))
9450               ;; If we fetched by Message-ID and the article came
9451               ;; from a different group, we fudge some bogus article
9452               ;; numbers for this article.
9453               (mail-header-set-number header gnus-reffed-article-number))
9454             (save-excursion
9455               (set-buffer gnus-summary-buffer)
9456               (decf gnus-reffed-article-number)
9457               (gnus-remove-header (mail-header-number header))
9458               (push header gnus-newsgroup-headers)
9459               (setq gnus-current-headers header)
9460               (push (mail-header-number header) gnus-newsgroup-limit)))
9461           header)))))
9462
9463 (defun gnus-remove-header (number)
9464   "Remove header NUMBER from `gnus-newsgroup-headers'."
9465   (if (and gnus-newsgroup-headers
9466            (= number (mail-header-number (car gnus-newsgroup-headers))))
9467       (pop gnus-newsgroup-headers)
9468     (let ((headers gnus-newsgroup-headers))
9469       (while (and (cdr headers)
9470                   (not (= number (mail-header-number (cadr headers)))))
9471         (pop headers))
9472       (when (cdr headers)
9473         (setcdr headers (cddr headers))))))
9474
9475 ;;;
9476 ;;; summary highlights
9477 ;;;
9478
9479 (defun gnus-highlight-selected-summary ()
9480   "Highlight selected article in summary buffer."
9481   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9482   (when gnus-summary-selected-face
9483     (save-excursion
9484       (let* ((beg (progn (beginning-of-line) (point)))
9485              (end (progn (end-of-line) (point)))
9486              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9487              (from (if (get-text-property beg gnus-mouse-face-prop)
9488                        beg
9489                      (or (next-single-property-change
9490                           beg gnus-mouse-face-prop nil end)
9491                          beg)))
9492              (to
9493               (if (= from end)
9494                   (- from 2)
9495                 (or (next-single-property-change
9496                      from gnus-mouse-face-prop nil end)
9497                     end))))
9498         ;; If no mouse-face prop on line we will have to = from = end,
9499         ;; so we highlight the entire line instead.
9500         (when (= (+ to 2) from)
9501           (setq from beg)
9502           (setq to end))
9503         (if gnus-newsgroup-selected-overlay
9504             ;; Move old overlay.
9505             (gnus-move-overlay
9506              gnus-newsgroup-selected-overlay from to (current-buffer))
9507           ;; Create new overlay.
9508           (gnus-overlay-put
9509            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9510            'face gnus-summary-selected-face))))))
9511
9512 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9513 (defun gnus-summary-highlight-line ()
9514   "Highlight current line according to `gnus-summary-highlight'."
9515   (let* ((list gnus-summary-highlight)
9516          (p (point))
9517          (end (progn (end-of-line) (point)))
9518          ;; now find out where the line starts and leave point there.
9519          (beg (progn (beginning-of-line) (point)))
9520          (article (gnus-summary-article-number))
9521          (score (or (cdr (assq (or article gnus-current-article)
9522                                gnus-newsgroup-scored))
9523                     gnus-summary-default-score 0))
9524          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9525          (inhibit-read-only t))
9526     ;; Eval the cars of the lists until we find a match.
9527     (let ((default gnus-summary-default-score))
9528       (while (and list
9529                   (not (eval (caar list))))
9530         (setq list (cdr list))))
9531     (let ((face (cdar list)))
9532       (unless (eq face (get-text-property beg 'face))
9533         (gnus-put-text-property-excluding-characters-with-faces
9534          beg end 'face
9535          (setq face (if (boundp face) (symbol-value face) face)))
9536         (when gnus-summary-highlight-line-function
9537           (funcall gnus-summary-highlight-line-function article face))))
9538     (goto-char p)))
9539
9540 (defun gnus-update-read-articles (group unread &optional compute)
9541   "Update the list of read articles in GROUP."
9542   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9543          (entry (gnus-gethash group gnus-newsrc-hashtb))
9544          (info (nth 2 entry))
9545          (prev 1)
9546          (unread (sort (copy-sequence unread) '<))
9547          read)
9548     (if (or (not info) (not active))
9549         ;; There is no info on this group if it was, in fact,
9550         ;; killed.  Gnus stores no information on killed groups, so
9551         ;; there's nothing to be done.
9552         ;; One could store the information somewhere temporarily,
9553         ;; perhaps...  Hmmm...
9554         ()
9555       ;; Remove any negative articles numbers.
9556       (while (and unread (< (car unread) 0))
9557         (setq unread (cdr unread)))
9558       ;; Remove any expired article numbers
9559       (while (and unread (< (car unread) (car active)))
9560         (setq unread (cdr unread)))
9561       ;; Compute the ranges of read articles by looking at the list of
9562       ;; unread articles.
9563       (while unread
9564         (when (/= (car unread) prev)
9565           (push (if (= prev (1- (car unread))) prev
9566                   (cons prev (1- (car unread))))
9567                 read))
9568         (setq prev (1+ (car unread)))
9569         (setq unread (cdr unread)))
9570       (when (<= prev (cdr active))
9571         (push (cons prev (cdr active)) read))
9572       (setq read (if (> (length read) 1) (nreverse read) read))
9573       (if compute
9574           read
9575         (save-excursion
9576           (let (setmarkundo)
9577             ;; Propagate the read marks to the backend.
9578             (when (gnus-check-backend-function 'request-set-mark group)
9579               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9580                     (add (gnus-remove-from-range read (gnus-info-read info))))
9581                 (when (or add del)
9582                   (unless (gnus-check-group group)
9583                     (error "Can't open server for %s" group))
9584                   (gnus-request-set-mark
9585                    group (delq nil (list (if add (list add 'add '(read)))
9586                                          (if del (list del 'del '(read))))))
9587                   (setq setmarkundo
9588                         `(gnus-request-set-mark
9589                           ,group
9590                           ',(delq nil (list
9591                                        (if del (list del 'add '(read)))
9592                                        (if add (list add 'del '(read))))))))))
9593             (set-buffer gnus-group-buffer)
9594             (gnus-undo-register
9595               `(progn
9596                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9597                  (gnus-info-set-read ',info ',(gnus-info-read info))
9598                  (gnus-get-unread-articles-in-group ',info 
9599                                                     (gnus-active ,group))
9600                  (gnus-group-update-group ,group t)
9601                  ,setmarkundo))))
9602         ;; Enter this list into the group info.
9603         (gnus-info-set-read info read)
9604         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9605         (gnus-get-unread-articles-in-group info (gnus-active group))
9606         t))))
9607
9608 (defun gnus-offer-save-summaries ()
9609   "Offer to save all active summary buffers."
9610   (save-excursion
9611     (let ((buflist (buffer-list))
9612           buffers bufname)
9613       ;; Go through all buffers and find all summaries.
9614       (while buflist
9615         (and (setq bufname (buffer-name (car buflist)))
9616              (string-match "Summary" bufname)
9617              (save-excursion
9618                (set-buffer bufname)
9619                ;; We check that this is, indeed, a summary buffer.
9620                (and (eq major-mode 'gnus-summary-mode)
9621                     ;; Also make sure this isn't bogus.
9622                     gnus-newsgroup-prepared
9623                     ;; Also make sure that this isn't a dead summary buffer.
9624                     (not gnus-dead-summary-mode)))
9625              (push bufname buffers))
9626         (setq buflist (cdr buflist)))
9627       ;; Go through all these summary buffers and offer to save them.
9628       (when buffers
9629         (map-y-or-n-p
9630          "Update summary buffer %s? "
9631          (lambda (buf)
9632            (switch-to-buffer buf)
9633            (gnus-summary-exit))
9634          buffers)))))
9635
9636 (defun gnus-summary-setup-default-charset ()
9637   "Setup newsgroup default charset."
9638   (if (equal gnus-newsgroup-name "nndraft:drafts")
9639       (setq gnus-newsgroup-charset nil)
9640     (let* ((name (and gnus-newsgroup-name
9641                       (gnus-group-real-name gnus-newsgroup-name)))
9642            (ignored-charsets
9643             (or gnus-newsgroup-ephemeral-ignored-charsets
9644                 (append
9645                  (and gnus-newsgroup-name
9646                       (or (gnus-group-find-parameter gnus-newsgroup-name
9647                                                      'ignored-charsets t)
9648                           (let ((alist gnus-group-ignored-charsets-alist)
9649                                 elem (charsets nil))
9650                             (while (setq elem (pop alist))
9651                               (when (and name
9652                                          (string-match (car elem) name))
9653                                 (setq alist nil
9654                                       charsets (cdr elem))))
9655                             charsets)))
9656                  gnus-newsgroup-ignored-charsets))))
9657       (setq gnus-newsgroup-charset
9658             (or gnus-newsgroup-ephemeral-charset
9659                 (and gnus-newsgroup-name
9660                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9661                          (let ((alist gnus-group-charset-alist)
9662                                elem charset)
9663                            (while (setq elem (pop alist))
9664                              (when (and name
9665                                         (string-match (car elem) name))
9666                                (setq alist nil
9667                                      charset (cadr elem))))
9668                            charset)))
9669                 gnus-default-charset))
9670       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9671            ignored-charsets))))
9672
9673 ;;;
9674 ;;; Mime Commands
9675 ;;;
9676
9677 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9678   "Display the current article buffer fully MIME-buttonized.
9679 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9680 treated as multipart/mixed."
9681   (interactive "P")
9682   (require 'gnus-art)
9683   (let ((gnus-unbuttonized-mime-types nil)
9684         (gnus-mime-display-multipart-as-mixed show-all-parts))
9685     (gnus-summary-show-article)))
9686
9687 (defun gnus-summary-repair-multipart (article)
9688   "Add a Content-Type header to a multipart article without one."
9689   (interactive (list (gnus-summary-article-number)))
9690   (gnus-with-article article
9691     (message-narrow-to-head)
9692     (goto-char (point-max))
9693     (widen)
9694     (when (search-forward "\n--" nil t)
9695       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9696         (message-narrow-to-head)
9697         (message-remove-header "Mime-Version")
9698         (message-remove-header "Content-Type")
9699         (goto-char (point-max))
9700         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9701                         separator))
9702         (insert "Mime-Version: 1.0\n")
9703         (widen))))
9704   (let (gnus-mark-article-hook)
9705     (gnus-summary-select-article t t nil article)))
9706
9707 (defun gnus-summary-toggle-display-buttonized ()
9708   "Toggle the buttonizing of the article buffer."
9709   (interactive)
9710   (require 'gnus-art)
9711   (if (setq gnus-inhibit-mime-unbuttonizing
9712             (not gnus-inhibit-mime-unbuttonizing))
9713       (let ((gnus-unbuttonized-mime-types nil))
9714         (gnus-summary-show-article))
9715     (gnus-summary-show-article)))
9716
9717 ;;;
9718 ;;; Generic summary marking commands
9719 ;;;
9720
9721 (defvar gnus-summary-marking-alist
9722   '((read gnus-del-mark "d")
9723     (unread gnus-unread-mark "u")
9724     (ticked gnus-ticked-mark "!")
9725     (dormant gnus-dormant-mark "?")
9726     (expirable gnus-expirable-mark "e"))
9727   "An alist of names/marks/keystrokes.")
9728
9729 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9730 (defvar gnus-summary-mark-map)
9731
9732 (defun gnus-summary-make-all-marking-commands ()
9733   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9734   (dolist (elem gnus-summary-marking-alist)
9735     (apply 'gnus-summary-make-marking-command elem)))
9736
9737 (defun gnus-summary-make-marking-command (name mark keystroke)
9738   (let ((map (make-sparse-keymap)))
9739     (define-key gnus-summary-generic-mark-map keystroke map)
9740     (dolist (lway `((next "next" next nil "n")
9741                     (next-unread "next unread" next t "N")
9742                     (prev "previous" prev nil "p")
9743                     (prev-unread "previous unread" prev t "P")
9744                     (nomove "" nil nil ,keystroke)))
9745       (let ((func (gnus-summary-make-marking-command-1
9746                    mark (car lway) lway name)))
9747         (setq func (eval func))
9748         (define-key map (nth 4 lway) func)))))
9749       
9750 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9751   `(defun ,(intern
9752             (format "gnus-summary-put-mark-as-%s%s"
9753                     name (if (eq way 'nomove)
9754                              ""
9755                            (concat "-" (symbol-name way)))))
9756      (n)
9757      ,(format
9758        "Mark the current article as %s%s.
9759 If N, the prefix, then repeat N times.
9760 If N is negative, move in reverse order.
9761 The difference between N and the actual number of articles marked is
9762 returned."
9763        name (cadr lway))
9764      (interactive "p")
9765      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9766     
9767 (defun gnus-summary-generic-mark (n mark move unread)
9768   "Mark N articles with MARK."
9769   (unless (eq major-mode 'gnus-summary-mode)
9770     (error "This command can only be used in the summary buffer"))
9771   (gnus-summary-show-thread)
9772   (let ((nummove
9773          (cond
9774           ((eq move 'next) 1)
9775           ((eq move 'prev) -1)
9776           (t 0))))
9777     (if (zerop nummove)
9778         (setq n 1)
9779       (when (< n 0)
9780         (setq n (abs n)
9781               nummove (* -1 nummove))))
9782     (while (and (> n 0)
9783                 (gnus-summary-mark-article nil mark)
9784                 (zerop (gnus-summary-next-subject nummove unread t)))
9785       (setq n (1- n)))
9786     (when (/= 0 n)
9787       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9788     (gnus-summary-recenter)
9789     (gnus-summary-position-point)
9790     (gnus-set-mode-line 'summary)
9791     n))
9792
9793 (gnus-summary-make-all-marking-commands)
9794
9795 (gnus-ems-redefine)
9796
9797 (provide 'gnus-sum)
9798
9799 (run-hooks 'gnus-sum-load-hook)
9800
9801 ;;; gnus-sum.el ends here