Only ask question once when sending.
[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-scores-exclude-files nil)
902 (defvar gnus-page-broken nil)
903 (defvar gnus-inhibit-mime-unbuttonizing nil)
904
905 (defvar gnus-original-article nil)
906 (defvar gnus-article-internal-prepare-hook nil)
907 (defvar gnus-newsgroup-process-stack nil)
908
909 (defvar gnus-thread-indent-array nil)
910 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
911 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
912   "Function called to sort the articles within a thread after it has been gathered together.")
913
914 (defvar gnus-summary-save-parts-type-history nil)
915 (defvar gnus-summary-save-parts-last-directory nil)
916
917 ;; Avoid highlighting in kill files.
918 (defvar gnus-summary-inhibit-highlight nil)
919 (defvar gnus-newsgroup-selected-overlay nil)
920 (defvar gnus-inhibit-limiting nil)
921 (defvar gnus-newsgroup-adaptive-score-file nil)
922 (defvar gnus-current-score-file nil)
923 (defvar gnus-current-move-group nil)
924 (defvar gnus-current-copy-group nil)
925 (defvar gnus-current-crosspost-group nil)
926
927 (defvar gnus-newsgroup-dependencies nil)
928 (defvar gnus-newsgroup-adaptive nil)
929 (defvar gnus-summary-display-article-function nil)
930 (defvar gnus-summary-highlight-line-function nil
931   "Function called after highlighting a summary line.")
932
933 (defvar gnus-summary-line-format-alist
934   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
935     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
936     (?s gnus-tmp-subject-or-nil ?s)
937     (?n gnus-tmp-name ?s)
938     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
939         ?s)
940     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
941             gnus-tmp-from) ?s)
942     (?F gnus-tmp-from ?s)
943     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
944     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
945     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
946     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
947     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
948     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
949     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
950     (?L gnus-tmp-lines ?d)
951     (?I gnus-tmp-indentation ?s)
952     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
953     (?R gnus-tmp-replied ?c)
954     (?\[ gnus-tmp-opening-bracket ?c)
955     (?\] gnus-tmp-closing-bracket ?c)
956     (?\> (make-string gnus-tmp-level ? ) ?s)
957     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
958     (?i gnus-tmp-score ?d)
959     (?z gnus-tmp-score-char ?c)
960     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
961     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
962     (?U gnus-tmp-unread ?c)
963     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
964     (?t (gnus-summary-number-of-articles-in-thread
965          (and (boundp 'thread) (car thread)) gnus-tmp-level)
966         ?d)
967     (?e (gnus-summary-number-of-articles-in-thread
968          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
969         ?c)
970     (?u gnus-tmp-user-defined ?s)
971     (?P (gnus-pick-line-number) ?d))
972   "An alist of format specifications that can appear in summary lines.
973 These are paired with what variables they correspond with, along with
974 the type of the variable (string, integer, character, etc).")
975
976 (defvar gnus-summary-dummy-line-format-alist
977   `((?S gnus-tmp-subject ?s)
978     (?N gnus-tmp-number ?d)
979     (?u gnus-tmp-user-defined ?s)))
980
981 (defvar gnus-summary-mode-line-format-alist
982   `((?G gnus-tmp-group-name ?s)
983     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
984     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
985     (?A gnus-tmp-article-number ?d)
986     (?Z gnus-tmp-unread-and-unselected ?s)
987     (?V gnus-version ?s)
988     (?U gnus-tmp-unread-and-unticked ?d)
989     (?S gnus-tmp-subject ?s)
990     (?e gnus-tmp-unselected ?d)
991     (?u gnus-tmp-user-defined ?s)
992     (?d (length gnus-newsgroup-dormant) ?d)
993     (?t (length gnus-newsgroup-marked) ?d)
994     (?r (length gnus-newsgroup-reads) ?d)
995     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
996     (?E gnus-newsgroup-expunged-tally ?d)
997     (?s (gnus-current-score-file-nondirectory) ?s)))
998
999 (defvar gnus-last-search-regexp nil
1000   "Default regexp for article search command.")
1001
1002 (defvar gnus-last-shell-command nil
1003   "Default shell command on article.")
1004
1005 (defvar gnus-newsgroup-begin nil)
1006 (defvar gnus-newsgroup-end nil)
1007 (defvar gnus-newsgroup-last-rmail nil)
1008 (defvar gnus-newsgroup-last-mail nil)
1009 (defvar gnus-newsgroup-last-folder nil)
1010 (defvar gnus-newsgroup-last-file nil)
1011 (defvar gnus-newsgroup-auto-expire nil)
1012 (defvar gnus-newsgroup-active nil)
1013
1014 (defvar gnus-newsgroup-data nil)
1015 (defvar gnus-newsgroup-data-reverse nil)
1016 (defvar gnus-newsgroup-limit nil)
1017 (defvar gnus-newsgroup-limits nil)
1018
1019 (defvar gnus-newsgroup-unreads nil
1020   "List of unread articles in the current newsgroup.")
1021
1022 (defvar gnus-newsgroup-unselected nil
1023   "List of unselected unread articles in the current newsgroup.")
1024
1025 (defvar gnus-newsgroup-reads nil
1026   "Alist of read articles and article marks in the current newsgroup.")
1027
1028 (defvar gnus-newsgroup-expunged-tally nil)
1029
1030 (defvar gnus-newsgroup-marked nil
1031   "List of ticked articles in the current newsgroup (a subset of unread art).")
1032
1033 (defvar gnus-newsgroup-killed nil
1034   "List of ranges of articles that have been through the scoring process.")
1035
1036 (defvar gnus-newsgroup-cached nil
1037   "List of articles that come from the article cache.")
1038
1039 (defvar gnus-newsgroup-saved nil
1040   "List of articles that have been saved.")
1041
1042 (defvar gnus-newsgroup-kill-headers nil)
1043
1044 (defvar gnus-newsgroup-replied nil
1045   "List of articles that have been replied to in the current newsgroup.")
1046
1047 (defvar gnus-newsgroup-expirable nil
1048   "List of articles in the current newsgroup that can be expired.")
1049
1050 (defvar gnus-newsgroup-processable nil
1051   "List of articles in the current newsgroup that can be processed.")
1052
1053 (defvar gnus-newsgroup-downloadable nil
1054   "List of articles in the current newsgroup that can be processed.")
1055
1056 (defvar gnus-newsgroup-undownloaded nil
1057   "List of articles in the current newsgroup that haven't been downloaded..")
1058
1059 (defvar gnus-newsgroup-unsendable nil
1060   "List of articles in the current newsgroup that won't be sent.")
1061
1062 (defvar gnus-newsgroup-bookmarks nil
1063   "List of articles in the current newsgroup that have bookmarks.")
1064
1065 (defvar gnus-newsgroup-dormant nil
1066   "List of dormant articles in the current newsgroup.")
1067
1068 (defvar gnus-newsgroup-scored nil
1069   "List of scored articles in the current newsgroup.")
1070
1071 (defvar gnus-newsgroup-headers nil
1072   "List of article headers in the current newsgroup.")
1073
1074 (defvar gnus-newsgroup-threads nil)
1075
1076 (defvar gnus-newsgroup-prepared nil
1077   "Whether the current group has been prepared properly.")
1078
1079 (defvar gnus-newsgroup-ancient nil
1080   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1081
1082 (defvar gnus-newsgroup-sparse nil)
1083
1084 (defvar gnus-current-article nil)
1085 (defvar gnus-article-current nil)
1086 (defvar gnus-current-headers nil)
1087 (defvar gnus-have-all-headers nil)
1088 (defvar gnus-last-article nil)
1089 (defvar gnus-newsgroup-history nil)
1090 (defvar gnus-newsgroup-charset nil)
1091 (defvar gnus-newsgroup-ephemeral-charset nil)
1092 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1093
1094 (defconst gnus-summary-local-variables
1095   '(gnus-newsgroup-name
1096     gnus-newsgroup-begin gnus-newsgroup-end
1097     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1098     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1099     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1100     gnus-newsgroup-unselected gnus-newsgroup-marked
1101     gnus-newsgroup-reads gnus-newsgroup-saved
1102     gnus-newsgroup-replied gnus-newsgroup-expirable
1103     gnus-newsgroup-processable gnus-newsgroup-killed
1104     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1105     gnus-newsgroup-unsendable
1106     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1107     gnus-newsgroup-headers gnus-newsgroup-threads
1108     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1109     gnus-current-article gnus-current-headers gnus-have-all-headers
1110     gnus-last-article gnus-article-internal-prepare-hook
1111     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1112     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1113     gnus-thread-expunge-below
1114     gnus-score-alist gnus-current-score-file
1115     (gnus-summary-expunge-below . global)
1116     (gnus-summary-mark-below . global)
1117     (gnus-orphan-score . global)
1118     gnus-newsgroup-active gnus-scores-exclude-files
1119     gnus-newsgroup-history gnus-newsgroup-ancient
1120     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1121     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1122     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1123     (gnus-newsgroup-expunged-tally . 0)
1124     gnus-cache-removable-articles gnus-newsgroup-cached
1125     gnus-newsgroup-data gnus-newsgroup-data-reverse
1126     gnus-newsgroup-limit gnus-newsgroup-limits
1127     gnus-newsgroup-charset)
1128   "Variables that are buffer-local to the summary buffers.")
1129
1130 ;; Byte-compiler warning.
1131 (defvar gnus-article-mode-map)
1132
1133 ;; MIME stuff.
1134
1135 (defvar gnus-decode-encoded-word-methods
1136   '(mail-decode-encoded-word-string)
1137   "List of methods used to decode encoded words.
1138
1139 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1140 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1141 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1142 whose names match REGEXP.
1143
1144 For example:
1145 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1146  mail-decode-encoded-word-string
1147  (\"chinese\" . rfc1843-decode-string))")
1148
1149 (defvar gnus-decode-encoded-word-methods-cache nil)
1150
1151 (defun gnus-multi-decode-encoded-word-string (string)
1152   "Apply the functions from `gnus-encoded-word-methods' that match."
1153   (unless (and gnus-decode-encoded-word-methods-cache
1154                (eq gnus-newsgroup-name
1155                    (car gnus-decode-encoded-word-methods-cache)))
1156     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1157     (mapcar (lambda (x)
1158               (if (symbolp x)
1159                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1160                 (if (and gnus-newsgroup-name
1161                          (string-match (car x) gnus-newsgroup-name))
1162                     (nconc gnus-decode-encoded-word-methods-cache
1163                            (list (cdr x))))))
1164           gnus-decode-encoded-word-methods))
1165   (let ((xlist gnus-decode-encoded-word-methods-cache))
1166     (pop xlist)
1167     (while xlist
1168       (setq string (funcall (pop xlist) string))))
1169   string)
1170
1171 ;; Subject simplification.
1172
1173 (defun gnus-simplify-whitespace (str)
1174   "Remove excessive whitespace from STR."
1175   (let ((mystr str))
1176     ;; Multiple spaces.
1177     (while (string-match "[ \t][ \t]+" mystr)
1178       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1179                           " "
1180                           (substring mystr (match-end 0)))))
1181     ;; Leading spaces.
1182     (when (string-match "^[ \t]+" mystr)
1183       (setq mystr (substring mystr (match-end 0))))
1184     ;; Trailing spaces.
1185     (when (string-match "[ \t]+$" mystr)
1186       (setq mystr (substring mystr 0 (match-beginning 0))))
1187     mystr))
1188
1189 (defsubst gnus-simplify-subject-re (subject)
1190   "Remove \"Re:\" from subject lines."
1191   (if (string-match "^[Rr][Ee]: *" subject)
1192       (substring subject (match-end 0))
1193     subject))
1194
1195 (defun gnus-simplify-subject (subject &optional re-only)
1196   "Remove `Re:' and words in parentheses.
1197 If RE-ONLY is non-nil, strip leading `Re:'s only."
1198   (let ((case-fold-search t))           ;Ignore case.
1199     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1200     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1201       (setq subject (substring subject (match-end 0))))
1202     ;; Remove uninteresting prefixes.
1203     (when (and (not re-only)
1204                gnus-simplify-ignored-prefixes
1205                (string-match gnus-simplify-ignored-prefixes subject))
1206       (setq subject (substring subject (match-end 0))))
1207     ;; Remove words in parentheses from end.
1208     (unless re-only
1209       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1210         (setq subject (substring subject 0 (match-beginning 0)))))
1211     ;; Return subject string.
1212     subject))
1213
1214 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1215 ;; all whitespace.
1216 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1217   (goto-char (point-min))
1218   (while (re-search-forward regexp nil t)
1219     (replace-match (or newtext ""))))
1220
1221 (defun gnus-simplify-buffer-fuzzy ()
1222   "Simplify string in the buffer fuzzily.
1223 The string in the accessible portion of the current buffer is simplified.
1224 It is assumed to be a single-line subject.
1225 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1226 matter is removed.  Additional things can be deleted by setting
1227 `gnus-simplify-subject-fuzzy-regexp'."
1228   (let ((case-fold-search t)
1229         (modified-tick))
1230     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1231
1232     (while (not (eq modified-tick (buffer-modified-tick)))
1233       (setq modified-tick (buffer-modified-tick))
1234       (cond
1235        ((listp gnus-simplify-subject-fuzzy-regexp)
1236         (mapcar 'gnus-simplify-buffer-fuzzy-step
1237                 gnus-simplify-subject-fuzzy-regexp))
1238        (gnus-simplify-subject-fuzzy-regexp
1239         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1240       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1241       (gnus-simplify-buffer-fuzzy-step
1242        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1243       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1244
1245     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1246     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1247     (gnus-simplify-buffer-fuzzy-step " $")
1248     (gnus-simplify-buffer-fuzzy-step "^ +")))
1249
1250 (defun gnus-simplify-subject-fuzzy (subject)
1251   "Simplify a subject string fuzzily.
1252 See `gnus-simplify-buffer-fuzzy' for details."
1253   (save-excursion
1254     (gnus-set-work-buffer)
1255     (let ((case-fold-search t))
1256       ;; Remove uninteresting prefixes.
1257       (when (and gnus-simplify-ignored-prefixes
1258                  (string-match gnus-simplify-ignored-prefixes subject))
1259         (setq subject (substring subject (match-end 0))))
1260       (insert subject)
1261       (inline (gnus-simplify-buffer-fuzzy))
1262       (buffer-string))))
1263
1264 (defsubst gnus-simplify-subject-fully (subject)
1265   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1266   (cond
1267    (gnus-simplify-subject-functions
1268     (gnus-map-function gnus-simplify-subject-functions subject))
1269    ((null gnus-summary-gather-subject-limit)
1270     (gnus-simplify-subject-re subject))
1271    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1272     (gnus-simplify-subject-fuzzy subject))
1273    ((numberp gnus-summary-gather-subject-limit)
1274     (gnus-limit-string (gnus-simplify-subject-re subject)
1275                        gnus-summary-gather-subject-limit))
1276    (t
1277     subject)))
1278
1279 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1280   "Check whether two subjects are equal.
1281 If optional argument simple-first is t, first argument is already
1282 simplified."
1283   (cond
1284    ((null simple-first)
1285     (equal (gnus-simplify-subject-fully s1)
1286            (gnus-simplify-subject-fully s2)))
1287    (t
1288     (equal s1
1289            (gnus-simplify-subject-fully s2)))))
1290
1291 (defun gnus-summary-bubble-group ()
1292   "Increase the score of the current group.
1293 This is a handy function to add to `gnus-summary-exit-hook' to
1294 increase the score of each group you read."
1295   (gnus-group-add-score gnus-newsgroup-name))
1296
1297 \f
1298 ;;;
1299 ;;; Gnus summary mode
1300 ;;;
1301
1302 (put 'gnus-summary-mode 'mode-class 'special)
1303
1304 (when t
1305   ;; Non-orthogonal keys
1306
1307   (gnus-define-keys gnus-summary-mode-map
1308     " " gnus-summary-next-page
1309     "\177" gnus-summary-prev-page
1310     [delete] gnus-summary-prev-page
1311     [backspace] gnus-summary-prev-page
1312     "\r" gnus-summary-scroll-up
1313     "\M-\r" gnus-summary-scroll-down
1314     "n" gnus-summary-next-unread-article
1315     "p" gnus-summary-prev-unread-article
1316     "N" gnus-summary-next-article
1317     "P" gnus-summary-prev-article
1318     "\M-\C-n" gnus-summary-next-same-subject
1319     "\M-\C-p" gnus-summary-prev-same-subject
1320     "\M-n" gnus-summary-next-unread-subject
1321     "\M-p" gnus-summary-prev-unread-subject
1322     "." gnus-summary-first-unread-article
1323     "," gnus-summary-best-unread-article
1324     "\M-s" gnus-summary-search-article-forward
1325     "\M-r" gnus-summary-search-article-backward
1326     "<" gnus-summary-beginning-of-article
1327     ">" gnus-summary-end-of-article
1328     "j" gnus-summary-goto-article
1329     "^" gnus-summary-refer-parent-article
1330     "\M-^" gnus-summary-refer-article
1331     "u" gnus-summary-tick-article-forward
1332     "!" gnus-summary-tick-article-forward
1333     "U" gnus-summary-tick-article-backward
1334     "d" gnus-summary-mark-as-read-forward
1335     "D" gnus-summary-mark-as-read-backward
1336     "E" gnus-summary-mark-as-expirable
1337     "\M-u" gnus-summary-clear-mark-forward
1338     "\M-U" gnus-summary-clear-mark-backward
1339     "k" gnus-summary-kill-same-subject-and-select
1340     "\C-k" gnus-summary-kill-same-subject
1341     "\M-\C-k" gnus-summary-kill-thread
1342     "\M-\C-l" gnus-summary-lower-thread
1343     "e" gnus-summary-edit-article
1344     "#" gnus-summary-mark-as-processable
1345     "\M-#" gnus-summary-unmark-as-processable
1346     "\M-\C-t" gnus-summary-toggle-threads
1347     "\M-\C-s" gnus-summary-show-thread
1348     "\M-\C-h" gnus-summary-hide-thread
1349     "\M-\C-f" gnus-summary-next-thread
1350     "\M-\C-b" gnus-summary-prev-thread
1351     [(meta down)] gnus-summary-next-thread
1352     [(meta up)] gnus-summary-prev-thread
1353     "\M-\C-u" gnus-summary-up-thread
1354     "\M-\C-d" gnus-summary-down-thread
1355     "&" gnus-summary-execute-command
1356     "c" gnus-summary-catchup-and-exit
1357     "\C-w" gnus-summary-mark-region-as-read
1358     "\C-t" gnus-summary-toggle-truncation
1359     "?" gnus-summary-mark-as-dormant
1360     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1361     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1362     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1363     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1364     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1365     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1366     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1367     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1368     "=" gnus-summary-expand-window
1369     "\C-x\C-s" gnus-summary-reselect-current-group
1370     "\M-g" gnus-summary-rescan-group
1371     "w" gnus-summary-stop-page-breaking
1372     "\C-c\C-r" gnus-summary-caesar-message
1373     "f" gnus-summary-followup
1374     "F" gnus-summary-followup-with-original
1375     "C" gnus-summary-cancel-article
1376     "r" gnus-summary-reply
1377     "R" gnus-summary-reply-with-original
1378     "\C-c\C-f" gnus-summary-mail-forward
1379     "o" gnus-summary-save-article
1380     "\C-o" gnus-summary-save-article-mail
1381     "|" gnus-summary-pipe-output
1382     "\M-k" gnus-summary-edit-local-kill
1383     "\M-K" gnus-summary-edit-global-kill
1384     ;; "V" gnus-version
1385     "\C-c\C-d" gnus-summary-describe-group
1386     "q" gnus-summary-exit
1387     "Q" gnus-summary-exit-no-update
1388     "\C-c\C-i" gnus-info-find-node
1389     gnus-mouse-2 gnus-mouse-pick-article
1390     "m" gnus-summary-mail-other-window
1391     "a" gnus-summary-post-news
1392     "x" gnus-summary-limit-to-unread
1393     "s" gnus-summary-isearch-article
1394     "t" gnus-summary-toggle-header
1395     "g" gnus-summary-show-article
1396     "l" gnus-summary-goto-last-article
1397     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1398     "\C-d" gnus-summary-enter-digest-group
1399     "\M-\C-d" gnus-summary-read-document
1400     "\M-\C-e" gnus-summary-edit-parameters
1401     "\M-\C-a" gnus-summary-customize-parameters
1402     "\C-c\C-b" gnus-bug
1403     "*" gnus-cache-enter-article
1404     "\M-*" gnus-cache-remove-article
1405     "\M-&" gnus-summary-universal-argument
1406     "\C-l" gnus-recenter
1407     "I" gnus-summary-increase-score
1408     "L" gnus-summary-lower-score
1409     "\M-i" gnus-symbolic-argument
1410     "h" gnus-summary-select-article-buffer
1411
1412     "b" gnus-article-view-part
1413     "\M-t" gnus-summary-toggle-display-buttonized
1414
1415     "V" gnus-summary-score-map
1416     "X" gnus-uu-extract-map
1417     "S" gnus-summary-send-map)
1418
1419   ;; Sort of orthogonal keymap
1420   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1421     "t" gnus-summary-tick-article-forward
1422     "!" gnus-summary-tick-article-forward
1423     "d" gnus-summary-mark-as-read-forward
1424     "r" gnus-summary-mark-as-read-forward
1425     "c" gnus-summary-clear-mark-forward
1426     " " gnus-summary-clear-mark-forward
1427     "e" gnus-summary-mark-as-expirable
1428     "x" gnus-summary-mark-as-expirable
1429     "?" gnus-summary-mark-as-dormant
1430     "b" gnus-summary-set-bookmark
1431     "B" gnus-summary-remove-bookmark
1432     "#" gnus-summary-mark-as-processable
1433     "\M-#" gnus-summary-unmark-as-processable
1434     "S" gnus-summary-limit-include-expunged
1435     "C" gnus-summary-catchup
1436     "H" gnus-summary-catchup-to-here
1437     "\C-c" gnus-summary-catchup-all
1438     "k" gnus-summary-kill-same-subject-and-select
1439     "K" gnus-summary-kill-same-subject
1440     "P" gnus-uu-mark-map)
1441
1442   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1443     "c" gnus-summary-clear-above
1444     "u" gnus-summary-tick-above
1445     "m" gnus-summary-mark-above
1446     "k" gnus-summary-kill-below)
1447
1448   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1449     "/" gnus-summary-limit-to-subject
1450     "n" gnus-summary-limit-to-articles
1451     "w" gnus-summary-pop-limit
1452     "s" gnus-summary-limit-to-subject
1453     "a" gnus-summary-limit-to-author
1454     "u" gnus-summary-limit-to-unread
1455     "m" gnus-summary-limit-to-marks
1456     "M" gnus-summary-limit-exclude-marks
1457     "v" gnus-summary-limit-to-score
1458     "*" gnus-summary-limit-include-cached
1459     "D" gnus-summary-limit-include-dormant
1460     "T" gnus-summary-limit-include-thread
1461     "d" gnus-summary-limit-exclude-dormant
1462     "t" gnus-summary-limit-to-age
1463     "x" gnus-summary-limit-to-extra
1464     "E" gnus-summary-limit-include-expunged
1465     "c" gnus-summary-limit-exclude-childless-dormant
1466     "C" gnus-summary-limit-mark-excluded-as-read)
1467
1468   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1469     "n" gnus-summary-next-unread-article
1470     "p" gnus-summary-prev-unread-article
1471     "N" gnus-summary-next-article
1472     "P" gnus-summary-prev-article
1473     "\C-n" gnus-summary-next-same-subject
1474     "\C-p" gnus-summary-prev-same-subject
1475     "\M-n" gnus-summary-next-unread-subject
1476     "\M-p" gnus-summary-prev-unread-subject
1477     "f" gnus-summary-first-unread-article
1478     "b" gnus-summary-best-unread-article
1479     "j" gnus-summary-goto-article
1480     "g" gnus-summary-goto-subject
1481     "l" gnus-summary-goto-last-article
1482     "o" gnus-summary-pop-article)
1483
1484   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1485     "k" gnus-summary-kill-thread
1486     "l" gnus-summary-lower-thread
1487     "i" gnus-summary-raise-thread
1488     "T" gnus-summary-toggle-threads
1489     "t" gnus-summary-rethread-current
1490     "^" gnus-summary-reparent-thread
1491     "s" gnus-summary-show-thread
1492     "S" gnus-summary-show-all-threads
1493     "h" gnus-summary-hide-thread
1494     "H" gnus-summary-hide-all-threads
1495     "n" gnus-summary-next-thread
1496     "p" gnus-summary-prev-thread
1497     "u" gnus-summary-up-thread
1498     "o" gnus-summary-top-thread
1499     "d" gnus-summary-down-thread
1500     "#" gnus-uu-mark-thread
1501     "\M-#" gnus-uu-unmark-thread)
1502
1503   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1504     "g" gnus-summary-prepare
1505     "c" gnus-summary-insert-cached-articles)
1506
1507   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1508     "c" gnus-summary-catchup-and-exit
1509     "C" gnus-summary-catchup-all-and-exit
1510     "E" gnus-summary-exit-no-update
1511     "Q" gnus-summary-exit
1512     "Z" gnus-summary-exit
1513     "n" gnus-summary-catchup-and-goto-next-group
1514     "R" gnus-summary-reselect-current-group
1515     "G" gnus-summary-rescan-group
1516     "N" gnus-summary-next-group
1517     "s" gnus-summary-save-newsrc
1518     "P" gnus-summary-prev-group)
1519
1520   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1521     " " gnus-summary-next-page
1522     "n" gnus-summary-next-page
1523     "\177" gnus-summary-prev-page
1524     [delete] gnus-summary-prev-page
1525     "p" gnus-summary-prev-page
1526     "\r" gnus-summary-scroll-up
1527     "\M-\r" gnus-summary-scroll-down
1528     "<" gnus-summary-beginning-of-article
1529     ">" gnus-summary-end-of-article
1530     "b" gnus-summary-beginning-of-article
1531     "e" gnus-summary-end-of-article
1532     "^" gnus-summary-refer-parent-article
1533     "r" gnus-summary-refer-parent-article
1534     "D" gnus-summary-enter-digest-group
1535     "R" gnus-summary-refer-references
1536     "T" gnus-summary-refer-thread
1537     "g" gnus-summary-show-article
1538     "s" gnus-summary-isearch-article
1539     "P" gnus-summary-print-article
1540     "t" gnus-article-babel)
1541
1542   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1543     "b" gnus-article-add-buttons
1544     "B" gnus-article-add-buttons-to-head
1545     "o" gnus-article-treat-overstrike
1546     "e" gnus-article-emphasize
1547     "w" gnus-article-fill-cited-article
1548     "Q" gnus-article-fill-long-lines
1549     "C" gnus-article-capitalize-sentences
1550     "c" gnus-article-remove-cr
1551     "q" gnus-article-de-quoted-unreadable
1552     "6" gnus-article-de-base64-unreadable
1553     "Z" gnus-article-decode-HZ
1554     "h" gnus-article-wash-html
1555     "f" gnus-article-display-x-face
1556     "l" gnus-summary-stop-page-breaking
1557     "r" gnus-summary-caesar-message
1558     "t" gnus-summary-toggle-header
1559     "v" gnus-summary-verbose-headers
1560     "H" gnus-article-strip-headers-in-body
1561     "d" gnus-article-treat-dumbquotes)
1562
1563   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1564     "a" gnus-article-hide
1565     "h" gnus-article-hide-headers
1566     "b" gnus-article-hide-boring-headers
1567     "s" gnus-article-hide-signature
1568     "c" gnus-article-hide-citation
1569     "C" gnus-article-hide-citation-in-followups
1570     "l" gnus-article-hide-list-identifiers
1571     "p" gnus-article-hide-pgp
1572     "B" gnus-article-strip-banner
1573     "P" gnus-article-hide-pem
1574     "\C-c" gnus-article-hide-citation-maybe)
1575
1576   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1577     "a" gnus-article-highlight
1578     "h" gnus-article-highlight-headers
1579     "c" gnus-article-highlight-citation
1580     "s" gnus-article-highlight-signature)
1581
1582   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1583     "w" gnus-article-decode-mime-words
1584     "c" gnus-article-decode-charset
1585     "v" gnus-mime-view-all-parts
1586     "b" gnus-article-view-part)
1587
1588   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1589     "z" gnus-article-date-ut
1590     "u" gnus-article-date-ut
1591     "l" gnus-article-date-local
1592     "e" gnus-article-date-lapsed
1593     "o" gnus-article-date-original
1594     "i" gnus-article-date-iso8601
1595     "s" gnus-article-date-user)
1596
1597   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1598     "t" gnus-article-remove-trailing-blank-lines
1599     "l" gnus-article-strip-leading-blank-lines
1600     "m" gnus-article-strip-multiple-blank-lines
1601     "a" gnus-article-strip-blank-lines
1602     "A" gnus-article-strip-all-blank-lines
1603     "s" gnus-article-strip-leading-space
1604     "e" gnus-article-strip-trailing-space)
1605
1606   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1607     "v" gnus-version
1608     "f" gnus-summary-fetch-faq
1609     "d" gnus-summary-describe-group
1610     "h" gnus-summary-describe-briefly
1611     "i" gnus-info-find-node)
1612
1613   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1614     "e" gnus-summary-expire-articles
1615     "\M-\C-e" gnus-summary-expire-articles-now
1616     "\177" gnus-summary-delete-article
1617     [delete] gnus-summary-delete-article
1618     [backspace] gnus-summary-delete-article
1619     "m" gnus-summary-move-article
1620     "r" gnus-summary-respool-article
1621     "w" gnus-summary-edit-article
1622     "c" gnus-summary-copy-article
1623     "B" gnus-summary-crosspost-article
1624     "q" gnus-summary-respool-query
1625     "t" gnus-summary-respool-trace
1626     "i" gnus-summary-import-article
1627     "p" gnus-summary-article-posted-p)
1628
1629   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1630     "o" gnus-summary-save-article
1631     "m" gnus-summary-save-article-mail
1632     "F" gnus-summary-write-article-file
1633     "r" gnus-summary-save-article-rmail
1634     "f" gnus-summary-save-article-file
1635     "b" gnus-summary-save-article-body-file
1636     "h" gnus-summary-save-article-folder
1637     "v" gnus-summary-save-article-vm
1638     "p" gnus-summary-pipe-output
1639     "s" gnus-soup-add-article)
1640
1641   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1642     "b" gnus-summary-display-buttonized
1643     "m" gnus-summary-repair-multipart
1644     "v" gnus-article-view-part
1645     "o" gnus-article-save-part
1646     "c" gnus-article-copy-part
1647     "e" gnus-article-externalize-part
1648     "i" gnus-article-inline-part
1649     "|" gnus-article-pipe-part))
1650
1651 (defun gnus-summary-make-menu-bar ()
1652   (gnus-turn-off-edit-menu 'summary)
1653
1654   (unless (boundp 'gnus-summary-misc-menu)
1655
1656     (easy-menu-define
1657      gnus-summary-kill-menu gnus-summary-mode-map ""
1658      (cons
1659       "Score"
1660       (nconc
1661        (list
1662         ["Enter score..." gnus-summary-score-entry t]
1663         ["Customize" gnus-score-customize t])
1664        (gnus-make-score-map 'increase)
1665        (gnus-make-score-map 'lower)
1666        '(("Mark"
1667           ["Kill below" gnus-summary-kill-below t]
1668           ["Mark above" gnus-summary-mark-above t]
1669           ["Tick above" gnus-summary-tick-above t]
1670           ["Clear above" gnus-summary-clear-above t])
1671          ["Current score" gnus-summary-current-score t]
1672          ["Set score" gnus-summary-set-score t]
1673          ["Switch current score file..." gnus-score-change-score-file t]
1674          ["Set mark below..." gnus-score-set-mark-below t]
1675          ["Set expunge below..." gnus-score-set-expunge-below t]
1676          ["Edit current score file" gnus-score-edit-current-scores t]
1677          ["Edit score file" gnus-score-edit-file t]
1678          ["Trace score" gnus-score-find-trace t]
1679          ["Find words" gnus-score-find-favourite-words t]
1680          ["Rescore buffer" gnus-summary-rescore t]
1681          ["Increase score..." gnus-summary-increase-score t]
1682          ["Lower score..." gnus-summary-lower-score t]))))
1683
1684     ;; Define both the Article menu in the summary buffer and the equivalent
1685     ;; Commands menu in the article buffer here for consistency.
1686     (let ((innards
1687            '(("Hide"
1688               ["All" gnus-article-hide t]
1689               ["Headers" gnus-article-hide-headers t]
1690               ["Signature" gnus-article-hide-signature t]
1691               ["Citation" gnus-article-hide-citation t]
1692               ["List identifiers" gnus-article-hide-list-identifiers t]
1693               ["PGP" gnus-article-hide-pgp t]
1694               ["Banner" gnus-article-strip-banner t]
1695               ["Boring headers" gnus-article-hide-boring-headers t])
1696              ("Highlight"
1697               ["All" gnus-article-highlight t]
1698               ["Headers" gnus-article-highlight-headers t]
1699               ["Signature" gnus-article-highlight-signature t]
1700               ["Citation" gnus-article-highlight-citation t])
1701              ("MIME"
1702               ["Words" gnus-article-decode-mime-words t]
1703               ["Charset" gnus-article-decode-charset t]
1704               ["QP" gnus-article-de-quoted-unreadable t]
1705               ["Base64" gnus-article-de-base64-unreadable t]
1706               ["View all" gnus-mime-view-all-parts t])
1707              ("Date"
1708               ["Local" gnus-article-date-local t]
1709               ["ISO8601" gnus-article-date-iso8601 t]
1710               ["UT" gnus-article-date-ut t]
1711               ["Original" gnus-article-date-original t]
1712               ["Lapsed" gnus-article-date-lapsed t]
1713               ["User-defined" gnus-article-date-user t])
1714              ("Washing"
1715               ("Remove Blanks"
1716                ["Leading" gnus-article-strip-leading-blank-lines t]
1717                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1718                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1719                ["All of the above" gnus-article-strip-blank-lines t]
1720                ["All" gnus-article-strip-all-blank-lines t]
1721                ["Leading space" gnus-article-strip-leading-space t]
1722                ["Trailing space" gnus-article-strip-trailing-space t])
1723               ["Overstrike" gnus-article-treat-overstrike t]
1724               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1725               ["Emphasis" gnus-article-emphasize t]
1726               ["Word wrap" gnus-article-fill-cited-article t]
1727               ["Fill long lines" gnus-article-fill-long-lines t]
1728               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1729               ["CR" gnus-article-remove-cr t]
1730               ["Show X-Face" gnus-article-display-x-face t]
1731               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1732               ["Base64" gnus-article-de-base64-unreadable t]
1733               ["Rot 13" gnus-summary-caesar-message t]
1734               ["Unix pipe" gnus-summary-pipe-message t]
1735               ["Add buttons" gnus-article-add-buttons t]
1736               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1737               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1738               ["Verbose header" gnus-summary-verbose-headers t]
1739               ["Toggle header" gnus-summary-toggle-header t]
1740               ["Html" gnus-article-wash-html t]
1741               ["HZ" gnus-article-decode-HZ t])
1742              ("Output"
1743               ["Save in default format" gnus-summary-save-article t]
1744               ["Save in file" gnus-summary-save-article-file t]
1745               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1746               ["Save in MH folder" gnus-summary-save-article-folder t]
1747               ["Save in VM folder" gnus-summary-save-article-vm t]
1748               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1749               ["Save body in file" gnus-summary-save-article-body-file t]
1750               ["Pipe through a filter" gnus-summary-pipe-output t]
1751               ["Add to SOUP packet" gnus-soup-add-article t]
1752               ["Print" gnus-summary-print-article t])
1753              ("Backend"
1754               ["Respool article..." gnus-summary-respool-article t]
1755               ["Move article..." gnus-summary-move-article
1756                (gnus-check-backend-function
1757                 'request-move-article gnus-newsgroup-name)]
1758               ["Copy article..." gnus-summary-copy-article t]
1759               ["Crosspost article..." gnus-summary-crosspost-article
1760                (gnus-check-backend-function
1761                 'request-replace-article gnus-newsgroup-name)]
1762               ["Import file..." gnus-summary-import-article t]
1763               ["Check if posted" gnus-summary-article-posted-p t]
1764               ["Edit article" gnus-summary-edit-article
1765                (not (gnus-group-read-only-p))]
1766               ["Delete article" gnus-summary-delete-article
1767                (gnus-check-backend-function
1768                 'request-expire-articles gnus-newsgroup-name)]
1769               ["Query respool" gnus-summary-respool-query t]
1770               ["Trace respool" gnus-summary-respool-trace t]
1771               ["Delete expirable articles" gnus-summary-expire-articles-now
1772                (gnus-check-backend-function
1773                 'request-expire-articles gnus-newsgroup-name)])
1774              ("Extract"
1775               ["Uudecode" gnus-uu-decode-uu t]
1776               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1777               ["Unshar" gnus-uu-decode-unshar t]
1778               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1779               ["Save" gnus-uu-decode-save t]
1780               ["Binhex" gnus-uu-decode-binhex t]
1781               ["Postscript" gnus-uu-decode-postscript t])
1782              ("Cache"
1783               ["Enter article" gnus-cache-enter-article t]
1784               ["Remove article" gnus-cache-remove-article t])
1785              ["Translate" gnus-article-babel t]
1786              ["Select article buffer" gnus-summary-select-article-buffer t]
1787              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1788              ["Isearch article..." gnus-summary-isearch-article t]
1789              ["Beginning of the article" gnus-summary-beginning-of-article t]
1790              ["End of the article" gnus-summary-end-of-article t]
1791              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1792              ["Fetch referenced articles" gnus-summary-refer-references t]
1793              ["Fetch current thread" gnus-summary-refer-thread t]
1794              ["Fetch article with id..." gnus-summary-refer-article t]
1795              ["Redisplay" gnus-summary-show-article t])))
1796       (easy-menu-define
1797        gnus-summary-article-menu gnus-summary-mode-map ""
1798        (cons "Article" innards))
1799
1800       (easy-menu-define
1801        gnus-article-commands-menu gnus-article-mode-map ""
1802        (cons "Commands" innards)))
1803
1804     (easy-menu-define
1805      gnus-summary-thread-menu gnus-summary-mode-map ""
1806      '("Threads"
1807        ["Toggle threading" gnus-summary-toggle-threads t]
1808        ["Hide threads" gnus-summary-hide-all-threads t]
1809        ["Show threads" gnus-summary-show-all-threads t]
1810        ["Hide thread" gnus-summary-hide-thread t]
1811        ["Show thread" gnus-summary-show-thread t]
1812        ["Go to next thread" gnus-summary-next-thread t]
1813        ["Go to previous thread" gnus-summary-prev-thread t]
1814        ["Go down thread" gnus-summary-down-thread t]
1815        ["Go up thread" gnus-summary-up-thread t]
1816        ["Top of thread" gnus-summary-top-thread t]
1817        ["Mark thread as read" gnus-summary-kill-thread t]
1818        ["Lower thread score" gnus-summary-lower-thread t]
1819        ["Raise thread score" gnus-summary-raise-thread t]
1820        ["Rethread current" gnus-summary-rethread-current t]))
1821
1822     (easy-menu-define
1823      gnus-summary-post-menu gnus-summary-mode-map ""
1824      '("Post"
1825        ["Post an article" gnus-summary-post-news t]
1826        ["Followup" gnus-summary-followup t]
1827        ["Followup and yank" gnus-summary-followup-with-original t]
1828        ["Supersede article" gnus-summary-supersede-article t]
1829        ["Cancel article" gnus-summary-cancel-article t]
1830        ["Reply" gnus-summary-reply t]
1831        ["Reply and yank" gnus-summary-reply-with-original t]
1832        ["Wide reply" gnus-summary-wide-reply t]
1833        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1834        ["Mail forward" gnus-summary-mail-forward t]
1835        ["Post forward" gnus-summary-post-forward t]
1836        ["Digest and mail" gnus-uu-digest-mail-forward t]
1837        ["Digest and post" gnus-uu-digest-post-forward t]
1838        ["Resend message" gnus-summary-resend-message t]
1839        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1840        ["Send a mail" gnus-summary-mail-other-window t]
1841        ["Uuencode and post" gnus-uu-post-news t]
1842        ["Followup via news" gnus-summary-followup-to-mail t]
1843        ["Followup via news and yank"
1844         gnus-summary-followup-to-mail-with-original t]
1845        ;;("Draft"
1846        ;;["Send" gnus-summary-send-draft t]
1847        ;;["Send bounced" gnus-resend-bounced-mail t])
1848        ))
1849
1850     (easy-menu-define
1851      gnus-summary-misc-menu gnus-summary-mode-map ""
1852      '("Misc"
1853        ("Mark Read"
1854         ["Mark as read" gnus-summary-mark-as-read-forward t]
1855         ["Mark same subject and select"
1856          gnus-summary-kill-same-subject-and-select t]
1857         ["Mark same subject" gnus-summary-kill-same-subject t]
1858         ["Catchup" gnus-summary-catchup t]
1859         ["Catchup all" gnus-summary-catchup-all t]
1860         ["Catchup to here" gnus-summary-catchup-to-here t]
1861         ["Catchup region" gnus-summary-mark-region-as-read t]
1862         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1863        ("Mark Various"
1864         ["Tick" gnus-summary-tick-article-forward t]
1865         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1866         ["Remove marks" gnus-summary-clear-mark-forward t]
1867         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1868         ["Set bookmark" gnus-summary-set-bookmark t]
1869         ["Remove bookmark" gnus-summary-remove-bookmark t])
1870        ("Mark Limit"
1871         ["Marks..." gnus-summary-limit-to-marks t]
1872         ["Subject..." gnus-summary-limit-to-subject t]
1873         ["Author..." gnus-summary-limit-to-author t]
1874         ["Age..." gnus-summary-limit-to-age t]
1875         ["Extra..." gnus-summary-limit-to-extra t]
1876         ["Score" gnus-summary-limit-to-score t]
1877         ["Unread" gnus-summary-limit-to-unread t]
1878         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1879         ["Articles" gnus-summary-limit-to-articles t]
1880         ["Pop limit" gnus-summary-pop-limit t]
1881         ["Show dormant" gnus-summary-limit-include-dormant t]
1882         ["Hide childless dormant"
1883          gnus-summary-limit-exclude-childless-dormant t]
1884         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1885         ["Hide marked" gnus-summary-limit-exclude-marks t]
1886         ["Show expunged" gnus-summary-show-all-expunged t])
1887        ("Process Mark"
1888         ["Set mark" gnus-summary-mark-as-processable t]
1889         ["Remove mark" gnus-summary-unmark-as-processable t]
1890         ["Remove all marks" gnus-summary-unmark-all-processable t]
1891         ["Mark above" gnus-uu-mark-over t]
1892         ["Mark series" gnus-uu-mark-series t]
1893         ["Mark region" gnus-uu-mark-region t]
1894         ["Unmark region" gnus-uu-unmark-region t]
1895         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1896         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1897         ["Mark all" gnus-uu-mark-all t]
1898         ["Mark buffer" gnus-uu-mark-buffer t]
1899         ["Mark sparse" gnus-uu-mark-sparse t]
1900         ["Mark thread" gnus-uu-mark-thread t]
1901         ["Unmark thread" gnus-uu-unmark-thread t]
1902         ("Process Mark Sets"
1903          ["Kill" gnus-summary-kill-process-mark t]
1904          ["Yank" gnus-summary-yank-process-mark
1905           gnus-newsgroup-process-stack]
1906          ["Save" gnus-summary-save-process-mark t]))
1907        ("Scroll article"
1908         ["Page forward" gnus-summary-next-page t]
1909         ["Page backward" gnus-summary-prev-page t]
1910         ["Line forward" gnus-summary-scroll-up t])
1911        ("Move"
1912         ["Next unread article" gnus-summary-next-unread-article t]
1913         ["Previous unread article" gnus-summary-prev-unread-article t]
1914         ["Next article" gnus-summary-next-article t]
1915         ["Previous article" gnus-summary-prev-article t]
1916         ["Next unread subject" gnus-summary-next-unread-subject t]
1917         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1918         ["Next article same subject" gnus-summary-next-same-subject t]
1919         ["Previous article same subject" gnus-summary-prev-same-subject t]
1920         ["First unread article" gnus-summary-first-unread-article t]
1921         ["Best unread article" gnus-summary-best-unread-article t]
1922         ["Go to subject number..." gnus-summary-goto-subject t]
1923         ["Go to article number..." gnus-summary-goto-article t]
1924         ["Go to the last article" gnus-summary-goto-last-article t]
1925         ["Pop article off history" gnus-summary-pop-article t])
1926        ("Sort"
1927         ["Sort by number" gnus-summary-sort-by-number t]
1928         ["Sort by author" gnus-summary-sort-by-author t]
1929         ["Sort by subject" gnus-summary-sort-by-subject t]
1930         ["Sort by date" gnus-summary-sort-by-date t]
1931         ["Sort by score" gnus-summary-sort-by-score t]
1932         ["Sort by lines" gnus-summary-sort-by-lines t]
1933         ["Sort by characters" gnus-summary-sort-by-chars t])
1934        ("Help"
1935         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1936         ["Describe group" gnus-summary-describe-group t]
1937         ["Read manual" gnus-info-find-node t])
1938        ("Modes"
1939         ["Pick and read" gnus-pick-mode t]
1940         ["Binary" gnus-binary-mode t])
1941        ("Regeneration"
1942         ["Regenerate" gnus-summary-prepare t]
1943         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1944         ["Toggle threading" gnus-summary-toggle-threads t])
1945        ["Filter articles..." gnus-summary-execute-command t]
1946        ["Run command on subjects..." gnus-summary-universal-argument t]
1947        ["Search articles forward..." gnus-summary-search-article-forward t]
1948        ["Search articles backward..." gnus-summary-search-article-backward t]
1949        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1950        ["Expand window" gnus-summary-expand-window t]
1951        ["Expire expirable articles" gnus-summary-expire-articles
1952         (gnus-check-backend-function
1953          'request-expire-articles gnus-newsgroup-name)]
1954        ["Edit local kill file" gnus-summary-edit-local-kill t]
1955        ["Edit main kill file" gnus-summary-edit-global-kill t]
1956        ["Edit group parameters" gnus-summary-edit-parameters t]
1957        ["Customize group parameters" gnus-summary-customize-parameters t]
1958        ["Send a bug report" gnus-bug t]
1959        ("Exit"
1960         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1961         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1962         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1963         ["Exit group" gnus-summary-exit t]
1964         ["Exit group without updating" gnus-summary-exit-no-update t]
1965         ["Exit and goto next group" gnus-summary-next-group t]
1966         ["Exit and goto prev group" gnus-summary-prev-group t]
1967         ["Reselect group" gnus-summary-reselect-current-group t]
1968         ["Rescan group" gnus-summary-rescan-group t]
1969         ["Update dribble" gnus-summary-save-newsrc t])))
1970
1971     (gnus-run-hooks 'gnus-summary-menu-hook)))
1972
1973 (defun gnus-score-set-default (var value)
1974   "A version of set that updates the GNU Emacs menu-bar."
1975   (set var value)
1976   ;; It is the message that forces the active status to be updated.
1977   (message ""))
1978
1979 (defun gnus-make-score-map (type)
1980   "Make a summary score map of type TYPE."
1981   (if t
1982       nil
1983     (let ((headers '(("author" "from" string)
1984                      ("subject" "subject" string)
1985                      ("article body" "body" string)
1986                      ("article head" "head" string)
1987                      ("xref" "xref" string)
1988                      ("extra header" "extra" string)
1989                      ("lines" "lines" number)
1990                      ("followups to author" "followup" string)))
1991           (types '((number ("less than" <)
1992                            ("greater than" >)
1993                            ("equal" =))
1994                    (string ("substring" s)
1995                            ("exact string" e)
1996                            ("fuzzy string" f)
1997                            ("regexp" r))))
1998           (perms '(("temporary" (current-time-string))
1999                    ("permanent" nil)
2000                    ("immediate" now)))
2001           header)
2002       (list
2003        (apply
2004         'nconc
2005         (list
2006          (if (eq type 'lower)
2007              "Lower score"
2008            "Increase score"))
2009         (let (outh)
2010           (while headers
2011             (setq header (car headers))
2012             (setq outh
2013                   (cons
2014                    (apply
2015                     'nconc
2016                     (list (car header))
2017                     (let ((ts (cdr (assoc (nth 2 header) types)))
2018                           outt)
2019                       (while ts
2020                         (setq outt
2021                               (cons
2022                                (apply
2023                                 'nconc
2024                                 (list (caar ts))
2025                                 (let ((ps perms)
2026                                       outp)
2027                                   (while ps
2028                                     (setq outp
2029                                           (cons
2030                                            (vector
2031                                             (caar ps)
2032                                             (list
2033                                              'gnus-summary-score-entry
2034                                              (nth 1 header)
2035                                              (if (or (string= (nth 1 header)
2036                                                               "head")
2037                                                      (string= (nth 1 header)
2038                                                               "body"))
2039                                                  ""
2040                                                (list 'gnus-summary-header
2041                                                      (nth 1 header)))
2042                                              (list 'quote (nth 1 (car ts)))
2043                                              (list 'gnus-score-delta-default
2044                                                    nil)
2045                                              (nth 1 (car ps))
2046                                              t)
2047                                             t)
2048                                            outp))
2049                                     (setq ps (cdr ps)))
2050                                   (list (nreverse outp))))
2051                                outt))
2052                         (setq ts (cdr ts)))
2053                       (list (nreverse outt))))
2054                    outh))
2055             (setq headers (cdr headers)))
2056           (list (nreverse outh))))))))
2057
2058 \f
2059
2060 (defun gnus-summary-mode (&optional group)
2061   "Major mode for reading articles.
2062
2063 All normal editing commands are switched off.
2064 \\<gnus-summary-mode-map>
2065 Each line in this buffer represents one article.  To read an
2066 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2067 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2068 respectively.
2069
2070 You can also post articles and send mail from this buffer.  To
2071 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2072 of an article, type `\\[gnus-summary-reply]'.
2073
2074 There are approx. one gazillion commands you can execute in this
2075 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2076
2077 The following commands are available:
2078
2079 \\{gnus-summary-mode-map}"
2080   (interactive)
2081   (when (gnus-visual-p 'summary-menu 'menu)
2082     (gnus-summary-make-menu-bar))
2083   (kill-all-local-variables)
2084   (gnus-summary-make-local-variables)
2085   (gnus-make-thread-indent-array)
2086   (gnus-simplify-mode-line)
2087   (setq major-mode 'gnus-summary-mode)
2088   (setq mode-name "Summary")
2089   (make-local-variable 'minor-mode-alist)
2090   (use-local-map gnus-summary-mode-map)
2091   (buffer-disable-undo)
2092   (setq buffer-read-only t)             ;Disable modification
2093   (setq truncate-lines t)
2094   (setq selective-display t)
2095   (setq selective-display-ellipses t)   ;Display `...'
2096   (gnus-summary-set-display-table)
2097   (gnus-set-default-directory)
2098   (setq gnus-newsgroup-name group)
2099   (make-local-variable 'gnus-summary-line-format)
2100   (make-local-variable 'gnus-summary-line-format-spec)
2101   (make-local-variable 'gnus-summary-dummy-line-format)
2102   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2103   (make-local-variable 'gnus-summary-mark-positions)
2104   (make-local-hook 'pre-command-hook)
2105   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2106   (gnus-run-hooks 'gnus-summary-mode-hook)
2107   (mm-enable-multibyte-mule4)
2108   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2109   (gnus-update-summary-mark-positions))
2110
2111 (defun gnus-summary-make-local-variables ()
2112   "Make all the local summary buffer variables."
2113   (let (global)
2114     (dolist (local gnus-summary-local-variables)
2115       (if (consp local)
2116           (progn
2117             (if (eq (cdr local) 'global)
2118                 ;; Copy the global value of the variable.
2119                 (setq global (symbol-value (car local)))
2120               ;; Use the value from the list.
2121               (setq global (eval (cdr local))))
2122             (set (make-local-variable (car local)) global))
2123         ;; Simple nil-valued local variable.
2124         (set (make-local-variable local) nil)))))
2125
2126 (defun gnus-summary-clear-local-variables ()
2127   (let ((locals gnus-summary-local-variables))
2128     (while locals
2129       (if (consp (car locals))
2130           (and (vectorp (caar locals))
2131                (set (caar locals) nil))
2132         (and (vectorp (car locals))
2133              (set (car locals) nil)))
2134       (setq locals (cdr locals)))))
2135
2136 ;; Summary data functions.
2137
2138 (defmacro gnus-data-number (data)
2139   `(car ,data))
2140
2141 (defmacro gnus-data-set-number (data number)
2142   `(setcar ,data ,number))
2143
2144 (defmacro gnus-data-mark (data)
2145   `(nth 1 ,data))
2146
2147 (defmacro gnus-data-set-mark (data mark)
2148   `(setcar (nthcdr 1 ,data) ,mark))
2149
2150 (defmacro gnus-data-pos (data)
2151   `(nth 2 ,data))
2152
2153 (defmacro gnus-data-set-pos (data pos)
2154   `(setcar (nthcdr 2 ,data) ,pos))
2155
2156 (defmacro gnus-data-header (data)
2157   `(nth 3 ,data))
2158
2159 (defmacro gnus-data-set-header (data header)
2160   `(setf (nth 3 ,data) ,header))
2161
2162 (defmacro gnus-data-level (data)
2163   `(nth 4 ,data))
2164
2165 (defmacro gnus-data-unread-p (data)
2166   `(= (nth 1 ,data) gnus-unread-mark))
2167
2168 (defmacro gnus-data-read-p (data)
2169   `(/= (nth 1 ,data) gnus-unread-mark))
2170
2171 (defmacro gnus-data-pseudo-p (data)
2172   `(consp (nth 3 ,data)))
2173
2174 (defmacro gnus-data-find (number)
2175   `(assq ,number gnus-newsgroup-data))
2176
2177 (defmacro gnus-data-find-list (number &optional data)
2178   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2179      (memq (assq ,number bdata)
2180            bdata)))
2181
2182 (defmacro gnus-data-make (number mark pos header level)
2183   `(list ,number ,mark ,pos ,header ,level))
2184
2185 (defun gnus-data-enter (after-article number mark pos header level offset)
2186   (let ((data (gnus-data-find-list after-article)))
2187     (unless data
2188       (error "No such article: %d" after-article))
2189     (setcdr data (cons (gnus-data-make number mark pos header level)
2190                        (cdr data)))
2191     (setq gnus-newsgroup-data-reverse nil)
2192     (gnus-data-update-list (cddr data) offset)))
2193
2194 (defun gnus-data-enter-list (after-article list &optional offset)
2195   (when list
2196     (let ((data (and after-article (gnus-data-find-list after-article)))
2197           (ilist list))
2198       (if (not (or data
2199                    after-article))
2200           (let ((odata gnus-newsgroup-data))
2201             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2202             (when offset
2203               (gnus-data-update-list odata offset)))
2204         ;; Find the last element in the list to be spliced into the main
2205         ;; list.
2206         (while (cdr list)
2207           (setq list (cdr list)))
2208         (if (not data)
2209             (progn
2210               (setcdr list gnus-newsgroup-data)
2211               (setq gnus-newsgroup-data ilist)
2212               (when offset
2213                 (gnus-data-update-list (cdr list) offset)))
2214           (setcdr list (cdr data))
2215           (setcdr data ilist)
2216           (when offset
2217             (gnus-data-update-list (cdr list) offset))))
2218       (setq gnus-newsgroup-data-reverse nil))))
2219
2220 (defun gnus-data-remove (article &optional offset)
2221   (let ((data gnus-newsgroup-data))
2222     (if (= (gnus-data-number (car data)) article)
2223         (progn
2224           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2225                 gnus-newsgroup-data-reverse nil)
2226           (when offset
2227             (gnus-data-update-list gnus-newsgroup-data offset)))
2228       (while (cdr data)
2229         (when (= (gnus-data-number (cadr data)) article)
2230           (setcdr data (cddr data))
2231           (when offset
2232             (gnus-data-update-list (cdr data) offset))
2233           (setq data nil
2234                 gnus-newsgroup-data-reverse nil))
2235         (setq data (cdr data))))))
2236
2237 (defmacro gnus-data-list (backward)
2238   `(if ,backward
2239        (or gnus-newsgroup-data-reverse
2240            (setq gnus-newsgroup-data-reverse
2241                  (reverse gnus-newsgroup-data)))
2242      gnus-newsgroup-data))
2243
2244 (defun gnus-data-update-list (data offset)
2245   "Add OFFSET to the POS of all data entries in DATA."
2246   (setq gnus-newsgroup-data-reverse nil)
2247   (while data
2248     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2249     (setq data (cdr data))))
2250
2251 (defun gnus-summary-article-pseudo-p (article)
2252   "Say whether this article is a pseudo article or not."
2253   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2254
2255 (defmacro gnus-summary-article-sparse-p (article)
2256   "Say whether this article is a sparse article or not."
2257   `(memq ,article gnus-newsgroup-sparse))
2258
2259 (defmacro gnus-summary-article-ancient-p (article)
2260   "Say whether this article is a sparse article or not."
2261   `(memq ,article gnus-newsgroup-ancient))
2262
2263 (defun gnus-article-parent-p (number)
2264   "Say whether this article is a parent or not."
2265   (let ((data (gnus-data-find-list number)))
2266     (and (cdr data)                     ; There has to be an article after...
2267          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2268             (gnus-data-level (nth 1 data))))))
2269
2270 (defun gnus-article-children (number)
2271   "Return a list of all children to NUMBER."
2272   (let* ((data (gnus-data-find-list number))
2273          (level (gnus-data-level (car data)))
2274          children)
2275     (setq data (cdr data))
2276     (while (and data
2277                 (= (gnus-data-level (car data)) (1+ level)))
2278       (push (gnus-data-number (car data)) children)
2279       (setq data (cdr data)))
2280     children))
2281
2282 (defmacro gnus-summary-skip-intangible ()
2283   "If the current article is intangible, then jump to a different article."
2284   '(let ((to (get-text-property (point) 'gnus-intangible)))
2285      (and to (gnus-summary-goto-subject to))))
2286
2287 (defmacro gnus-summary-article-intangible-p ()
2288   "Say whether this article is intangible or not."
2289   '(get-text-property (point) 'gnus-intangible))
2290
2291 (defun gnus-article-read-p (article)
2292   "Say whether ARTICLE is read or not."
2293   (not (or (memq article gnus-newsgroup-marked)
2294            (memq article gnus-newsgroup-unreads)
2295            (memq article gnus-newsgroup-unselected)
2296            (memq article gnus-newsgroup-dormant))))
2297
2298 ;; Some summary mode macros.
2299
2300 (defmacro gnus-summary-article-number ()
2301   "The article number of the article on the current line.
2302 If there isn's an article number here, then we return the current
2303 article number."
2304   '(progn
2305      (gnus-summary-skip-intangible)
2306      (or (get-text-property (point) 'gnus-number)
2307          (gnus-summary-last-subject))))
2308
2309 (defmacro gnus-summary-article-header (&optional number)
2310   "Return the header of article NUMBER."
2311   `(gnus-data-header (gnus-data-find
2312                       ,(or number '(gnus-summary-article-number)))))
2313
2314 (defmacro gnus-summary-thread-level (&optional number)
2315   "Return the level of thread that starts with article NUMBER."
2316   `(if (and (eq gnus-summary-make-false-root 'dummy)
2317             (get-text-property (point) 'gnus-intangible))
2318        0
2319      (gnus-data-level (gnus-data-find
2320                        ,(or number '(gnus-summary-article-number))))))
2321
2322 (defmacro gnus-summary-article-mark (&optional number)
2323   "Return the mark of article NUMBER."
2324   `(gnus-data-mark (gnus-data-find
2325                     ,(or number '(gnus-summary-article-number)))))
2326
2327 (defmacro gnus-summary-article-pos (&optional number)
2328   "Return the position of the line of article NUMBER."
2329   `(gnus-data-pos (gnus-data-find
2330                    ,(or number '(gnus-summary-article-number)))))
2331
2332 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2333 (defmacro gnus-summary-article-subject (&optional number)
2334   "Return current subject string or nil if nothing."
2335   `(let ((headers
2336           ,(if number
2337                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2338              '(gnus-data-header (assq (gnus-summary-article-number)
2339                                       gnus-newsgroup-data)))))
2340      (and headers
2341           (vectorp headers)
2342           (mail-header-subject headers))))
2343
2344 (defmacro gnus-summary-article-score (&optional number)
2345   "Return current article score."
2346   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2347                   gnus-newsgroup-scored))
2348        gnus-summary-default-score 0))
2349
2350 (defun gnus-summary-article-children (&optional number)
2351   "Return a list of article numbers that are children of article NUMBER."
2352   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2353          (level (gnus-data-level (car data)))
2354          l children)
2355     (while (and (setq data (cdr data))
2356                 (> (setq l (gnus-data-level (car data))) level))
2357       (and (= (1+ level) l)
2358            (push (gnus-data-number (car data))
2359                  children)))
2360     (nreverse children)))
2361
2362 (defun gnus-summary-article-parent (&optional number)
2363   "Return the article number of the parent of article NUMBER."
2364   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2365                                     (gnus-data-list t)))
2366          (level (gnus-data-level (car data))))
2367     (if (zerop level)
2368         ()                              ; This is a root.
2369       ;; We search until we find an article with a level less than
2370       ;; this one.  That function has to be the parent.
2371       (while (and (setq data (cdr data))
2372                   (not (< (gnus-data-level (car data)) level))))
2373       (and data (gnus-data-number (car data))))))
2374
2375 (defun gnus-unread-mark-p (mark)
2376   "Say whether MARK is the unread mark."
2377   (= mark gnus-unread-mark))
2378
2379 (defun gnus-read-mark-p (mark)
2380   "Say whether MARK is one of the marks that mark as read.
2381 This is all marks except unread, ticked, dormant, and expirable."
2382   (not (or (= mark gnus-unread-mark)
2383            (= mark gnus-ticked-mark)
2384            (= mark gnus-dormant-mark)
2385            (= mark gnus-expirable-mark))))
2386
2387 (defmacro gnus-article-mark (number)
2388   "Return the MARK of article NUMBER.
2389 This macro should only be used when computing the mark the \"first\"
2390 time; i.e., when generating the summary lines.  After that,
2391 `gnus-summary-article-mark' should be used to examine the
2392 marks of articles."
2393   `(cond
2394     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2395     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2396     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2397     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2398     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2399     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2400     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2401     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2402            gnus-ancient-mark))))
2403
2404 ;; Saving hidden threads.
2405
2406 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2407 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2408
2409 (defmacro gnus-save-hidden-threads (&rest forms)
2410   "Save hidden threads, eval FORMS, and restore the hidden threads."
2411   (let ((config (make-symbol "config")))
2412     `(let ((,config (gnus-hidden-threads-configuration)))
2413        (unwind-protect
2414            (save-excursion
2415              ,@forms)
2416          (gnus-restore-hidden-threads-configuration ,config)))))
2417
2418 (defun gnus-data-compute-positions ()
2419   "Compute the positions of all articles."
2420   (setq gnus-newsgroup-data-reverse nil)
2421   (let ((data gnus-newsgroup-data))
2422     (save-excursion
2423       (gnus-save-hidden-threads
2424         (gnus-summary-show-all-threads)
2425         (goto-char (point-min))
2426         (while data
2427           (while (get-text-property (point) 'gnus-intangible)
2428             (forward-line 1))
2429           (gnus-data-set-pos (car data) (+ (point) 3))
2430           (setq data (cdr data))
2431           (forward-line 1))))))
2432
2433 (defun gnus-hidden-threads-configuration ()
2434   "Return the current hidden threads configuration."
2435   (save-excursion
2436     (let (config)
2437       (goto-char (point-min))
2438       (while (search-forward "\r" nil t)
2439         (push (1- (point)) config))
2440       config)))
2441
2442 (defun gnus-restore-hidden-threads-configuration (config)
2443   "Restore hidden threads configuration from CONFIG."
2444   (save-excursion
2445     (let (point buffer-read-only)
2446       (while (setq point (pop config))
2447         (when (and (< point (point-max))
2448                    (goto-char point)
2449                    (eq (char-after) ?\n))
2450           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2451
2452 ;; Various summary mode internalish functions.
2453
2454 (defun gnus-mouse-pick-article (e)
2455   (interactive "e")
2456   (mouse-set-point e)
2457   (gnus-summary-next-page nil t))
2458
2459 (defun gnus-summary-set-display-table ()
2460   "Change the display table.
2461 Odd characters have a tendency to mess
2462 up nicely formatted displays - we make all possible glyphs
2463 display only a single character."
2464
2465   ;; We start from the standard display table, if any.
2466   (let ((table (or (copy-sequence standard-display-table)
2467                    (make-display-table)))
2468         (i 32))
2469     ;; Nix out all the control chars...
2470     (while (>= (setq i (1- i)) 0)
2471       (aset table i [??]))
2472     ;; ... but not newline and cr, of course.  (cr is necessary for the
2473     ;; selective display).
2474     (aset table ?\n nil)
2475     (aset table ?\r nil)
2476     ;; We keep TAB as well.
2477     (aset table ?\t nil)
2478     ;; We nix out any glyphs over 126 that are not set already.
2479     (let ((i 256))
2480       (while (>= (setq i (1- i)) 127)
2481         ;; Only modify if the entry is nil.
2482         (unless (aref table i)
2483           (aset table i [??]))))
2484     (setq buffer-display-table table)))
2485
2486 (defun gnus-summary-setup-buffer (group)
2487   "Initialize summary buffer."
2488   (let ((buffer (concat "*Summary " group "*")))
2489     (if (get-buffer buffer)
2490         (progn
2491           (set-buffer buffer)
2492           (setq gnus-summary-buffer (current-buffer))
2493           (not gnus-newsgroup-prepared))
2494       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2495       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2496       (gnus-summary-mode group)
2497       (when gnus-carpal
2498         (gnus-carpal-setup-buffer 'summary))
2499       (unless gnus-single-article-buffer
2500         (make-local-variable 'gnus-article-buffer)
2501         (make-local-variable 'gnus-article-current)
2502         (make-local-variable 'gnus-original-article-buffer))
2503       (setq gnus-newsgroup-name group)
2504       t)))
2505
2506 (defun gnus-set-global-variables ()
2507   "Set the global equivalents of the buffer-local variables.
2508 They are set to the latest values they had.  These reflect the summary
2509 buffer that was in action when the last article was fetched."
2510   (when (eq major-mode 'gnus-summary-mode)
2511     (setq gnus-summary-buffer (current-buffer))
2512     (let ((name gnus-newsgroup-name)
2513           (marked gnus-newsgroup-marked)
2514           (unread gnus-newsgroup-unreads)
2515           (headers gnus-current-headers)
2516           (data gnus-newsgroup-data)
2517           (summary gnus-summary-buffer)
2518           (article-buffer gnus-article-buffer)
2519           (original gnus-original-article-buffer)
2520           (gac gnus-article-current)
2521           (reffed gnus-reffed-article-number)
2522           (score-file gnus-current-score-file)
2523           (default-charset gnus-newsgroup-charset))
2524       (save-excursion
2525         (set-buffer gnus-group-buffer)
2526         (setq gnus-newsgroup-name name
2527               gnus-newsgroup-marked marked
2528               gnus-newsgroup-unreads unread
2529               gnus-current-headers headers
2530               gnus-newsgroup-data data
2531               gnus-article-current gac
2532               gnus-summary-buffer summary
2533               gnus-article-buffer article-buffer
2534               gnus-original-article-buffer original
2535               gnus-reffed-article-number reffed
2536               gnus-current-score-file score-file
2537               gnus-newsgroup-charset default-charset)
2538         ;; The article buffer also has local variables.
2539         (when (gnus-buffer-live-p gnus-article-buffer)
2540           (set-buffer gnus-article-buffer)
2541           (setq gnus-summary-buffer summary))))))
2542
2543 (defun gnus-summary-article-unread-p (article)
2544   "Say whether ARTICLE is unread or not."
2545   (memq article gnus-newsgroup-unreads))
2546
2547 (defun gnus-summary-first-article-p (&optional article)
2548   "Return whether ARTICLE is the first article in the buffer."
2549   (if (not (setq article (or article (gnus-summary-article-number))))
2550       nil
2551     (eq article (caar gnus-newsgroup-data))))
2552
2553 (defun gnus-summary-last-article-p (&optional article)
2554   "Return whether ARTICLE is the last article in the buffer."
2555   (if (not (setq article (or article (gnus-summary-article-number))))
2556       ;; All non-existent numbers are the last article.  :-)
2557       t
2558     (not (cdr (gnus-data-find-list article)))))
2559
2560 (defun gnus-make-thread-indent-array ()
2561   (let ((n 200))
2562     (unless (and gnus-thread-indent-array
2563                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2564       (setq gnus-thread-indent-array (make-vector 201 "")
2565             gnus-thread-indent-array-level gnus-thread-indent-level)
2566       (while (>= n 0)
2567         (aset gnus-thread-indent-array n
2568               (make-string (* n gnus-thread-indent-level) ? ))
2569         (setq n (1- n))))))
2570
2571 (defun gnus-update-summary-mark-positions ()
2572   "Compute where the summary marks are to go."
2573   (save-excursion
2574     (when (gnus-buffer-exists-p gnus-summary-buffer)
2575       (set-buffer gnus-summary-buffer))
2576     (let ((gnus-replied-mark 129)
2577           (gnus-score-below-mark 130)
2578           (gnus-score-over-mark 130)
2579           (gnus-download-mark 131)
2580           (spec gnus-summary-line-format-spec)
2581           gnus-visual pos)
2582       (save-excursion
2583         (gnus-set-work-buffer)
2584         (let ((gnus-summary-line-format-spec spec)
2585               (gnus-newsgroup-downloadable '((0 . t))))
2586           (gnus-summary-insert-line
2587            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2588           (goto-char (point-min))
2589           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2590                                              (- (point) 2)))))
2591           (goto-char (point-min))
2592           (push (cons 'replied (and (search-forward "\201" nil t)
2593                                     (- (point) 2)))
2594                 pos)
2595           (goto-char (point-min))
2596           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2597                 pos)
2598           (goto-char (point-min))
2599           (push (cons 'download
2600                       (and (search-forward "\203" nil t) (- (point) 2)))
2601                 pos)))
2602       (setq gnus-summary-mark-positions pos))))
2603
2604 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2605   "Insert a dummy root in the summary buffer."
2606   (beginning-of-line)
2607   (gnus-add-text-properties
2608    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2609    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2610
2611 (defun gnus-summary-from-or-to-or-newsgroups (header)
2612   (let ((to (cdr (assq 'To (mail-header-extra header))))
2613         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2614         (mail-parse-charset gnus-newsgroup-charset)
2615         (mail-parse-ignored-charsets
2616          (save-excursion (set-buffer gnus-summary-buffer)
2617                          gnus-newsgroup-ignored-charsets)))
2618     (cond
2619      ((and to
2620            gnus-ignored-from-addresses
2621            (string-match gnus-ignored-from-addresses
2622                          (mail-header-from header)))
2623       (concat "-> "
2624               (or (car (funcall gnus-extract-address-components
2625                                 (funcall
2626                                  gnus-decode-encoded-word-function to)))
2627                   (funcall gnus-decode-encoded-word-function to))))
2628      ((and newsgroups
2629            gnus-ignored-from-addresses
2630            (string-match gnus-ignored-from-addresses
2631                          (mail-header-from header)))
2632       (concat "=> " newsgroups))
2633      (t
2634       (or (car (funcall gnus-extract-address-components
2635                         (mail-header-from header)))
2636           (mail-header-from header))))))
2637
2638 (defun gnus-summary-insert-line (gnus-tmp-header
2639                                  gnus-tmp-level gnus-tmp-current
2640                                  gnus-tmp-unread gnus-tmp-replied
2641                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2642                                  &optional gnus-tmp-dummy gnus-tmp-score
2643                                  gnus-tmp-process)
2644   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2645          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2646          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2647          (gnus-tmp-score-char
2648           (if (or (null gnus-summary-default-score)
2649                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2650                       gnus-summary-zcore-fuzz))
2651               ?  ;Whitespace
2652             (if (< gnus-tmp-score gnus-summary-default-score)
2653                 gnus-score-below-mark gnus-score-over-mark)))
2654          (gnus-tmp-replied
2655           (cond (gnus-tmp-process gnus-process-mark)
2656                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2657                  gnus-cached-mark)
2658                 (gnus-tmp-replied gnus-replied-mark)
2659                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2660                  gnus-saved-mark)
2661                 (t gnus-unread-mark)))
2662          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2663          (gnus-tmp-name
2664           (cond
2665            ((string-match "<[^>]+> *$" gnus-tmp-from)
2666             (let ((beg (match-beginning 0)))
2667               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2668                        (substring gnus-tmp-from (1+ (match-beginning 0))
2669                                   (1- (match-end 0))))
2670                   (substring gnus-tmp-from 0 beg))))
2671            ((string-match "(.+)" gnus-tmp-from)
2672             (substring gnus-tmp-from
2673                        (1+ (match-beginning 0)) (1- (match-end 0))))
2674            (t gnus-tmp-from)))
2675          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2676          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2677          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2678          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2679          (buffer-read-only nil))
2680     (when (string= gnus-tmp-name "")
2681       (setq gnus-tmp-name gnus-tmp-from))
2682     (unless (numberp gnus-tmp-lines)
2683       (setq gnus-tmp-lines 0))
2684     (gnus-put-text-property
2685      (point)
2686      (progn (eval gnus-summary-line-format-spec) (point))
2687      'gnus-number gnus-tmp-number)
2688     (when (gnus-visual-p 'summary-highlight 'highlight)
2689       (forward-line -1)
2690       (gnus-run-hooks 'gnus-summary-update-hook)
2691       (forward-line 1))))
2692
2693 (defun gnus-summary-update-line (&optional dont-update)
2694   "Update summary line after change."
2695   (when (and gnus-summary-default-score
2696              (not gnus-summary-inhibit-highlight))
2697     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2698            (article (gnus-summary-article-number))
2699            (score (gnus-summary-article-score article)))
2700       (unless dont-update
2701         (if (and gnus-summary-mark-below
2702                  (< (gnus-summary-article-score)
2703                     gnus-summary-mark-below))
2704             ;; This article has a low score, so we mark it as read.
2705             (when (memq article gnus-newsgroup-unreads)
2706               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2707           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2708             ;; This article was previously marked as read on account
2709             ;; of a low score, but now it has risen, so we mark it as
2710             ;; unread.
2711             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2712         (gnus-summary-update-mark
2713          (if (or (null gnus-summary-default-score)
2714                  (<= (abs (- score gnus-summary-default-score))
2715                      gnus-summary-zcore-fuzz))
2716              ?  ;Whitespace
2717            (if (< score gnus-summary-default-score)
2718                gnus-score-below-mark gnus-score-over-mark))
2719          'score))
2720       ;; Do visual highlighting.
2721       (when (gnus-visual-p 'summary-highlight 'highlight)
2722         (gnus-run-hooks 'gnus-summary-update-hook)))))
2723
2724 (defvar gnus-tmp-new-adopts nil)
2725
2726 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2727   "Return the number of articles in THREAD.
2728 This may be 0 in some cases -- if none of the articles in
2729 the thread are to be displayed."
2730   (let* ((number
2731           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2732           (cond
2733            ((not (listp thread))
2734             1)
2735            ((and (consp thread) (cdr thread))
2736             (apply
2737              '+ 1 (mapcar
2738                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2739            ((null thread)
2740             1)
2741            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2742             1)
2743            (t 0))))
2744     (when (and level (zerop level) gnus-tmp-new-adopts)
2745       (incf number
2746             (apply '+ (mapcar
2747                        'gnus-summary-number-of-articles-in-thread
2748                        gnus-tmp-new-adopts))))
2749     (if char
2750         (if (> number 1) gnus-not-empty-thread-mark
2751           gnus-empty-thread-mark)
2752       number)))
2753
2754 (defun gnus-summary-set-local-parameters (group)
2755   "Go through the local params of GROUP and set all variable specs in that list."
2756   (let ((params (gnus-group-find-parameter group))
2757         elem)
2758     (while params
2759       (setq elem (car params)
2760             params (cdr params))
2761       (and (consp elem)                 ; Has to be a cons.
2762            (consp (cdr elem))           ; The cdr has to be a list.
2763            (symbolp (car elem))         ; Has to be a symbol in there.
2764            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2765            (ignore-errors               ; So we set it.
2766              (make-local-variable (car elem))
2767              (set (car elem) (eval (nth 1 elem))))))))
2768
2769 (defun gnus-summary-read-group (group &optional show-all no-article
2770                                       kill-buffer no-display backward
2771                                       select-articles)
2772   "Start reading news in newsgroup GROUP.
2773 If SHOW-ALL is non-nil, already read articles are also listed.
2774 If NO-ARTICLE is non-nil, no article is selected initially.
2775 If NO-DISPLAY, don't generate a summary buffer."
2776   (let (result)
2777     (while (and group
2778                 (null (setq result
2779                             (let ((gnus-auto-select-next nil))
2780                               (or (gnus-summary-read-group-1
2781                                    group show-all no-article
2782                                    kill-buffer no-display
2783                                    select-articles)
2784                                   (setq show-all nil
2785                                         select-articles nil)))))
2786                 (eq gnus-auto-select-next 'quietly))
2787       (set-buffer gnus-group-buffer)
2788       ;; The entry function called above goes to the next
2789       ;; group automatically, so we go two groups back
2790       ;; if we are searching for the previous group.
2791       (when backward
2792         (gnus-group-prev-unread-group 2))
2793       (if (not (equal group (gnus-group-group-name)))
2794           (setq group (gnus-group-group-name))
2795         (setq group nil)))
2796     result))
2797
2798 (defun gnus-summary-read-group-1 (group show-all no-article
2799                                         kill-buffer no-display
2800                                         &optional select-articles)
2801   ;; Killed foreign groups can't be entered.
2802   (when (and (not (gnus-group-native-p group))
2803              (not (gnus-gethash group gnus-newsrc-hashtb)))
2804     (error "Dead non-native groups can't be entered"))
2805   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2806   (let* ((new-group (gnus-summary-setup-buffer group))
2807          (quit-config (gnus-group-quit-config group))
2808          (did-select (and new-group (gnus-select-newsgroup
2809                                      group show-all select-articles))))
2810     (cond
2811      ;; This summary buffer exists already, so we just select it.
2812      ((not new-group)
2813       (gnus-set-global-variables)
2814       (when kill-buffer
2815         (gnus-kill-or-deaden-summary kill-buffer))
2816       (gnus-configure-windows 'summary 'force)
2817       (gnus-set-mode-line 'summary)
2818       (gnus-summary-position-point)
2819       (message "")
2820       t)
2821      ;; We couldn't select this group.
2822      ((null did-select)
2823       (when (and (eq major-mode 'gnus-summary-mode)
2824                  (not (equal (current-buffer) kill-buffer)))
2825         (kill-buffer (current-buffer))
2826         (if (not quit-config)
2827             (progn
2828               ;; Update the info -- marks might need to be removed,
2829               ;; for instance.
2830               (gnus-summary-update-info)
2831               (set-buffer gnus-group-buffer)
2832               (gnus-group-jump-to-group group)
2833               (gnus-group-next-unread-group 1))
2834           (gnus-handle-ephemeral-exit quit-config)))
2835       (gnus-message 3 "Can't select group")
2836       nil)
2837      ;; The user did a `C-g' while prompting for number of articles,
2838      ;; so we exit this group.
2839      ((eq did-select 'quit)
2840       (and (eq major-mode 'gnus-summary-mode)
2841            (not (equal (current-buffer) kill-buffer))
2842            (kill-buffer (current-buffer)))
2843       (when kill-buffer
2844         (gnus-kill-or-deaden-summary kill-buffer))
2845       (if (not quit-config)
2846           (progn
2847             (set-buffer gnus-group-buffer)
2848             (gnus-group-jump-to-group group)
2849             (gnus-group-next-unread-group 1)
2850             (gnus-configure-windows 'group 'force))
2851         (gnus-handle-ephemeral-exit quit-config))
2852       ;; Finally signal the quit.
2853       (signal 'quit nil))
2854      ;; The group was successfully selected.
2855      (t
2856       (gnus-set-global-variables)
2857       ;; Save the active value in effect when the group was entered.
2858       (setq gnus-newsgroup-active
2859             (gnus-copy-sequence
2860              (gnus-active gnus-newsgroup-name)))
2861       ;; You can change the summary buffer in some way with this hook.
2862       (gnus-run-hooks 'gnus-select-group-hook)
2863       ;; Set any local variables in the group parameters.
2864       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2865       (gnus-update-format-specifications
2866        nil 'summary 'summary-mode 'summary-dummy)
2867       (gnus-update-summary-mark-positions)
2868       ;; Do score processing.
2869       (when gnus-use-scoring
2870         (gnus-possibly-score-headers))
2871       ;; Check whether to fill in the gaps in the threads.
2872       (when gnus-build-sparse-threads
2873         (gnus-build-sparse-threads))
2874       ;; Find the initial limit.
2875       (if gnus-show-threads
2876           (if show-all
2877               (let ((gnus-newsgroup-dormant nil))
2878                 (gnus-summary-initial-limit show-all))
2879             (gnus-summary-initial-limit show-all))
2880         ;; When untreaded, all articles are always shown.
2881         (setq gnus-newsgroup-limit
2882               (mapcar
2883                (lambda (header) (mail-header-number header))
2884                gnus-newsgroup-headers)))
2885       ;; Generate the summary buffer.
2886       (unless no-display
2887         (gnus-summary-prepare))
2888       (when gnus-use-trees
2889         (gnus-tree-open group)
2890         (setq gnus-summary-highlight-line-function
2891               'gnus-tree-highlight-article))
2892       ;; If the summary buffer is empty, but there are some low-scored
2893       ;; articles or some excluded dormants, we include these in the
2894       ;; buffer.
2895       (when (and (zerop (buffer-size))
2896                  (not no-display))
2897         (cond (gnus-newsgroup-dormant
2898                (gnus-summary-limit-include-dormant))
2899               ((and gnus-newsgroup-scored show-all)
2900                (gnus-summary-limit-include-expunged t))))
2901       ;; Function `gnus-apply-kill-file' must be called in this hook.
2902       (gnus-run-hooks 'gnus-apply-kill-hook)
2903       (if (and (zerop (buffer-size))
2904                (not no-display))
2905           (progn
2906             ;; This newsgroup is empty.
2907             (gnus-summary-catchup-and-exit nil t)
2908             (gnus-message 6 "No unread news")
2909             (when kill-buffer
2910               (gnus-kill-or-deaden-summary kill-buffer))
2911             ;; Return nil from this function.
2912             nil)
2913         ;; Hide conversation thread subtrees.  We cannot do this in
2914         ;; gnus-summary-prepare-hook since kill processing may not
2915         ;; work with hidden articles.
2916         (and gnus-show-threads
2917              gnus-thread-hide-subtree
2918              (gnus-summary-hide-all-threads))
2919         (when kill-buffer
2920           (gnus-kill-or-deaden-summary kill-buffer))
2921         ;; Show first unread article if requested.
2922         (if (and (not no-article)
2923                  (not no-display)
2924                  gnus-newsgroup-unreads
2925                  gnus-auto-select-first)
2926             (progn
2927               (gnus-configure-windows 'summary)
2928               (cond
2929                ((eq gnus-auto-select-first 'best)
2930                 (gnus-summary-best-unread-article))
2931                ((eq gnus-auto-select-first t)
2932                 (gnus-summary-first-unread-article))
2933                ((gnus-functionp gnus-auto-select-first)
2934                 (funcall gnus-auto-select-first))))
2935           ;; Don't select any articles, just move point to the first
2936           ;; article in the group.
2937           (goto-char (point-min))
2938           (gnus-summary-position-point)
2939           (gnus-configure-windows 'summary 'force)
2940           (gnus-set-mode-line 'summary))
2941         (when (get-buffer-window gnus-group-buffer t)
2942           ;; Gotta use windows, because recenter does weird stuff if
2943           ;; the current buffer ain't the displayed window.
2944           (let ((owin (selected-window)))
2945             (select-window (get-buffer-window gnus-group-buffer t))
2946             (when (gnus-group-goto-group group)
2947               (recenter))
2948             (select-window owin)))
2949         ;; Mark this buffer as "prepared".
2950         (setq gnus-newsgroup-prepared t)
2951         (gnus-run-hooks 'gnus-summary-prepared-hook)
2952         t)))))
2953
2954 (defun gnus-summary-prepare ()
2955   "Generate the summary buffer."
2956   (interactive)
2957   (let ((buffer-read-only nil))
2958     (erase-buffer)
2959     (setq gnus-newsgroup-data nil
2960           gnus-newsgroup-data-reverse nil)
2961     (gnus-run-hooks 'gnus-summary-generate-hook)
2962     ;; Generate the buffer, either with threads or without.
2963     (when gnus-newsgroup-headers
2964       (gnus-summary-prepare-threads
2965        (if gnus-show-threads
2966            (gnus-sort-gathered-threads
2967             (funcall gnus-summary-thread-gathering-function
2968                      (gnus-sort-threads
2969                       (gnus-cut-threads (gnus-make-threads)))))
2970          ;; Unthreaded display.
2971          (gnus-sort-articles gnus-newsgroup-headers))))
2972     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2973     ;; Call hooks for modifying summary buffer.
2974     (goto-char (point-min))
2975     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2976
2977 (defsubst gnus-general-simplify-subject (subject)
2978   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2979   (setq subject
2980         (cond
2981          ;; Truncate the subject.
2982          (gnus-simplify-subject-functions
2983           (gnus-map-function gnus-simplify-subject-functions subject))
2984          ((numberp gnus-summary-gather-subject-limit)
2985           (setq subject (gnus-simplify-subject-re subject))
2986           (if (> (length subject) gnus-summary-gather-subject-limit)
2987               (substring subject 0 gnus-summary-gather-subject-limit)
2988             subject))
2989          ;; Fuzzily simplify it.
2990          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2991           (gnus-simplify-subject-fuzzy subject))
2992          ;; Just remove the leading "Re:".
2993          (t
2994           (gnus-simplify-subject-re subject))))
2995
2996   (if (and gnus-summary-gather-exclude-subject
2997            (string-match gnus-summary-gather-exclude-subject subject))
2998       nil                               ; This article shouldn't be gathered
2999     subject))
3000
3001 (defun gnus-summary-simplify-subject-query ()
3002   "Query where the respool algorithm would put this article."
3003   (interactive)
3004   (gnus-summary-select-article)
3005   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3006
3007 (defun gnus-gather-threads-by-subject (threads)
3008   "Gather threads by looking at Subject headers."
3009   (if (not gnus-summary-make-false-root)
3010       threads
3011     (let ((hashtb (gnus-make-hashtable 1024))
3012           (prev threads)
3013           (result threads)
3014           subject hthread whole-subject)
3015       (while threads
3016         (setq subject (gnus-general-simplify-subject
3017                        (setq whole-subject (mail-header-subject
3018                                             (caar threads)))))
3019         (when subject
3020           (if (setq hthread (gnus-gethash subject hashtb))
3021               (progn
3022                 ;; We enter a dummy root into the thread, if we
3023                 ;; haven't done that already.
3024                 (unless (stringp (caar hthread))
3025                   (setcar hthread (list whole-subject (car hthread))))
3026                 ;; We add this new gathered thread to this gathered
3027                 ;; thread.
3028                 (setcdr (car hthread)
3029                         (nconc (cdar hthread) (list (car threads))))
3030                 ;; Remove it from the list of threads.
3031                 (setcdr prev (cdr threads))
3032                 (setq threads prev))
3033             ;; Enter this thread into the hash table.
3034             (gnus-sethash subject threads hashtb)))
3035         (setq prev threads)
3036         (setq threads (cdr threads)))
3037       result)))
3038
3039 (defun gnus-gather-threads-by-references (threads)
3040   "Gather threads by looking at References headers."
3041   (let ((idhashtb (gnus-make-hashtable 1024))
3042         (thhashtb (gnus-make-hashtable 1024))
3043         (prev threads)
3044         (result threads)
3045         ids references id gthread gid entered ref)
3046     (while threads
3047       (when (setq references (mail-header-references (caar threads)))
3048         (setq id (mail-header-id (caar threads))
3049               ids (gnus-split-references references)
3050               entered nil)
3051         (while (setq ref (pop ids))
3052           (setq ids (delete ref ids))
3053           (if (not (setq gid (gnus-gethash ref idhashtb)))
3054               (progn
3055                 (gnus-sethash ref id idhashtb)
3056                 (gnus-sethash id threads thhashtb))
3057             (setq gthread (gnus-gethash gid thhashtb))
3058             (unless entered
3059               ;; We enter a dummy root into the thread, if we
3060               ;; haven't done that already.
3061               (unless (stringp (caar gthread))
3062                 (setcar gthread (list (mail-header-subject (caar gthread))
3063                                       (car gthread))))
3064               ;; We add this new gathered thread to this gathered
3065               ;; thread.
3066               (setcdr (car gthread)
3067                       (nconc (cdar gthread) (list (car threads)))))
3068             ;; Add it into the thread hash table.
3069             (gnus-sethash id gthread thhashtb)
3070             (setq entered t)
3071             ;; Remove it from the list of threads.
3072             (setcdr prev (cdr threads))
3073             (setq threads prev))))
3074       (setq prev threads)
3075       (setq threads (cdr threads)))
3076     result))
3077
3078 (defun gnus-sort-gathered-threads (threads)
3079   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3080   (let ((result threads))
3081     (while threads
3082       (when (stringp (caar threads))
3083         (setcdr (car threads)
3084                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3085       (setq threads (cdr threads)))
3086     result))
3087
3088 (defun gnus-thread-loop-p (root thread)
3089   "Say whether ROOT is in THREAD."
3090   (let ((stack (list thread))
3091         (infloop 0)
3092         th)
3093     (while (setq thread (pop stack))
3094       (setq th (cdr thread))
3095       (while (and th
3096                   (not (eq (caar th) root)))
3097         (pop th))
3098       (if th
3099           ;; We have found a loop.
3100           (let (ref-dep)
3101             (setcdr thread (delq (car th) (cdr thread)))
3102             (if (boundp (setq ref-dep (intern "none"
3103                                               gnus-newsgroup-dependencies)))
3104                 (setcdr (symbol-value ref-dep)
3105                         (nconc (cdr (symbol-value ref-dep))
3106                                (list (car th))))
3107               (set ref-dep (list nil (car th))))
3108             (setq infloop 1
3109                   stack nil))
3110         ;; Push all the subthreads onto the stack.
3111         (push (cdr thread) stack)))
3112     infloop))
3113
3114 (defun gnus-make-threads ()
3115   "Go through the dependency hashtb and find the roots.  Return all threads."
3116   (let (threads)
3117     (while (catch 'infloop
3118              (mapatoms
3119               (lambda (refs)
3120                 ;; Deal with self-referencing References loops.
3121                 (when (and (car (symbol-value refs))
3122                            (not (zerop
3123                                  (apply
3124                                   '+
3125                                   (mapcar
3126                                    (lambda (thread)
3127                                      (gnus-thread-loop-p
3128                                       (car (symbol-value refs)) thread))
3129                                    (cdr (symbol-value refs)))))))
3130                   (setq threads nil)
3131                   (throw 'infloop t))
3132                 (unless (car (symbol-value refs))
3133                   ;; These threads do not refer back to any other articles,
3134                   ;; so they're roots.
3135                   (setq threads (append (cdr (symbol-value refs)) threads))))
3136               gnus-newsgroup-dependencies)))
3137     threads))
3138
3139 ;; Build the thread tree.
3140 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3141   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3142
3143 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3144 if it was already present.
3145
3146 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3147 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3148 Message-IDs will be renamed be renamed to a unique Message-ID before
3149 being entered.
3150
3151 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3152   (let* ((id (mail-header-id header))
3153          (id-dep (and id (intern id dependencies)))
3154          ref ref-dep ref-header)
3155     ;; Enter this `header' in the `dependencies' table.
3156     (cond
3157      ((not id-dep)
3158       (setq header nil))
3159      ;; The first two cases do the normal part: enter a new `header'
3160      ;; in the `dependencies' table.
3161      ((not (boundp id-dep))
3162       (set id-dep (list header)))
3163      ((null (car (symbol-value id-dep)))
3164       (setcar (symbol-value id-dep) header))
3165
3166      ;; From here the `header' was already present in the
3167      ;; `dependencies' table.
3168      (force-new
3169       ;; Overrides an existing entry;
3170       ;; just set the header part of the entry.
3171       (setcar (symbol-value id-dep) header))
3172
3173      ;; Renames the existing `header' to a unique Message-ID.
3174      ((not gnus-summary-ignore-duplicates)
3175       ;; An article with this Message-ID has already been seen.
3176       ;; We rename the Message-ID.
3177       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3178            (list header))
3179       (mail-header-set-id header id))
3180
3181      ;; The last case ignores an existing entry, except it adds any
3182      ;; additional Xrefs (in case the two articles came from different
3183      ;; servers.
3184      ;; Also sets `header' to `nil' meaning that the `dependencies'
3185      ;; table was *not* modified.
3186      (t
3187       (mail-header-set-xref
3188        (car (symbol-value id-dep))
3189        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3190                    "")
3191                (or (mail-header-xref header) "")))
3192       (setq header nil)))
3193
3194     (when header
3195       ;; First check if that we are not creating a References loop.
3196       (setq ref (gnus-parent-id (mail-header-references header)))
3197       (while (and ref
3198                   (setq ref-dep (intern-soft ref dependencies))
3199                   (boundp ref-dep)
3200                   (setq ref-header (car (symbol-value ref-dep))))
3201         (if (string= id ref)
3202             ;; Yuk!  This is a reference loop.  Make the article be a
3203             ;; root article.
3204             (progn
3205               (mail-header-set-references (car (symbol-value id-dep)) "none")
3206               (setq ref nil))
3207           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3208       (setq ref (gnus-parent-id (mail-header-references header)))
3209       (setq ref-dep (intern (or ref "none") dependencies))
3210       (if (boundp ref-dep)
3211           (setcdr (symbol-value ref-dep)
3212                   (nconc (cdr (symbol-value ref-dep))
3213                          (list (symbol-value id-dep))))
3214         (set ref-dep (list nil (symbol-value id-dep)))))
3215     header))
3216
3217 (defun gnus-build-sparse-threads ()
3218   (let ((headers gnus-newsgroup-headers)
3219         (mail-parse-charset gnus-newsgroup-charset)
3220         (gnus-summary-ignore-duplicates t)
3221         header references generation relations
3222         subject child end new-child date)
3223     ;; First we create an alist of generations/relations, where
3224     ;; generations is how much we trust the relation, and the relation
3225     ;; is parent/child.
3226     (gnus-message 7 "Making sparse threads...")
3227     (save-excursion
3228       (nnheader-set-temp-buffer " *gnus sparse threads*")
3229       (while (setq header (pop headers))
3230         (when (and (setq references (mail-header-references header))
3231                    (not (string= references "")))
3232           (insert references)
3233           (setq child (mail-header-id header)
3234                 subject (mail-header-subject header)
3235                 date (mail-header-date header)
3236                 generation 0)
3237           (while (search-backward ">" nil t)
3238             (setq end (1+ (point)))
3239             (when (search-backward "<" nil t)
3240               (setq new-child (buffer-substring (point) end))
3241               (push (list (incf generation)
3242                           child (setq child new-child)
3243                           subject date)
3244                     relations)))
3245           (when child
3246             (push (list (1+ generation) child nil subject) relations))
3247           (erase-buffer)))
3248       (kill-buffer (current-buffer)))
3249     ;; Sort over trustworthiness.
3250     (mapcar
3251      (lambda (relation)
3252        (when (gnus-dependencies-add-header
3253               (make-full-mail-header
3254                gnus-reffed-article-number
3255                (nth 3 relation) "" (or (nth 4 relation) "")
3256                (nth 1 relation)
3257                (or (nth 2 relation) "") 0 0 "")
3258               gnus-newsgroup-dependencies nil)
3259          (push gnus-reffed-article-number gnus-newsgroup-limit)
3260          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3261          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3262                gnus-newsgroup-reads)
3263          (decf gnus-reffed-article-number)))
3264      (sort relations 'car-less-than-car))
3265     (gnus-message 7 "Making sparse threads...done")))
3266
3267 (defun gnus-build-old-threads ()
3268   ;; Look at all the articles that refer back to old articles, and
3269   ;; fetch the headers for the articles that aren't there.  This will
3270   ;; build complete threads - if the roots haven't been expired by the
3271   ;; server, that is.
3272   (let ((mail-parse-charset gnus-newsgroup-charset)
3273         id heads)
3274     (mapatoms
3275      (lambda (refs)
3276        (when (not (car (symbol-value refs)))
3277          (setq heads (cdr (symbol-value refs)))
3278          (while heads
3279            (if (memq (mail-header-number (caar heads))
3280                      gnus-newsgroup-dormant)
3281                (setq heads (cdr heads))
3282              (setq id (symbol-name refs))
3283              (while (and (setq id (gnus-build-get-header id))
3284                          (not (car (gnus-id-to-thread id)))))
3285              (setq heads nil)))))
3286      gnus-newsgroup-dependencies)))
3287
3288 ;; This function has to be called with point after the article number
3289 ;; on the beginning of the line.
3290 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3291   (let ((eol (gnus-point-at-eol))
3292         (buffer (current-buffer))
3293         header)
3294
3295     ;; overview: [num subject from date id refs chars lines misc]
3296     (unwind-protect
3297         (progn
3298           (narrow-to-region (point) eol)
3299           (unless (eobp)
3300             (forward-char))
3301
3302           (setq header
3303                 (make-full-mail-header
3304                  number                 ; number
3305                  (funcall gnus-decode-encoded-word-function
3306                           (nnheader-nov-field)) ; subject
3307                  (funcall gnus-decode-encoded-word-function
3308                           (nnheader-nov-field)) ; from
3309                  (nnheader-nov-field)   ; date
3310                  (nnheader-nov-read-message-id) ; id
3311                  (nnheader-nov-field)   ; refs
3312                  (nnheader-nov-read-integer) ; chars
3313                  (nnheader-nov-read-integer) ; lines
3314                  (unless (eobp)
3315                    (if (looking-at "Xref: ")
3316                        (goto-char (match-end 0)))
3317                    (nnheader-nov-field)) ; Xref
3318                  (nnheader-nov-parse-extra)))) ; extra
3319
3320       (widen))
3321
3322     (when gnus-alter-header-function
3323       (funcall gnus-alter-header-function header))
3324     (gnus-dependencies-add-header header dependencies force-new)))
3325
3326 (defun gnus-build-get-header (id)
3327   "Look through the buffer of NOV lines and find the header to ID.
3328 Enter this line into the dependencies hash table, and return
3329 the id of the parent article (if any)."
3330   (let ((deps gnus-newsgroup-dependencies)
3331         found header)
3332     (prog1
3333         (save-excursion
3334           (set-buffer nntp-server-buffer)
3335           (let ((case-fold-search nil))
3336             (goto-char (point-min))
3337             (while (and (not found)
3338                         (search-forward id nil t))
3339               (beginning-of-line)
3340               (setq found (looking-at
3341                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3342                                    (regexp-quote id))))
3343               (or found (beginning-of-line 2)))
3344             (when found
3345               (beginning-of-line)
3346               (and
3347                (setq header (gnus-nov-parse-line
3348                              (read (current-buffer)) deps))
3349                (gnus-parent-id (mail-header-references header))))))
3350       (when header
3351         (let ((number (mail-header-number header)))
3352           (push number gnus-newsgroup-limit)
3353           (push header gnus-newsgroup-headers)
3354           (if (memq number gnus-newsgroup-unselected)
3355               (progn
3356                 (push number gnus-newsgroup-unreads)
3357                 (setq gnus-newsgroup-unselected
3358                       (delq number gnus-newsgroup-unselected)))
3359             (push number gnus-newsgroup-ancient)))))))
3360
3361 (defun gnus-build-all-threads ()
3362   "Read all the headers."
3363   (let ((gnus-summary-ignore-duplicates t)
3364         (mail-parse-charset gnus-newsgroup-charset)
3365         (dependencies gnus-newsgroup-dependencies)
3366         header article)
3367     (save-excursion
3368       (set-buffer nntp-server-buffer)
3369       (let ((case-fold-search nil))
3370         (goto-char (point-min))
3371         (while (not (eobp))
3372           (ignore-errors
3373             (setq article (read (current-buffer))
3374                   header (gnus-nov-parse-line article dependencies)))
3375           (when header
3376             (save-excursion
3377               (set-buffer gnus-summary-buffer)
3378               (push header gnus-newsgroup-headers)
3379               (if (memq (setq article (mail-header-number header))
3380                         gnus-newsgroup-unselected)
3381                   (progn
3382                     (push article gnus-newsgroup-unreads)
3383                     (setq gnus-newsgroup-unselected
3384                           (delq article gnus-newsgroup-unselected)))
3385                 (push article gnus-newsgroup-ancient)))
3386             (forward-line 1)))))))
3387
3388 (defun gnus-summary-update-article-line (article header)
3389   "Update the line for ARTICLE using HEADERS."
3390   (let* ((id (mail-header-id header))
3391          (thread (gnus-id-to-thread id)))
3392     (unless thread
3393       (error "Article in no thread"))
3394     ;; Update the thread.
3395     (setcar thread header)
3396     (gnus-summary-goto-subject article)
3397     (let* ((datal (gnus-data-find-list article))
3398            (data (car datal))
3399            (length (when (cdr datal)
3400                      (- (gnus-data-pos data)
3401                         (gnus-data-pos (cadr datal)))))
3402            (buffer-read-only nil)
3403            (level (gnus-summary-thread-level)))
3404       (gnus-delete-line)
3405       (gnus-summary-insert-line
3406        header level nil (gnus-article-mark article)
3407        (memq article gnus-newsgroup-replied)
3408        (memq article gnus-newsgroup-expirable)
3409        ;; Only insert the Subject string when it's different
3410        ;; from the previous Subject string.
3411        (if (and
3412             gnus-show-threads
3413             (gnus-subject-equal
3414              (condition-case ()
3415                  (mail-header-subject
3416                   (gnus-data-header
3417                    (cadr
3418                     (gnus-data-find-list
3419                      article
3420                      (gnus-data-list t)))))
3421                ;; Error on the side of excessive subjects.
3422                (error ""))
3423              (mail-header-subject header)))
3424            ""
3425          (mail-header-subject header))
3426        nil (cdr (assq article gnus-newsgroup-scored))
3427        (memq article gnus-newsgroup-processable))
3428       (when length
3429         (gnus-data-update-list
3430          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3431
3432 (defun gnus-summary-update-article (article &optional iheader)
3433   "Update ARTICLE in the summary buffer."
3434   (set-buffer gnus-summary-buffer)
3435   (let* ((header (gnus-summary-article-header article))
3436          (id (mail-header-id header))
3437          (data (gnus-data-find article))
3438          (thread (gnus-id-to-thread id))
3439          (references (mail-header-references header))
3440          (parent
3441           (gnus-id-to-thread
3442            (or (gnus-parent-id
3443                 (when (and references
3444                            (not (equal "" references)))
3445                   references))
3446                "none")))
3447          (buffer-read-only nil)
3448          (old (car thread)))
3449     (when thread
3450       (unless iheader
3451         (setcar thread nil)
3452         (when parent
3453           (delq thread parent)))
3454       (if (gnus-summary-insert-subject id header)
3455           ;; Set the (possibly) new article number in the data structure.
3456           (gnus-data-set-number data (gnus-id-to-article id))
3457         (setcar thread old)
3458         nil))))
3459
3460 (defun gnus-rebuild-thread (id &optional line)
3461   "Rebuild the thread containing ID.
3462 If LINE, insert the rebuilt thread starting on line LINE."
3463   (let ((buffer-read-only nil)
3464         old-pos current thread data)
3465     (if (not gnus-show-threads)
3466         (setq thread (list (car (gnus-id-to-thread id))))
3467       ;; Get the thread this article is part of.
3468       (setq thread (gnus-remove-thread id)))
3469     (setq old-pos (gnus-point-at-bol))
3470     (setq current (save-excursion
3471                     (and (zerop (forward-line -1))
3472                          (gnus-summary-article-number))))
3473     ;; If this is a gathered thread, we have to go some re-gathering.
3474     (when (stringp (car thread))
3475       (let ((subject (car thread))
3476             roots thr)
3477         (setq thread (cdr thread))
3478         (while thread
3479           (unless (memq (setq thr (gnus-id-to-thread
3480                                    (gnus-root-id
3481                                     (mail-header-id (caar thread)))))
3482                         roots)
3483             (push thr roots))
3484           (setq thread (cdr thread)))
3485         ;; We now have all (unique) roots.
3486         (if (= (length roots) 1)
3487             ;; All the loose roots are now one solid root.
3488             (setq thread (car roots))
3489           (setq thread (cons subject (gnus-sort-threads roots))))))
3490     (let (threads)
3491       ;; We then insert this thread into the summary buffer.
3492       (when line
3493         (goto-char (point-min))
3494         (forward-line (1- line)))
3495       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3496         (if gnus-show-threads
3497             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3498           (gnus-summary-prepare-unthreaded thread))
3499         (setq data (nreverse gnus-newsgroup-data))
3500         (setq threads gnus-newsgroup-threads))
3501       ;; We splice the new data into the data structure.
3502       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3503       ;;!!! then we want to insert at the beginning of the buffer.
3504       ;;!!! That happens to be true with Gnus now, but that may
3505       ;;!!! change in the future.  Perhaps.
3506       (gnus-data-enter-list
3507        (if line nil current) data (- (point) old-pos))
3508       (setq gnus-newsgroup-threads
3509             (nconc threads gnus-newsgroup-threads))
3510       (gnus-data-compute-positions))))
3511
3512 (defun gnus-number-to-header (number)
3513   "Return the header for article NUMBER."
3514   (let ((headers gnus-newsgroup-headers))
3515     (while (and headers
3516                 (not (= number (mail-header-number (car headers)))))
3517       (pop headers))
3518     (when headers
3519       (car headers))))
3520
3521 (defun gnus-parent-headers (in-headers &optional generation)
3522   "Return the headers of the GENERATIONeth parent of HEADERS."
3523   (unless generation
3524     (setq generation 1))
3525   (let ((parent t)
3526         (headers in-headers)
3527         references)
3528     (while (and parent
3529                 (not (zerop generation))
3530                 (setq references (mail-header-references headers)))
3531       (setq headers (if (and references
3532                              (setq parent (gnus-parent-id references)))
3533                         (car (gnus-id-to-thread parent))
3534                       nil))
3535       (decf generation))
3536     (and (not (eq headers in-headers))
3537          headers)))
3538
3539 (defun gnus-id-to-thread (id)
3540   "Return the (sub-)thread where ID appears."
3541   (gnus-gethash id gnus-newsgroup-dependencies))
3542
3543 (defun gnus-id-to-article (id)
3544   "Return the article number of ID."
3545   (let ((thread (gnus-id-to-thread id)))
3546     (when (and thread
3547                (car thread))
3548       (mail-header-number (car thread)))))
3549
3550 (defun gnus-id-to-header (id)
3551   "Return the article headers of ID."
3552   (car (gnus-id-to-thread id)))
3553
3554 (defun gnus-article-displayed-root-p (article)
3555   "Say whether ARTICLE is a root(ish) article."
3556   (let ((level (gnus-summary-thread-level article))
3557         (refs (mail-header-references  (gnus-summary-article-header article)))
3558         particle)
3559     (cond
3560      ((null level) nil)
3561      ((zerop level) t)
3562      ((null refs) t)
3563      ((null (gnus-parent-id refs)) t)
3564      ((and (= 1 level)
3565            (null (setq particle (gnus-id-to-article
3566                                  (gnus-parent-id refs))))
3567            (null (gnus-summary-thread-level particle)))))))
3568
3569 (defun gnus-root-id (id)
3570   "Return the id of the root of the thread where ID appears."
3571   (let (last-id prev)
3572     (while (and id (setq prev (car (gnus-id-to-thread id))))
3573       (setq last-id id
3574             id (gnus-parent-id (mail-header-references prev))))
3575     last-id))
3576
3577 (defun gnus-articles-in-thread (thread)
3578   "Return the list of articles in THREAD."
3579   (cons (mail-header-number (car thread))
3580         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3581
3582 (defun gnus-remove-thread (id &optional dont-remove)
3583   "Remove the thread that has ID in it."
3584   (let (headers thread last-id)
3585     ;; First go up in this thread until we find the root.
3586     (setq last-id (gnus-root-id id)
3587           headers (message-flatten-list (gnus-id-to-thread last-id)))
3588     ;; We have now found the real root of this thread.  It might have
3589     ;; been gathered into some loose thread, so we have to search
3590     ;; through the threads to find the thread we wanted.
3591     (let ((threads gnus-newsgroup-threads)
3592           sub)
3593       (while threads
3594         (setq sub (car threads))
3595         (if (stringp (car sub))
3596             ;; This is a gathered thread, so we look at the roots
3597             ;; below it to find whether this article is in this
3598             ;; gathered root.
3599             (progn
3600               (setq sub (cdr sub))
3601               (while sub
3602                 (when (member (caar sub) headers)
3603                   (setq thread (car threads)
3604                         threads nil
3605                         sub nil))
3606                 (setq sub (cdr sub))))
3607           ;; It's an ordinary thread, so we check it.
3608           (when (eq (car sub) (car headers))
3609             (setq thread sub
3610                   threads nil)))
3611         (setq threads (cdr threads)))
3612       ;; If this article is in no thread, then it's a root.
3613       (if thread
3614           (unless dont-remove
3615             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3616         (setq thread (gnus-id-to-thread last-id)))
3617       (when thread
3618         (prog1
3619             thread                      ; We return this thread.
3620           (unless dont-remove
3621             (if (stringp (car thread))
3622                 (progn
3623                   ;; If we use dummy roots, then we have to remove the
3624                   ;; dummy root as well.
3625                   (when (eq gnus-summary-make-false-root 'dummy)
3626                     ;; We go to the dummy root by going to
3627                     ;; the first sub-"thread", and then one line up.
3628                     (gnus-summary-goto-article
3629                      (mail-header-number (caadr thread)))
3630                     (forward-line -1)
3631                     (gnus-delete-line)
3632                     (gnus-data-compute-positions))
3633                   (setq thread (cdr thread))
3634                   (while thread
3635                     (gnus-remove-thread-1 (car thread))
3636                     (setq thread (cdr thread))))
3637               (gnus-remove-thread-1 thread))))))))
3638
3639 (defun gnus-remove-thread-1 (thread)
3640   "Remove the thread THREAD recursively."
3641   (let ((number (mail-header-number (pop thread)))
3642         d)
3643     (setq thread (reverse thread))
3644     (while thread
3645       (gnus-remove-thread-1 (pop thread)))
3646     (when (setq d (gnus-data-find number))
3647       (goto-char (gnus-data-pos d))
3648       (gnus-summary-show-thread)
3649       (gnus-data-remove
3650        number
3651        (- (gnus-point-at-bol)
3652           (prog1
3653               (1+ (gnus-point-at-eol))
3654             (gnus-delete-line)))))))
3655
3656 (defun gnus-sort-threads-1 (threads func)
3657   (sort (mapcar (lambda (thread)
3658                   (cons (car thread)
3659                         (and (cdr thread)
3660                              (gnus-sort-threads-1 (cdr thread) func))))
3661                 threads) func))
3662
3663 (defun gnus-sort-threads (threads)
3664   "Sort THREADS."
3665   (if (not gnus-thread-sort-functions)
3666       threads
3667     (gnus-message 8 "Sorting threads...")
3668     (prog1
3669         (gnus-sort-threads-1 
3670          threads 
3671          (gnus-make-sort-function gnus-thread-sort-functions))
3672       (gnus-message 8 "Sorting threads...done"))))
3673
3674 (defun gnus-sort-articles (articles)
3675   "Sort ARTICLES."
3676   (when gnus-article-sort-functions
3677     (gnus-message 7 "Sorting articles...")
3678     (prog1
3679         (setq gnus-newsgroup-headers
3680               (sort articles (gnus-make-sort-function
3681                               gnus-article-sort-functions)))
3682       (gnus-message 7 "Sorting articles...done"))))
3683
3684 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3685 (defmacro gnus-thread-header (thread)
3686   "Return header of first article in THREAD.
3687 Note that THREAD must never, ever be anything else than a variable -
3688 using some other form will lead to serious barfage."
3689   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3690   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3691   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3692         (vector thread) 2))
3693
3694 (defsubst gnus-article-sort-by-number (h1 h2)
3695   "Sort articles by article number."
3696   (< (mail-header-number h1)
3697      (mail-header-number h2)))
3698
3699 (defun gnus-thread-sort-by-number (h1 h2)
3700   "Sort threads by root article number."
3701   (gnus-article-sort-by-number
3702    (gnus-thread-header h1) (gnus-thread-header h2)))
3703
3704 (defsubst gnus-article-sort-by-lines (h1 h2)
3705   "Sort articles by article Lines header."
3706   (< (mail-header-lines h1)
3707      (mail-header-lines h2)))
3708
3709 (defun gnus-thread-sort-by-lines (h1 h2)
3710   "Sort threads by root article Lines header."
3711   (gnus-article-sort-by-lines
3712    (gnus-thread-header h1) (gnus-thread-header h2)))
3713
3714 (defsubst gnus-article-sort-by-chars (h1 h2)
3715   "Sort articles by octet length."
3716   (< (mail-header-chars h1)
3717      (mail-header-chars h2)))
3718
3719 (defun gnus-thread-sort-by-chars (h1 h2)
3720   "Sort threads by root article octet length."
3721   (gnus-article-sort-by-chars
3722    (gnus-thread-header h1) (gnus-thread-header h2)))
3723
3724 (defsubst gnus-article-sort-by-author (h1 h2)
3725   "Sort articles by root author."
3726   (string-lessp
3727    (let ((extract (funcall
3728                    gnus-extract-address-components
3729                    (mail-header-from h1))))
3730      (or (car extract) (cadr extract) ""))
3731    (let ((extract (funcall
3732                    gnus-extract-address-components
3733                    (mail-header-from h2))))
3734      (or (car extract) (cadr extract) ""))))
3735
3736 (defun gnus-thread-sort-by-author (h1 h2)
3737   "Sort threads by root author."
3738   (gnus-article-sort-by-author
3739    (gnus-thread-header h1)  (gnus-thread-header h2)))
3740
3741 (defsubst gnus-article-sort-by-subject (h1 h2)
3742   "Sort articles by root subject."
3743   (string-lessp
3744    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3745    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3746
3747 (defun gnus-thread-sort-by-subject (h1 h2)
3748   "Sort threads by root subject."
3749   (gnus-article-sort-by-subject
3750    (gnus-thread-header h1) (gnus-thread-header h2)))
3751
3752 (defsubst gnus-article-sort-by-date (h1 h2)
3753   "Sort articles by root article date."
3754   (time-less-p
3755    (gnus-date-get-time (mail-header-date h1))
3756    (gnus-date-get-time (mail-header-date h2))))
3757
3758 (defun gnus-thread-sort-by-date (h1 h2)
3759   "Sort threads by root article date."
3760   (gnus-article-sort-by-date
3761    (gnus-thread-header h1) (gnus-thread-header h2)))
3762
3763 (defsubst gnus-article-sort-by-score (h1 h2)
3764   "Sort articles by root article score.
3765 Unscored articles will be counted as having a score of zero."
3766   (> (or (cdr (assq (mail-header-number h1)
3767                     gnus-newsgroup-scored))
3768          gnus-summary-default-score 0)
3769      (or (cdr (assq (mail-header-number h2)
3770                     gnus-newsgroup-scored))
3771          gnus-summary-default-score 0)))
3772
3773 (defun gnus-thread-sort-by-score (h1 h2)
3774   "Sort threads by root article score."
3775   (gnus-article-sort-by-score
3776    (gnus-thread-header h1) (gnus-thread-header h2)))
3777
3778 (defun gnus-thread-sort-by-total-score (h1 h2)
3779   "Sort threads by the sum of all scores in the thread.
3780 Unscored articles will be counted as having a score of zero."
3781   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3782
3783 (defun gnus-thread-total-score (thread)
3784   ;; This function find the total score of THREAD.
3785   (cond ((null thread)
3786          0)
3787         ((consp thread)
3788          (if (stringp (car thread))
3789              (apply gnus-thread-score-function 0
3790                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3791            (gnus-thread-total-score-1 thread)))
3792         (t
3793          (gnus-thread-total-score-1 (list thread)))))
3794
3795 (defun gnus-thread-total-score-1 (root)
3796   ;; This function find the total score of the thread below ROOT.
3797   (setq root (car root))
3798   (apply gnus-thread-score-function
3799          (or (append
3800               (mapcar 'gnus-thread-total-score
3801                       (cdr (gnus-id-to-thread (mail-header-id root))))
3802               (when (> (mail-header-number root) 0)
3803                 (list (or (cdr (assq (mail-header-number root)
3804                                      gnus-newsgroup-scored))
3805                           gnus-summary-default-score 0))))
3806              (list gnus-summary-default-score)
3807              '(0))))
3808
3809 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3810 (defvar gnus-tmp-prev-subject nil)
3811 (defvar gnus-tmp-false-parent nil)
3812 (defvar gnus-tmp-root-expunged nil)
3813 (defvar gnus-tmp-dummy-line nil)
3814
3815 (defvar gnus-tmp-header)
3816 (defun gnus-extra-header (type &optional header)
3817   "Return the extra header of TYPE."
3818   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3819       ""))
3820
3821 (defun gnus-summary-prepare-threads (threads)
3822   "Prepare summary buffer from THREADS and indentation LEVEL.
3823 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3824 or a straight list of headers."
3825   (gnus-message 7 "Generating summary...")
3826
3827   (setq gnus-newsgroup-threads threads)
3828   (beginning-of-line)
3829
3830   (let ((gnus-tmp-level 0)
3831         (default-score (or gnus-summary-default-score 0))
3832         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3833         thread number subject stack state gnus-tmp-gathered beg-match
3834         new-roots gnus-tmp-new-adopts thread-end
3835         gnus-tmp-header gnus-tmp-unread
3836         gnus-tmp-replied gnus-tmp-subject-or-nil
3837         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3838         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3839         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3840
3841     (setq gnus-tmp-prev-subject nil)
3842
3843     (if (vectorp (car threads))
3844         ;; If this is a straight (sic) list of headers, then a
3845         ;; threaded summary display isn't required, so we just create
3846         ;; an unthreaded one.
3847         (gnus-summary-prepare-unthreaded threads)
3848
3849       ;; Do the threaded display.
3850
3851       (while (or threads stack gnus-tmp-new-adopts new-roots)
3852
3853         (if (and (= gnus-tmp-level 0)
3854                  (or (not stack)
3855                      (= (caar stack) 0))
3856                  (not gnus-tmp-false-parent)
3857                  (or gnus-tmp-new-adopts new-roots))
3858             (if gnus-tmp-new-adopts
3859                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3860                       thread (list (car gnus-tmp-new-adopts))
3861                       gnus-tmp-header (caar thread)
3862                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3863               (when new-roots
3864                 (setq thread (list (car new-roots))
3865                       gnus-tmp-header (caar thread)
3866                       new-roots (cdr new-roots))))
3867
3868           (if threads
3869               ;; If there are some threads, we do them before the
3870               ;; threads on the stack.
3871               (setq thread threads
3872                     gnus-tmp-header (caar thread))
3873             ;; There were no current threads, so we pop something off
3874             ;; the stack.
3875             (setq state (car stack)
3876                   gnus-tmp-level (car state)
3877                   thread (cdr state)
3878                   stack (cdr stack)
3879                   gnus-tmp-header (caar thread))))
3880
3881         (setq gnus-tmp-false-parent nil)
3882         (setq gnus-tmp-root-expunged nil)
3883         (setq thread-end nil)
3884
3885         (if (stringp gnus-tmp-header)
3886             ;; The header is a dummy root.
3887             (cond
3888              ((eq gnus-summary-make-false-root 'adopt)
3889               ;; We let the first article adopt the rest.
3890               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3891                                                (cddar thread)))
3892               (setq gnus-tmp-gathered
3893                     (nconc (mapcar
3894                             (lambda (h) (mail-header-number (car h)))
3895                             (cddar thread))
3896                            gnus-tmp-gathered))
3897               (setq thread (cons (list (caar thread)
3898                                        (cadar thread))
3899                                  (cdr thread)))
3900               (setq gnus-tmp-level -1
3901                     gnus-tmp-false-parent t))
3902              ((eq gnus-summary-make-false-root 'empty)
3903               ;; We print adopted articles with empty subject fields.
3904               (setq gnus-tmp-gathered
3905                     (nconc (mapcar
3906                             (lambda (h) (mail-header-number (car h)))
3907                             (cddar thread))
3908                            gnus-tmp-gathered))
3909               (setq gnus-tmp-level -1))
3910              ((eq gnus-summary-make-false-root 'dummy)
3911               ;; We remember that we probably want to output a dummy
3912               ;; root.
3913               (setq gnus-tmp-dummy-line gnus-tmp-header)
3914               (setq gnus-tmp-prev-subject gnus-tmp-header))
3915              (t
3916               ;; We do not make a root for the gathered
3917               ;; sub-threads at all.
3918               (setq gnus-tmp-level -1)))
3919
3920           (setq number (mail-header-number gnus-tmp-header)
3921                 subject (mail-header-subject gnus-tmp-header))
3922
3923           (cond
3924            ;; If the thread has changed subject, we might want to make
3925            ;; this subthread into a root.
3926            ((and (null gnus-thread-ignore-subject)
3927                  (not (zerop gnus-tmp-level))
3928                  gnus-tmp-prev-subject
3929                  (not (inline
3930                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3931             (setq new-roots (nconc new-roots (list (car thread)))
3932                   thread-end t
3933                   gnus-tmp-header nil))
3934            ;; If the article lies outside the current limit,
3935            ;; then we do not display it.
3936            ((not (memq number gnus-newsgroup-limit))
3937             (setq gnus-tmp-gathered
3938                   (nconc (mapcar
3939                           (lambda (h) (mail-header-number (car h)))
3940                           (cdar thread))
3941                          gnus-tmp-gathered))
3942             (setq gnus-tmp-new-adopts (if (cdar thread)
3943                                           (append gnus-tmp-new-adopts
3944                                                   (cdar thread))
3945                                         gnus-tmp-new-adopts)
3946                   thread-end t
3947                   gnus-tmp-header nil)
3948             (when (zerop gnus-tmp-level)
3949               (setq gnus-tmp-root-expunged t)))
3950            ;; Perhaps this article is to be marked as read?
3951            ((and gnus-summary-mark-below
3952                  (< (or (cdr (assq number gnus-newsgroup-scored))
3953                         default-score)
3954                     gnus-summary-mark-below)
3955                  ;; Don't touch sparse articles.
3956                  (not (gnus-summary-article-sparse-p number))
3957                  (not (gnus-summary-article-ancient-p number)))
3958             (setq gnus-newsgroup-unreads
3959                   (delq number gnus-newsgroup-unreads))
3960             (if gnus-newsgroup-auto-expire
3961                 (push number gnus-newsgroup-expirable)
3962               (push (cons number gnus-low-score-mark)
3963                     gnus-newsgroup-reads))))
3964
3965           (when gnus-tmp-header
3966             ;; We may have an old dummy line to output before this
3967             ;; article.
3968             (when (and gnus-tmp-dummy-line
3969                        (gnus-subject-equal
3970                         gnus-tmp-dummy-line
3971                         (mail-header-subject gnus-tmp-header)))
3972               (gnus-summary-insert-dummy-line
3973                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3974               (setq gnus-tmp-dummy-line nil))
3975
3976             ;; Compute the mark.
3977             (setq gnus-tmp-unread (gnus-article-mark number))
3978
3979             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3980                                   gnus-tmp-header gnus-tmp-level)
3981                   gnus-newsgroup-data)
3982
3983             ;; Actually insert the line.
3984             (setq
3985              gnus-tmp-subject-or-nil
3986              (cond
3987               ((and gnus-thread-ignore-subject
3988                     gnus-tmp-prev-subject
3989                     (not (inline (gnus-subject-equal
3990                                   gnus-tmp-prev-subject subject))))
3991                subject)
3992               ((zerop gnus-tmp-level)
3993                (if (and (eq gnus-summary-make-false-root 'empty)
3994                         (memq number gnus-tmp-gathered)
3995                         gnus-tmp-prev-subject
3996                         (inline (gnus-subject-equal
3997                                  gnus-tmp-prev-subject subject)))
3998                    gnus-summary-same-subject
3999                  subject))
4000               (t gnus-summary-same-subject)))
4001             (if (and (eq gnus-summary-make-false-root 'adopt)
4002                      (= gnus-tmp-level 1)
4003                      (memq number gnus-tmp-gathered))
4004                 (setq gnus-tmp-opening-bracket ?\<
4005                       gnus-tmp-closing-bracket ?\>)
4006               (setq gnus-tmp-opening-bracket ?\[
4007                     gnus-tmp-closing-bracket ?\]))
4008             (setq
4009              gnus-tmp-indentation
4010              (aref gnus-thread-indent-array gnus-tmp-level)
4011              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4012              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4013                                 gnus-summary-default-score 0)
4014              gnus-tmp-score-char
4015              (if (or (null gnus-summary-default-score)
4016                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4017                          gnus-summary-zcore-fuzz))
4018                  ?  ;Whitespace
4019                (if (< gnus-tmp-score gnus-summary-default-score)
4020                    gnus-score-below-mark gnus-score-over-mark))
4021              gnus-tmp-replied
4022              (cond ((memq number gnus-newsgroup-processable)
4023                     gnus-process-mark)
4024                    ((memq number gnus-newsgroup-cached)
4025                     gnus-cached-mark)
4026                    ((memq number gnus-newsgroup-replied)
4027                     gnus-replied-mark)
4028                    ((memq number gnus-newsgroup-saved)
4029                     gnus-saved-mark)
4030                    (t gnus-unread-mark))
4031              gnus-tmp-from (mail-header-from gnus-tmp-header)
4032              gnus-tmp-name
4033              (cond
4034               ((string-match "<[^>]+> *$" gnus-tmp-from)
4035                (setq beg-match (match-beginning 0))
4036                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4037                         (substring gnus-tmp-from (1+ (match-beginning 0))
4038                                    (1- (match-end 0))))
4039                    (substring gnus-tmp-from 0 beg-match)))
4040               ((string-match "(.+)" gnus-tmp-from)
4041                (substring gnus-tmp-from
4042                           (1+ (match-beginning 0)) (1- (match-end 0))))
4043               (t gnus-tmp-from)))
4044             (when (string= gnus-tmp-name "")
4045               (setq gnus-tmp-name gnus-tmp-from))
4046             (unless (numberp gnus-tmp-lines)
4047               (setq gnus-tmp-lines 0))
4048             (gnus-put-text-property
4049              (point)
4050              (progn (eval gnus-summary-line-format-spec) (point))
4051              'gnus-number number)
4052             (when gnus-visual-p
4053               (forward-line -1)
4054               (gnus-run-hooks 'gnus-summary-update-hook)
4055               (forward-line 1))
4056
4057             (setq gnus-tmp-prev-subject subject)))
4058
4059         (when (nth 1 thread)
4060           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4061         (incf gnus-tmp-level)
4062         (setq threads (if thread-end nil (cdar thread)))
4063         (unless threads
4064           (setq gnus-tmp-level 0)))))
4065   (gnus-message 7 "Generating summary...done"))
4066
4067 (defun gnus-summary-prepare-unthreaded (headers)
4068   "Generate an unthreaded summary buffer based on HEADERS."
4069   (let (header number mark)
4070
4071     (beginning-of-line)
4072
4073     (while headers
4074       ;; We may have to root out some bad articles...
4075       (when (memq (setq number (mail-header-number
4076                                 (setq header (pop headers))))
4077                   gnus-newsgroup-limit)
4078         ;; Mark article as read when it has a low score.
4079         (when (and gnus-summary-mark-below
4080                    (< (or (cdr (assq number gnus-newsgroup-scored))
4081                           gnus-summary-default-score 0)
4082                       gnus-summary-mark-below)
4083                    (not (gnus-summary-article-ancient-p number)))
4084           (setq gnus-newsgroup-unreads
4085                 (delq number gnus-newsgroup-unreads))
4086           (if gnus-newsgroup-auto-expire
4087               (push number gnus-newsgroup-expirable)
4088             (push (cons number gnus-low-score-mark)
4089                   gnus-newsgroup-reads)))
4090
4091         (setq mark (gnus-article-mark number))
4092         (push (gnus-data-make number mark (1+ (point)) header 0)
4093               gnus-newsgroup-data)
4094         (gnus-summary-insert-line
4095          header 0 number
4096          mark (memq number gnus-newsgroup-replied)
4097          (memq number gnus-newsgroup-expirable)
4098          (mail-header-subject header) nil
4099          (cdr (assq number gnus-newsgroup-scored))
4100          (memq number gnus-newsgroup-processable))))))
4101
4102 (defun gnus-summary-remove-list-identifiers ()
4103   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4104   (let ((regexp (if (stringp gnus-list-identifiers)
4105                     gnus-list-identifiers
4106                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4107     (dolist (header gnus-newsgroup-headers)
4108       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4109                                   " *\\)\\)+\\(Re: +\\)?\\)")
4110                           (mail-header-subject header))
4111         (mail-header-set-subject
4112          header (concat (substring (mail-header-subject header)
4113                                    0 (match-beginning 1))
4114                         (or
4115                          (match-string 3 (mail-header-subject header))
4116                          (match-string 5 (mail-header-subject header)))
4117                         (substring (mail-header-subject header)
4118                                    (match-end 1))))))))
4119
4120 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4121   "Select newsgroup GROUP.
4122 If READ-ALL is non-nil, all articles in the group are selected.
4123 If SELECT-ARTICLES, only select those articles from GROUP."
4124   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4125          ;;!!! Dirty hack; should be removed.
4126          (gnus-summary-ignore-duplicates
4127           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4128               t
4129             gnus-summary-ignore-duplicates))
4130          (info (nth 2 entry))
4131          articles fetched-articles cached)
4132
4133     (unless (gnus-check-server
4134              (setq gnus-current-select-method
4135                    (gnus-find-method-for-group group)))
4136       (error "Couldn't open server"))
4137
4138     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4139         (gnus-activate-group group)     ; Or we can activate it...
4140         (progn                          ; Or we bug out.
4141           (when (equal major-mode 'gnus-summary-mode)
4142             (kill-buffer (current-buffer)))
4143           (error "Couldn't request group %s: %s"
4144                  group (gnus-status-message group))))
4145
4146     (unless (gnus-request-group group t)
4147       (when (equal major-mode 'gnus-summary-mode)
4148         (kill-buffer (current-buffer)))
4149       (error "Couldn't request group %s: %s"
4150              group (gnus-status-message group)))
4151
4152     (setq gnus-newsgroup-name group)
4153     (setq gnus-newsgroup-unselected nil)
4154     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4155     (gnus-summary-setup-default-charset)
4156
4157     ;; Adjust and set lists of article marks.
4158     (when info
4159       (gnus-adjust-marked-articles info))
4160
4161     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4162     (when (gnus-virtual-group-p group)
4163       (setq cached gnus-newsgroup-cached))
4164
4165     (setq gnus-newsgroup-unreads
4166           (gnus-set-difference
4167            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4168            gnus-newsgroup-dormant))
4169
4170     (setq gnus-newsgroup-processable nil)
4171
4172     (gnus-update-read-articles group gnus-newsgroup-unreads)
4173
4174     (if (setq articles select-articles)
4175         (setq gnus-newsgroup-unselected
4176               (gnus-sorted-intersection
4177                gnus-newsgroup-unreads
4178                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4179       (setq articles (gnus-articles-to-read group read-all)))
4180
4181     (cond
4182      ((null articles)
4183       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4184       'quit)
4185      ((eq articles 0) nil)
4186      (t
4187       ;; Init the dependencies hash table.
4188       (setq gnus-newsgroup-dependencies
4189             (gnus-make-hashtable (length articles)))
4190       (gnus-set-global-variables)
4191       ;; Retrieve the headers and read them in.
4192       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4193       (setq gnus-newsgroup-headers
4194             (if (eq 'nov
4195                     (setq gnus-headers-retrieved-by
4196                           (gnus-retrieve-headers
4197                            articles gnus-newsgroup-name
4198                            ;; We might want to fetch old headers, but
4199                            ;; not if there is only 1 article.
4200                            (and (or (and
4201                                      (not (eq gnus-fetch-old-headers 'some))
4202                                      (not (numberp gnus-fetch-old-headers)))
4203                                     (> (length articles) 1))
4204                                 gnus-fetch-old-headers))))
4205                 (gnus-get-newsgroup-headers-xover
4206                  articles nil nil gnus-newsgroup-name t)
4207               (gnus-get-newsgroup-headers)))
4208       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4209
4210       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4211       (when cached
4212         (setq gnus-newsgroup-cached cached))
4213
4214       ;; Suppress duplicates?
4215       (when gnus-suppress-duplicates
4216         (gnus-dup-suppress-articles))
4217
4218       ;; Set the initial limit.
4219       (setq gnus-newsgroup-limit (copy-sequence articles))
4220       ;; Remove canceled articles from the list of unread articles.
4221       (setq gnus-newsgroup-unreads
4222             (gnus-set-sorted-intersection
4223              gnus-newsgroup-unreads
4224              (setq fetched-articles
4225                    (mapcar (lambda (headers) (mail-header-number headers))
4226                            gnus-newsgroup-headers))))
4227       ;; Removed marked articles that do not exist.
4228       (gnus-update-missing-marks
4229        (gnus-sorted-complement fetched-articles articles))
4230       ;; We might want to build some more threads first.
4231       (when (and gnus-fetch-old-headers
4232                  (eq gnus-headers-retrieved-by 'nov))
4233         (if (eq gnus-fetch-old-headers 'invisible)
4234             (gnus-build-all-threads)
4235           (gnus-build-old-threads)))
4236       ;; Let the Gnus agent mark articles as read.
4237       (when gnus-agent
4238         (gnus-agent-get-undownloaded-list))
4239       ;; Remove list identifiers from subject
4240       (when gnus-list-identifiers
4241         (gnus-summary-remove-list-identifiers))
4242       ;; Check whether auto-expire is to be done in this group.
4243       (setq gnus-newsgroup-auto-expire
4244             (gnus-group-auto-expirable-p group))
4245       ;; Set up the article buffer now, if necessary.
4246       (unless gnus-single-article-buffer
4247         (gnus-article-setup-buffer))
4248       ;; First and last article in this newsgroup.
4249       (when gnus-newsgroup-headers
4250         (setq gnus-newsgroup-begin
4251               (mail-header-number (car gnus-newsgroup-headers))
4252               gnus-newsgroup-end
4253               (mail-header-number
4254                (gnus-last-element gnus-newsgroup-headers))))
4255       ;; GROUP is successfully selected.
4256       (or gnus-newsgroup-headers t)))))
4257
4258 (defun gnus-articles-to-read (group &optional read-all)
4259   "Find out what articles the user wants to read."
4260   (let* ((articles
4261           ;; Select all articles if `read-all' is non-nil, or if there
4262           ;; are no unread articles.
4263           (if (or read-all
4264                   (and (zerop (length gnus-newsgroup-marked))
4265                        (zerop (length gnus-newsgroup-unreads)))
4266                   (eq (gnus-group-find-parameter group 'display)
4267                       'all))
4268               (or
4269                (gnus-uncompress-range (gnus-active group))
4270                (gnus-cache-articles-in-group group))
4271             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4272                           (copy-sequence gnus-newsgroup-unreads))
4273                   '<)))
4274          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4275          (scored (length scored-list))
4276          (number (length articles))
4277          (marked (+ (length gnus-newsgroup-marked)
4278                     (length gnus-newsgroup-dormant)))
4279          (select
4280           (cond
4281            ((numberp read-all)
4282             read-all)
4283            (t
4284             (condition-case ()
4285                 (cond
4286                  ((and (or (<= scored marked) (= scored number))
4287                        (numberp gnus-large-newsgroup)
4288                        (> number gnus-large-newsgroup))
4289                   (let ((input
4290                          (read-string
4291                           (format
4292                            "How many articles from %s (default %d): "
4293                            (gnus-limit-string gnus-newsgroup-name 35)
4294                            number))))
4295                     (if (string-match "^[ \t]*$" input) number input)))
4296                  ((and (> scored marked) (< scored number)
4297                        (> (- scored number) 20))
4298                   (let ((input
4299                          (read-string
4300                           (format "%s %s (%d scored, %d total): "
4301                                   "How many articles from"
4302                                   group scored number))))
4303                     (if (string-match "^[ \t]*$" input)
4304                         number input)))
4305                  (t number))
4306               (quit
4307                (message "Quit getting the articles to read")
4308                nil))))))
4309     (setq select (if (stringp select) (string-to-number select) select))
4310     (if (or (null select) (zerop select))
4311         select
4312       (if (and (not (zerop scored)) (<= (abs select) scored))
4313           (progn
4314             (setq articles (sort scored-list '<))
4315             (setq number (length articles)))
4316         (setq articles (copy-sequence articles)))
4317
4318       (when (< (abs select) number)
4319         (if (< select 0)
4320             ;; Select the N oldest articles.
4321             (setcdr (nthcdr (1- (abs select)) articles) nil)
4322           ;; Select the N most recent articles.
4323           (setq articles (nthcdr (- number select) articles))))
4324       (setq gnus-newsgroup-unselected
4325             (gnus-sorted-intersection
4326              gnus-newsgroup-unreads
4327              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4328       (when gnus-alter-articles-to-read-function
4329         (setq gnus-newsgroup-unreads
4330               (sort 
4331                (funcall gnus-alter-articles-to-read-function
4332                         gnus-newsgroup-name gnus-newsgroup-unreads)
4333                '<)))
4334       articles)))
4335
4336 (defun gnus-killed-articles (killed articles)
4337   (let (out)
4338     (while articles
4339       (when (inline (gnus-member-of-range (car articles) killed))
4340         (push (car articles) out))
4341       (setq articles (cdr articles)))
4342     out))
4343
4344 (defun gnus-uncompress-marks (marks)
4345   "Uncompress the mark ranges in MARKS."
4346   (let ((uncompressed '(score bookmark))
4347         out)
4348     (while marks
4349       (if (memq (caar marks) uncompressed)
4350           (push (car marks) out)
4351         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4352       (setq marks (cdr marks)))
4353     out))
4354
4355 (defun gnus-adjust-marked-articles (info)
4356   "Set all article lists and remove all marks that are no longer valid."
4357   (let* ((marked-lists (gnus-info-marks info))
4358          (active (gnus-active (gnus-info-group info)))
4359          (min (car active))
4360          (max (cdr active))
4361          (types gnus-article-mark-lists)
4362          (uncompressed '(score bookmark killed))
4363          marks var articles article mark)
4364
4365     (while marked-lists
4366       (setq marks (pop marked-lists))
4367       (set (setq var (intern (format "gnus-newsgroup-%s"
4368                                      (car (rassq (setq mark (car marks))
4369                                                  types)))))
4370            (if (memq (car marks) uncompressed) (cdr marks)
4371              (gnus-uncompress-range (cdr marks))))
4372
4373       (setq articles (symbol-value var))
4374
4375       ;; All articles have to be subsets of the active articles.
4376       (cond
4377        ;; Adjust "simple" lists.
4378        ((memq mark '(tick dormant expire reply save))
4379         (while articles
4380           (when (or (< (setq article (pop articles)) min) (> article max))
4381             (set var (delq article (symbol-value var))))))
4382        ;; Adjust assocs.
4383        ((memq mark uncompressed)
4384         (when (not (listp (cdr (symbol-value var))))
4385           (set var (list (symbol-value var))))
4386         (when (not (listp (cdr articles)))
4387           (setq articles (list articles)))
4388         (while articles
4389           (when (or (not (consp (setq article (pop articles))))
4390                     (< (car article) min)
4391                     (> (car article) max))
4392             (set var (delq article (symbol-value var))))))))))
4393
4394 (defun gnus-update-missing-marks (missing)
4395   "Go through the list of MISSING articles and remove them from the mark lists."
4396   (when missing
4397     (let ((types gnus-article-mark-lists)
4398           var m)
4399       ;; Go through all types.
4400       (while types
4401         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4402         (when (symbol-value var)
4403           ;; This list has articles.  So we delete all missing articles
4404           ;; from it.
4405           (setq m missing)
4406           (while m
4407             (set var (delq (pop m) (symbol-value var)))))))))
4408
4409 (defun gnus-update-marks ()
4410   "Enter the various lists of marked articles into the newsgroup info list."
4411   (let ((types gnus-article-mark-lists)
4412         (info (gnus-get-info gnus-newsgroup-name))
4413         (uncompressed '(score bookmark killed))
4414         type list newmarked symbol delta-marks)
4415     (when info
4416       ;; Add all marks lists to the list of marks lists.
4417       (while (setq type (pop types))
4418         (setq list (symbol-value
4419                     (setq symbol
4420                           (intern (format "gnus-newsgroup-%s"
4421                                           (car type))))))
4422
4423         (when list
4424           ;; Get rid of the entries of the articles that have the
4425           ;; default score.
4426           (when (and (eq (cdr type) 'score)
4427                      gnus-save-score
4428                      list)
4429             (let* ((arts list)
4430                    (prev (cons nil list))
4431                    (all prev))
4432               (while arts
4433                 (if (or (not (consp (car arts)))
4434                         (= (cdar arts) gnus-summary-default-score))
4435                     (setcdr prev (cdr arts))
4436                   (setq prev arts))
4437                 (setq arts (cdr arts)))
4438               (setq list (cdr all)))))
4439
4440         (unless (memq (cdr type) uncompressed)
4441           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4442        
4443         (when (gnus-check-backend-function
4444                'request-set-mark gnus-newsgroup-name)
4445           ;; propagate flags to server, with the following exceptions:
4446           ;; uncompressed:s are not proper flags (they are cons cells)
4447           ;; cache is a internal gnus flag
4448           ;; download are local to one gnus installation (well)
4449           ;; unsend are for nndraft groups only
4450           ;; xxx: generality of this?  this suits nnimap anyway
4451           (unless (memq (cdr type) (append '(cache download unsend)
4452                                            uncompressed))
4453             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4454                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4455                    (add (gnus-remove-from-range
4456                          (gnus-copy-sequence list) old)))
4457               (when add
4458                 (push (list add 'add (list (cdr type))) delta-marks))
4459               (when del
4460                 (push (list del 'del (list (cdr type))) delta-marks)))))
4461           
4462         (when list
4463           (push (cons (cdr type) list) newmarked)))
4464
4465       (when delta-marks
4466         (unless (gnus-check-group gnus-newsgroup-name)
4467           (error "Can't open server for %s" gnus-newsgroup-name))
4468         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4469           
4470       ;; Enter these new marks into the info of the group.
4471       (if (nthcdr 3 info)
4472           (setcar (nthcdr 3 info) newmarked)
4473         ;; Add the marks lists to the end of the info.
4474         (when newmarked
4475           (setcdr (nthcdr 2 info) (list newmarked))))
4476
4477       ;; Cut off the end of the info if there's nothing else there.
4478       (let ((i 5))
4479         (while (and (> i 2)
4480                     (not (nth i info)))
4481           (when (nthcdr (decf i) info)
4482             (setcdr (nthcdr i info) nil)))))))
4483
4484 (defun gnus-set-mode-line (where)
4485   "Set the mode line of the article or summary buffers.
4486 If WHERE is `summary', the summary mode line format will be used."
4487   ;; Is this mode line one we keep updated?
4488   (when (and (memq where gnus-updated-mode-lines)
4489              (symbol-value
4490               (intern (format "gnus-%s-mode-line-format-spec" where))))
4491     (let (mode-string)
4492       (save-excursion
4493         ;; We evaluate this in the summary buffer since these
4494         ;; variables are buffer-local to that buffer.
4495         (set-buffer gnus-summary-buffer)
4496         ;; We bind all these variables that are used in the `eval' form
4497         ;; below.
4498         (let* ((mformat (symbol-value
4499                          (intern
4500                           (format "gnus-%s-mode-line-format-spec" where))))
4501                (gnus-tmp-group-name (gnus-group-name-decode 
4502                                      gnus-newsgroup-name
4503                                      (gnus-group-name-charset 
4504                                       nil
4505                                       gnus-newsgroup-name)))
4506                (gnus-tmp-article-number (or gnus-current-article 0))
4507                (gnus-tmp-unread gnus-newsgroup-unreads)
4508                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4509                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4510                (gnus-tmp-unread-and-unselected
4511                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4512                             (zerop gnus-tmp-unselected))
4513                        "")
4514                       ((zerop gnus-tmp-unselected)
4515                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4516                       (t (format "{%d(+%d) more}"
4517                                  gnus-tmp-unread-and-unticked
4518                                  gnus-tmp-unselected))))
4519                (gnus-tmp-subject
4520                 (if (and gnus-current-headers
4521                          (vectorp gnus-current-headers))
4522                     (gnus-mode-string-quote
4523                      (mail-header-subject gnus-current-headers))
4524                   ""))
4525                bufname-length max-len
4526                gnus-tmp-header);; passed as argument to any user-format-funcs
4527           (setq mode-string (eval mformat))
4528           (setq bufname-length (if (string-match "%b" mode-string)
4529                                    (- (length
4530                                        (buffer-name
4531                                         (if (eq where 'summary)
4532                                             nil
4533                                           (get-buffer gnus-article-buffer))))
4534                                       2)
4535                                  0))
4536           (setq max-len (max 4 (if gnus-mode-non-string-length
4537                                    (- (window-width)
4538                                       gnus-mode-non-string-length
4539                                       bufname-length)
4540                                  (length mode-string))))
4541           ;; We might have to chop a bit of the string off...
4542           (when (> (length mode-string) max-len)
4543             (setq mode-string
4544                   (concat (truncate-string-to-width mode-string (- max-len 3))
4545                           "...")))
4546           ;; Pad the mode string a bit.
4547           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4548       ;; Update the mode line.
4549       (setq mode-line-buffer-identification
4550             (gnus-mode-line-buffer-identification (list mode-string)))
4551       (set-buffer-modified-p t))))
4552
4553 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4554   "Go through the HEADERS list and add all Xrefs to a hash table.
4555 The resulting hash table is returned, or nil if no Xrefs were found."
4556   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4557          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4558          (xref-hashtb (gnus-make-hashtable))
4559          start group entry number xrefs header)
4560     (while headers
4561       (setq header (pop headers))
4562       (when (and (setq xrefs (mail-header-xref header))
4563                  (not (memq (setq number (mail-header-number header))
4564                             unreads)))
4565         (setq start 0)
4566         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4567           (setq start (match-end 0))
4568           (setq group (if prefix
4569                           (concat prefix (substring xrefs (match-beginning 1)
4570                                                     (match-end 1)))
4571                         (substring xrefs (match-beginning 1) (match-end 1))))
4572           (setq number
4573                 (string-to-int (substring xrefs (match-beginning 2)
4574                                           (match-end 2))))
4575           (if (setq entry (gnus-gethash group xref-hashtb))
4576               (setcdr entry (cons number (cdr entry)))
4577             (gnus-sethash group (cons number nil) xref-hashtb)))))
4578     (and start xref-hashtb)))
4579
4580 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4581   "Look through all the headers and mark the Xrefs as read."
4582   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4583         name entry info xref-hashtb idlist method nth4)
4584     (save-excursion
4585       (set-buffer gnus-group-buffer)
4586       (when (setq xref-hashtb
4587                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4588         (mapatoms
4589          (lambda (group)
4590            (unless (string= from-newsgroup (setq name (symbol-name group)))
4591              (setq idlist (symbol-value group))
4592              ;; Dead groups are not updated.
4593              (and (prog1
4594                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4595                             info (nth 2 entry))
4596                     (when (stringp (setq nth4 (gnus-info-method info)))
4597                       (setq nth4 (gnus-server-to-method nth4))))
4598                   ;; Only do the xrefs if the group has the same
4599                   ;; select method as the group we have just read.
4600                   (or (gnus-methods-equal-p
4601                        nth4 (gnus-find-method-for-group from-newsgroup))
4602                       virtual
4603                       (equal nth4 (setq method (gnus-find-method-for-group
4604                                                 from-newsgroup)))
4605                       (and (equal (car nth4) (car method))
4606                            (equal (nth 1 nth4) (nth 1 method))))
4607                   gnus-use-cross-reference
4608                   (or (not (eq gnus-use-cross-reference t))
4609                       virtual
4610                       ;; Only do cross-references on subscribed
4611                       ;; groups, if that is what is wanted.
4612                       (<= (gnus-info-level info) gnus-level-subscribed))
4613                   (gnus-group-make-articles-read name idlist))))
4614          xref-hashtb)))))
4615
4616 (defun gnus-compute-read-articles (group articles)
4617   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4618          (info (nth 2 entry))
4619          (active (gnus-active group))
4620          ninfo)
4621     (when entry
4622       ;; First peel off all invalid article numbers.
4623       (when active
4624         (let ((ids articles)
4625               id first)
4626           (while (setq id (pop ids))
4627             (when (and first (> id (cdr active)))
4628               ;; We'll end up in this situation in one particular
4629               ;; obscure situation.  If you re-scan a group and get
4630               ;; a new article that is cross-posted to a different
4631               ;; group that has not been re-scanned, you might get
4632               ;; crossposted article that has a higher number than
4633               ;; Gnus believes possible.  So we re-activate this
4634               ;; group as well.  This might mean doing the
4635               ;; crossposting thingy will *increase* the number
4636               ;; of articles in some groups.  Tsk, tsk.
4637               (setq active (or (gnus-activate-group group) active)))
4638             (when (or (> id (cdr active))
4639                       (< id (car active)))
4640               (setq articles (delq id articles))))))
4641       ;; If the read list is nil, we init it.
4642       (if (and active
4643                (null (gnus-info-read info))
4644                (> (car active) 1))
4645           (setq ninfo (cons 1 (1- (car active))))
4646         (setq ninfo (gnus-info-read info)))
4647       ;; Then we add the read articles to the range.
4648       (gnus-add-to-range
4649        ninfo (setq articles (sort articles '<))))))
4650
4651 (defun gnus-group-make-articles-read (group articles)
4652   "Update the info of GROUP to say that ARTICLES are read."
4653   (let* ((num 0)
4654          (entry (gnus-gethash group gnus-newsrc-hashtb))
4655          (info (nth 2 entry))
4656          (active (gnus-active group))
4657          range)
4658     (when entry
4659       (setq range (gnus-compute-read-articles group articles))
4660       (save-excursion
4661         (set-buffer gnus-group-buffer)
4662         (gnus-undo-register
4663           `(progn
4664              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4665              (gnus-info-set-read ',info ',(gnus-info-read info))
4666              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4667              (gnus-group-update-group ,group t))))
4668       ;; Add the read articles to the range.
4669       (gnus-info-set-read info range)
4670       ;; Then we have to re-compute how many unread
4671       ;; articles there are in this group.
4672       (when active
4673         (cond
4674          ((not range)
4675           (setq num (- (1+ (cdr active)) (car active))))
4676          ((not (listp (cdr range)))
4677           (setq num (- (cdr active) (- (1+ (cdr range))
4678                                        (car range)))))
4679          (t
4680           (while range
4681             (if (numberp (car range))
4682                 (setq num (1+ num))
4683               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4684             (setq range (cdr range)))
4685           (setq num (- (cdr active) num))))
4686         ;; Update the number of unread articles.
4687         (setcar entry num)
4688         ;; Update the group buffer.
4689         (gnus-group-update-group group t)))))
4690
4691 (defvar gnus-newsgroup-none-id 0)
4692
4693 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4694   (let ((cur nntp-server-buffer)
4695         (dependencies
4696          (or dependencies
4697              (save-excursion (set-buffer gnus-summary-buffer)
4698                              gnus-newsgroup-dependencies)))
4699         headers id end ref
4700         (mail-parse-charset gnus-newsgroup-charset)
4701         (mail-parse-ignored-charsets
4702          (save-excursion (condition-case nil
4703                              (set-buffer gnus-summary-buffer)
4704                            (error))
4705                          gnus-newsgroup-ignored-charsets)))
4706     (save-excursion
4707       (set-buffer nntp-server-buffer)
4708       ;; Translate all TAB characters into SPACE characters.
4709       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4710       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4711       (gnus-run-hooks 'gnus-parse-headers-hook)
4712       (let ((case-fold-search t)
4713             in-reply-to header p lines chars)
4714         (goto-char (point-min))
4715         ;; Search to the beginning of the next header.  Error messages
4716         ;; do not begin with 2 or 3.
4717         (while (re-search-forward "^[23][0-9]+ " nil t)
4718           (setq id nil
4719                 ref nil)
4720           ;; This implementation of this function, with nine
4721           ;; search-forwards instead of the one re-search-forward and
4722           ;; a case (which basically was the old function) is actually
4723           ;; about twice as fast, even though it looks messier.  You
4724           ;; can't have everything, I guess.  Speed and elegance
4725           ;; doesn't always go hand in hand.
4726           (setq
4727            header
4728            (vector
4729             ;; Number.
4730             (prog1
4731                 (read cur)
4732               (end-of-line)
4733               (setq p (point))
4734               (narrow-to-region (point)
4735                                 (or (and (search-forward "\n.\n" nil t)
4736                                          (- (point) 2))
4737                                     (point))))
4738             ;; Subject.
4739             (progn
4740               (goto-char p)
4741               (if (search-forward "\nsubject: " nil t)
4742                   (funcall gnus-decode-encoded-word-function
4743                            (nnheader-header-value))
4744                 "(none)"))
4745             ;; From.
4746             (progn
4747               (goto-char p)
4748               (if (or (search-forward "\nfrom: " nil t)
4749                       (search-forward "\nfrom:" nil t))
4750                   (funcall gnus-decode-encoded-word-function
4751                            (nnheader-header-value))
4752                 "(nobody)"))
4753             ;; Date.
4754             (progn
4755               (goto-char p)
4756               (if (search-forward "\ndate: " nil t)
4757                   (nnheader-header-value) ""))
4758             ;; Message-ID.
4759             (progn
4760               (goto-char p)
4761               (setq id (if (re-search-forward
4762                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4763                            ;; We do it this way to make sure the Message-ID
4764                            ;; is (somewhat) syntactically valid.
4765                            (buffer-substring (match-beginning 1)
4766                                              (match-end 1))
4767                          ;; If there was no message-id, we just fake one
4768                          ;; to make subsequent routines simpler.
4769                          (nnheader-generate-fake-message-id))))
4770             ;; References.
4771             (progn
4772               (goto-char p)
4773               (if (search-forward "\nreferences: " nil t)
4774                   (progn
4775                     (setq end (point))
4776                     (prog1
4777                         (nnheader-header-value)
4778                       (setq ref
4779                             (buffer-substring
4780                              (progn
4781                                (end-of-line)
4782                                (search-backward ">" end t)
4783                                (1+ (point)))
4784                              (progn
4785                                (search-backward "<" end t)
4786                                (point))))))
4787                 ;; Get the references from the in-reply-to header if there
4788                 ;; were no references and the in-reply-to header looks
4789                 ;; promising.
4790                 (if (and (search-forward "\nin-reply-to: " nil t)
4791                          (setq in-reply-to (nnheader-header-value))
4792                          (string-match "<[^>]+>" in-reply-to))
4793                     (let (ref2)
4794                       (setq ref (substring in-reply-to (match-beginning 0)
4795                                            (match-end 0)))
4796                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4797                         (setq ref2 (substring in-reply-to (match-beginning 0)
4798                                               (match-end 0)))
4799                         (when (> (length ref2) (length ref))
4800                           (setq ref ref2)))
4801                       ref)
4802                   (setq ref nil))))
4803             ;; Chars.
4804             (progn
4805               (goto-char p)
4806               (if (search-forward "\nchars: " nil t)
4807                   (if (numberp (setq chars (ignore-errors (read cur))))
4808                       chars 0)
4809                 0))
4810             ;; Lines.
4811             (progn
4812               (goto-char p)
4813               (if (search-forward "\nlines: " nil t)
4814                   (if (numberp (setq lines (ignore-errors (read cur))))
4815                       lines 0)
4816                 0))
4817             ;; Xref.
4818             (progn
4819               (goto-char p)
4820               (and (search-forward "\nxref: " nil t)
4821                    (nnheader-header-value)))
4822             ;; Extra.
4823             (when gnus-extra-headers
4824               (let ((extra gnus-extra-headers)
4825                     out)
4826                 (while extra
4827                   (goto-char p)
4828                   (when (search-forward
4829                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4830                     (push (cons (car extra) (nnheader-header-value))
4831                           out))
4832                   (pop extra))
4833                 out))))
4834           (when (equal id ref)
4835             (setq ref nil))
4836
4837           (when gnus-alter-header-function
4838             (funcall gnus-alter-header-function header)
4839             (setq id (mail-header-id header)
4840                   ref (gnus-parent-id (mail-header-references header))))
4841
4842           (when (setq header
4843                       (gnus-dependencies-add-header
4844                        header dependencies force-new))
4845             (push header headers))
4846           (goto-char (point-max))
4847           (widen))
4848         (nreverse headers)))))
4849
4850 ;; Goes through the xover lines and returns a list of vectors
4851 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4852                                                   force-new dependencies
4853                                                   group also-fetch-heads)
4854   "Parse the news overview data in the server buffer.
4855 Return a list of headers that match SEQUENCE (see
4856 `nntp-retrieve-headers')."
4857   ;; Get the Xref when the users reads the articles since most/some
4858   ;; NNTP servers do not include Xrefs when using XOVER.
4859   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4860   (let ((mail-parse-charset gnus-newsgroup-charset)
4861         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4862         (cur nntp-server-buffer)
4863         (dependencies (or dependencies gnus-newsgroup-dependencies))
4864         number headers header)
4865     (save-excursion
4866       (set-buffer nntp-server-buffer)
4867       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4868       ;; Allow the user to mangle the headers before parsing them.
4869       (gnus-run-hooks 'gnus-parse-headers-hook)
4870       (goto-char (point-min))
4871       (while (not (eobp))
4872         (condition-case ()
4873             (while (and sequence (not (eobp)))
4874               (setq number (read cur))
4875               (while (and sequence
4876                           (< (car sequence) number))
4877                 (setq sequence (cdr sequence)))
4878               (and sequence
4879                    (eq number (car sequence))
4880                    (progn
4881                      (setq sequence (cdr sequence))
4882                      (setq header (inline
4883                                     (gnus-nov-parse-line
4884                                      number dependencies force-new))))
4885                    (push header headers))
4886               (forward-line 1))
4887           (error
4888            (gnus-error 4 "Strange nov line (%d)"
4889                        (count-lines (point-min) (point)))))
4890         (forward-line 1))
4891       ;; A common bug in inn is that if you have posted an article and
4892       ;; then retrieves the active file, it will answer correctly --
4893       ;; the new article is included.  However, a NOV entry for the
4894       ;; article may not have been generated yet, so this may fail.
4895       ;; We work around this problem by retrieving the last few
4896       ;; headers using HEAD.
4897       (if (or (not also-fetch-heads)
4898               (not sequence))
4899           ;; We (probably) got all the headers.
4900           (nreverse headers)
4901         (let ((gnus-nov-is-evil t))
4902           (nconc
4903            (nreverse headers)
4904            (when (gnus-retrieve-headers sequence group)
4905              (gnus-get-newsgroup-headers))))))))
4906
4907 (defun gnus-article-get-xrefs ()
4908   "Fill in the Xref value in `gnus-current-headers', if necessary.
4909 This is meant to be called in `gnus-article-internal-prepare-hook'."
4910   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4911                                  gnus-current-headers)))
4912     (or (not gnus-use-cross-reference)
4913         (not headers)
4914         (and (mail-header-xref headers)
4915              (not (string= (mail-header-xref headers) "")))
4916         (let ((case-fold-search t)
4917               xref)
4918           (save-restriction
4919             (nnheader-narrow-to-headers)
4920             (goto-char (point-min))
4921             (when (or (and (not (eobp))
4922                            (eq (downcase (char-after)) ?x)
4923                            (looking-at "Xref:"))
4924                       (search-forward "\nXref:" nil t))
4925               (goto-char (1+ (match-end 0)))
4926               (setq xref (buffer-substring (point)
4927                                            (progn (end-of-line) (point))))
4928               (mail-header-set-xref headers xref)))))))
4929
4930 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4931   "Find article ID and insert the summary line for that article.
4932 OLD-HEADER can either be a header or a line number to insert
4933 the subject line on."
4934   (let* ((line (and (numberp old-header) old-header))
4935          (old-header (and (vectorp old-header) old-header))
4936          (header (cond ((and old-header use-old-header)
4937                         old-header)
4938                        ((and (numberp id)
4939                              (gnus-number-to-header id))
4940                         (gnus-number-to-header id))
4941                        (t
4942                         (gnus-read-header id))))
4943          (number (and (numberp id) id))
4944          d)
4945     (when header
4946       ;; Rebuild the thread that this article is part of and go to the
4947       ;; article we have fetched.
4948       (when (and (not gnus-show-threads)
4949                  old-header)
4950         (when (and number
4951                    (setq d (gnus-data-find (mail-header-number old-header))))
4952           (goto-char (gnus-data-pos d))
4953           (gnus-data-remove
4954            number
4955            (- (gnus-point-at-bol)
4956               (prog1
4957                   (1+ (gnus-point-at-eol))
4958                 (gnus-delete-line))))))
4959       (when old-header
4960         (mail-header-set-number header (mail-header-number old-header)))
4961       (setq gnus-newsgroup-sparse
4962             (delq (setq number (mail-header-number header))
4963                   gnus-newsgroup-sparse))
4964       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4965       (push number gnus-newsgroup-limit)
4966       (gnus-rebuild-thread (mail-header-id header) line)
4967       (gnus-summary-goto-subject number nil t))
4968     (when (and (numberp number)
4969                (> number 0))
4970       ;; We have to update the boundaries even if we can't fetch the
4971       ;; article if ID is a number -- so that the next `P' or `N'
4972       ;; command will fetch the previous (or next) article even
4973       ;; if the one we tried to fetch this time has been canceled.
4974       (when (> number gnus-newsgroup-end)
4975         (setq gnus-newsgroup-end number))
4976       (when (< number gnus-newsgroup-begin)
4977         (setq gnus-newsgroup-begin number))
4978       (setq gnus-newsgroup-unselected
4979             (delq number gnus-newsgroup-unselected)))
4980     ;; Report back a success?
4981     (and header (mail-header-number header))))
4982
4983 ;;; Process/prefix in the summary buffer
4984
4985 (defun gnus-summary-work-articles (n)
4986   "Return a list of articles to be worked upon.
4987 The prefix argument, the list of process marked articles, and the
4988 current article will be taken into consideration."
4989   (save-excursion
4990     (set-buffer gnus-summary-buffer)
4991     (cond
4992      (n
4993       ;; A numerical prefix has been given.
4994       (setq n (prefix-numeric-value n))
4995       (let ((backward (< n 0))
4996             (n (abs (prefix-numeric-value n)))
4997             articles article)
4998         (save-excursion
4999           (while
5000               (and (> n 0)
5001                    (push (setq article (gnus-summary-article-number))
5002                          articles)
5003                    (if backward
5004                        (gnus-summary-find-prev nil article)
5005                      (gnus-summary-find-next nil article)))
5006             (decf n)))
5007         (nreverse articles)))
5008      ((and (gnus-region-active-p) (mark))
5009       (message "region active")
5010       ;; Work on the region between point and mark.
5011       (let ((max (max (point) (mark)))
5012             articles article)
5013         (save-excursion
5014           (goto-char (min (min (point) (mark))))
5015           (while
5016               (and
5017                (push (setq article (gnus-summary-article-number)) articles)
5018                (gnus-summary-find-next nil article)
5019                (< (point) max)))
5020           (nreverse articles))))
5021      (gnus-newsgroup-processable
5022       ;; There are process-marked articles present.
5023       ;; Save current state.
5024       (gnus-summary-save-process-mark)
5025       ;; Return the list.
5026       (reverse gnus-newsgroup-processable))
5027      (t
5028       ;; Just return the current article.
5029       (list (gnus-summary-article-number))))))
5030
5031 (defmacro gnus-summary-iterate (arg &rest forms)
5032   "Iterate over the process/prefixed articles and do FORMS.
5033 ARG is the interactive prefix given to the command.  FORMS will be
5034 executed with point over the summary line of the articles."
5035   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5036     `(let ((,articles (gnus-summary-work-articles ,arg)))
5037        (while ,articles
5038          (gnus-summary-goto-subject (car ,articles))
5039          ,@forms
5040          (pop ,articles)))))
5041
5042 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5043 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5044
5045 (defun gnus-summary-save-process-mark ()
5046   "Push the current set of process marked articles on the stack."
5047   (interactive)
5048   (push (copy-sequence gnus-newsgroup-processable)
5049         gnus-newsgroup-process-stack))
5050
5051 (defun gnus-summary-kill-process-mark ()
5052   "Push the current set of process marked articles on the stack and unmark."
5053   (interactive)
5054   (gnus-summary-save-process-mark)
5055   (gnus-summary-unmark-all-processable))
5056
5057 (defun gnus-summary-yank-process-mark ()
5058   "Pop the last process mark state off the stack and restore it."
5059   (interactive)
5060   (unless gnus-newsgroup-process-stack
5061     (error "Empty mark stack"))
5062   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5063
5064 (defun gnus-summary-process-mark-set (set)
5065   "Make SET into the current process marked articles."
5066   (gnus-summary-unmark-all-processable)
5067   (while set
5068     (gnus-summary-set-process-mark (pop set))))
5069
5070 ;;; Searching and stuff
5071
5072 (defun gnus-summary-search-group (&optional backward use-level)
5073   "Search for next unread newsgroup.
5074 If optional argument BACKWARD is non-nil, search backward instead."
5075   (save-excursion
5076     (set-buffer gnus-group-buffer)
5077     (when (gnus-group-search-forward
5078            backward nil (if use-level (gnus-group-group-level) nil))
5079       (gnus-group-group-name))))
5080
5081 (defun gnus-summary-best-group (&optional exclude-group)
5082   "Find the name of the best unread group.
5083 If EXCLUDE-GROUP, do not go to this group."
5084   (save-excursion
5085     (set-buffer gnus-group-buffer)
5086     (save-excursion
5087       (gnus-group-best-unread-group exclude-group))))
5088
5089 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5090   (if backward (gnus-summary-find-prev)
5091     (let* ((dummy (gnus-summary-article-intangible-p))
5092            (article (or article (gnus-summary-article-number)))
5093            (arts (gnus-data-find-list article))
5094            result)
5095       (when (and (not dummy)
5096                  (or (not gnus-summary-check-current)
5097                      (not unread)
5098                      (not (gnus-data-unread-p (car arts)))))
5099         (setq arts (cdr arts)))
5100       (when (setq result
5101                   (if unread
5102                       (progn
5103                         (while arts
5104                           (when (or (and undownloaded
5105                                          (eq gnus-undownloaded-mark
5106                                              (gnus-data-mark (car arts))))
5107                                     (gnus-data-unread-p (car arts)))
5108                             (setq result (car arts)
5109                                   arts nil))
5110                           (setq arts (cdr arts)))
5111                         result)
5112                     (car arts)))
5113         (goto-char (gnus-data-pos result))
5114         (gnus-data-number result)))))
5115
5116 (defun gnus-summary-find-prev (&optional unread article)
5117   (let* ((eobp (eobp))
5118          (article (or article (gnus-summary-article-number)))
5119          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5120          result)
5121     (when (and (not eobp)
5122                (or (not gnus-summary-check-current)
5123                    (not unread)
5124                    (not (gnus-data-unread-p (car arts)))))
5125       (setq arts (cdr arts)))
5126     (when (setq result
5127                 (if unread
5128                     (progn
5129                       (while arts
5130                         (when (gnus-data-unread-p (car arts))
5131                           (setq result (car arts)
5132                                 arts nil))
5133                         (setq arts (cdr arts)))
5134                       result)
5135                   (car arts)))
5136       (goto-char (gnus-data-pos result))
5137       (gnus-data-number result))))
5138
5139 (defun gnus-summary-find-subject (subject &optional unread backward article)
5140   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5141          (article (or article (gnus-summary-article-number)))
5142          (articles (gnus-data-list backward))
5143          (arts (gnus-data-find-list article articles))
5144          result)
5145     (when (or (not gnus-summary-check-current)
5146               (not unread)
5147               (not (gnus-data-unread-p (car arts))))
5148       (setq arts (cdr arts)))
5149     (while arts
5150       (and (or (not unread)
5151                (gnus-data-unread-p (car arts)))
5152            (vectorp (gnus-data-header (car arts)))
5153            (gnus-subject-equal
5154             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5155            (setq result (car arts)
5156                  arts nil))
5157       (setq arts (cdr arts)))
5158     (and result
5159          (goto-char (gnus-data-pos result))
5160          (gnus-data-number result))))
5161
5162 (defun gnus-summary-search-forward (&optional unread subject backward)
5163   "Search forward for an article.
5164 If UNREAD, look for unread articles.  If SUBJECT, look for
5165 articles with that subject.  If BACKWARD, search backward instead."
5166   (cond (subject (gnus-summary-find-subject subject unread backward))
5167         (backward (gnus-summary-find-prev unread))
5168         (t (gnus-summary-find-next unread))))
5169
5170 (defun gnus-recenter (&optional n)
5171   "Center point in window and redisplay frame.
5172 Also do horizontal recentering."
5173   (interactive "P")
5174   (when (and gnus-auto-center-summary
5175              (not (eq gnus-auto-center-summary 'vertical)))
5176     (gnus-horizontal-recenter))
5177   (recenter n))
5178
5179 (defun gnus-summary-recenter ()
5180   "Center point in the summary window.
5181 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5182 displayed, no centering will be performed."
5183   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5184   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5185   (interactive)
5186   (let* ((top (cond ((< (window-height) 4) 0)
5187                     ((< (window-height) 7) 1)
5188                     (t (if (numberp gnus-auto-center-summary)
5189                            gnus-auto-center-summary
5190                          2))))
5191          (height (1- (window-height)))
5192          (bottom (save-excursion (goto-char (point-max))
5193                                  (forward-line (- height))
5194                                  (point)))
5195          (window (get-buffer-window (current-buffer))))
5196     ;; The user has to want it.
5197     (when gnus-auto-center-summary
5198       (when (get-buffer-window gnus-article-buffer)
5199         ;; Only do recentering when the article buffer is displayed,
5200         ;; Set the window start to either `bottom', which is the biggest
5201         ;; possible valid number, or the second line from the top,
5202         ;; whichever is the least.
5203         (set-window-start
5204          window (min bottom (save-excursion
5205                               (forward-line (- top)) (point)))
5206          t))
5207       ;; Do horizontal recentering while we're at it.
5208       (when (and (get-buffer-window (current-buffer) t)
5209                  (not (eq gnus-auto-center-summary 'vertical)))
5210         (let ((selected (selected-window)))
5211           (select-window (get-buffer-window (current-buffer) t))
5212           (gnus-summary-position-point)
5213           (gnus-horizontal-recenter)
5214           (select-window selected))))))
5215
5216 (defun gnus-summary-jump-to-group (newsgroup)
5217   "Move point to NEWSGROUP in group mode buffer."
5218   ;; Keep update point of group mode buffer if visible.
5219   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5220       (save-window-excursion
5221         ;; Take care of tree window mode.
5222         (when (get-buffer-window gnus-group-buffer)
5223           (pop-to-buffer gnus-group-buffer))
5224         (gnus-group-jump-to-group newsgroup))
5225     (save-excursion
5226       ;; Take care of tree window mode.
5227       (if (get-buffer-window gnus-group-buffer)
5228           (pop-to-buffer gnus-group-buffer)
5229         (set-buffer gnus-group-buffer))
5230       (gnus-group-jump-to-group newsgroup))))
5231
5232 ;; This function returns a list of article numbers based on the
5233 ;; difference between the ranges of read articles in this group and
5234 ;; the range of active articles.
5235 (defun gnus-list-of-unread-articles (group)
5236   (let* ((read (gnus-info-read (gnus-get-info group)))
5237          (active (or (gnus-active group) (gnus-activate-group group)))
5238          (last (cdr active))
5239          first nlast unread)
5240     ;; If none are read, then all are unread.
5241     (if (not read)
5242         (setq first (car active))
5243       ;; If the range of read articles is a single range, then the
5244       ;; first unread article is the article after the last read
5245       ;; article.  Sounds logical, doesn't it?
5246       (if (and (not (listp (cdr read)))
5247                (or (< (car read) (car active))
5248                    (progn (setq read (list read))
5249                           nil)))
5250           (setq first (max (car active) (1+ (cdr read))))
5251         ;; `read' is a list of ranges.
5252         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5253                                   (caar read)))
5254                   1)
5255           (setq first (car active)))
5256         (while read
5257           (when first
5258             (while (< first nlast)
5259               (push first unread)
5260               (setq first (1+ first))))
5261           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5262           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5263           (setq read (cdr read)))))
5264     ;; And add the last unread articles.
5265     (while (<= first last)
5266       (push first unread)
5267       (setq first (1+ first)))
5268     ;; Return the list of unread articles.
5269     (delq 0 (nreverse unread))))
5270
5271 (defun gnus-list-of-read-articles (group)
5272   "Return a list of unread, unticked and non-dormant articles."
5273   (let* ((info (gnus-get-info group))
5274          (marked (gnus-info-marks info))
5275          (active (gnus-active group)))
5276     (and info active
5277          (gnus-set-difference
5278           (gnus-sorted-complement
5279            (gnus-uncompress-range active)
5280            (gnus-list-of-unread-articles group))
5281           (append
5282            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5283            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5284
5285 ;; Various summary commands
5286
5287 (defun gnus-summary-select-article-buffer ()
5288   "Reconfigure windows to show article buffer."
5289   (interactive)
5290   (if (not (gnus-buffer-live-p gnus-article-buffer))
5291       (error "There is no article buffer for this summary buffer")
5292     (gnus-configure-windows 'article)
5293     (select-window (get-buffer-window gnus-article-buffer))))
5294
5295 (defun gnus-summary-universal-argument (arg)
5296   "Perform any operation on all articles that are process/prefixed."
5297   (interactive "P")
5298   (let ((articles (gnus-summary-work-articles arg))
5299         func article)
5300     (if (eq
5301          (setq
5302           func
5303           (key-binding
5304            (read-key-sequence
5305             (substitute-command-keys
5306              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5307          'undefined)
5308         (gnus-error 1 "Undefined key")
5309       (save-excursion
5310         (while articles
5311           (gnus-summary-goto-subject (setq article (pop articles)))
5312           (let (gnus-newsgroup-processable)
5313             (command-execute func))
5314           (gnus-summary-remove-process-mark article)))))
5315   (gnus-summary-position-point))
5316
5317 (defun gnus-summary-toggle-truncation (&optional arg)
5318   "Toggle truncation of summary lines.
5319 With arg, turn line truncation on iff arg is positive."
5320   (interactive "P")
5321   (setq truncate-lines
5322         (if (null arg) (not truncate-lines)
5323           (> (prefix-numeric-value arg) 0)))
5324   (redraw-display))
5325
5326 (defun gnus-summary-reselect-current-group (&optional all rescan)
5327   "Exit and then reselect the current newsgroup.
5328 The prefix argument ALL means to select all articles."
5329   (interactive "P")
5330   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5331     (error "Ephemeral groups can't be reselected"))
5332   (let ((current-subject (gnus-summary-article-number))
5333         (group gnus-newsgroup-name))
5334     (setq gnus-newsgroup-begin nil)
5335     (gnus-summary-exit)
5336     ;; We have to adjust the point of group mode buffer because
5337     ;; point was moved to the next unread newsgroup by exiting.
5338     (gnus-summary-jump-to-group group)
5339     (when rescan
5340       (save-excursion
5341         (gnus-group-get-new-news-this-group 1)))
5342     (gnus-group-read-group all t)
5343     (gnus-summary-goto-subject current-subject nil t)))
5344
5345 (defun gnus-summary-rescan-group (&optional all)
5346   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5347   (interactive "P")
5348   (gnus-summary-reselect-current-group all t))
5349
5350 (defun gnus-summary-update-info (&optional non-destructive)
5351   (save-excursion
5352     (let ((group gnus-newsgroup-name))
5353       (when group
5354         (when gnus-newsgroup-kill-headers
5355           (setq gnus-newsgroup-killed
5356                 (gnus-compress-sequence
5357                  (nconc
5358                   (gnus-set-sorted-intersection
5359                    (gnus-uncompress-range gnus-newsgroup-killed)
5360                    (setq gnus-newsgroup-unselected
5361                          (sort gnus-newsgroup-unselected '<)))
5362                   (setq gnus-newsgroup-unreads
5363                         (sort gnus-newsgroup-unreads '<)))
5364                  t)))
5365         (unless (listp (cdr gnus-newsgroup-killed))
5366           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5367         (let ((headers gnus-newsgroup-headers))
5368           ;; Set the new ranges of read articles.
5369           (save-excursion
5370             (set-buffer gnus-group-buffer)
5371             (gnus-undo-force-boundary))
5372           (gnus-update-read-articles
5373            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5374           ;; Set the current article marks.
5375           (let ((gnus-newsgroup-scored
5376                  (if (and (not gnus-save-score)
5377                           (not non-destructive))
5378                      nil
5379                    gnus-newsgroup-scored)))
5380             (save-excursion
5381               (gnus-update-marks)))
5382           ;; Do the cross-ref thing.
5383           (when gnus-use-cross-reference
5384             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5385           ;; Do not switch windows but change the buffer to work.
5386           (set-buffer gnus-group-buffer)
5387           (unless (gnus-ephemeral-group-p group)
5388             (gnus-group-update-group group)))))))
5389
5390 (defun gnus-summary-save-newsrc (&optional force)
5391   "Save the current number of read/marked articles in the dribble buffer.
5392 The dribble buffer will then be saved.
5393 If FORCE (the prefix), also save the .newsrc file(s)."
5394   (interactive "P")
5395   (gnus-summary-update-info t)
5396   (if force
5397       (gnus-save-newsrc-file)
5398     (gnus-dribble-save)))
5399
5400 (defun gnus-summary-exit (&optional temporary)
5401   "Exit reading current newsgroup, and then return to group selection mode.
5402 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5403   (interactive)
5404   (gnus-set-global-variables)
5405   (when (gnus-buffer-live-p gnus-article-buffer)
5406     (save-excursion
5407       (set-buffer gnus-article-buffer)
5408       (mm-destroy-parts gnus-article-mime-handles)
5409       ;; Set it to nil for safety reason.
5410       (setq gnus-article-mime-handle-alist nil)
5411       (setq gnus-article-mime-handles nil)))
5412   (gnus-kill-save-kill-buffer)
5413   (gnus-async-halt-prefetch)
5414   (let* ((group gnus-newsgroup-name)
5415          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5416          (mode major-mode)
5417          (group-point nil)
5418          (buf (current-buffer)))
5419     (unless quit-config
5420       ;; Do adaptive scoring, and possibly save score files.
5421       (when gnus-newsgroup-adaptive
5422         (gnus-score-adaptive))
5423       (when gnus-use-scoring
5424         (gnus-score-save)))
5425     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5426     ;; If we have several article buffers, we kill them at exit.
5427     (unless gnus-single-article-buffer
5428       (gnus-kill-buffer gnus-original-article-buffer)
5429       (setq gnus-article-current nil))
5430     (when gnus-use-cache
5431       (gnus-cache-possibly-remove-articles)
5432       (gnus-cache-save-buffers))
5433     (gnus-async-prefetch-remove-group group)
5434     (when gnus-suppress-duplicates
5435       (gnus-dup-enter-articles))
5436     (when gnus-use-trees
5437       (gnus-tree-close group))
5438     (when gnus-use-cache
5439       (gnus-cache-write-active))
5440     ;; Remove entries for this group.
5441     (nnmail-purge-split-history (gnus-group-real-name group))
5442     ;; Make all changes in this group permanent.
5443     (unless quit-config
5444       (gnus-run-hooks 'gnus-exit-group-hook)
5445       (gnus-summary-update-info))
5446     (gnus-close-group group)
5447     ;; Make sure where we were, and go to next newsgroup.
5448     (set-buffer gnus-group-buffer)
5449     (unless quit-config
5450       (gnus-group-jump-to-group group))
5451     (gnus-run-hooks 'gnus-summary-exit-hook)
5452     (unless (or quit-config
5453                 ;; If this group has disappeared from the summary
5454                 ;; buffer, don't skip forwards.
5455                 (not (string= group (gnus-group-group-name))))
5456       (gnus-group-next-unread-group 1))
5457     (setq group-point (point))
5458     (if temporary
5459         nil                             ;Nothing to do.
5460       ;; If we have several article buffers, we kill them at exit.
5461       (unless gnus-single-article-buffer
5462         (gnus-kill-buffer gnus-article-buffer)
5463         (gnus-kill-buffer gnus-original-article-buffer)
5464         (setq gnus-article-current nil))
5465       (set-buffer buf)
5466       (if (not gnus-kill-summary-on-exit)
5467           (gnus-deaden-summary)
5468         ;; We set all buffer-local variables to nil.  It is unclear why
5469         ;; this is needed, but if we don't, buffer-local variables are
5470         ;; not garbage-collected, it seems.  This would the lead to en
5471         ;; ever-growing Emacs.
5472         (gnus-summary-clear-local-variables)
5473         (when (get-buffer gnus-article-buffer)
5474           (bury-buffer gnus-article-buffer))
5475         ;; We clear the global counterparts of the buffer-local
5476         ;; variables as well, just to be on the safe side.
5477         (set-buffer gnus-group-buffer)
5478         (gnus-summary-clear-local-variables)
5479         ;; Return to group mode buffer.
5480         (when (eq mode 'gnus-summary-mode)
5481           (gnus-kill-buffer buf)))
5482       (setq gnus-current-select-method gnus-select-method)
5483       (pop-to-buffer gnus-group-buffer)
5484       (if (not quit-config)
5485           (progn
5486             (goto-char group-point)
5487             (gnus-configure-windows 'group 'force))
5488         (gnus-handle-ephemeral-exit quit-config))
5489       ;; Clear the current group name.
5490       (unless quit-config
5491         (setq gnus-newsgroup-name nil)))))
5492
5493 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5494 (defun gnus-summary-exit-no-update (&optional no-questions)
5495   "Quit reading current newsgroup without updating read article info."
5496   (interactive)
5497   (let* ((group gnus-newsgroup-name)
5498          (quit-config (gnus-group-quit-config group)))
5499     (when (or no-questions
5500               gnus-expert-user
5501               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5502       (gnus-async-halt-prefetch)
5503       (mapcar 'funcall
5504               (delq 'gnus-summary-expire-articles
5505                     (copy-sequence gnus-summary-prepare-exit-hook)))
5506       (when (gnus-buffer-live-p gnus-article-buffer)
5507         (save-excursion
5508           (set-buffer gnus-article-buffer)
5509           (mm-destroy-parts gnus-article-mime-handles)
5510           ;; Set it to nil for safety reason.
5511           (setq gnus-article-mime-handle-alist nil)
5512           (setq gnus-article-mime-handles nil)))
5513       ;; If we have several article buffers, we kill them at exit.
5514       (unless gnus-single-article-buffer
5515         (gnus-kill-buffer gnus-article-buffer)
5516         (gnus-kill-buffer gnus-original-article-buffer)
5517         (setq gnus-article-current nil))
5518       (if (not gnus-kill-summary-on-exit)
5519           (gnus-deaden-summary)
5520         (gnus-close-group group)
5521         (gnus-summary-clear-local-variables)
5522         (set-buffer gnus-group-buffer)
5523         (gnus-summary-clear-local-variables)
5524         (when (get-buffer gnus-summary-buffer)
5525           (kill-buffer gnus-summary-buffer)))
5526       (unless gnus-single-article-buffer
5527         (setq gnus-article-current nil))
5528       (when gnus-use-trees
5529         (gnus-tree-close group))
5530       (gnus-async-prefetch-remove-group group)
5531       (when (get-buffer gnus-article-buffer)
5532         (bury-buffer gnus-article-buffer))
5533       ;; Return to the group buffer.
5534       (gnus-configure-windows 'group 'force)
5535       ;; Clear the current group name.
5536       (setq gnus-newsgroup-name nil)
5537       (when (equal (gnus-group-group-name) group)
5538         (gnus-group-next-unread-group 1))
5539       (when quit-config
5540         (gnus-handle-ephemeral-exit quit-config)))))
5541
5542 (defun gnus-handle-ephemeral-exit (quit-config)
5543   "Handle movement when leaving an ephemeral group.
5544 The state which existed when entering the ephemeral is reset."
5545   (if (not (buffer-name (car quit-config)))
5546       (gnus-configure-windows 'group 'force)
5547     (set-buffer (car quit-config))
5548     (cond ((eq major-mode 'gnus-summary-mode)
5549            (gnus-set-global-variables))
5550           ((eq major-mode 'gnus-article-mode)
5551            (save-excursion
5552              ;; The `gnus-summary-buffer' variable may point
5553              ;; to the old summary buffer when using a single
5554              ;; article buffer.
5555              (unless (gnus-buffer-live-p gnus-summary-buffer)
5556                (set-buffer gnus-group-buffer))
5557              (set-buffer gnus-summary-buffer)
5558              (gnus-set-global-variables))))
5559     (if (or (eq (cdr quit-config) 'article)
5560             (eq (cdr quit-config) 'pick))
5561         (progn
5562           ;; The current article may be from the ephemeral group
5563           ;; thus it is best that we reload this article
5564           (gnus-summary-show-article)
5565           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5566               (gnus-configure-windows 'pick 'force)
5567             (gnus-configure-windows (cdr quit-config) 'force)))
5568       (gnus-configure-windows (cdr quit-config) 'force))
5569     (when (eq major-mode 'gnus-summary-mode)
5570       (gnus-summary-next-subject 1 nil t)
5571       (gnus-summary-recenter)
5572       (gnus-summary-position-point))))
5573
5574 ;;; Dead summaries.
5575
5576 (defvar gnus-dead-summary-mode-map nil)
5577
5578 (unless gnus-dead-summary-mode-map
5579   (setq gnus-dead-summary-mode-map (make-keymap))
5580   (suppress-keymap gnus-dead-summary-mode-map)
5581   (substitute-key-definition
5582    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5583   (let ((keys '("\C-d" "\r" "\177" [delete])))
5584     (while keys
5585       (define-key gnus-dead-summary-mode-map
5586         (pop keys) 'gnus-summary-wake-up-the-dead))))
5587
5588 (defvar gnus-dead-summary-mode nil
5589   "Minor mode for Gnus summary buffers.")
5590
5591 (defun gnus-dead-summary-mode (&optional arg)
5592   "Minor mode for Gnus summary buffers."
5593   (interactive "P")
5594   (when (eq major-mode 'gnus-summary-mode)
5595     (make-local-variable 'gnus-dead-summary-mode)
5596     (setq gnus-dead-summary-mode
5597           (if (null arg) (not gnus-dead-summary-mode)
5598             (> (prefix-numeric-value arg) 0)))
5599     (when gnus-dead-summary-mode
5600       (gnus-add-minor-mode
5601        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5602
5603 (defun gnus-deaden-summary ()
5604   "Make the current summary buffer into a dead summary buffer."
5605   ;; Kill any previous dead summary buffer.
5606   (when (and gnus-dead-summary
5607              (buffer-name gnus-dead-summary))
5608     (save-excursion
5609       (set-buffer gnus-dead-summary)
5610       (when gnus-dead-summary-mode
5611         (kill-buffer (current-buffer)))))
5612   ;; Make this the current dead summary.
5613   (setq gnus-dead-summary (current-buffer))
5614   (gnus-dead-summary-mode 1)
5615   (let ((name (buffer-name)))
5616     (when (string-match "Summary" name)
5617       (rename-buffer
5618        (concat (substring name 0 (match-beginning 0)) "Dead "
5619                (substring name (match-beginning 0)))
5620        t)
5621       (bury-buffer))))
5622
5623 (defun gnus-kill-or-deaden-summary (buffer)
5624   "Kill or deaden the summary BUFFER."
5625   (save-excursion
5626     (when (and (buffer-name buffer)
5627                (not gnus-single-article-buffer))
5628       (save-excursion
5629         (set-buffer buffer)
5630         (gnus-kill-buffer gnus-article-buffer)
5631         (gnus-kill-buffer gnus-original-article-buffer)))
5632     (cond (gnus-kill-summary-on-exit
5633            (when (and gnus-use-trees
5634                       (gnus-buffer-exists-p buffer))
5635              (save-excursion
5636                (set-buffer buffer)
5637                (gnus-tree-close gnus-newsgroup-name)))
5638            (gnus-kill-buffer buffer))
5639           ((gnus-buffer-exists-p buffer)
5640            (save-excursion
5641              (set-buffer buffer)
5642              (gnus-deaden-summary))))))
5643
5644 (defun gnus-summary-wake-up-the-dead (&rest args)
5645   "Wake up the dead summary buffer."
5646   (interactive)
5647   (gnus-dead-summary-mode -1)
5648   (let ((name (buffer-name)))
5649     (when (string-match "Dead " name)
5650       (rename-buffer
5651        (concat (substring name 0 (match-beginning 0))
5652                (substring name (match-end 0)))
5653        t)))
5654   (gnus-message 3 "This dead summary is now alive again"))
5655
5656 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5657 (defun gnus-summary-fetch-faq (&optional faq-dir)
5658   "Fetch the FAQ for the current group.
5659 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5660 in."
5661   (interactive
5662    (list
5663     (when current-prefix-arg
5664       (completing-read
5665        "Faq dir: " (and (listp gnus-group-faq-directory)
5666                         (mapcar (lambda (file) (list file))
5667                                 gnus-group-faq-directory))))))
5668   (let (gnus-faq-buffer)
5669     (when (setq gnus-faq-buffer
5670                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5671       (gnus-configure-windows 'summary-faq))))
5672
5673 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5674 (defun gnus-summary-describe-group (&optional force)
5675   "Describe the current newsgroup."
5676   (interactive "P")
5677   (gnus-group-describe-group force gnus-newsgroup-name))
5678
5679 (defun gnus-summary-describe-briefly ()
5680   "Describe summary mode commands briefly."
5681   (interactive)
5682   (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")))
5683
5684 ;; Walking around group mode buffer from summary mode.
5685
5686 (defun gnus-summary-next-group (&optional no-article target-group backward)
5687   "Exit current newsgroup and then select next unread newsgroup.
5688 If prefix argument NO-ARTICLE is non-nil, no article is selected
5689 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5690 previous group instead."
5691   (interactive "P")
5692   ;; Stop pre-fetching.
5693   (gnus-async-halt-prefetch)
5694   (let ((current-group gnus-newsgroup-name)
5695         (current-buffer (current-buffer))
5696         entered)
5697     ;; First we semi-exit this group to update Xrefs and all variables.
5698     ;; We can't do a real exit, because the window conf must remain
5699     ;; the same in case the user is prompted for info, and we don't
5700     ;; want the window conf to change before that...
5701     (gnus-summary-exit t)
5702     (while (not entered)
5703       ;; Then we find what group we are supposed to enter.
5704       (set-buffer gnus-group-buffer)
5705       (gnus-group-jump-to-group current-group)
5706       (setq target-group
5707             (or target-group
5708                 (if (eq gnus-keep-same-level 'best)
5709                     (gnus-summary-best-group gnus-newsgroup-name)
5710                   (gnus-summary-search-group backward gnus-keep-same-level))))
5711       (if (not target-group)
5712           ;; There are no further groups, so we return to the group
5713           ;; buffer.
5714           (progn
5715             (gnus-message 5 "Returning to the group buffer")
5716             (setq entered t)
5717             (when (gnus-buffer-live-p current-buffer)
5718               (set-buffer current-buffer)
5719               (gnus-summary-exit))
5720             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5721         ;; We try to enter the target group.
5722         (gnus-group-jump-to-group target-group)
5723         (let ((unreads (gnus-group-group-unread)))
5724           (if (and (or (eq t unreads)
5725                        (and unreads (not (zerop unreads))))
5726                    (gnus-summary-read-group
5727                     target-group nil no-article
5728                     (and (buffer-name current-buffer) current-buffer)
5729                     nil backward))
5730               (setq entered t)
5731             (setq current-group target-group
5732                   target-group nil)))))))
5733
5734 (defun gnus-summary-prev-group (&optional no-article)
5735   "Exit current newsgroup and then select previous unread newsgroup.
5736 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5737   (interactive "P")
5738   (gnus-summary-next-group no-article nil t))
5739
5740 ;; Walking around summary lines.
5741
5742 (defun gnus-summary-first-subject (&optional unread undownloaded)
5743   "Go to the first unread subject.
5744 If UNREAD is non-nil, go to the first unread article.
5745 Returns the article selected or nil if there are no unread articles."
5746   (interactive "P")
5747   (prog1
5748       (cond
5749        ;; Empty summary.
5750        ((null gnus-newsgroup-data)
5751         (gnus-message 3 "No articles in the group")
5752         nil)
5753        ;; Pick the first article.
5754        ((not unread)
5755         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5756         (gnus-data-number (car gnus-newsgroup-data)))
5757        ;; No unread articles.
5758        ((null gnus-newsgroup-unreads)
5759         (gnus-message 3 "No more unread articles")
5760         nil)
5761        ;; Find the first unread article.
5762        (t
5763         (let ((data gnus-newsgroup-data))
5764           (while (and data
5765                       (and (not (and undownloaded
5766                                      (eq gnus-undownloaded-mark
5767                                          (gnus-data-mark (car data)))))
5768                            (not (gnus-data-unread-p (car data)))))
5769             (setq data (cdr data)))
5770           (when data
5771             (goto-char (gnus-data-pos (car data)))
5772             (gnus-data-number (car data))))))
5773     (gnus-summary-position-point)))
5774
5775 (defun gnus-summary-next-subject (n &optional unread dont-display)
5776   "Go to next N'th summary line.
5777 If N is negative, go to the previous N'th subject line.
5778 If UNREAD is non-nil, only unread articles are selected.
5779 The difference between N and the actual number of steps taken is
5780 returned."
5781   (interactive "p")
5782   (let ((backward (< n 0))
5783         (n (abs n)))
5784     (while (and (> n 0)
5785                 (if backward
5786                     (gnus-summary-find-prev unread)
5787                   (gnus-summary-find-next unread)))
5788       (unless (zerop (setq n (1- n)))
5789         (gnus-summary-show-thread)))
5790     (when (/= 0 n)
5791       (gnus-message 7 "No more%s articles"
5792                     (if unread " unread" "")))
5793     (unless dont-display
5794       (gnus-summary-recenter)
5795       (gnus-summary-position-point))
5796     n))
5797
5798 (defun gnus-summary-next-unread-subject (n)
5799   "Go to next N'th unread summary line."
5800   (interactive "p")
5801   (gnus-summary-next-subject n t))
5802
5803 (defun gnus-summary-prev-subject (n &optional unread)
5804   "Go to previous N'th summary line.
5805 If optional argument UNREAD is non-nil, only unread article is selected."
5806   (interactive "p")
5807   (gnus-summary-next-subject (- n) unread))
5808
5809 (defun gnus-summary-prev-unread-subject (n)
5810   "Go to previous N'th unread summary line."
5811   (interactive "p")
5812   (gnus-summary-next-subject (- n) t))
5813
5814 (defun gnus-summary-goto-subject (article &optional force silent)
5815   "Go the subject line of ARTICLE.
5816 If FORCE, also allow jumping to articles not currently shown."
5817   (interactive "nArticle number: ")
5818   (let ((b (point))
5819         (data (gnus-data-find article)))
5820     ;; We read in the article if we have to.
5821     (and (not data)
5822          force
5823          (gnus-summary-insert-subject
5824           article
5825           (if (or (numberp force) (vectorp force)) force)
5826           t)
5827          (setq data (gnus-data-find article)))
5828     (goto-char b)
5829     (if (not data)
5830         (progn
5831           (unless silent
5832             (gnus-message 3 "Can't find article %d" article))
5833           nil)
5834       (goto-char (gnus-data-pos data))
5835       (gnus-summary-position-point)
5836       article)))
5837
5838 ;; Walking around summary lines with displaying articles.
5839
5840 (defun gnus-summary-expand-window (&optional arg)
5841   "Make the summary buffer take up the entire Emacs frame.
5842 Given a prefix, will force an `article' buffer configuration."
5843   (interactive "P")
5844   (if arg
5845       (gnus-configure-windows 'article 'force)
5846     (gnus-configure-windows 'summary 'force)))
5847
5848 (defun gnus-summary-display-article (article &optional all-header)
5849   "Display ARTICLE in article buffer."
5850   (when (gnus-buffer-live-p gnus-article-buffer)
5851     (with-current-buffer gnus-article-buffer
5852       (mm-enable-multibyte-mule4)))
5853   (gnus-set-global-variables)
5854   (if (null article)
5855       nil
5856     (prog1
5857         (if gnus-summary-display-article-function
5858             (funcall gnus-summary-display-article-function article all-header)
5859           (gnus-article-prepare article all-header))
5860       (gnus-run-hooks 'gnus-select-article-hook)
5861       (when (and gnus-current-article
5862                  (not (zerop gnus-current-article)))
5863         (gnus-summary-goto-subject gnus-current-article))
5864       (gnus-summary-recenter)
5865       (when (and gnus-use-trees gnus-show-threads)
5866         (gnus-possibly-generate-tree article)
5867         (gnus-highlight-selected-tree article))
5868       ;; Successfully display article.
5869       (gnus-article-set-window-start
5870        (cdr (assq article gnus-newsgroup-bookmarks))))))
5871
5872 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5873   "Select the current article.
5874 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5875 non-nil, the article will be re-fetched even if it already present in
5876 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5877 be displayed."
5878   ;; Make sure we are in the summary buffer to work around bbdb bug.
5879   (unless (eq major-mode 'gnus-summary-mode)
5880     (set-buffer gnus-summary-buffer))
5881   (let ((article (or article (gnus-summary-article-number)))
5882         (all-headers (not (not all-headers))) ;Must be T or NIL.
5883         gnus-summary-display-article-function)
5884     (and (not pseudo)
5885          (gnus-summary-article-pseudo-p article)
5886          (error "This is a pseudo-article"))
5887     (save-excursion
5888       (set-buffer gnus-summary-buffer)
5889       (if (or (and gnus-single-article-buffer
5890                    (or (null gnus-current-article)
5891                        (null gnus-article-current)
5892                        (null (get-buffer gnus-article-buffer))
5893                        (not (eq article (cdr gnus-article-current)))
5894                        (not (equal (car gnus-article-current)
5895                                    gnus-newsgroup-name))))
5896               (and (not gnus-single-article-buffer)
5897                    (or (null gnus-current-article)
5898                        (not (eq gnus-current-article article))))
5899               force)
5900           ;; The requested article is different from the current article.
5901           (progn
5902             (gnus-summary-display-article article all-headers)
5903             (when (gnus-buffer-live-p gnus-article-buffer)
5904                (with-current-buffer gnus-article-buffer
5905                 (if (not gnus-article-decoded-p) ;; a local variable
5906                     (mm-disable-multibyte-mule4))))
5907             (when (or all-headers gnus-show-all-headers)
5908               (gnus-article-show-all-headers))
5909             (gnus-article-set-window-start
5910              (cdr (assq article gnus-newsgroup-bookmarks)))
5911             article)
5912         (when (or all-headers gnus-show-all-headers)
5913           (gnus-article-show-all-headers))
5914         'old))))
5915
5916 (defun gnus-summary-set-current-mark (&optional current-mark)
5917   "Obsolete function."
5918   nil)
5919
5920 (defun gnus-summary-next-article (&optional unread subject backward push)
5921   "Select the next article.
5922 If UNREAD, only unread articles are selected.
5923 If SUBJECT, only articles with SUBJECT are selected.
5924 If BACKWARD, the previous article is selected instead of the next."
5925   (interactive "P")
5926   (cond
5927    ;; Is there such an article?
5928    ((and (gnus-summary-search-forward unread subject backward)
5929          (or (gnus-summary-display-article (gnus-summary-article-number))
5930              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5931     (gnus-summary-position-point))
5932    ;; If not, we try the first unread, if that is wanted.
5933    ((and subject
5934          gnus-auto-select-same
5935          (gnus-summary-first-unread-article))
5936     (gnus-summary-position-point)
5937     (gnus-message 6 "Wrapped"))
5938    ;; Try to get next/previous article not displayed in this group.
5939    ((and gnus-auto-extend-newsgroup
5940          (not unread) (not subject))
5941     (gnus-summary-goto-article
5942      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5943      nil (count-lines (point-min) (point))))
5944    ;; Go to next/previous group.
5945    (t
5946     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5947       (gnus-summary-jump-to-group gnus-newsgroup-name))
5948     (let ((cmd last-command-char)
5949           (point
5950            (save-excursion
5951              (set-buffer gnus-group-buffer)
5952              (point)))
5953           (group
5954            (if (eq gnus-keep-same-level 'best)
5955                (gnus-summary-best-group gnus-newsgroup-name)
5956              (gnus-summary-search-group backward gnus-keep-same-level))))
5957       ;; For some reason, the group window gets selected.  We change
5958       ;; it back.
5959       (select-window (get-buffer-window (current-buffer)))
5960       ;; Select next unread newsgroup automagically.
5961       (cond
5962        ((or (not gnus-auto-select-next)
5963             (not cmd))
5964         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5965        ((or (eq gnus-auto-select-next 'quietly)
5966             (and (eq gnus-auto-select-next 'slightly-quietly)
5967                  push)
5968             (and (eq gnus-auto-select-next 'almost-quietly)
5969                  (gnus-summary-last-article-p)))
5970         ;; Select quietly.
5971         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5972             (gnus-summary-exit)
5973           (gnus-message 7 "No more%s articles (%s)..."
5974                         (if unread " unread" "")
5975                         (if group (concat "selecting " group)
5976                           "exiting"))
5977           (gnus-summary-next-group nil group backward)))
5978        (t
5979         (when (gnus-key-press-event-p last-input-event)
5980           (gnus-summary-walk-group-buffer
5981            gnus-newsgroup-name cmd unread backward point))))))))
5982
5983 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5984   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5985                       (?\C-p (gnus-group-prev-unread-group 1))))
5986         (cursor-in-echo-area t)
5987         keve key group ended)
5988     (save-excursion
5989       (set-buffer gnus-group-buffer)
5990       (goto-char start)
5991       (setq group
5992             (if (eq gnus-keep-same-level 'best)
5993                 (gnus-summary-best-group gnus-newsgroup-name)
5994               (gnus-summary-search-group backward gnus-keep-same-level))))
5995     (while (not ended)
5996       (gnus-message
5997        5 "No more%s articles%s" (if unread " unread" "")
5998        (if (and group
5999                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6000            (format " (Type %s for %s [%s])"
6001                    (single-key-description cmd) group
6002                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6003          (format " (Type %s to exit %s)"
6004                  (single-key-description cmd)
6005                  gnus-newsgroup-name)))
6006       ;; Confirm auto selection.
6007       (setq key (car (setq keve (gnus-read-event-char))))
6008       (setq ended t)
6009       (cond
6010        ((assq key keystrokes)
6011         (let ((obuf (current-buffer)))
6012           (switch-to-buffer gnus-group-buffer)
6013           (when group
6014             (gnus-group-jump-to-group group))
6015           (eval (cadr (assq key keystrokes)))
6016           (setq group (gnus-group-group-name))
6017           (switch-to-buffer obuf))
6018         (setq ended nil))
6019        ((equal key cmd)
6020         (if (or (not group)
6021                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6022             (gnus-summary-exit)
6023           (gnus-summary-next-group nil group backward)))
6024        (t
6025         (push (cdr keve) unread-command-events))))))
6026
6027 (defun gnus-summary-next-unread-article ()
6028   "Select unread article after current one."
6029   (interactive)
6030   (gnus-summary-next-article
6031    (or (not (eq gnus-summary-goto-unread 'never))
6032        (gnus-summary-last-article-p (gnus-summary-article-number)))
6033    (and gnus-auto-select-same
6034         (gnus-summary-article-subject))))
6035
6036 (defun gnus-summary-prev-article (&optional unread subject)
6037   "Select the article after the current one.
6038 If UNREAD is non-nil, only unread articles are selected."
6039   (interactive "P")
6040   (gnus-summary-next-article unread subject t))
6041
6042 (defun gnus-summary-prev-unread-article ()
6043   "Select unread article before current one."
6044   (interactive)
6045   (gnus-summary-prev-article
6046    (or (not (eq gnus-summary-goto-unread 'never))
6047        (gnus-summary-first-article-p (gnus-summary-article-number)))
6048    (and gnus-auto-select-same
6049         (gnus-summary-article-subject))))
6050
6051 (defun gnus-summary-next-page (&optional lines circular)
6052   "Show next page of the selected article.
6053 If at the end of the current article, select the next article.
6054 LINES says how many lines should be scrolled up.
6055
6056 If CIRCULAR is non-nil, go to the start of the article instead of
6057 selecting the next article when reaching the end of the current
6058 article."
6059   (interactive "P")
6060   (setq gnus-summary-buffer (current-buffer))
6061   (gnus-set-global-variables)
6062   (let ((article (gnus-summary-article-number))
6063         (article-window (get-buffer-window gnus-article-buffer t))
6064         endp)
6065     ;; If the buffer is empty, we have no article.
6066     (unless article
6067       (error "No article to select"))
6068     (gnus-configure-windows 'article)
6069     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6070         (if (and (eq gnus-summary-goto-unread 'never)
6071                  (not (gnus-summary-last-article-p article)))
6072             (gnus-summary-next-article)
6073           (gnus-summary-next-unread-article))
6074       (if (or (null gnus-current-article)
6075               (null gnus-article-current)
6076               (/= article (cdr gnus-article-current))
6077               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6078           ;; Selected subject is different from current article's.
6079           (gnus-summary-display-article article)
6080         (when article-window
6081           (gnus-eval-in-buffer-window gnus-article-buffer
6082             (setq endp (gnus-article-next-page lines)))
6083           (when endp
6084             (cond (circular
6085                    (gnus-summary-beginning-of-article))
6086                   (lines
6087                    (gnus-message 3 "End of message"))
6088                   ((null lines)
6089                    (if (and (eq gnus-summary-goto-unread 'never)
6090                             (not (gnus-summary-last-article-p article)))
6091                        (gnus-summary-next-article)
6092                      (gnus-summary-next-unread-article))))))))
6093     (gnus-summary-recenter)
6094     (gnus-summary-position-point)))
6095
6096 (defun gnus-summary-prev-page (&optional lines move)
6097   "Show previous page of selected article.
6098 Argument LINES specifies lines to be scrolled down.
6099 If MOVE, move to the previous unread article if point is at
6100 the beginning of the buffer."
6101   (interactive "P")
6102   (let ((article (gnus-summary-article-number))
6103         (article-window (get-buffer-window gnus-article-buffer t))
6104         endp)
6105     (gnus-configure-windows 'article)
6106     (if (or (null gnus-current-article)
6107             (null gnus-article-current)
6108             (/= article (cdr gnus-article-current))
6109             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6110         ;; Selected subject is different from current article's.
6111         (gnus-summary-display-article article)
6112       (gnus-summary-recenter)
6113       (when article-window
6114         (gnus-eval-in-buffer-window gnus-article-buffer
6115           (setq endp (gnus-article-prev-page lines)))
6116         (when (and move endp)
6117           (cond (lines
6118                  (gnus-message 3 "Beginning of message"))
6119                 ((null lines)
6120                  (if (and (eq gnus-summary-goto-unread 'never)
6121                           (not (gnus-summary-first-article-p article)))
6122                      (gnus-summary-prev-article)
6123                    (gnus-summary-prev-unread-article))))))))
6124   (gnus-summary-position-point))
6125
6126 (defun gnus-summary-prev-page-or-article (&optional lines)
6127   "Show previous page of selected article.
6128 Argument LINES specifies lines to be scrolled down.
6129 If at the beginning of the article, go to the next article."
6130   (interactive "P")
6131   (gnus-summary-prev-page lines t))
6132
6133 (defun gnus-summary-scroll-up (lines)
6134   "Scroll up (or down) one line current article.
6135 Argument LINES specifies lines to be scrolled up (or down if negative)."
6136   (interactive "p")
6137   (gnus-configure-windows 'article)
6138   (gnus-summary-show-thread)
6139   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6140     (gnus-eval-in-buffer-window gnus-article-buffer
6141       (cond ((> lines 0)
6142              (when (gnus-article-next-page lines)
6143                (gnus-message 3 "End of message")))
6144             ((< lines 0)
6145              (gnus-article-prev-page (- lines))))))
6146   (gnus-summary-recenter)
6147   (gnus-summary-position-point))
6148
6149 (defun gnus-summary-scroll-down (lines)
6150   "Scroll down (or up) one line current article.
6151 Argument LINES specifies lines to be scrolled down (or up if negative)."
6152   (interactive "p")
6153   (gnus-summary-scroll-up (- lines)))
6154
6155 (defun gnus-summary-next-same-subject ()
6156   "Select next article which has the same subject as current one."
6157   (interactive)
6158   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6159
6160 (defun gnus-summary-prev-same-subject ()
6161   "Select previous article which has the same subject as current one."
6162   (interactive)
6163   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6164
6165 (defun gnus-summary-next-unread-same-subject ()
6166   "Select next unread article which has the same subject as current one."
6167   (interactive)
6168   (gnus-summary-next-article t (gnus-summary-article-subject)))
6169
6170 (defun gnus-summary-prev-unread-same-subject ()
6171   "Select previous unread article which has the same subject as current one."
6172   (interactive)
6173   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6174
6175 (defun gnus-summary-first-unread-article ()
6176   "Select the first unread article.
6177 Return nil if there are no unread articles."
6178   (interactive)
6179   (prog1
6180       (when (gnus-summary-first-subject t)
6181         (gnus-summary-show-thread)
6182         (gnus-summary-first-subject t)
6183         (gnus-summary-display-article (gnus-summary-article-number)))
6184     (gnus-summary-position-point)))
6185
6186 (defun gnus-summary-first-unread-subject ()
6187   "Place the point on the subject line of the first unread article.
6188 Return nil if there are no unread articles."
6189   (interactive)
6190   (prog1
6191       (when (gnus-summary-first-subject t)
6192         (gnus-summary-show-thread)
6193         (gnus-summary-first-subject t))
6194     (gnus-summary-position-point)))
6195
6196 (defun gnus-summary-first-article ()
6197   "Select the first article.
6198 Return nil if there are no articles."
6199   (interactive)
6200   (prog1
6201       (when (gnus-summary-first-subject)
6202         (gnus-summary-show-thread)
6203         (gnus-summary-first-subject)
6204         (gnus-summary-display-article (gnus-summary-article-number)))
6205     (gnus-summary-position-point)))
6206
6207 (defun gnus-summary-best-unread-article ()
6208   "Select the unread article with the highest score."
6209   (interactive)
6210   (let ((best -1000000)
6211         (data gnus-newsgroup-data)
6212         article score)
6213     (while data
6214       (and (gnus-data-unread-p (car data))
6215            (> (setq score
6216                     (gnus-summary-article-score (gnus-data-number (car data))))
6217               best)
6218            (setq best score
6219                  article (gnus-data-number (car data))))
6220       (setq data (cdr data)))
6221     (prog1
6222         (if article
6223             (gnus-summary-goto-article article)
6224           (error "No unread articles"))
6225       (gnus-summary-position-point))))
6226
6227 (defun gnus-summary-last-subject ()
6228   "Go to the last displayed subject line in the group."
6229   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6230     (when article
6231       (gnus-summary-goto-subject article))))
6232
6233 (defun gnus-summary-goto-article (article &optional all-headers force)
6234   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6235 If ALL-HEADERS is non-nil, no header lines are hidden.
6236 If FORCE, go to the article even if it isn't displayed.  If FORCE
6237 is a number, it is the line the article is to be displayed on."
6238   (interactive
6239    (list
6240     (completing-read
6241      "Article number or Message-ID: "
6242      (mapcar (lambda (number) (list (int-to-string number)))
6243              gnus-newsgroup-limit))
6244     current-prefix-arg
6245     t))
6246   (prog1
6247       (if (and (stringp article)
6248                (string-match "@" article))
6249           (gnus-summary-refer-article article)
6250         (when (stringp article)
6251           (setq article (string-to-number article)))
6252         (if (gnus-summary-goto-subject article force)
6253             (gnus-summary-display-article article all-headers)
6254           (gnus-message 4 "Couldn't go to article %s" article) nil))
6255     (gnus-summary-position-point)))
6256
6257 (defun gnus-summary-goto-last-article ()
6258   "Go to the previously read article."
6259   (interactive)
6260   (prog1
6261       (when gnus-last-article
6262         (gnus-summary-goto-article gnus-last-article nil t))
6263     (gnus-summary-position-point)))
6264
6265 (defun gnus-summary-pop-article (number)
6266   "Pop one article off the history and go to the previous.
6267 NUMBER articles will be popped off."
6268   (interactive "p")
6269   (let (to)
6270     (setq gnus-newsgroup-history
6271           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6272     (if to
6273         (gnus-summary-goto-article (car to) nil t)
6274       (error "Article history empty")))
6275   (gnus-summary-position-point))
6276
6277 ;; Summary commands and functions for limiting the summary buffer.
6278
6279 (defun gnus-summary-limit-to-articles (n)
6280   "Limit the summary buffer to the next N articles.
6281 If not given a prefix, use the process marked articles instead."
6282   (interactive "P")
6283   (prog1
6284       (let ((articles (gnus-summary-work-articles n)))
6285         (setq gnus-newsgroup-processable nil)
6286         (gnus-summary-limit articles))
6287     (gnus-summary-position-point)))
6288
6289 (defun gnus-summary-pop-limit (&optional total)
6290   "Restore the previous limit.
6291 If given a prefix, remove all limits."
6292   (interactive "P")
6293   (when total
6294     (setq gnus-newsgroup-limits
6295           (list (mapcar (lambda (h) (mail-header-number h))
6296                         gnus-newsgroup-headers))))
6297   (unless gnus-newsgroup-limits
6298     (error "No limit to pop"))
6299   (prog1
6300       (gnus-summary-limit nil 'pop)
6301     (gnus-summary-position-point)))
6302
6303 (defun gnus-summary-limit-to-subject (subject &optional header)
6304   "Limit the summary buffer to articles that have subjects that match a regexp."
6305   (interactive "sLimit to subject (regexp): ")
6306   (unless header
6307     (setq header "subject"))
6308   (when (not (equal "" subject))
6309     (prog1
6310         (let ((articles (gnus-summary-find-matching
6311                          (or header "subject") subject 'all)))
6312           (unless articles
6313             (error "Found no matches for \"%s\"" subject))
6314           (gnus-summary-limit articles))
6315       (gnus-summary-position-point))))
6316
6317 (defun gnus-summary-limit-to-author (from)
6318   "Limit the summary buffer to articles that have authors that match a regexp."
6319   (interactive "sLimit to author (regexp): ")
6320   (gnus-summary-limit-to-subject from "from"))
6321
6322 (defun gnus-summary-limit-to-age (age &optional younger-p)
6323   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6324 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6325 articles that are younger than AGE days."
6326   (interactive
6327    (let ((younger current-prefix-arg)
6328          (days-got nil)
6329          days)
6330      (while (not days-got)
6331        (setq days (if younger
6332                       (read-string "Limit to articles within (in days): ")
6333                     (read-string "Limit to articles old than (in days): ")))
6334        (when (> (length days) 0)
6335          (setq days (read days)))
6336        (if (numberp days)
6337            (setq days-got t)
6338          (message "Please enter a number.")
6339          (sleep-for 1)))
6340      (list days younger)))
6341   (prog1
6342       (let ((data gnus-newsgroup-data)
6343             (cutoff (days-to-time age))
6344             articles d date is-younger)
6345         (while (setq d (pop data))
6346           (when (and (vectorp (gnus-data-header d))
6347                      (setq date (mail-header-date (gnus-data-header d))))
6348             (setq is-younger (time-less-p
6349                               (time-since (condition-case ()
6350                                               (date-to-time date)
6351                                             (error '(0 0))))
6352                               cutoff))
6353             (when (if younger-p
6354                       is-younger
6355                     (not is-younger))
6356               (push (gnus-data-number d) articles))))
6357         (gnus-summary-limit (nreverse articles)))
6358     (gnus-summary-position-point)))
6359
6360 (defun gnus-summary-limit-to-extra (header regexp)
6361   "Limit the summary buffer to articles that match an 'extra' header."
6362   (interactive
6363    (let ((header
6364           (intern
6365            (gnus-completing-read
6366             (symbol-name (car gnus-extra-headers))
6367             "Limit extra header:"
6368             (mapcar (lambda (x)
6369                       (cons (symbol-name x) x))
6370                     gnus-extra-headers)
6371             nil
6372             t))))
6373      (list header
6374            (read-string (format "Limit to header %s (regexp): " header)))))
6375   (when (not (equal "" regexp))
6376     (prog1
6377         (let ((articles (gnus-summary-find-matching
6378                          (cons 'extra header) regexp 'all)))
6379           (unless articles
6380             (error "Found no matches for \"%s\"" regexp))
6381           (gnus-summary-limit articles))
6382       (gnus-summary-position-point))))
6383
6384 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6385 (make-obsolete
6386  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6387
6388 (defun gnus-summary-limit-to-unread (&optional all)
6389   "Limit the summary buffer to articles that are not marked as read.
6390 If ALL is non-nil, limit strictly to unread articles."
6391   (interactive "P")
6392   (if all
6393       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6394     (gnus-summary-limit-to-marks
6395      ;; Concat all the marks that say that an article is read and have
6396      ;; those removed.
6397      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6398            gnus-killed-mark gnus-kill-file-mark
6399            gnus-low-score-mark gnus-expirable-mark
6400            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6401            gnus-duplicate-mark gnus-souped-mark)
6402      'reverse)))
6403
6404 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6405 (make-obsolete 'gnus-summary-delete-marked-with
6406                'gnus-summary-limit-exlude-marks)
6407
6408 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6409   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6410 If REVERSE, limit the summary buffer to articles that are marked
6411 with MARKS.  MARKS can either be a string of marks or a list of marks.
6412 Returns how many articles were removed."
6413   (interactive "sMarks: ")
6414   (gnus-summary-limit-to-marks marks t))
6415
6416 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6417   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6418 If REVERSE (the prefix), limit the summary buffer to articles that are
6419 not marked with MARKS.  MARKS can either be a string of marks or a
6420 list of marks.
6421 Returns how many articles were removed."
6422   (interactive "sMarks: \nP")
6423   (prog1
6424       (let ((data gnus-newsgroup-data)
6425             (marks (if (listp marks) marks
6426                      (append marks nil))) ; Transform to list.
6427             articles)
6428         (while data
6429           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6430                   (memq (gnus-data-mark (car data)) marks))
6431             (push (gnus-data-number (car data)) articles))
6432           (setq data (cdr data)))
6433         (gnus-summary-limit articles))
6434     (gnus-summary-position-point)))
6435
6436 (defun gnus-summary-limit-to-score (&optional score)
6437   "Limit to articles with score at or above SCORE."
6438   (interactive "P")
6439   (setq score (if score
6440                   (prefix-numeric-value score)
6441                 (or gnus-summary-default-score 0)))
6442   (let ((data gnus-newsgroup-data)
6443         articles)
6444     (while data
6445       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6446                 score)
6447         (push (gnus-data-number (car data)) articles))
6448       (setq data (cdr data)))
6449     (prog1
6450         (gnus-summary-limit articles)
6451       (gnus-summary-position-point))))
6452
6453 (defun gnus-summary-limit-include-thread (id)
6454   "Display all the hidden articles that in the current thread."
6455   (interactive (list (mail-header-id (gnus-summary-article-header))))
6456   (let ((articles (gnus-articles-in-thread
6457                    (gnus-id-to-thread (gnus-root-id id)))))
6458     (prog1
6459         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6460       (gnus-summary-position-point))))
6461
6462 (defun gnus-summary-limit-include-dormant ()
6463   "Display all the hidden articles that are marked as dormant.
6464 Note that this command only works on a subset of the articles currently
6465 fetched for this group."
6466   (interactive)
6467   (unless gnus-newsgroup-dormant
6468     (error "There are no dormant articles in this group"))
6469   (prog1
6470       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6471     (gnus-summary-position-point)))
6472
6473 (defun gnus-summary-limit-exclude-dormant ()
6474   "Hide all dormant articles."
6475   (interactive)
6476   (prog1
6477       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6478     (gnus-summary-position-point)))
6479
6480 (defun gnus-summary-limit-exclude-childless-dormant ()
6481   "Hide all dormant articles that have no children."
6482   (interactive)
6483   (let ((data (gnus-data-list t))
6484         articles d children)
6485     ;; Find all articles that are either not dormant or have
6486     ;; children.
6487     (while (setq d (pop data))
6488       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6489                 (and (setq children
6490                            (gnus-article-children (gnus-data-number d)))
6491                      (let (found)
6492                        (while children
6493                          (when (memq (car children) articles)
6494                            (setq children nil
6495                                  found t))
6496                          (pop children))
6497                        found)))
6498         (push (gnus-data-number d) articles)))
6499     ;; Do the limiting.
6500     (prog1
6501         (gnus-summary-limit articles)
6502       (gnus-summary-position-point))))
6503
6504 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6505   "Mark all unread excluded articles as read.
6506 If ALL, mark even excluded ticked and dormants as read."
6507   (interactive "P")
6508   (let ((articles (gnus-sorted-complement
6509                    (sort
6510                     (mapcar (lambda (h) (mail-header-number h))
6511                             gnus-newsgroup-headers)
6512                     '<)
6513                    (sort gnus-newsgroup-limit '<)))
6514         article)
6515     (setq gnus-newsgroup-unreads
6516           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6517     (if all
6518         (setq gnus-newsgroup-dormant nil
6519               gnus-newsgroup-marked nil
6520               gnus-newsgroup-reads
6521               (nconc
6522                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6523                gnus-newsgroup-reads))
6524       (while (setq article (pop articles))
6525         (unless (or (memq article gnus-newsgroup-dormant)
6526                     (memq article gnus-newsgroup-marked))
6527           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6528
6529 (defun gnus-summary-limit (articles &optional pop)
6530   (if pop
6531       ;; We pop the previous limit off the stack and use that.
6532       (setq articles (car gnus-newsgroup-limits)
6533             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6534     ;; We use the new limit, so we push the old limit on the stack.
6535     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6536   ;; Set the limit.
6537   (setq gnus-newsgroup-limit articles)
6538   (let ((total (length gnus-newsgroup-data))
6539         (data (gnus-data-find-list (gnus-summary-article-number)))
6540         (gnus-summary-mark-below nil)   ; Inhibit this.
6541         found)
6542     ;; This will do all the work of generating the new summary buffer
6543     ;; according to the new limit.
6544     (gnus-summary-prepare)
6545     ;; Hide any threads, possibly.
6546     (and gnus-show-threads
6547          gnus-thread-hide-subtree
6548          (gnus-summary-hide-all-threads))
6549     ;; Try to return to the article you were at, or one in the
6550     ;; neighborhood.
6551     (when data
6552       ;; We try to find some article after the current one.
6553       (while data
6554         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6555           (setq data nil
6556                 found t))
6557         (setq data (cdr data))))
6558     (unless found
6559       ;; If there is no data, that means that we were after the last
6560       ;; article.  The same goes when we can't find any articles
6561       ;; after the current one.
6562       (goto-char (point-max))
6563       (gnus-summary-find-prev))
6564     (gnus-set-mode-line 'summary)
6565     ;; We return how many articles were removed from the summary
6566     ;; buffer as a result of the new limit.
6567     (- total (length gnus-newsgroup-data))))
6568
6569 (defsubst gnus-invisible-cut-children (threads)
6570   (let ((num 0))
6571     (while threads
6572       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6573         (incf num))
6574       (pop threads))
6575     (< num 2)))
6576
6577 (defsubst gnus-cut-thread (thread)
6578   "Go forwards in the thread until we find an article that we want to display."
6579   (when (or (eq gnus-fetch-old-headers 'some)
6580             (eq gnus-fetch-old-headers 'invisible)
6581             (numberp gnus-fetch-old-headers)
6582             (eq gnus-build-sparse-threads 'some)
6583             (eq gnus-build-sparse-threads 'more))
6584     ;; Deal with old-fetched headers and sparse threads.
6585     (while (and
6586             thread
6587             (or
6588              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6589              (gnus-summary-article-ancient-p
6590               (mail-header-number (car thread))))
6591             (if (or (<= (length (cdr thread)) 1)
6592                     (eq gnus-fetch-old-headers 'invisible))
6593                 (setq gnus-newsgroup-limit
6594                       (delq (mail-header-number (car thread))
6595                             gnus-newsgroup-limit)
6596                       thread (cadr thread))
6597               (when (gnus-invisible-cut-children (cdr thread))
6598                 (let ((th (cdr thread)))
6599                   (while th
6600                     (if (memq (mail-header-number (caar th))
6601                               gnus-newsgroup-limit)
6602                         (setq thread (car th)
6603                               th nil)
6604                       (setq th (cdr th))))))))))
6605   thread)
6606
6607 (defun gnus-cut-threads (threads)
6608   "Cut off all uninteresting articles from the beginning of threads."
6609   (when (or (eq gnus-fetch-old-headers 'some)
6610             (eq gnus-fetch-old-headers 'invisible)
6611             (numberp gnus-fetch-old-headers)
6612             (eq gnus-build-sparse-threads 'some)
6613             (eq gnus-build-sparse-threads 'more))
6614     (let ((th threads))
6615       (while th
6616         (setcar th (gnus-cut-thread (car th)))
6617         (setq th (cdr th)))))
6618   ;; Remove nixed out threads.
6619   (delq nil threads))
6620
6621 (defun gnus-summary-initial-limit (&optional show-if-empty)
6622   "Figure out what the initial limit is supposed to be on group entry.
6623 This entails weeding out unwanted dormants, low-scored articles,
6624 fetch-old-headers verbiage, and so on."
6625   ;; Most groups have nothing to remove.
6626   (if (or gnus-inhibit-limiting
6627           (and (null gnus-newsgroup-dormant)
6628                (not (eq gnus-fetch-old-headers 'some))
6629                (not (numberp gnus-fetch-old-headers))
6630                (not (eq gnus-fetch-old-headers 'invisible))
6631                (null gnus-summary-expunge-below)
6632                (not (eq gnus-build-sparse-threads 'some))
6633                (not (eq gnus-build-sparse-threads 'more))
6634                (null gnus-thread-expunge-below)
6635                (not gnus-use-nocem)))
6636       ()                                ; Do nothing.
6637     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6638     (setq gnus-newsgroup-limit nil)
6639     (mapatoms
6640      (lambda (node)
6641        (unless (car (symbol-value node))
6642          ;; These threads have no parents -- they are roots.
6643          (let ((nodes (cdr (symbol-value node)))
6644                thread)
6645            (while nodes
6646              (if (and gnus-thread-expunge-below
6647                       (< (gnus-thread-total-score (car nodes))
6648                          gnus-thread-expunge-below))
6649                  (gnus-expunge-thread (pop nodes))
6650                (setq thread (pop nodes))
6651                (gnus-summary-limit-children thread))))))
6652      gnus-newsgroup-dependencies)
6653     ;; If this limitation resulted in an empty group, we might
6654     ;; pop the previous limit and use it instead.
6655     (when (and (not gnus-newsgroup-limit)
6656                show-if-empty)
6657       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6658     gnus-newsgroup-limit))
6659
6660 (defun gnus-summary-limit-children (thread)
6661   "Return 1 if this subthread is visible and 0 if it is not."
6662   ;; First we get the number of visible children to this thread.  This
6663   ;; is done by recursing down the thread using this function, so this
6664   ;; will really go down to a leaf article first, before slowly
6665   ;; working its way up towards the root.
6666   (when thread
6667     (let ((children
6668            (if (cdr thread)
6669                (apply '+ (mapcar 'gnus-summary-limit-children
6670                                  (cdr thread)))
6671              0))
6672           (number (mail-header-number (car thread)))
6673           score)
6674       (if (and
6675            (not (memq number gnus-newsgroup-marked))
6676            (or
6677             ;; If this article is dormant and has absolutely no visible
6678             ;; children, then this article isn't visible.
6679             (and (memq number gnus-newsgroup-dormant)
6680                  (zerop children))
6681             ;; If this is "fetch-old-headered" and there is no
6682             ;; visible children, then we don't want this article.
6683             (and (or (eq gnus-fetch-old-headers 'some)
6684                      (numberp gnus-fetch-old-headers))
6685                  (gnus-summary-article-ancient-p number)
6686                  (zerop children))
6687             ;; If this is "fetch-old-headered" and `invisible', then
6688             ;; we don't want this article.
6689             (and (eq gnus-fetch-old-headers 'invisible)
6690                  (gnus-summary-article-ancient-p number))
6691             ;; If this is a sparsely inserted article with no children,
6692             ;; we don't want it.
6693             (and (eq gnus-build-sparse-threads 'some)
6694                  (gnus-summary-article-sparse-p number)
6695                  (zerop children))
6696             ;; If we use expunging, and this article is really
6697             ;; low-scored, then we don't want this article.
6698             (when (and gnus-summary-expunge-below
6699                        (< (setq score
6700                                 (or (cdr (assq number gnus-newsgroup-scored))
6701                                     gnus-summary-default-score))
6702                           gnus-summary-expunge-below))
6703               ;; We increase the expunge-tally here, but that has
6704               ;; nothing to do with the limits, really.
6705               (incf gnus-newsgroup-expunged-tally)
6706               ;; We also mark as read here, if that's wanted.
6707               (when (and gnus-summary-mark-below
6708                          (< score gnus-summary-mark-below))
6709                 (setq gnus-newsgroup-unreads
6710                       (delq number gnus-newsgroup-unreads))
6711                 (if gnus-newsgroup-auto-expire
6712                     (push number gnus-newsgroup-expirable)
6713                   (push (cons number gnus-low-score-mark)
6714                         gnus-newsgroup-reads)))
6715               t)
6716             ;; Check NoCeM things.
6717             (if (and gnus-use-nocem
6718                      (gnus-nocem-unwanted-article-p
6719                       (mail-header-id (car thread))))
6720                 (progn
6721                   (setq gnus-newsgroup-unreads
6722                         (delq number gnus-newsgroup-unreads))
6723                   t))))
6724           ;; Nope, invisible article.
6725           0
6726         ;; Ok, this article is to be visible, so we add it to the limit
6727         ;; and return 1.
6728         (push number gnus-newsgroup-limit)
6729         1))))
6730
6731 (defun gnus-expunge-thread (thread)
6732   "Mark all articles in THREAD as read."
6733   (let* ((number (mail-header-number (car thread))))
6734     (incf gnus-newsgroup-expunged-tally)
6735     ;; We also mark as read here, if that's wanted.
6736     (setq gnus-newsgroup-unreads
6737           (delq number gnus-newsgroup-unreads))
6738     (if gnus-newsgroup-auto-expire
6739         (push number gnus-newsgroup-expirable)
6740       (push (cons number gnus-low-score-mark)
6741             gnus-newsgroup-reads)))
6742   ;; Go recursively through all subthreads.
6743   (mapcar 'gnus-expunge-thread (cdr thread)))
6744
6745 ;; Summary article oriented commands
6746
6747 (defun gnus-summary-refer-parent-article (n)
6748   "Refer parent article N times.
6749 If N is negative, go to ancestor -N instead.
6750 The difference between N and the number of articles fetched is returned."
6751   (interactive "p")
6752   (let ((skip 1)
6753         error header ref)
6754     (when (not (natnump n))
6755       (setq skip (abs n)
6756             n 1))
6757     (while (and (> n 0)
6758                 (not error))
6759       (setq header (gnus-summary-article-header))
6760       (if (and (eq (mail-header-number header)
6761                    (cdr gnus-article-current))
6762                (equal gnus-newsgroup-name
6763                       (car gnus-article-current)))
6764           ;; If we try to find the parent of the currently
6765           ;; displayed article, then we take a look at the actual
6766           ;; References header, since this is slightly more
6767           ;; reliable than the References field we got from the
6768           ;; server.
6769           (save-excursion
6770             (set-buffer gnus-original-article-buffer)
6771             (nnheader-narrow-to-headers)
6772             (unless (setq ref (message-fetch-field "references"))
6773               (setq ref (message-fetch-field "in-reply-to")))
6774             (widen))
6775         (setq ref
6776               ;; It's not the current article, so we take a bet on
6777               ;; the value we got from the server.
6778               (mail-header-references header)))
6779       (if (and ref
6780                (not (equal ref "")))
6781           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6782             (gnus-message 1 "Couldn't find parent"))
6783         (gnus-message 1 "No references in article %d"
6784                       (gnus-summary-article-number))
6785         (setq error t))
6786       (decf n))
6787     (gnus-summary-position-point)
6788     n))
6789
6790 (defun gnus-summary-refer-references ()
6791   "Fetch all articles mentioned in the References header.
6792 Return the number of articles fetched."
6793   (interactive)
6794   (let ((ref (mail-header-references (gnus-summary-article-header)))
6795         (current (gnus-summary-article-number))
6796         (n 0))
6797     (if (or (not ref)
6798             (equal ref ""))
6799         (error "No References in the current article")
6800       ;; For each Message-ID in the References header...
6801       (while (string-match "<[^>]*>" ref)
6802         (incf n)
6803         ;; ... fetch that article.
6804         (gnus-summary-refer-article
6805          (prog1 (match-string 0 ref)
6806            (setq ref (substring ref (match-end 0))))))
6807       (gnus-summary-goto-subject current)
6808       (gnus-summary-position-point)
6809       n)))
6810
6811 (defun gnus-summary-refer-thread (&optional limit)
6812   "Fetch all articles in the current thread.
6813 If LIMIT (the numerical prefix), fetch that many old headers instead
6814 of what's specified by the `gnus-refer-thread-limit' variable."
6815   (interactive "P")
6816   (let ((id (mail-header-id (gnus-summary-article-header)))
6817         (limit (if limit (prefix-numeric-value limit)
6818                  gnus-refer-thread-limit)))
6819     ;; We want to fetch LIMIT *old* headers, but we also have to
6820     ;; re-fetch all the headers in the current buffer, because many of
6821     ;; them may be undisplayed.  So we adjust LIMIT.
6822     (when (numberp limit)
6823       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6824     (unless (eq gnus-fetch-old-headers 'invisible)
6825       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6826       ;; Retrieve the headers and read them in.
6827       (if (eq (gnus-retrieve-headers
6828                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6829               'nov)
6830           (gnus-build-all-threads)
6831         (error "Can't fetch thread from backends that don't support NOV"))
6832       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6833     (gnus-summary-limit-include-thread id)))
6834
6835 (defun gnus-summary-refer-article (message-id)
6836   "Fetch an article specified by MESSAGE-ID."
6837   (interactive "sMessage-ID: ")
6838   (when (and (stringp message-id)
6839              (not (zerop (length message-id))))
6840     ;; Construct the correct Message-ID if necessary.
6841     ;; Suggested by tale@pawl.rpi.edu.
6842     (unless (string-match "^<" message-id)
6843       (setq message-id (concat "<" message-id)))
6844     (unless (string-match ">$" message-id)
6845       (setq message-id (concat message-id ">")))
6846     (let* ((header (gnus-id-to-header message-id))
6847            (sparse (and header
6848                         (gnus-summary-article-sparse-p
6849                          (mail-header-number header))
6850                         (memq (mail-header-number header)
6851                               gnus-newsgroup-limit)))
6852            number)
6853       (cond
6854        ;; If the article is present in the buffer we just go to it.
6855        ((and header
6856              (or (not (gnus-summary-article-sparse-p
6857                        (mail-header-number header)))
6858                  sparse))
6859         (prog1
6860             (gnus-summary-goto-article
6861              (mail-header-number header) nil t)
6862           (when sparse
6863             (gnus-summary-update-article (mail-header-number header)))))
6864        (t
6865         ;; We fetch the article.
6866         (catch 'found
6867           (dolist (gnus-override-method (gnus-refer-article-methods))
6868             (gnus-check-server gnus-override-method)
6869             ;; Fetch the header, and display the article.
6870             (when (setq number (gnus-summary-insert-subject message-id))
6871               (gnus-summary-select-article nil nil nil number)
6872               (throw 'found t)))
6873           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6874
6875 (defun gnus-refer-article-methods ()
6876   "Return a list of referrable methods."
6877   (cond
6878    ;; No method, so we default to current and native.
6879    ((null gnus-refer-article-method)
6880     (list gnus-current-select-method gnus-select-method))
6881    ;; Current.
6882    ((eq 'current gnus-refer-article-method)
6883     (list gnus-current-select-method))
6884    ;; List of select methods.
6885    ((not (and (symbolp (car gnus-refer-article-method))
6886               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6887     (let (out)
6888       (dolist (method gnus-refer-article-method)
6889         (push (if (eq 'current method)
6890                   gnus-current-select-method
6891                 method)
6892               out))
6893       (nreverse out)))
6894    ;; One single select method.
6895    (t
6896     (list gnus-refer-article-method))))
6897
6898 (defun gnus-summary-edit-parameters ()
6899   "Edit the group parameters of the current group."
6900   (interactive)
6901   (gnus-group-edit-group gnus-newsgroup-name 'params))
6902
6903 (defun gnus-summary-customize-parameters ()
6904   "Customize the group parameters of the current group."
6905   (interactive)
6906   (gnus-group-customize gnus-newsgroup-name))
6907
6908 (defun gnus-summary-enter-digest-group (&optional force)
6909   "Enter an nndoc group based on the current article.
6910 If FORCE, force a digest interpretation.  If not, try
6911 to guess what the document format is."
6912   (interactive "P")
6913   (let ((conf gnus-current-window-configuration))
6914     (save-excursion
6915       (gnus-summary-select-article))
6916     (setq gnus-current-window-configuration conf)
6917     (let* ((name (format "%s-%d"
6918                          (gnus-group-prefixed-name
6919                           gnus-newsgroup-name (list 'nndoc ""))
6920                          (save-excursion
6921                            (set-buffer gnus-summary-buffer)
6922                            gnus-current-article)))
6923            (ogroup gnus-newsgroup-name)
6924            (params (append (gnus-info-params (gnus-get-info ogroup))
6925                            (list (cons 'to-group ogroup))
6926                            (list (cons 'save-article-group ogroup))))
6927            (case-fold-search t)
6928            (buf (current-buffer))
6929            dig to-address)
6930       (save-excursion
6931         (set-buffer gnus-original-article-buffer)
6932         ;; Have the digest group inherit the main mail address of
6933         ;; the parent article.
6934         (when (setq to-address (or (message-fetch-field "reply-to")
6935                                    (message-fetch-field "from")))
6936           (setq params (append 
6937                         (list (cons 'to-address 
6938                                     (funcall gnus-decode-encoded-word-function
6939                                              to-address))))))
6940         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6941         (insert-buffer-substring gnus-original-article-buffer)
6942         ;; Remove lines that may lead nndoc to misinterpret the
6943         ;; document type.
6944         (narrow-to-region
6945          (goto-char (point-min))
6946          (or (search-forward "\n\n" nil t) (point)))
6947         (goto-char (point-min))
6948         (delete-matching-lines "^Path:\\|^From ")
6949         (widen))
6950       (unwind-protect
6951           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6952                     (gnus-newsgroup-ephemeral-ignored-charsets
6953                      gnus-newsgroup-ignored-charsets))
6954                 (gnus-group-read-ephemeral-group
6955                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6956                               (nndoc-article-type
6957                                ,(if force 'mbox 'guess))) t))
6958               ;; Make all postings to this group go to the parent group.
6959               (nconc (gnus-info-params (gnus-get-info name))
6960                      params)
6961             ;; Couldn't select this doc group.
6962             (switch-to-buffer buf)
6963             (gnus-set-global-variables)
6964             (gnus-configure-windows 'summary)
6965             (gnus-message 3 "Article couldn't be entered?"))
6966         (kill-buffer dig)))))
6967
6968 (defun gnus-summary-read-document (n)
6969   "Open a new group based on the current article(s).
6970 This will allow you to read digests and other similar
6971 documents as newsgroups.
6972 Obeys the standard process/prefix convention."
6973   (interactive "P")
6974   (let* ((articles (gnus-summary-work-articles n))
6975          (ogroup gnus-newsgroup-name)
6976          (params (append (gnus-info-params (gnus-get-info ogroup))
6977                          (list (cons 'to-group ogroup))))
6978          article group egroup groups vgroup)
6979     (while (setq article (pop articles))
6980       (setq group (format "%s-%d" gnus-newsgroup-name article))
6981       (gnus-summary-remove-process-mark article)
6982       (when (gnus-summary-display-article article)
6983         (save-excursion
6984           (with-temp-buffer
6985             (insert-buffer-substring gnus-original-article-buffer)
6986             ;; Remove some headers that may lead nndoc to make
6987             ;; the wrong guess.
6988             (message-narrow-to-head)
6989             (goto-char (point-min))
6990             (delete-matching-lines "^\\(Path\\):\\|^From ")
6991             (widen)
6992             (if (setq egroup
6993                       (gnus-group-read-ephemeral-group
6994                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6995                                      (nndoc-article-type guess))
6996                        t nil t))
6997                 (progn
6998                   ;; Make all postings to this group go to the parent group.
6999                   (nconc (gnus-info-params (gnus-get-info egroup))
7000                          params)
7001                   (push egroup groups))
7002               ;; Couldn't select this doc group.
7003               (gnus-error 3 "Article couldn't be entered"))))))
7004     ;; Now we have selected all the documents.
7005     (cond
7006      ((not groups)
7007       (error "None of the articles could be interpreted as documents"))
7008      ((gnus-group-read-ephemeral-group
7009        (setq vgroup (format
7010                      "nnvirtual:%s-%s" gnus-newsgroup-name
7011                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7012        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7013        t
7014        (cons (current-buffer) 'summary)))
7015      (t
7016       (error "Couldn't select virtual nndoc group")))))
7017
7018 (defun gnus-summary-isearch-article (&optional regexp-p)
7019   "Do incremental search forward on the current article.
7020 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7021   (interactive "P")
7022   (gnus-summary-select-article)
7023   (gnus-configure-windows 'article)
7024   (gnus-eval-in-buffer-window gnus-article-buffer
7025     (save-restriction
7026       (widen)
7027       (isearch-forward regexp-p))))
7028
7029 (defun gnus-summary-search-article-forward (regexp &optional backward)
7030   "Search for an article containing REGEXP forward.
7031 If BACKWARD, search backward instead."
7032   (interactive
7033    (list (read-string
7034           (format "Search article %s (regexp%s): "
7035                   (if current-prefix-arg "backward" "forward")
7036                   (if gnus-last-search-regexp
7037                       (concat ", default " gnus-last-search-regexp)
7038                     "")))
7039          current-prefix-arg))
7040   (if (string-equal regexp "")
7041       (setq regexp (or gnus-last-search-regexp ""))
7042     (setq gnus-last-search-regexp regexp))
7043   (if (gnus-summary-search-article regexp backward)
7044       (gnus-summary-show-thread)
7045     (error "Search failed: \"%s\"" regexp)))
7046
7047 (defun gnus-summary-search-article-backward (regexp)
7048   "Search for an article containing REGEXP backward."
7049   (interactive
7050    (list (read-string
7051           (format "Search article backward (regexp%s): "
7052                   (if gnus-last-search-regexp
7053                       (concat ", default " gnus-last-search-regexp)
7054                     "")))))
7055   (gnus-summary-search-article-forward regexp 'backward))
7056
7057 (defun gnus-summary-search-article (regexp &optional backward)
7058   "Search for an article containing REGEXP.
7059 Optional argument BACKWARD means do search for backward.
7060 `gnus-select-article-hook' is not called during the search."
7061   ;; We have to require this here to make sure that the following
7062   ;; dynamic binding isn't shadowed by autoloading.
7063   (require 'gnus-async)
7064   (require 'gnus-art)
7065   (let ((gnus-select-article-hook nil)  ;Disable hook.
7066         (gnus-article-prepare-hook nil)
7067         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7068         (gnus-use-article-prefetch nil)
7069         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7070         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7071         (sum (current-buffer))
7072         (gnus-display-mime-function nil)
7073         (found nil)
7074         point)
7075     (gnus-save-hidden-threads
7076       (gnus-summary-select-article)
7077       (set-buffer gnus-article-buffer)
7078       (goto-char (window-point (get-buffer-window (current-buffer))))
7079       (when backward
7080         (forward-line -1))
7081       (while (not found)
7082         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7083         (if (if backward
7084                 (re-search-backward regexp nil t)
7085               (re-search-forward regexp nil t))
7086             ;; We found the regexp.
7087             (progn
7088               (setq found 'found)
7089               (beginning-of-line)
7090               (set-window-start
7091                (get-buffer-window (current-buffer))
7092                (point))
7093               (forward-line 1)
7094               (set-window-point
7095                (get-buffer-window (current-buffer))
7096                (point))
7097               (set-buffer sum)
7098               (setq point (point)))
7099           ;; We didn't find it, so we go to the next article.
7100           (set-buffer sum)
7101           (setq found 'not)
7102           (while (eq found 'not)
7103             (if (not (if backward (gnus-summary-find-prev)
7104                        (gnus-summary-find-next)))
7105                 ;; No more articles.
7106                 (setq found t)
7107               ;; Select the next article and adjust point.
7108               (unless (gnus-summary-article-sparse-p
7109                        (gnus-summary-article-number))
7110                 (setq found nil)
7111                 (gnus-summary-select-article)
7112                 (set-buffer gnus-article-buffer)
7113                 (widen)
7114                 (goto-char (if backward (point-max) (point-min))))))))
7115       (gnus-message 7 ""))
7116     ;; Return whether we found the regexp.
7117     (when (eq found 'found)
7118       (goto-char point)
7119       (gnus-summary-show-thread)
7120       (gnus-summary-goto-subject gnus-current-article)
7121       (gnus-summary-position-point)
7122       t)))
7123
7124 (defun gnus-summary-find-matching (header regexp &optional backward unread
7125                                           not-case-fold)
7126   "Return a list of all articles that match REGEXP on HEADER.
7127 The search stars on the current article and goes forwards unless
7128 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7129 If UNREAD is non-nil, only unread articles will
7130 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7131 in the comparisons."
7132   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7133                 (gnus-data-find-list
7134                  (gnus-summary-article-number) (gnus-data-list backward))))
7135         (case-fold-search (not not-case-fold))
7136         articles d func)
7137     (if (consp header)
7138         (if (eq (car header) 'extra)
7139             (setq func
7140                   `(lambda (h)
7141                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7142                          "")))
7143           (error "%s is an invalid header" header))
7144       (unless (fboundp (intern (concat "mail-header-" header)))
7145         (error "%s is not a valid header" header))
7146       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7147     (while data
7148       (setq d (car data))
7149       (and (or (not unread)             ; We want all articles...
7150                (gnus-data-unread-p d))  ; Or just unreads.
7151            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7152            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7153            (push (gnus-data-number d) articles)) ; Success!
7154       (setq data (cdr data)))
7155     (nreverse articles)))
7156
7157 (defun gnus-summary-execute-command (header regexp command &optional backward)
7158   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7159 If HEADER is an empty string (or nil), the match is done on the entire
7160 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7161   (interactive
7162    (list (let ((completion-ignore-case t))
7163            (completing-read
7164             "Header name: "
7165             (mapcar (lambda (string) (list string))
7166                     '("Number" "Subject" "From" "Lines" "Date"
7167                       "Message-ID" "Xref" "References" "Body"))
7168             nil 'require-match))
7169          (read-string "Regexp: ")
7170          (read-key-sequence "Command: ")
7171          current-prefix-arg))
7172   (when (equal header "Body")
7173     (setq header ""))
7174   ;; Hidden thread subtrees must be searched as well.
7175   (gnus-summary-show-all-threads)
7176   ;; We don't want to change current point nor window configuration.
7177   (save-excursion
7178     (save-window-excursion
7179       (gnus-message 6 "Executing %s..." (key-description command))
7180       ;; We'd like to execute COMMAND interactively so as to give arguments.
7181       (gnus-execute header regexp
7182                     `(call-interactively ',(key-binding command))
7183                     backward)
7184       (gnus-message 6 "Executing %s...done" (key-description command)))))
7185
7186 (defun gnus-summary-beginning-of-article ()
7187   "Scroll the article back to the beginning."
7188   (interactive)
7189   (gnus-summary-select-article)
7190   (gnus-configure-windows 'article)
7191   (gnus-eval-in-buffer-window gnus-article-buffer
7192     (widen)
7193     (goto-char (point-min))
7194     (when gnus-page-broken
7195       (gnus-narrow-to-page))))
7196
7197 (defun gnus-summary-end-of-article ()
7198   "Scroll to the end of the article."
7199   (interactive)
7200   (gnus-summary-select-article)
7201   (gnus-configure-windows 'article)
7202   (gnus-eval-in-buffer-window gnus-article-buffer
7203     (widen)
7204     (goto-char (point-max))
7205     (recenter -3)
7206     (when gnus-page-broken
7207       (gnus-narrow-to-page))))
7208
7209 (defun gnus-summary-print-article (&optional filename n)
7210   "Generate and print a PostScript image of the N next (mail) articles.
7211
7212 If N is negative, print the N previous articles.  If N is nil and articles
7213 have been marked with the process mark, print these instead.
7214
7215 If the optional first argument FILENAME is nil, send the image to the
7216 printer.  If FILENAME is a string, save the PostScript image in a file with
7217 that name.  If FILENAME is a number, prompt the user for the name of the file
7218 to save in."
7219   (interactive (list (ps-print-preprint current-prefix-arg)
7220                      current-prefix-arg))
7221   (dolist (article (gnus-summary-work-articles n))
7222     (gnus-summary-select-article nil nil 'pseudo article)
7223     (gnus-eval-in-buffer-window gnus-article-buffer
7224       (let ((buffer (generate-new-buffer " *print*")))
7225         (unwind-protect
7226             (progn
7227               (copy-to-buffer buffer (point-min) (point-max))
7228               (set-buffer buffer)
7229               (gnus-article-delete-invisible-text)
7230               (let ((ps-left-header
7231                      (list
7232                       (concat "("
7233                               (mail-header-subject gnus-current-headers) ")")
7234                       (concat "("
7235                               (mail-header-from gnus-current-headers) ")")))
7236                     (ps-right-header
7237                      (list
7238                       "/pagenumberstring load"
7239                       (concat "("
7240                               (mail-header-date gnus-current-headers) ")"))))
7241                 (gnus-run-hooks 'gnus-ps-print-hook)
7242                 (save-excursion
7243                   (ps-print-buffer-with-faces filename))))
7244           (kill-buffer buffer))))))
7245
7246 (defun gnus-summary-show-article (&optional arg)
7247   "Force re-fetching of the current article.
7248 If ARG (the prefix) is a number, show the article with the charset
7249 defined in `gnus-summary-show-article-charset-alist', or the charset
7250 inputed.
7251 If ARG (the prefix) is non-nil and not a number, show the raw article
7252 without any article massaging functions being run."
7253   (interactive "P")
7254   (cond
7255    ((numberp arg)
7256     (let ((gnus-newsgroup-charset
7257            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7258                (read-coding-system "Charset: ")))
7259           (gnus-newsgroup-ignored-charsets 'gnus-all))
7260       (gnus-summary-select-article nil 'force)))
7261    ((not arg)
7262     ;; Select the article the normal way.
7263     (gnus-summary-select-article nil 'force))
7264    (t
7265     ;; We have to require this here to make sure that the following
7266     ;; dynamic binding isn't shadowed by autoloading.
7267     (require 'gnus-async)
7268     (require 'gnus-art)
7269     ;; Bind the article treatment functions to nil.
7270     (let ((gnus-have-all-headers t)
7271           gnus-article-prepare-hook
7272           gnus-article-decode-hook
7273           gnus-display-mime-function
7274           gnus-break-pages)
7275       ;; Destroy any MIME parts.
7276       (when (gnus-buffer-live-p gnus-article-buffer)
7277         (save-excursion
7278           (set-buffer gnus-article-buffer)
7279           (mm-destroy-parts gnus-article-mime-handles)
7280           ;; Set it to nil for safety reason.
7281           (setq gnus-article-mime-handle-alist nil)
7282           (setq gnus-article-mime-handles nil)))
7283       (gnus-summary-select-article nil 'force))))
7284   (gnus-summary-goto-subject gnus-current-article)
7285   (gnus-summary-position-point))
7286
7287 (defun gnus-summary-verbose-headers (&optional arg)
7288   "Toggle permanent full header display.
7289 If ARG is a positive number, turn header display on.
7290 If ARG is a negative number, turn header display off."
7291   (interactive "P")
7292   (setq gnus-show-all-headers
7293         (cond ((or (not (numberp arg))
7294                    (zerop arg))
7295                (not gnus-show-all-headers))
7296               ((natnump arg)
7297                t)))
7298   (gnus-summary-show-article))
7299
7300 (defun gnus-summary-toggle-header (&optional arg)
7301   "Show the headers if they are hidden, or hide them if they are shown.
7302 If ARG is a positive number, show the entire header.
7303 If ARG is a negative number, hide the unwanted header lines."
7304   (interactive "P")
7305   (save-excursion
7306     (set-buffer gnus-article-buffer)
7307     (save-restriction
7308       (let* ((buffer-read-only nil)
7309              (inhibit-point-motion-hooks t)
7310              hidden e)
7311         (setq hidden
7312               (if (numberp arg)
7313                   (>= arg 0)
7314                 (save-restriction
7315                   (article-narrow-to-head)
7316                   (gnus-article-hidden-text-p 'headers))))
7317         (goto-char (point-min))
7318         (when (search-forward "\n\n" nil t)
7319           (delete-region (point-min) (1- (point))))
7320         (goto-char (point-min))
7321         (save-excursion
7322           (set-buffer gnus-original-article-buffer)
7323           (goto-char (point-min))
7324           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7325         (insert-buffer-substring gnus-original-article-buffer 1 e)
7326         (save-restriction
7327           (narrow-to-region (point-min) (point))
7328           (article-decode-encoded-words)
7329           (if  hidden
7330               (let ((gnus-treat-hide-headers nil)
7331                     (gnus-treat-hide-boring-headers nil))
7332                 (setq gnus-article-wash-types
7333                       (delq 'headers gnus-article-wash-types))
7334                 (gnus-treat-article 'head))
7335             (gnus-treat-article 'head)))
7336         (gnus-set-mode-line 'article)))))
7337
7338 (defun gnus-summary-show-all-headers ()
7339   "Make all header lines visible."
7340   (interactive)
7341   (gnus-article-show-all-headers))
7342
7343 (defun gnus-summary-caesar-message (&optional arg)
7344   "Caesar rotate the current article by 13.
7345 The numerical prefix specifies how many places to rotate each letter
7346 forward."
7347   (interactive "P")
7348   (gnus-summary-select-article)
7349   (let ((mail-header-separator ""))
7350     (gnus-eval-in-buffer-window gnus-article-buffer
7351       (save-restriction
7352         (widen)
7353         (let ((start (window-start))
7354               buffer-read-only)
7355           (message-caesar-buffer-body arg)
7356           (set-window-start (get-buffer-window (current-buffer)) start))))))
7357
7358 (defun gnus-summary-stop-page-breaking ()
7359   "Stop page breaking in the current article."
7360   (interactive)
7361   (gnus-summary-select-article)
7362   (gnus-eval-in-buffer-window gnus-article-buffer
7363     (widen)
7364     (when (gnus-visual-p 'page-marker)
7365       (let ((buffer-read-only nil))
7366         (gnus-remove-text-with-property 'gnus-prev)
7367         (gnus-remove-text-with-property 'gnus-next))
7368       (setq gnus-page-broken nil))))
7369
7370 (defun gnus-summary-move-article (&optional n to-newsgroup
7371                                             select-method action)
7372   "Move the current article to a different newsgroup.
7373 If N is a positive number, move the N next articles.
7374 If N is a negative number, move the N previous articles.
7375 If N is nil and any articles have been marked with the process mark,
7376 move those articles instead.
7377 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7378 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7379 re-spool using this method.
7380
7381 For this function to work, both the current newsgroup and the
7382 newsgroup that you want to move to have to support the `request-move'
7383 and `request-accept' functions.
7384
7385 ACTION can be either `move' (the default), `crosspost' or `copy'."
7386   (interactive "P")
7387   (unless action
7388     (setq action 'move))
7389   ;; Disable marking as read.
7390   (let (gnus-mark-article-hook)
7391     (save-window-excursion
7392       (gnus-summary-select-article)))
7393   ;; Check whether the source group supports the required functions.
7394   (cond ((and (eq action 'move)
7395               (not (gnus-check-backend-function
7396                     'request-move-article gnus-newsgroup-name)))
7397          (error "The current group does not support article moving"))
7398         ((and (eq action 'crosspost)
7399               (not (gnus-check-backend-function
7400                     'request-replace-article gnus-newsgroup-name)))
7401          (error "The current group does not support article editing")))
7402   (let ((articles (gnus-summary-work-articles n))
7403         (prefix (if (gnus-check-backend-function
7404                     'request-move-article gnus-newsgroup-name)
7405                     (gnus-group-real-prefix gnus-newsgroup-name)
7406                   ""))
7407         (names '((move "Move" "Moving")
7408                  (copy "Copy" "Copying")
7409                  (crosspost "Crosspost" "Crossposting")))
7410         (copy-buf (save-excursion
7411                     (nnheader-set-temp-buffer " *copy article*")))
7412         art-group to-method new-xref article to-groups)
7413     (unless (assq action names)
7414       (error "Unknown action %s" action))
7415     ;; Read the newsgroup name.
7416     (when (and (not to-newsgroup)
7417                (not select-method))
7418       (setq to-newsgroup
7419             (gnus-read-move-group-name
7420              (cadr (assq action names))
7421              (symbol-value (intern (format "gnus-current-%s-group" action)))
7422              articles prefix))
7423       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7424     (setq to-method (or select-method
7425                         (gnus-server-to-method
7426                          (gnus-group-method to-newsgroup))))
7427     ;; Check the method we are to move this article to...
7428     (unless (gnus-check-backend-function
7429              'request-accept-article (car to-method))
7430       (error "%s does not support article copying" (car to-method)))
7431     (unless (gnus-check-server to-method)
7432       (error "Can't open server %s" (car to-method)))
7433     (gnus-message 6 "%s to %s: %s..."
7434                   (caddr (assq action names))
7435                   (or (car select-method) to-newsgroup) articles)
7436     (while articles
7437       (setq article (pop articles))
7438       (setq
7439        art-group
7440        (cond
7441         ;; Move the article.
7442         ((eq action 'move)
7443          ;; Remove this article from future suppression.
7444          (gnus-dup-unsuppress-article article)
7445          (gnus-request-move-article
7446           article                       ; Article to move
7447           gnus-newsgroup-name           ; From newsgroup
7448           (nth 1 (gnus-find-method-for-group
7449                   gnus-newsgroup-name)) ; Server
7450           (list 'gnus-request-accept-article
7451                 to-newsgroup (list 'quote select-method)
7452                 (not articles) t)       ; Accept form
7453           (not articles)))              ; Only save nov last time
7454         ;; Copy the article.
7455         ((eq action 'copy)
7456          (save-excursion
7457            (set-buffer copy-buf)
7458            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7459              (gnus-request-accept-article
7460               to-newsgroup select-method (not articles) t))))
7461         ;; Crosspost the article.
7462         ((eq action 'crosspost)
7463          (let ((xref (message-tokenize-header
7464                       (mail-header-xref (gnus-summary-article-header article))
7465                       " ")))
7466            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7467                                   ":" article))
7468            (unless xref
7469              (setq xref (list (system-name))))
7470            (setq new-xref
7471                  (concat
7472                   (mapconcat 'identity
7473                              (delete "Xref:" (delete new-xref xref))
7474                              " ")
7475                   " " new-xref))
7476            (save-excursion
7477              (set-buffer copy-buf)
7478              ;; First put the article in the destination group.
7479              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7480              (when (consp (setq art-group
7481                                 (gnus-request-accept-article
7482                                  to-newsgroup select-method (not articles))))
7483                (setq new-xref (concat new-xref " " (car art-group)
7484                                       ":" (cdr art-group)))
7485                ;; Now we have the new Xrefs header, so we insert
7486                ;; it and replace the new article.
7487                (nnheader-replace-header "Xref" new-xref)
7488                (gnus-request-replace-article
7489                 (cdr art-group) to-newsgroup (current-buffer))
7490                art-group))))))
7491       (cond
7492        ((not art-group)
7493         (gnus-message 1 "Couldn't %s article %s: %s"
7494                       (cadr (assq action names)) article
7495                       (nnheader-get-report (car to-method))))
7496        ((eq art-group 'junk)
7497         (when (eq action 'move)
7498           (gnus-summary-mark-article article gnus-canceled-mark)
7499           (gnus-message 4 "Deleted article %s" article)))
7500        (t
7501         (let* ((pto-group (gnus-group-prefixed-name
7502                            (car art-group) to-method))
7503                (entry
7504                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7505                (info (nth 2 entry))
7506                (to-group (gnus-info-group info))
7507                to-marks)
7508           ;; Update the group that has been moved to.
7509           (when (and info
7510                      (memq action '(move copy)))
7511             (unless (member to-group to-groups)
7512               (push to-group to-groups))
7513
7514             (unless (memq article gnus-newsgroup-unreads)
7515               (push 'read to-marks)
7516               (gnus-info-set-read
7517                info (gnus-add-to-range (gnus-info-read info)
7518                                        (list (cdr art-group)))))
7519
7520             ;; See whether the article is to be put in the cache.
7521             (let ((marks gnus-article-mark-lists)
7522                   (to-article (cdr art-group)))
7523
7524               ;; Enter the article into the cache in the new group,
7525               ;; if that is required.
7526               (when gnus-use-cache
7527                 (gnus-cache-possibly-enter-article
7528                  to-group to-article
7529                  (memq article gnus-newsgroup-marked)
7530                  (memq article gnus-newsgroup-dormant)
7531                  (memq article gnus-newsgroup-unreads)))
7532
7533               (when gnus-preserve-marks
7534                 ;; Copy any marks over to the new group.
7535                 (when (and (equal to-group gnus-newsgroup-name)
7536                            (not (memq article gnus-newsgroup-unreads)))
7537                   ;; Mark this article as read in this group.
7538                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7539                   (setcdr (gnus-active to-group) to-article)
7540                   (setcdr gnus-newsgroup-active to-article))
7541
7542                 (while marks
7543                   (when (memq article (symbol-value
7544                                        (intern (format "gnus-newsgroup-%s"
7545                                                        (caar marks)))))
7546                     (push (cdar marks) to-marks)
7547                     ;; If the other group is the same as this group,
7548                     ;; then we have to add the mark to the list.
7549                     (when (equal to-group gnus-newsgroup-name)
7550                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7551                            (cons to-article
7552                                  (symbol-value
7553                                   (intern (format "gnus-newsgroup-%s"
7554                                                   (caar marks)))))))
7555                     ;; Copy the marks to other group.
7556                     (gnus-add-marked-articles
7557                      to-group (cdar marks) (list to-article) info))
7558                   (setq marks (cdr marks)))
7559
7560                 (gnus-request-set-mark to-group (list (list (list to-article)
7561                                                             'set
7562                                                             to-marks))))
7563
7564               (gnus-dribble-enter
7565                (concat "(gnus-group-set-info '"
7566                        (gnus-prin1-to-string (gnus-get-info to-group))
7567                        ")"))))
7568
7569           ;; Update the Xref header in this article to point to
7570           ;; the new crossposted article we have just created.
7571           (when (eq action 'crosspost)
7572             (save-excursion
7573               (set-buffer copy-buf)
7574               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7575               (nnheader-replace-header "Xref" new-xref)
7576               (gnus-request-replace-article
7577                article gnus-newsgroup-name (current-buffer)))))
7578
7579         ;;;!!!Why is this necessary?
7580         (set-buffer gnus-summary-buffer)
7581
7582         (gnus-summary-goto-subject article)
7583         (when (eq action 'move)
7584           (gnus-summary-mark-article article gnus-canceled-mark))))
7585       (gnus-summary-remove-process-mark article))
7586     ;; Re-activate all groups that have been moved to.
7587     (while to-groups
7588       (save-excursion
7589         (set-buffer gnus-group-buffer)
7590         (when (gnus-group-goto-group (car to-groups) t)
7591           (gnus-group-get-new-news-this-group 1 t))
7592         (pop to-groups)))
7593
7594     (gnus-kill-buffer copy-buf)
7595     (gnus-summary-position-point)
7596     (gnus-set-mode-line 'summary)))
7597
7598 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7599   "Move the current article to a different newsgroup.
7600 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7601 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7602 re-spool using this method."
7603   (interactive "P")
7604   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7605
7606 (defun gnus-summary-crosspost-article (&optional n)
7607   "Crosspost the current article to some other group."
7608   (interactive "P")
7609   (gnus-summary-move-article n nil nil 'crosspost))
7610
7611 (defcustom gnus-summary-respool-default-method nil
7612   "Default method for respooling an article.
7613 If nil, use to the current newsgroup method."
7614   :type '(choice (gnus-select-method :value (nnml ""))
7615                  (const nil))
7616   :group 'gnus-summary-mail)
7617
7618 (defun gnus-summary-respool-article (&optional n method)
7619   "Respool the current article.
7620 The article will be squeezed through the mail spooling process again,
7621 which means that it will be put in some mail newsgroup or other
7622 depending on `nnmail-split-methods'.
7623 If N is a positive number, respool the N next articles.
7624 If N is a negative number, respool the N previous articles.
7625 If N is nil and any articles have been marked with the process mark,
7626 respool those articles instead.
7627
7628 Respooling can be done both from mail groups and \"real\" newsgroups.
7629 In the former case, the articles in question will be moved from the
7630 current group into whatever groups they are destined to.  In the
7631 latter case, they will be copied into the relevant groups."
7632   (interactive
7633    (list current-prefix-arg
7634          (let* ((methods (gnus-methods-using 'respool))
7635                 (methname
7636                  (symbol-name (or gnus-summary-respool-default-method
7637                                   (car (gnus-find-method-for-group
7638                                         gnus-newsgroup-name)))))
7639                 (method
7640                  (gnus-completing-read
7641                   methname "What backend do you want to use when respooling?"
7642                   methods nil t nil 'gnus-mail-method-history))
7643                 ms)
7644            (cond
7645             ((zerop (length (setq ms (gnus-servers-using-backend
7646                                       (intern method)))))
7647              (list (intern method) ""))
7648             ((= 1 (length ms))
7649              (car ms))
7650             (t
7651              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7652                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7653                            ms-alist))))))))
7654   (unless method
7655     (error "No method given for respooling"))
7656   (if (assoc (symbol-name
7657               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7658              (gnus-methods-using 'respool))
7659       (gnus-summary-move-article n nil method)
7660     (gnus-summary-copy-article n nil method)))
7661
7662 (defun gnus-summary-import-article (file)
7663   "Import an arbitrary file into a mail newsgroup."
7664   (interactive "fImport file: ")
7665   (let ((group gnus-newsgroup-name)
7666         (now (current-time))
7667         atts lines)
7668     (unless (gnus-check-backend-function 'request-accept-article group)
7669       (error "%s does not support article importing" group))
7670     (or (file-readable-p file)
7671         (not (file-regular-p file))
7672         (error "Can't read %s" file))
7673     (save-excursion
7674       (set-buffer (gnus-get-buffer-create " *import file*"))
7675       (erase-buffer)
7676       (nnheader-insert-file-contents file)
7677       (goto-char (point-min))
7678       (unless (nnheader-article-p)
7679         ;; This doesn't look like an article, so we fudge some headers.
7680         (setq atts (file-attributes file)
7681               lines (count-lines (point-min) (point-max)))
7682         (insert "From: " (read-string "From: ") "\n"
7683                 "Subject: " (read-string "Subject: ") "\n"
7684                 "Date: " (message-make-date (nth 5 atts))
7685                 "\n"
7686                 "Message-ID: " (message-make-message-id) "\n"
7687                 "Lines: " (int-to-string lines) "\n"
7688                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7689       (gnus-request-accept-article group nil t)
7690       (kill-buffer (current-buffer)))))
7691
7692 (defun gnus-summary-article-posted-p ()
7693   "Say whether the current (mail) article is available from news as well.
7694 This will be the case if the article has both been mailed and posted."
7695   (interactive)
7696   (let ((id (mail-header-references (gnus-summary-article-header)))
7697         (gnus-override-method (car (gnus-refer-article-methods))))
7698     (if (gnus-request-head id "")
7699         (gnus-message 2 "The current message was found on %s"
7700                       gnus-override-method)
7701       (gnus-message 2 "The current message couldn't be found on %s"
7702                     gnus-override-method)
7703       nil)))
7704
7705 (defun gnus-summary-expire-articles (&optional now)
7706   "Expire all articles that are marked as expirable in the current group."
7707   (interactive)
7708   (when (gnus-check-backend-function
7709          'request-expire-articles gnus-newsgroup-name)
7710     ;; This backend supports expiry.
7711     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7712            (expirable (if total
7713                           (progn
7714                             ;; We need to update the info for
7715                             ;; this group for `gnus-list-of-read-articles'
7716                             ;; to give us the right answer.
7717                             (gnus-run-hooks 'gnus-exit-group-hook)
7718                             (gnus-summary-update-info)
7719                             (gnus-list-of-read-articles gnus-newsgroup-name))
7720                         (setq gnus-newsgroup-expirable
7721                               (sort gnus-newsgroup-expirable '<))))
7722            (expiry-wait (if now 'immediate
7723                           (gnus-group-find-parameter
7724                            gnus-newsgroup-name 'expiry-wait)))
7725            (nnmail-expiry-target
7726             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7727                 nnmail-expiry-target))
7728            es)
7729       (when expirable
7730         ;; There are expirable articles in this group, so we run them
7731         ;; through the expiry process.
7732         (gnus-message 6 "Expiring articles...")
7733         (unless (gnus-check-group gnus-newsgroup-name)
7734           (error "Can't open server for %s" gnus-newsgroup-name))
7735         ;; The list of articles that weren't expired is returned.
7736         (save-excursion
7737           (if expiry-wait
7738               (let ((nnmail-expiry-wait-function nil)
7739                     (nnmail-expiry-wait expiry-wait))
7740                 (setq es (gnus-request-expire-articles
7741                           expirable gnus-newsgroup-name)))
7742             (setq es (gnus-request-expire-articles
7743                       expirable gnus-newsgroup-name))))
7744         (unless total
7745           (setq gnus-newsgroup-expirable es))
7746         ;; We go through the old list of expirable, and mark all
7747         ;; really expired articles as nonexistent.
7748         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7749           (let ((gnus-use-cache nil))
7750             (while expirable
7751               (unless (memq (car expirable) es)
7752                 (when (gnus-data-find (car expirable))
7753                   (gnus-summary-mark-article
7754                    (car expirable) gnus-canceled-mark)))
7755               (setq expirable (cdr expirable)))))
7756         (gnus-message 6 "Expiring articles...done")))))
7757
7758 (defun gnus-summary-expire-articles-now ()
7759   "Expunge all expirable articles in the current group.
7760 This means that *all* articles that are marked as expirable will be
7761 deleted forever, right now."
7762   (interactive)
7763   (or gnus-expert-user
7764       (gnus-yes-or-no-p
7765        "Are you really, really, really sure you want to delete all these messages? ")
7766       (error "Phew!"))
7767   (gnus-summary-expire-articles t))
7768
7769 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7770 (defun gnus-summary-delete-article (&optional n)
7771   "Delete the N next (mail) articles.
7772 This command actually deletes articles.  This is not a marking
7773 command.  The article will disappear forever from your life, never to
7774 return.
7775 If N is negative, delete backwards.
7776 If N is nil and articles have been marked with the process mark,
7777 delete these instead."
7778   (interactive "P")
7779   (unless (gnus-check-backend-function 'request-expire-articles
7780                                        gnus-newsgroup-name)
7781     (error "The current newsgroup does not support article deletion"))
7782   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7783     (error "Couldn't open server"))
7784   ;; Compute the list of articles to delete.
7785   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7786         not-deleted)
7787     (if (and gnus-novice-user
7788              (not (gnus-yes-or-no-p
7789                    (format "Do you really want to delete %s forever? "
7790                            (if (> (length articles) 1)
7791                                (format "these %s articles" (length articles))
7792                              "this article")))))
7793         ()
7794       ;; Delete the articles.
7795       (setq not-deleted (gnus-request-expire-articles
7796                          articles gnus-newsgroup-name 'force))
7797       (while articles
7798         (gnus-summary-remove-process-mark (car articles))
7799         ;; The backend might not have been able to delete the article
7800         ;; after all.
7801         (unless (memq (car articles) not-deleted)
7802           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7803         (setq articles (cdr articles)))
7804       (when not-deleted
7805         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7806     (gnus-summary-position-point)
7807     (gnus-set-mode-line 'summary)
7808     not-deleted))
7809
7810 (defun gnus-summary-edit-article (&optional arg)
7811   "Edit the current article.
7812 This will have permanent effect only in mail groups.
7813 If ARG is nil, edit the decoded articles.
7814 If ARG is 1, edit the raw articles. 
7815 If ARG is 2, edit the raw articles even in read-only groups.
7816 Otherwise, allow editing of articles even in read-only
7817 groups."
7818   (interactive "P")
7819   (let (force raw)
7820     (cond 
7821      ((null arg))
7822      ((eq arg 1) (setq raw t))
7823      ((eq arg 2) (setq raw t
7824                        force t))
7825      (t (setq force t)))
7826     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7827         (error "Can't edit the raw article in group nndraft:drafts."))
7828     (save-excursion
7829       (set-buffer gnus-summary-buffer)
7830       (let ((mail-parse-charset gnus-newsgroup-charset)
7831             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7832         (gnus-set-global-variables)
7833         (when (and (not force)
7834                    (gnus-group-read-only-p))
7835           (error "The current newsgroup does not support article editing"))
7836         (gnus-summary-show-article t)
7837         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7838           (with-current-buffer gnus-article-buffer
7839             (mm-enable-multibyte-mule4)))
7840         (if (equal gnus-newsgroup-name "nndraft:drafts")
7841             (setq raw t))
7842         (gnus-article-edit-article
7843          (if raw 'ignore 
7844            #'(lambda () 
7845                (let ((mbl mml-buffer-list))
7846                  (setq mml-buffer-list nil)
7847                  (mime-to-mml)
7848                  (make-local-hook 'kill-buffer-hook)
7849                  (let ((mml-buffer-list mml-buffer-list))
7850                    (setq mml-buffer-list mbl)
7851                    (make-local-variable 'mml-buffer-list))
7852                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7853          `(lambda (no-highlight)
7854             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7855                   (message-options message-options)
7856                   (mail-parse-ignored-charsets 
7857                    ',gnus-newsgroup-ignored-charsets))
7858               ,(if (not raw) '(progn 
7859                                 (mml-to-mime)
7860                                 (mml-destroy-buffers)
7861                                 (remove-hook 'kill-buffer-hook 
7862                                              'mml-destroy-buffers t)
7863                                 (kill-local-variable 'mml-buffer-list)))
7864               (gnus-summary-edit-article-done
7865                ,(or (mail-header-references gnus-current-headers) "")
7866                ,(gnus-group-read-only-p) 
7867                ,gnus-summary-buffer no-highlight))))))))
7868
7869 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7870
7871 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7872                                                  no-highlight)
7873   "Make edits to the current article permanent."
7874   (interactive)
7875   ;; Replace the article.
7876   (let ((buf (current-buffer)))
7877     (with-temp-buffer
7878       (insert-buffer-substring buf)
7879       (if (and (not read-only)
7880                (not (gnus-request-replace-article
7881                      (cdr gnus-article-current) (car gnus-article-current)
7882                      (current-buffer) t)))
7883           (error "Couldn't replace article")
7884         ;; Update the summary buffer.
7885         (if (and references
7886                  (equal (message-tokenize-header references " ")
7887                         (message-tokenize-header
7888                          (or (message-fetch-field "references") "") " ")))
7889             ;; We only have to update this line.
7890             (save-excursion
7891               (save-restriction
7892                 (message-narrow-to-head)
7893                 (let ((head (buffer-string))
7894                       header)
7895                   (with-temp-buffer
7896                     (insert (format "211 %d Article retrieved.\n"
7897                                     (cdr gnus-article-current)))
7898                     (insert head)
7899                     (insert ".\n")
7900                     (let ((nntp-server-buffer (current-buffer)))
7901                       (setq header (car (gnus-get-newsgroup-headers
7902                                          (save-excursion
7903                                            (set-buffer gnus-summary-buffer)
7904                                            gnus-newsgroup-dependencies)
7905                                          t))))
7906                     (save-excursion
7907                       (set-buffer gnus-summary-buffer)
7908                       (gnus-data-set-header
7909                        (gnus-data-find (cdr gnus-article-current))
7910                        header)
7911                       (gnus-summary-update-article-line
7912                        (cdr gnus-article-current) header))))))
7913           ;; Update threads.
7914           (set-buffer (or buffer gnus-summary-buffer))
7915           (gnus-summary-update-article (cdr gnus-article-current)))
7916         ;; Prettify the article buffer again.
7917         (unless no-highlight
7918           (save-excursion
7919             (set-buffer gnus-article-buffer)
7920             ;;;!!! Fix this -- article should be rehighlighted.
7921             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7922             (set-buffer gnus-original-article-buffer)
7923             (gnus-request-article
7924              (cdr gnus-article-current)
7925              (car gnus-article-current) (current-buffer))))
7926         ;; Prettify the summary buffer line.
7927         (when (gnus-visual-p 'summary-highlight 'highlight)
7928           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7929
7930 (defun gnus-summary-edit-wash (key)
7931   "Perform editing command KEY in the article buffer."
7932   (interactive
7933    (list
7934     (progn
7935       (message "%s" (concat (this-command-keys) "- "))
7936       (read-char))))
7937   (message "")
7938   (gnus-summary-edit-article)
7939   (execute-kbd-macro (concat (this-command-keys) key))
7940   (gnus-article-edit-done))
7941
7942 ;;; Respooling
7943
7944 (defun gnus-summary-respool-query (&optional silent trace)
7945   "Query where the respool algorithm would put this article."
7946   (interactive)
7947   (let (gnus-mark-article-hook)
7948     (gnus-summary-select-article)
7949     (save-excursion
7950       (set-buffer gnus-original-article-buffer)
7951       (save-restriction
7952         (message-narrow-to-head)
7953         (let ((groups (nnmail-article-group 'identity trace)))
7954           (unless silent
7955             (if groups
7956                 (message "This message would go to %s"
7957                          (mapconcat 'car groups ", "))
7958               (message "This message would go to no groups"))
7959             groups))))))
7960
7961 (defun gnus-summary-respool-trace ()
7962   "Trace where the respool algorithm would put this article.
7963 Display a buffer showing all fancy splitting patterns which matched."
7964   (interactive)
7965   (gnus-summary-respool-query nil t))
7966
7967 ;; Summary marking commands.
7968
7969 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7970   "Mark articles which has the same subject as read, and then select the next.
7971 If UNMARK is positive, remove any kind of mark.
7972 If UNMARK is negative, tick articles."
7973   (interactive "P")
7974   (when unmark
7975     (setq unmark (prefix-numeric-value unmark)))
7976   (let ((count
7977          (gnus-summary-mark-same-subject
7978           (gnus-summary-article-subject) unmark)))
7979     ;; Select next unread article.  If auto-select-same mode, should
7980     ;; select the first unread article.
7981     (gnus-summary-next-article t (and gnus-auto-select-same
7982                                       (gnus-summary-article-subject)))
7983     (gnus-message 7 "%d article%s marked as %s"
7984                   count (if (= count 1) " is" "s are")
7985                   (if unmark "unread" "read"))))
7986
7987 (defun gnus-summary-kill-same-subject (&optional unmark)
7988   "Mark articles which has the same subject as read.
7989 If UNMARK is positive, remove any kind of mark.
7990 If UNMARK is negative, tick articles."
7991   (interactive "P")
7992   (when unmark
7993     (setq unmark (prefix-numeric-value unmark)))
7994   (let ((count
7995          (gnus-summary-mark-same-subject
7996           (gnus-summary-article-subject) unmark)))
7997     ;; If marked as read, go to next unread subject.
7998     (when (null unmark)
7999       ;; Go to next unread subject.
8000       (gnus-summary-next-subject 1 t))
8001     (gnus-message 7 "%d articles are marked as %s"
8002                   count (if unmark "unread" "read"))))
8003
8004 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8005   "Mark articles with same SUBJECT as read, and return marked number.
8006 If optional argument UNMARK is positive, remove any kinds of marks.
8007 If optional argument UNMARK is negative, mark articles as unread instead."
8008   (let ((count 1))
8009     (save-excursion
8010       (cond
8011        ((null unmark)                   ; Mark as read.
8012         (while (and
8013                 (progn
8014                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8015                   (gnus-summary-show-thread) t)
8016                 (gnus-summary-find-subject subject))
8017           (setq count (1+ count))))
8018        ((> unmark 0)                    ; Tick.
8019         (while (and
8020                 (progn
8021                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8022                   (gnus-summary-show-thread) t)
8023                 (gnus-summary-find-subject subject))
8024           (setq count (1+ count))))
8025        (t                               ; Mark as unread.
8026         (while (and
8027                 (progn
8028                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8029                   (gnus-summary-show-thread) t)
8030                 (gnus-summary-find-subject subject))
8031           (setq count (1+ count)))))
8032       (gnus-set-mode-line 'summary)
8033       ;; Return the number of marked articles.
8034       count)))
8035
8036 (defun gnus-summary-mark-as-processable (n &optional unmark)
8037   "Set the process mark on the next N articles.
8038 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8039 the process mark instead.  The difference between N and the actual
8040 number of articles marked is returned."
8041   (interactive "p")
8042   (let ((backward (< n 0))
8043         (n (abs n)))
8044     (while (and
8045             (> n 0)
8046             (if unmark
8047                 (gnus-summary-remove-process-mark
8048                  (gnus-summary-article-number))
8049               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8050             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8051       (setq n (1- n)))
8052     (when (/= 0 n)
8053       (gnus-message 7 "No more articles"))
8054     (gnus-summary-recenter)
8055     (gnus-summary-position-point)
8056     n))
8057
8058 (defun gnus-summary-unmark-as-processable (n)
8059   "Remove the process mark from the next N articles.
8060 If N is negative, unmark backward instead.  The difference between N and
8061 the actual number of articles unmarked is returned."
8062   (interactive "p")
8063   (gnus-summary-mark-as-processable n t))
8064
8065 (defun gnus-summary-unmark-all-processable ()
8066   "Remove the process mark from all articles."
8067   (interactive)
8068   (save-excursion
8069     (while gnus-newsgroup-processable
8070       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8071   (gnus-summary-position-point))
8072
8073 (defun gnus-summary-mark-as-expirable (n)
8074   "Mark N articles forward as expirable.
8075 If N is negative, mark backward instead.  The difference between N and
8076 the actual number of articles marked is returned."
8077   (interactive "p")
8078   (gnus-summary-mark-forward n gnus-expirable-mark))
8079
8080 (defun gnus-summary-mark-article-as-replied (article)
8081   "Mark ARTICLE replied and update the summary line."
8082   (push article gnus-newsgroup-replied)
8083   (let ((buffer-read-only nil))
8084     (when (gnus-summary-goto-subject article nil t)
8085       (gnus-summary-update-secondary-mark article))))
8086
8087 (defun gnus-summary-set-bookmark (article)
8088   "Set a bookmark in current article."
8089   (interactive (list (gnus-summary-article-number)))
8090   (when (or (not (get-buffer gnus-article-buffer))
8091             (not gnus-current-article)
8092             (not gnus-article-current)
8093             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8094     (error "No current article selected"))
8095   ;; Remove old bookmark, if one exists.
8096   (let ((old (assq article gnus-newsgroup-bookmarks)))
8097     (when old
8098       (setq gnus-newsgroup-bookmarks
8099             (delq old gnus-newsgroup-bookmarks))))
8100   ;; Set the new bookmark, which is on the form
8101   ;; (article-number . line-number-in-body).
8102   (push
8103    (cons article
8104          (save-excursion
8105            (set-buffer gnus-article-buffer)
8106            (count-lines
8107             (min (point)
8108                  (save-excursion
8109                    (goto-char (point-min))
8110                    (search-forward "\n\n" nil t)
8111                    (point)))
8112             (point))))
8113    gnus-newsgroup-bookmarks)
8114   (gnus-message 6 "A bookmark has been added to the current article."))
8115
8116 (defun gnus-summary-remove-bookmark (article)
8117   "Remove the bookmark from the current article."
8118   (interactive (list (gnus-summary-article-number)))
8119   ;; Remove old bookmark, if one exists.
8120   (let ((old (assq article gnus-newsgroup-bookmarks)))
8121     (if old
8122         (progn
8123           (setq gnus-newsgroup-bookmarks
8124                 (delq old gnus-newsgroup-bookmarks))
8125           (gnus-message 6 "Removed bookmark."))
8126       (gnus-message 6 "No bookmark in current article."))))
8127
8128 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8129 (defun gnus-summary-mark-as-dormant (n)
8130   "Mark N articles forward as dormant.
8131 If N is negative, mark backward instead.  The difference between N and
8132 the actual number of articles marked is returned."
8133   (interactive "p")
8134   (gnus-summary-mark-forward n gnus-dormant-mark))
8135
8136 (defun gnus-summary-set-process-mark (article)
8137   "Set the process mark on ARTICLE and update the summary line."
8138   (setq gnus-newsgroup-processable
8139         (cons article
8140               (delq article gnus-newsgroup-processable)))
8141   (when (gnus-summary-goto-subject article)
8142     (gnus-summary-show-thread)
8143     (gnus-summary-goto-subject article)
8144     (gnus-summary-update-secondary-mark article)))
8145
8146 (defun gnus-summary-remove-process-mark (article)
8147   "Remove the process mark from ARTICLE and update the summary line."
8148   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8149   (when (gnus-summary-goto-subject article)
8150     (gnus-summary-show-thread)
8151     (gnus-summary-goto-subject article)
8152     (gnus-summary-update-secondary-mark article)))
8153
8154 (defun gnus-summary-set-saved-mark (article)
8155   "Set the process mark on ARTICLE and update the summary line."
8156   (push article gnus-newsgroup-saved)
8157   (when (gnus-summary-goto-subject article)
8158     (gnus-summary-update-secondary-mark article)))
8159
8160 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8161   "Mark N articles as read forwards.
8162 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8163 The difference between N and the actual number of articles marked is
8164 returned.
8165 Iff NO-EXPIRE, auto-expiry will be inhibited."
8166   (interactive "p")
8167   (gnus-summary-show-thread)
8168   (let ((backward (< n 0))
8169         (gnus-summary-goto-unread
8170          (and gnus-summary-goto-unread
8171               (not (eq gnus-summary-goto-unread 'never))
8172               (not (memq mark (list gnus-unread-mark
8173                                     gnus-ticked-mark gnus-dormant-mark)))))
8174         (n (abs n))
8175         (mark (or mark gnus-del-mark)))
8176     (while (and (> n 0)
8177                 (gnus-summary-mark-article nil mark no-expire)
8178                 (zerop (gnus-summary-next-subject
8179                         (if backward -1 1)
8180                         (and gnus-summary-goto-unread
8181                              (not (eq gnus-summary-goto-unread 'never)))
8182                         t)))
8183       (setq n (1- n)))
8184     (when (/= 0 n)
8185       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8186     (gnus-summary-recenter)
8187     (gnus-summary-position-point)
8188     (gnus-set-mode-line 'summary)
8189     n))
8190
8191 (defun gnus-summary-mark-article-as-read (mark)
8192   "Mark the current article quickly as read with MARK."
8193   (let ((article (gnus-summary-article-number)))
8194     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8195     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8196     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8197     (push (cons article mark) gnus-newsgroup-reads)
8198     ;; Possibly remove from cache, if that is used.
8199     (when gnus-use-cache
8200       (gnus-cache-enter-remove-article article))
8201     ;; Allow the backend to change the mark.
8202     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8203     ;; Check for auto-expiry.
8204     (when (and gnus-newsgroup-auto-expire
8205                (memq mark gnus-auto-expirable-marks))
8206       (setq mark gnus-expirable-mark)
8207       ;; Let the backend know about the mark change.
8208       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8209       (push article gnus-newsgroup-expirable))
8210     ;; Set the mark in the buffer.
8211     (gnus-summary-update-mark mark 'unread)
8212     t))
8213
8214 (defun gnus-summary-mark-article-as-unread (mark)
8215   "Mark the current article quickly as unread with MARK."
8216   (let* ((article (gnus-summary-article-number))
8217          (old-mark (gnus-summary-article-mark article)))
8218     ;; Allow the backend to change the mark.
8219     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8220     (if (eq mark old-mark)
8221         t
8222       (if (<= article 0)
8223           (progn
8224             (gnus-error 1 "Can't mark negative article numbers")
8225             nil)
8226         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8227         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8228         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8229         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8230         (cond ((= mark gnus-ticked-mark)
8231                (push article gnus-newsgroup-marked))
8232               ((= mark gnus-dormant-mark)
8233                (push article gnus-newsgroup-dormant))
8234               (t
8235                (push article gnus-newsgroup-unreads)))
8236         (gnus-pull article gnus-newsgroup-reads)
8237
8238         ;; See whether the article is to be put in the cache.
8239         (and gnus-use-cache
8240              (vectorp (gnus-summary-article-header article))
8241              (save-excursion
8242                (gnus-cache-possibly-enter-article
8243                 gnus-newsgroup-name article
8244                 (= mark gnus-ticked-mark)
8245                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8246
8247         ;; Fix the mark.
8248         (gnus-summary-update-mark mark 'unread)
8249         t))))
8250
8251 (defun gnus-summary-mark-article (&optional article mark no-expire)
8252   "Mark ARTICLE with MARK.  MARK can be any character.
8253 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8254 `??' (dormant) and `?E' (expirable).
8255 If MARK is nil, then the default character `?r' is used.
8256 If ARTICLE is nil, then the article on the current line will be
8257 marked.
8258 Iff NO-EXPIRE, auto-expiry will be inhibited."
8259   ;; The mark might be a string.
8260   (when (stringp mark)
8261     (setq mark (aref mark 0)))
8262   ;; If no mark is given, then we check auto-expiring.
8263   (when (null mark)
8264     (setq mark gnus-del-mark))
8265   (when (and (not no-expire)
8266              gnus-newsgroup-auto-expire
8267              (memq mark gnus-auto-expirable-marks))
8268     (setq mark gnus-expirable-mark))
8269   (let ((article (or article (gnus-summary-article-number)))
8270         (old-mark (gnus-summary-article-mark article)))
8271     ;; Allow the backend to change the mark.
8272     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8273     (if (eq mark old-mark)
8274         t
8275       (unless article
8276         (error "No article on current line"))
8277       (if (not (if (or (= mark gnus-unread-mark)
8278                        (= mark gnus-ticked-mark)
8279                        (= mark gnus-dormant-mark))
8280                    (gnus-mark-article-as-unread article mark)
8281                  (gnus-mark-article-as-read article mark)))
8282           t
8283         ;; See whether the article is to be put in the cache.
8284         (and gnus-use-cache
8285              (not (= mark gnus-canceled-mark))
8286              (vectorp (gnus-summary-article-header article))
8287              (save-excursion
8288                (gnus-cache-possibly-enter-article
8289                 gnus-newsgroup-name article
8290                 (= mark gnus-ticked-mark)
8291                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8292
8293         (when (gnus-summary-goto-subject article nil t)
8294           (let ((buffer-read-only nil))
8295             (gnus-summary-show-thread)
8296             ;; Fix the mark.
8297             (gnus-summary-update-mark mark 'unread)
8298             t))))))
8299
8300 (defun gnus-summary-update-secondary-mark (article)
8301   "Update the secondary (read, process, cache) mark."
8302   (gnus-summary-update-mark
8303    (cond ((memq article gnus-newsgroup-processable)
8304           gnus-process-mark)
8305          ((memq article gnus-newsgroup-cached)
8306           gnus-cached-mark)
8307          ((memq article gnus-newsgroup-replied)
8308           gnus-replied-mark)
8309          ((memq article gnus-newsgroup-saved)
8310           gnus-saved-mark)
8311          (t gnus-unread-mark))
8312    'replied)
8313   (when (gnus-visual-p 'summary-highlight 'highlight)
8314     (gnus-run-hooks 'gnus-summary-update-hook))
8315   t)
8316
8317 (defun gnus-summary-update-mark (mark type)
8318   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8319         (buffer-read-only nil))
8320     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8321     (when forward
8322       (when (looking-at "\r")
8323         (incf forward))
8324       (when (<= (+ forward (point)) (point-max))
8325         ;; Go to the right position on the line.
8326         (goto-char (+ forward (point)))
8327         ;; Replace the old mark with the new mark.
8328         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8329         ;; Optionally update the marks by some user rule.
8330         (when (eq type 'unread)
8331           (gnus-data-set-mark
8332            (gnus-data-find (gnus-summary-article-number)) mark)
8333           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8334
8335 (defun gnus-mark-article-as-read (article &optional mark)
8336   "Enter ARTICLE in the pertinent lists and remove it from others."
8337   ;; Make the article expirable.
8338   (let ((mark (or mark gnus-del-mark)))
8339     (if (= mark gnus-expirable-mark)
8340         (push article gnus-newsgroup-expirable)
8341       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8342     ;; Remove from unread and marked lists.
8343     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8344     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8345     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8346     (push (cons article mark) gnus-newsgroup-reads)
8347     ;; Possibly remove from cache, if that is used.
8348     (when gnus-use-cache
8349       (gnus-cache-enter-remove-article article))
8350     t))
8351
8352 (defun gnus-mark-article-as-unread (article &optional mark)
8353   "Enter ARTICLE in the pertinent lists and remove it from others."
8354   (let ((mark (or mark gnus-ticked-mark)))
8355     (if (<= article 0)
8356         (progn
8357           (gnus-error 1 "Can't mark negative article numbers")
8358           nil)
8359       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8360             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8361             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8362             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8363
8364       ;; Unsuppress duplicates?
8365       (when gnus-suppress-duplicates
8366         (gnus-dup-unsuppress-article article))
8367
8368       (cond ((= mark gnus-ticked-mark)
8369              (push article gnus-newsgroup-marked))
8370             ((= mark gnus-dormant-mark)
8371              (push article gnus-newsgroup-dormant))
8372             (t
8373              (push article gnus-newsgroup-unreads)))
8374       (gnus-pull article gnus-newsgroup-reads)
8375       t)))
8376
8377 (defalias 'gnus-summary-mark-as-unread-forward
8378   'gnus-summary-tick-article-forward)
8379 (make-obsolete 'gnus-summary-mark-as-unread-forward
8380                'gnus-summary-tick-article-forward)
8381 (defun gnus-summary-tick-article-forward (n)
8382   "Tick N articles forwards.
8383 If N is negative, tick backwards instead.
8384 The difference between N and the number of articles ticked is returned."
8385   (interactive "p")
8386   (gnus-summary-mark-forward n gnus-ticked-mark))
8387
8388 (defalias 'gnus-summary-mark-as-unread-backward
8389   'gnus-summary-tick-article-backward)
8390 (make-obsolete 'gnus-summary-mark-as-unread-backward
8391                'gnus-summary-tick-article-backward)
8392 (defun gnus-summary-tick-article-backward (n)
8393   "Tick N articles backwards.
8394 The difference between N and the number of articles ticked is returned."
8395   (interactive "p")
8396   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8397
8398 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8399 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8400 (defun gnus-summary-tick-article (&optional article clear-mark)
8401   "Mark current article as unread.
8402 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8403 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8404   (interactive)
8405   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8406                                        gnus-ticked-mark)))
8407
8408 (defun gnus-summary-mark-as-read-forward (n)
8409   "Mark N articles as read forwards.
8410 If N is negative, mark backwards instead.
8411 The difference between N and the actual number of articles marked is
8412 returned."
8413   (interactive "p")
8414   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8415
8416 (defun gnus-summary-mark-as-read-backward (n)
8417   "Mark the N articles as read backwards.
8418 The difference between N and the actual number of articles marked is
8419 returned."
8420   (interactive "p")
8421   (gnus-summary-mark-forward
8422    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8423
8424 (defun gnus-summary-mark-as-read (&optional article mark)
8425   "Mark current article as read.
8426 ARTICLE specifies the article to be marked as read.
8427 MARK specifies a string to be inserted at the beginning of the line."
8428   (gnus-summary-mark-article article mark))
8429
8430 (defun gnus-summary-clear-mark-forward (n)
8431   "Clear marks from N articles forward.
8432 If N is negative, clear backward instead.
8433 The difference between N and the number of marks cleared is returned."
8434   (interactive "p")
8435   (gnus-summary-mark-forward n gnus-unread-mark))
8436
8437 (defun gnus-summary-clear-mark-backward (n)
8438   "Clear marks from N articles backward.
8439 The difference between N and the number of marks cleared is returned."
8440   (interactive "p")
8441   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8442
8443 (defun gnus-summary-mark-unread-as-read ()
8444   "Intended to be used by `gnus-summary-mark-article-hook'."
8445   (when (memq gnus-current-article gnus-newsgroup-unreads)
8446     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8447
8448 (defun gnus-summary-mark-read-and-unread-as-read ()
8449   "Intended to be used by `gnus-summary-mark-article-hook'."
8450   (let ((mark (gnus-summary-article-mark)))
8451     (when (or (gnus-unread-mark-p mark)
8452               (gnus-read-mark-p mark))
8453       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8454
8455 (defun gnus-summary-mark-region-as-read (point mark all)
8456   "Mark all unread articles between point and mark as read.
8457 If given a prefix, mark all articles between point and mark as read,
8458 even ticked and dormant ones."
8459   (interactive "r\nP")
8460   (save-excursion
8461     (let (article)
8462       (goto-char point)
8463       (beginning-of-line)
8464       (while (and
8465               (< (point) mark)
8466               (progn
8467                 (when (or all
8468                           (memq (setq article (gnus-summary-article-number))
8469                                 gnus-newsgroup-unreads))
8470                   (gnus-summary-mark-article article gnus-del-mark))
8471                 t)
8472               (gnus-summary-find-next))))))
8473
8474 (defun gnus-summary-mark-below (score mark)
8475   "Mark articles with score less than SCORE with MARK."
8476   (interactive "P\ncMark: ")
8477   (setq score (if score
8478                   (prefix-numeric-value score)
8479                 (or gnus-summary-default-score 0)))
8480   (save-excursion
8481     (set-buffer gnus-summary-buffer)
8482     (goto-char (point-min))
8483     (while
8484         (progn
8485           (and (< (gnus-summary-article-score) score)
8486                (gnus-summary-mark-article nil mark))
8487           (gnus-summary-find-next)))))
8488
8489 (defun gnus-summary-kill-below (&optional score)
8490   "Mark articles with score below SCORE as read."
8491   (interactive "P")
8492   (gnus-summary-mark-below score gnus-killed-mark))
8493
8494 (defun gnus-summary-clear-above (&optional score)
8495   "Clear all marks from articles with score above SCORE."
8496   (interactive "P")
8497   (gnus-summary-mark-above score gnus-unread-mark))
8498
8499 (defun gnus-summary-tick-above (&optional score)
8500   "Tick all articles with score above SCORE."
8501   (interactive "P")
8502   (gnus-summary-mark-above score gnus-ticked-mark))
8503
8504 (defun gnus-summary-mark-above (score mark)
8505   "Mark articles with score over 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 (and (progn
8514                   (when (> (gnus-summary-article-score) score)
8515                     (gnus-summary-mark-article nil mark))
8516                   t)
8517                 (gnus-summary-find-next)))))
8518
8519 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8520 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8521 (defun gnus-summary-limit-include-expunged (&optional no-error)
8522   "Display all the hidden articles that were expunged for low scores."
8523   (interactive)
8524   (let ((buffer-read-only nil))
8525     (let ((scored gnus-newsgroup-scored)
8526           headers h)
8527       (while scored
8528         (unless (gnus-summary-goto-subject (caar scored))
8529           (and (setq h (gnus-summary-article-header (caar scored)))
8530                (< (cdar scored) gnus-summary-expunge-below)
8531                (push h headers)))
8532         (setq scored (cdr scored)))
8533       (if (not headers)
8534           (when (not no-error)
8535             (error "No expunged articles hidden"))
8536         (goto-char (point-min))
8537         (gnus-summary-prepare-unthreaded (nreverse headers))
8538         (goto-char (point-min))
8539         (gnus-summary-position-point)
8540         t))))
8541
8542 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8543   "Mark all unread articles in this newsgroup as read.
8544 If prefix argument ALL is non-nil, ticked and dormant articles will
8545 also be marked as read.
8546 If QUIETLY is non-nil, no questions will be asked.
8547 If TO-HERE is non-nil, it should be a point in the buffer.  All
8548 articles before this point will be marked as read.
8549 Note that this function will only catch up the unread article
8550 in the current summary buffer limitation.
8551 The number of articles marked as read is returned."
8552   (interactive "P")
8553   (prog1
8554       (save-excursion
8555         (when (or quietly
8556                   (not gnus-interactive-catchup) ;Without confirmation?
8557                   gnus-expert-user
8558                   (gnus-y-or-n-p
8559                    (if all
8560                        "Mark absolutely all articles as read? "
8561                      "Mark all unread articles as read? ")))
8562           (if (and not-mark
8563                    (not gnus-newsgroup-adaptive)
8564                    (not gnus-newsgroup-auto-expire)
8565                    (not gnus-suppress-duplicates)
8566                    (or (not gnus-use-cache)
8567                        (eq gnus-use-cache 'passive)))
8568               (progn
8569                 (when all
8570                   (setq gnus-newsgroup-marked nil
8571                         gnus-newsgroup-dormant nil))
8572                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8573             ;; We actually mark all articles as canceled, which we
8574             ;; have to do when using auto-expiry or adaptive scoring.
8575             (gnus-summary-show-all-threads)
8576             (when (gnus-summary-first-subject (not all) t)
8577               (while (and
8578                       (if to-here (< (point) to-here) t)
8579                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8580                       (gnus-summary-find-next (not all) nil nil t))))
8581             (gnus-set-mode-line 'summary))
8582           t))
8583     (gnus-summary-position-point)))
8584
8585 (defun gnus-summary-catchup-to-here (&optional all)
8586   "Mark all unticked articles before the current one as read.
8587 If ALL is non-nil, also mark ticked and dormant articles as read."
8588   (interactive "P")
8589   (save-excursion
8590     (gnus-save-hidden-threads
8591       (let ((beg (point)))
8592         ;; We check that there are unread articles.
8593         (when (or all (gnus-summary-find-prev))
8594           (gnus-summary-catchup all t beg)))))
8595   (gnus-summary-position-point))
8596
8597 (defun gnus-summary-catchup-all (&optional quietly)
8598   "Mark all articles in this newsgroup as read."
8599   (interactive "P")
8600   (gnus-summary-catchup t quietly))
8601
8602 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8603   "Mark all unread articles in this group as read, then exit.
8604 If prefix argument ALL is non-nil, all articles are marked as read."
8605   (interactive "P")
8606   (when (gnus-summary-catchup all quietly nil 'fast)
8607     ;; Select next newsgroup or exit.
8608     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8609              (eq gnus-auto-select-next 'quietly))
8610         (gnus-summary-next-group nil)
8611       (gnus-summary-exit))))
8612
8613 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8614   "Mark all articles in this newsgroup as read, and then exit."
8615   (interactive "P")
8616   (gnus-summary-catchup-and-exit t quietly))
8617
8618 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8619   "Mark all articles in this group as read and select the next group.
8620 If given a prefix, mark all articles, unread as well as ticked, as
8621 read."
8622   (interactive "P")
8623   (save-excursion
8624     (gnus-summary-catchup all))
8625   (gnus-summary-next-group))
8626
8627 ;;;
8628 ;;; with article
8629 ;;;
8630
8631 (defmacro gnus-with-article (article &rest forms)
8632   "Select ARTICLE and perform FORMS in the original article buffer.
8633 Then replace the article with the result."
8634   `(progn
8635      ;; We don't want the article to be marked as read.
8636      (let (gnus-mark-article-hook)
8637        (gnus-summary-select-article t t nil ,article))
8638      (set-buffer gnus-original-article-buffer)
8639      ,@forms
8640      (if (not (gnus-check-backend-function
8641                'request-replace-article (car gnus-article-current)))
8642          (gnus-message 5 "Read-only group; not replacing")
8643        (unless (gnus-request-replace-article
8644                 ,article (car gnus-article-current)
8645                 (current-buffer) t)
8646          (error "Couldn't replace article")))
8647      ;; The cache and backlog have to be flushed somewhat.
8648      (when gnus-keep-backlog
8649        (gnus-backlog-remove-article
8650         (car gnus-article-current) (cdr gnus-article-current)))
8651      (when gnus-use-cache
8652        (gnus-cache-update-article
8653         (car gnus-article-current) (cdr gnus-article-current)))))
8654
8655 (put 'gnus-with-article 'lisp-indent-function 1)
8656 (put 'gnus-with-article 'edebug-form-spec '(form body))
8657
8658 ;; Thread-based commands.
8659
8660 (defun gnus-summary-articles-in-thread (&optional article)
8661   "Return a list of all articles in the current thread.
8662 If ARTICLE is non-nil, return all articles in the thread that starts
8663 with that article."
8664   (let* ((article (or article (gnus-summary-article-number)))
8665          (data (gnus-data-find-list article))
8666          (top-level (gnus-data-level (car data)))
8667          (top-subject
8668           (cond ((null gnus-thread-operation-ignore-subject)
8669                  (gnus-simplify-subject-re
8670                   (mail-header-subject (gnus-data-header (car data)))))
8671                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8672                  (gnus-simplify-subject-fuzzy
8673                   (mail-header-subject (gnus-data-header (car data)))))
8674                 (t nil)))
8675          (end-point (save-excursion
8676                       (if (gnus-summary-go-to-next-thread)
8677                           (point) (point-max))))
8678          articles)
8679     (while (and data
8680                 (< (gnus-data-pos (car data)) end-point))
8681       (when (or (not top-subject)
8682                 (string= top-subject
8683                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8684                              (gnus-simplify-subject-fuzzy
8685                               (mail-header-subject
8686                                (gnus-data-header (car data))))
8687                            (gnus-simplify-subject-re
8688                             (mail-header-subject
8689                              (gnus-data-header (car data)))))))
8690         (push (gnus-data-number (car data)) articles))
8691       (unless (and (setq data (cdr data))
8692                    (> (gnus-data-level (car data)) top-level))
8693         (setq data nil)))
8694     ;; Return the list of articles.
8695     (nreverse articles)))
8696
8697 (defun gnus-summary-rethread-current ()
8698   "Rethread the thread the current article is part of."
8699   (interactive)
8700   (let* ((gnus-show-threads t)
8701          (article (gnus-summary-article-number))
8702          (id (mail-header-id (gnus-summary-article-header)))
8703          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8704     (unless id
8705       (error "No article on the current line"))
8706     (gnus-rebuild-thread id)
8707     (gnus-summary-goto-subject article)))
8708
8709 (defun gnus-summary-reparent-thread ()
8710   "Make the current article child of the marked (or previous) article.
8711
8712 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8713 is non-nil or the Subject: of both articles are the same."
8714   (interactive)
8715   (unless (not (gnus-group-read-only-p))
8716     (error "The current newsgroup does not support article editing"))
8717   (unless (<= (length gnus-newsgroup-processable) 1)
8718     (error "No more than one article may be marked"))
8719   (save-window-excursion
8720     (let ((gnus-article-buffer " *reparent*")
8721           (current-article (gnus-summary-article-number))
8722           ;; First grab the marked article, otherwise one line up.
8723           (parent-article (if (not (null gnus-newsgroup-processable))
8724                               (car gnus-newsgroup-processable)
8725                             (save-excursion
8726                               (if (eq (forward-line -1) 0)
8727                                   (gnus-summary-article-number)
8728                                 (error "Beginning of summary buffer"))))))
8729       (unless (not (eq current-article parent-article))
8730         (error "An article may not be self-referential"))
8731       (let ((message-id (mail-header-id
8732                          (gnus-summary-article-header parent-article))))
8733         (unless (and message-id (not (equal message-id "")))
8734           (error "No message-id in desired parent"))
8735         (gnus-with-article current-article
8736           (save-restriction
8737             (goto-char (point-min))
8738             (message-narrow-to-head)
8739             (if (re-search-forward "^References: " nil t)
8740                 (progn
8741                   (re-search-forward "^[^ \t]" nil t)
8742                   (forward-line -1)
8743                   (end-of-line)
8744                   (insert " " message-id))
8745               (insert "References: " message-id "\n"))))
8746         (set-buffer gnus-summary-buffer)
8747         (gnus-summary-unmark-all-processable)
8748         (gnus-summary-update-article current-article)
8749         (gnus-summary-rethread-current)
8750         (gnus-message 3 "Article %d is now the child of article %d"
8751                       current-article parent-article)))))
8752
8753 (defun gnus-summary-toggle-threads (&optional arg)
8754   "Toggle showing conversation threads.
8755 If ARG is positive number, turn showing conversation threads on."
8756   (interactive "P")
8757   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8758     (setq gnus-show-threads
8759           (if (null arg) (not gnus-show-threads)
8760             (> (prefix-numeric-value arg) 0)))
8761     (gnus-summary-prepare)
8762     (gnus-summary-goto-subject current)
8763     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8764     (gnus-summary-position-point)))
8765
8766 (defun gnus-summary-show-all-threads ()
8767   "Show all threads."
8768   (interactive)
8769   (save-excursion
8770     (let ((buffer-read-only nil))
8771       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8772   (gnus-summary-position-point))
8773
8774 (defun gnus-summary-show-thread ()
8775   "Show thread subtrees.
8776 Returns nil if no thread was there to be shown."
8777   (interactive)
8778   (let ((buffer-read-only nil)
8779         (orig (point))
8780         ;; first goto end then to beg, to have point at beg after let
8781         (end (progn (end-of-line) (point)))
8782         (beg (progn (beginning-of-line) (point))))
8783     (prog1
8784         ;; Any hidden lines here?
8785         (search-forward "\r" end t)
8786       (subst-char-in-region beg end ?\^M ?\n t)
8787       (goto-char orig)
8788       (gnus-summary-position-point))))
8789
8790 (defun gnus-summary-hide-all-threads ()
8791   "Hide all thread subtrees."
8792   (interactive)
8793   (save-excursion
8794     (goto-char (point-min))
8795     (gnus-summary-hide-thread)
8796     (while (zerop (gnus-summary-next-thread 1 t))
8797       (gnus-summary-hide-thread)))
8798   (gnus-summary-position-point))
8799
8800 (defun gnus-summary-hide-thread ()
8801   "Hide thread subtrees.
8802 Returns nil if no threads were there to be hidden."
8803   (interactive)
8804   (let ((buffer-read-only nil)
8805         (start (point))
8806         (article (gnus-summary-article-number)))
8807     (goto-char start)
8808     ;; Go forward until either the buffer ends or the subthread
8809     ;; ends.
8810     (when (and (not (eobp))
8811                (or (zerop (gnus-summary-next-thread 1 t))
8812                    (goto-char (point-max))))
8813       (prog1
8814           (if (and (> (point) start)
8815                    (search-backward "\n" start t))
8816               (progn
8817                 (subst-char-in-region start (point) ?\n ?\^M)
8818                 (gnus-summary-goto-subject article))
8819             (goto-char start)
8820             nil)))))
8821
8822 (defun gnus-summary-go-to-next-thread (&optional previous)
8823   "Go to the same level (or less) next thread.
8824 If PREVIOUS is non-nil, go to previous thread instead.
8825 Return the article number moved to, or nil if moving was impossible."
8826   (let ((level (gnus-summary-thread-level))
8827         (way (if previous -1 1))
8828         (beg (point)))
8829     (forward-line way)
8830     (while (and (not (eobp))
8831                 (< level (gnus-summary-thread-level)))
8832       (forward-line way))
8833     (if (eobp)
8834         (progn
8835           (goto-char beg)
8836           nil)
8837       (setq beg (point))
8838       (prog1
8839           (gnus-summary-article-number)
8840         (goto-char beg)))))
8841
8842 (defun gnus-summary-next-thread (n &optional silent)
8843   "Go to the same level next N'th thread.
8844 If N is negative, search backward instead.
8845 Returns the difference between N and the number of skips actually
8846 done.
8847
8848 If SILENT, don't output messages."
8849   (interactive "p")
8850   (let ((backward (< n 0))
8851         (n (abs n)))
8852     (while (and (> n 0)
8853                 (gnus-summary-go-to-next-thread backward))
8854       (decf n))
8855     (unless silent
8856       (gnus-summary-position-point))
8857     (when (and (not silent) (/= 0 n))
8858       (gnus-message 7 "No more threads"))
8859     n))
8860
8861 (defun gnus-summary-prev-thread (n)
8862   "Go to the same level previous N'th thread.
8863 Returns the difference between N and the number of skips actually
8864 done."
8865   (interactive "p")
8866   (gnus-summary-next-thread (- n)))
8867
8868 (defun gnus-summary-go-down-thread ()
8869   "Go down one level in the current thread."
8870   (let ((children (gnus-summary-article-children)))
8871     (when children
8872       (gnus-summary-goto-subject (car children)))))
8873
8874 (defun gnus-summary-go-up-thread ()
8875   "Go up one level in the current thread."
8876   (let ((parent (gnus-summary-article-parent)))
8877     (when parent
8878       (gnus-summary-goto-subject parent))))
8879
8880 (defun gnus-summary-down-thread (n)
8881   "Go down thread N steps.
8882 If N is negative, go up instead.
8883 Returns the difference between N and how many steps down that were
8884 taken."
8885   (interactive "p")
8886   (let ((up (< n 0))
8887         (n (abs n)))
8888     (while (and (> n 0)
8889                 (if up (gnus-summary-go-up-thread)
8890                   (gnus-summary-go-down-thread)))
8891       (setq n (1- n)))
8892     (gnus-summary-position-point)
8893     (when (/= 0 n)
8894       (gnus-message 7 "Can't go further"))
8895     n))
8896
8897 (defun gnus-summary-up-thread (n)
8898   "Go up thread N steps.
8899 If N is negative, go up instead.
8900 Returns the difference between N and how many steps down that were
8901 taken."
8902   (interactive "p")
8903   (gnus-summary-down-thread (- n)))
8904
8905 (defun gnus-summary-top-thread ()
8906   "Go to the top of the thread."
8907   (interactive)
8908   (while (gnus-summary-go-up-thread))
8909   (gnus-summary-article-number))
8910
8911 (defun gnus-summary-kill-thread (&optional unmark)
8912   "Mark articles under current thread as read.
8913 If the prefix argument is positive, remove any kinds of marks.
8914 If the prefix argument is negative, tick articles instead."
8915   (interactive "P")
8916   (when unmark
8917     (setq unmark (prefix-numeric-value unmark)))
8918   (let ((articles (gnus-summary-articles-in-thread)))
8919     (save-excursion
8920       ;; Expand the thread.
8921       (gnus-summary-show-thread)
8922       ;; Mark all the articles.
8923       (while articles
8924         (gnus-summary-goto-subject (car articles))
8925         (cond ((null unmark)
8926                (gnus-summary-mark-article-as-read gnus-killed-mark))
8927               ((> unmark 0)
8928                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8929               (t
8930                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8931         (setq articles (cdr articles))))
8932     ;; Hide killed subtrees.
8933     (and (null unmark)
8934          gnus-thread-hide-killed
8935          (gnus-summary-hide-thread))
8936     ;; If marked as read, go to next unread subject.
8937     (when (null unmark)
8938       ;; Go to next unread subject.
8939       (gnus-summary-next-subject 1 t)))
8940   (gnus-set-mode-line 'summary))
8941
8942 ;; Summary sorting commands
8943
8944 (defun gnus-summary-sort-by-number (&optional reverse)
8945   "Sort the summary buffer by article number.
8946 Argument REVERSE means reverse order."
8947   (interactive "P")
8948   (gnus-summary-sort 'number reverse))
8949
8950 (defun gnus-summary-sort-by-author (&optional reverse)
8951   "Sort the summary buffer by author name alphabetically.
8952 If `case-fold-search' is non-nil, case of letters is ignored.
8953 Argument REVERSE means reverse order."
8954   (interactive "P")
8955   (gnus-summary-sort 'author reverse))
8956
8957 (defun gnus-summary-sort-by-subject (&optional reverse)
8958   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8959 If `case-fold-search' is non-nil, case of letters is ignored.
8960 Argument REVERSE means reverse order."
8961   (interactive "P")
8962   (gnus-summary-sort 'subject reverse))
8963
8964 (defun gnus-summary-sort-by-date (&optional reverse)
8965   "Sort the summary buffer by date.
8966 Argument REVERSE means reverse order."
8967   (interactive "P")
8968   (gnus-summary-sort 'date reverse))
8969
8970 (defun gnus-summary-sort-by-score (&optional reverse)
8971   "Sort the summary buffer by score.
8972 Argument REVERSE means reverse order."
8973   (interactive "P")
8974   (gnus-summary-sort 'score reverse))
8975
8976 (defun gnus-summary-sort-by-lines (&optional reverse)
8977   "Sort the summary buffer by the number of lines.
8978 Argument REVERSE means reverse order."
8979   (interactive "P")
8980   (gnus-summary-sort 'lines reverse))
8981
8982 (defun gnus-summary-sort-by-chars (&optional reverse)
8983   "Sort the summary buffer by article length.
8984 Argument REVERSE means reverse order."
8985   (interactive "P")
8986   (gnus-summary-sort 'chars reverse))
8987
8988 (defun gnus-summary-sort (predicate reverse)
8989   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8990   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8991          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8992          (gnus-thread-sort-functions
8993           (if (not reverse)
8994               thread
8995             `(lambda (t1 t2)
8996                (,thread t2 t1))))
8997          (gnus-sort-gathered-threads-function
8998           gnus-thread-sort-functions)
8999          (gnus-article-sort-functions
9000           (if (not reverse)
9001               article
9002             `(lambda (t1 t2)
9003                (,article t2 t1))))
9004          (buffer-read-only)
9005          (gnus-summary-prepare-hook nil))
9006     ;; We do the sorting by regenerating the threads.
9007     (gnus-summary-prepare)
9008     ;; Hide subthreads if needed.
9009     (when (and gnus-show-threads gnus-thread-hide-subtree)
9010       (gnus-summary-hide-all-threads))))
9011
9012 ;; Summary saving commands.
9013
9014 (defun gnus-summary-save-article (&optional n not-saved)
9015   "Save the current article using the default saver function.
9016 If N is a positive number, save the N next articles.
9017 If N is a negative number, save the N previous articles.
9018 If N is nil and any articles have been marked with the process mark,
9019 save those articles instead.
9020 The variable `gnus-default-article-saver' specifies the saver function."
9021   (interactive "P")
9022   (let* ((articles (gnus-summary-work-articles n))
9023          (save-buffer (save-excursion
9024                         (nnheader-set-temp-buffer " *Gnus Save*")))
9025          (num (length articles))
9026          header file)
9027     (dolist (article articles)
9028       (setq header (gnus-summary-article-header article))
9029       (if (not (vectorp header))
9030           ;; This is a pseudo-article.
9031           (if (assq 'name header)
9032               (gnus-copy-file (cdr (assq 'name header)))
9033             (gnus-message 1 "Article %d is unsaveable" article))
9034         ;; This is a real article.
9035         (save-window-excursion
9036           (gnus-summary-select-article t nil nil article))
9037         (save-excursion
9038           (set-buffer save-buffer)
9039           (erase-buffer)
9040           (insert-buffer-substring gnus-original-article-buffer))
9041         (setq file (gnus-article-save save-buffer file num))
9042         (gnus-summary-remove-process-mark article)
9043         (unless not-saved
9044           (gnus-summary-set-saved-mark article))))
9045     (gnus-kill-buffer save-buffer)
9046     (gnus-summary-position-point)
9047     (gnus-set-mode-line 'summary)
9048     n))
9049
9050 (defun gnus-summary-pipe-output (&optional arg)
9051   "Pipe the current article to a subprocess.
9052 If N is a positive number, pipe the N next articles.
9053 If N is a negative number, pipe the N previous articles.
9054 If N is nil and any articles have been marked with the process mark,
9055 pipe those articles instead."
9056   (interactive "P")
9057   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9058     (gnus-summary-save-article arg t))
9059   (gnus-configure-windows 'pipe))
9060
9061 (defun gnus-summary-save-article-mail (&optional arg)
9062   "Append the current article to an mail file.
9063 If N is a positive number, save the N next articles.
9064 If N is a negative number, save the N previous articles.
9065 If N is nil and any articles have been marked with the process mark,
9066 save those articles instead."
9067   (interactive "P")
9068   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9069     (gnus-summary-save-article arg)))
9070
9071 (defun gnus-summary-save-article-rmail (&optional arg)
9072   "Append the current article to an rmail file.
9073 If N is a positive number, save the N next articles.
9074 If N is a negative number, save the N previous articles.
9075 If N is nil and any articles have been marked with the process mark,
9076 save those articles instead."
9077   (interactive "P")
9078   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9079     (gnus-summary-save-article arg)))
9080
9081 (defun gnus-summary-save-article-file (&optional arg)
9082   "Append the current article to a file.
9083 If N is a positive number, save the N next articles.
9084 If N is a negative number, save the N previous articles.
9085 If N is nil and any articles have been marked with the process mark,
9086 save those articles instead."
9087   (interactive "P")
9088   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9089     (gnus-summary-save-article arg)))
9090
9091 (defun gnus-summary-write-article-file (&optional arg)
9092   "Write the current article to a file, deleting the previous 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-write-to-file))
9099     (gnus-summary-save-article arg)))
9100
9101 (defun gnus-summary-save-article-body-file (&optional arg)
9102   "Append the current article body to a 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-body-in-file))
9109     (gnus-summary-save-article arg)))
9110
9111 (defun gnus-summary-pipe-message (program)
9112   "Pipe the current article through PROGRAM."
9113   (interactive "sProgram: ")
9114   (gnus-summary-select-article)
9115   (let ((mail-header-separator ""))
9116     (gnus-eval-in-buffer-window gnus-article-buffer
9117       (save-restriction
9118         (widen)
9119         (let ((start (window-start))
9120               buffer-read-only)
9121           (message-pipe-buffer-body program)
9122           (set-window-start (get-buffer-window (current-buffer)) start))))))
9123
9124 (defun gnus-get-split-value (methods)
9125   "Return a value based on the split METHODS."
9126   (let (split-name method result match)
9127     (when methods
9128       (save-excursion
9129         (set-buffer gnus-original-article-buffer)
9130         (save-restriction
9131           (nnheader-narrow-to-headers)
9132           (while (and methods (not split-name))
9133             (goto-char (point-min))
9134             (setq method (pop methods))
9135             (setq match (car method))
9136             (when (cond
9137                    ((stringp match)
9138                     ;; Regular expression.
9139                     (ignore-errors
9140                       (re-search-forward match nil t)))
9141                    ((gnus-functionp match)
9142                     ;; Function.
9143                     (save-restriction
9144                       (widen)
9145                       (setq result (funcall match gnus-newsgroup-name))))
9146                    ((consp match)
9147                     ;; Form.
9148                     (save-restriction
9149                       (widen)
9150                       (setq result (eval match)))))
9151               (setq split-name (cdr method))
9152               (cond ((stringp result)
9153                      (push (expand-file-name
9154                             result gnus-article-save-directory)
9155                            split-name))
9156                     ((consp result)
9157                      (setq split-name (append result split-name)))))))))
9158     (nreverse split-name)))
9159
9160 (defun gnus-valid-move-group-p (group)
9161   (and (boundp group)
9162        (symbol-name group)
9163        (symbol-value group)
9164        (gnus-get-function (gnus-find-method-for-group
9165                            (symbol-name group)) 'request-accept-article t)))
9166
9167 (defun gnus-read-move-group-name (prompt default articles prefix)
9168   "Read a group name."
9169   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9170          (minibuffer-confirm-incomplete nil) ; XEmacs
9171          (prom
9172           (format "%s %s to:"
9173                   prompt
9174                   (if (> (length articles) 1)
9175                       (format "these %d articles" (length articles))
9176                     "this article")))
9177          (to-newsgroup
9178           (cond
9179            ((null split-name)
9180             (gnus-completing-read default prom
9181                                   gnus-active-hashtb
9182                                   'gnus-valid-move-group-p
9183                                   nil prefix
9184                                   'gnus-group-history))
9185            ((= 1 (length split-name))
9186             (gnus-completing-read (car split-name) prom
9187                                   gnus-active-hashtb
9188                                   'gnus-valid-move-group-p
9189                                   nil nil
9190                                   'gnus-group-history))
9191            (t
9192             (gnus-completing-read nil prom
9193                                   (mapcar (lambda (el) (list el))
9194                                           (nreverse split-name))
9195                                   nil nil nil
9196                                   'gnus-group-history))))
9197          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9198     (when to-newsgroup
9199       (if (or (string= to-newsgroup "")
9200               (string= to-newsgroup prefix))
9201           (setq to-newsgroup default))
9202       (unless to-newsgroup
9203         (error "No group name entered"))
9204       (or (gnus-active to-newsgroup)
9205           (gnus-activate-group to-newsgroup nil nil to-method)
9206           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9207                                      to-newsgroup))
9208               (or (and (gnus-request-create-group to-newsgroup to-method)
9209                        (gnus-activate-group
9210                         to-newsgroup nil nil to-method)
9211                        (gnus-subscribe-group to-newsgroup))
9212                   (error "Couldn't create group %s" to-newsgroup)))
9213           (error "No such group: %s" to-newsgroup)))
9214     to-newsgroup))
9215
9216 (defun gnus-summary-save-parts (type dir n &optional reverse)
9217   "Save parts matching TYPE to DIR.
9218 If REVERSE, save parts that do not match TYPE."
9219   (interactive
9220    (list (read-string "Save parts of type: " 
9221                       (or (car gnus-summary-save-parts-type-history)
9222                           gnus-summary-save-parts-default-mime)
9223                       'gnus-summary-save-parts-type-history)
9224          (setq gnus-summary-save-parts-last-directory
9225                (read-file-name "Save to directory: " 
9226                                gnus-summary-save-parts-last-directory
9227                                nil t))
9228          current-prefix-arg))
9229   (gnus-summary-iterate n
9230     (let ((gnus-display-mime-function nil)
9231           (gnus-inhibit-treatment t))
9232       (gnus-summary-select-article))
9233     (save-excursion
9234       (set-buffer gnus-article-buffer)
9235       (let ((handles (or gnus-article-mime-handles
9236                          (mm-dissect-buffer) (mm-uu-dissect))))
9237         (when handles
9238           (gnus-summary-save-parts-1 type dir handles reverse)
9239           (unless gnus-article-mime-handles ;; Don't destroy this case.
9240             (mm-destroy-parts handles)))))))
9241
9242 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9243   (if (stringp (car handle))
9244       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9245               (cdr handle))
9246     (when (if reverse
9247               (not (string-match type (mm-handle-media-type handle)))
9248             (string-match type (mm-handle-media-type handle)))
9249       (let ((file (expand-file-name
9250                    (file-name-nondirectory
9251                     (or
9252                      (mail-content-type-get
9253                       (mm-handle-disposition handle) 'filename)
9254                      (concat gnus-newsgroup-name
9255                              "." (number-to-string
9256                                   (cdr gnus-article-current)))))
9257                    dir)))
9258         (unless (file-exists-p file)
9259           (mm-save-part-to-file handle file))))))
9260
9261 ;; Summary extract commands
9262
9263 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9264   (let ((buffer-read-only nil)
9265         (article (gnus-summary-article-number))
9266         after-article b e)
9267     (unless (gnus-summary-goto-subject article)
9268       (error "No such article: %d" article))
9269     (gnus-summary-position-point)
9270     ;; If all commands are to be bunched up on one line, we collect
9271     ;; them here.
9272     (unless gnus-view-pseudos-separately
9273       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9274             files action)
9275         (while ps
9276           (setq action (cdr (assq 'action (car ps))))
9277           (setq files (list (cdr (assq 'name (car ps)))))
9278           (while (and ps (cdr ps)
9279                       (string= (or action "1")
9280                                (or (cdr (assq 'action (cadr ps))) "2")))
9281             (push (cdr (assq 'name (cadr ps))) files)
9282             (setcdr ps (cddr ps)))
9283           (when files
9284             (when (not (string-match "%s" action))
9285               (push " " files))
9286             (push " " files)
9287             (when (assq 'execute (car ps))
9288               (setcdr (assq 'execute (car ps))
9289                       (funcall (if (string-match "%s" action)
9290                                    'format 'concat)
9291                                action
9292                                (mapconcat
9293                                 (lambda (f)
9294                                   (if (equal f " ")
9295                                       f
9296                                     (mm-quote-arg f)))
9297                                 files " ")))))
9298           (setq ps (cdr ps)))))
9299     (if (and gnus-view-pseudos (not not-view))
9300         (while pslist
9301           (when (assq 'execute (car pslist))
9302             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9303                                   (eq gnus-view-pseudos 'not-confirm)))
9304           (setq pslist (cdr pslist)))
9305       (save-excursion
9306         (while pslist
9307           (setq after-article (or (cdr (assq 'article (car pslist)))
9308                                   (gnus-summary-article-number)))
9309           (gnus-summary-goto-subject after-article)
9310           (forward-line 1)
9311           (setq b (point))
9312           (insert "    " (file-name-nondirectory
9313                           (cdr (assq 'name (car pslist))))
9314                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9315           (setq e (point))
9316           (forward-line -1)             ; back to `b'
9317           (gnus-add-text-properties
9318            b (1- e) (list 'gnus-number gnus-reffed-article-number
9319                           gnus-mouse-face-prop gnus-mouse-face))
9320           (gnus-data-enter
9321            after-article gnus-reffed-article-number
9322            gnus-unread-mark b (car pslist) 0 (- e b))
9323           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9324           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9325           (setq pslist (cdr pslist)))))))
9326
9327 (defun gnus-pseudos< (p1 p2)
9328   (let ((c1 (cdr (assq 'action p1)))
9329         (c2 (cdr (assq 'action p2))))
9330     (and c1 c2 (string< c1 c2))))
9331
9332 (defun gnus-request-pseudo-article (props)
9333   (cond ((assq 'execute props)
9334          (gnus-execute-command (cdr (assq 'execute props)))))
9335   (let ((gnus-current-article (gnus-summary-article-number)))
9336     (gnus-run-hooks 'gnus-mark-article-hook)))
9337
9338 (defun gnus-execute-command (command &optional automatic)
9339   (save-excursion
9340     (gnus-article-setup-buffer)
9341     (set-buffer gnus-article-buffer)
9342     (setq buffer-read-only nil)
9343     (let ((command (if automatic command
9344                      (read-string "Command: " (cons command 0)))))
9345       (erase-buffer)
9346       (insert "$ " command "\n\n")
9347       (if gnus-view-pseudo-asynchronously
9348           (start-process "gnus-execute" (current-buffer) shell-file-name
9349                          shell-command-switch command)
9350         (call-process shell-file-name nil t nil
9351                       shell-command-switch command)))))
9352
9353 ;; Summary kill commands.
9354
9355 (defun gnus-summary-edit-global-kill (article)
9356   "Edit the \"global\" kill file."
9357   (interactive (list (gnus-summary-article-number)))
9358   (gnus-group-edit-global-kill article))
9359
9360 (defun gnus-summary-edit-local-kill ()
9361   "Edit a local kill file applied to the current newsgroup."
9362   (interactive)
9363   (setq gnus-current-headers (gnus-summary-article-header))
9364   (gnus-group-edit-local-kill
9365    (gnus-summary-article-number) gnus-newsgroup-name))
9366
9367 ;;; Header reading.
9368
9369 (defun gnus-read-header (id &optional header)
9370   "Read the headers of article ID and enter them into the Gnus system."
9371   (let ((group gnus-newsgroup-name)
9372         (gnus-override-method
9373          (or
9374           gnus-override-method
9375           (and (gnus-news-group-p gnus-newsgroup-name)
9376                (car (gnus-refer-article-methods)))))
9377         where)
9378     ;; First we check to see whether the header in question is already
9379     ;; fetched.
9380     (if (stringp id)
9381         ;; This is a Message-ID.
9382         (setq header (or header (gnus-id-to-header id)))
9383       ;; This is an article number.
9384       (setq header (or header (gnus-summary-article-header id))))
9385     (if (and header
9386              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9387         ;; We have found the header.
9388         header
9389       ;; If this is a sparse article, we have to nix out its
9390       ;; previous entry in the thread hashtb.
9391       (when (and header
9392                  (gnus-summary-article-sparse-p (mail-header-number header)))
9393         (let* ((parent (gnus-parent-id (mail-header-references header)))
9394                (thread (and parent (gnus-id-to-thread parent))))
9395           (when thread
9396             (delq (assq header thread) thread))))
9397       ;; We have to really fetch the header to this article.
9398       (save-excursion
9399         (set-buffer nntp-server-buffer)
9400         (when (setq where (gnus-request-head id group))
9401           (nnheader-fold-continuation-lines)
9402           (goto-char (point-max))
9403           (insert ".\n")
9404           (goto-char (point-min))
9405           (insert "211 ")
9406           (princ (cond
9407                   ((numberp id) id)
9408                   ((cdr where) (cdr where))
9409                   (header (mail-header-number header))
9410                   (t gnus-reffed-article-number))
9411                  (current-buffer))
9412           (insert " Article retrieved.\n"))
9413         (if (or (not where)
9414                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9415             ()                          ; Malformed head.
9416           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9417             (when (and (stringp id)
9418                        (not (string= (gnus-group-real-name group)
9419                                      (car where))))
9420               ;; If we fetched by Message-ID and the article came
9421               ;; from a different group, we fudge some bogus article
9422               ;; numbers for this article.
9423               (mail-header-set-number header gnus-reffed-article-number))
9424             (save-excursion
9425               (set-buffer gnus-summary-buffer)
9426               (decf gnus-reffed-article-number)
9427               (gnus-remove-header (mail-header-number header))
9428               (push header gnus-newsgroup-headers)
9429               (setq gnus-current-headers header)
9430               (push (mail-header-number header) gnus-newsgroup-limit)))
9431           header)))))
9432
9433 (defun gnus-remove-header (number)
9434   "Remove header NUMBER from `gnus-newsgroup-headers'."
9435   (if (and gnus-newsgroup-headers
9436            (= number (mail-header-number (car gnus-newsgroup-headers))))
9437       (pop gnus-newsgroup-headers)
9438     (let ((headers gnus-newsgroup-headers))
9439       (while (and (cdr headers)
9440                   (not (= number (mail-header-number (cadr headers)))))
9441         (pop headers))
9442       (when (cdr headers)
9443         (setcdr headers (cddr headers))))))
9444
9445 ;;;
9446 ;;; summary highlights
9447 ;;;
9448
9449 (defun gnus-highlight-selected-summary ()
9450   "Highlight selected article in summary buffer."
9451   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9452   (when gnus-summary-selected-face
9453     (save-excursion
9454       (let* ((beg (progn (beginning-of-line) (point)))
9455              (end (progn (end-of-line) (point)))
9456              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9457              (from (if (get-text-property beg gnus-mouse-face-prop)
9458                        beg
9459                      (or (next-single-property-change
9460                           beg gnus-mouse-face-prop nil end)
9461                          beg)))
9462              (to
9463               (if (= from end)
9464                   (- from 2)
9465                 (or (next-single-property-change
9466                      from gnus-mouse-face-prop nil end)
9467                     end))))
9468         ;; If no mouse-face prop on line we will have to = from = end,
9469         ;; so we highlight the entire line instead.
9470         (when (= (+ to 2) from)
9471           (setq from beg)
9472           (setq to end))
9473         (if gnus-newsgroup-selected-overlay
9474             ;; Move old overlay.
9475             (gnus-move-overlay
9476              gnus-newsgroup-selected-overlay from to (current-buffer))
9477           ;; Create new overlay.
9478           (gnus-overlay-put
9479            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9480            'face gnus-summary-selected-face))))))
9481
9482 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9483 (defun gnus-summary-highlight-line ()
9484   "Highlight current line according to `gnus-summary-highlight'."
9485   (let* ((list gnus-summary-highlight)
9486          (p (point))
9487          (end (progn (end-of-line) (point)))
9488          ;; now find out where the line starts and leave point there.
9489          (beg (progn (beginning-of-line) (point)))
9490          (article (gnus-summary-article-number))
9491          (score (or (cdr (assq (or article gnus-current-article)
9492                                gnus-newsgroup-scored))
9493                     gnus-summary-default-score 0))
9494          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9495          (inhibit-read-only t))
9496     ;; Eval the cars of the lists until we find a match.
9497     (let ((default gnus-summary-default-score))
9498       (while (and list
9499                   (not (eval (caar list))))
9500         (setq list (cdr list))))
9501     (let ((face (cdar list)))
9502       (unless (eq face (get-text-property beg 'face))
9503         (gnus-put-text-property-excluding-characters-with-faces
9504          beg end 'face
9505          (setq face (if (boundp face) (symbol-value face) face)))
9506         (when gnus-summary-highlight-line-function
9507           (funcall gnus-summary-highlight-line-function article face))))
9508     (goto-char p)))
9509
9510 (defun gnus-update-read-articles (group unread &optional compute)
9511   "Update the list of read articles in GROUP."
9512   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9513          (entry (gnus-gethash group gnus-newsrc-hashtb))
9514          (info (nth 2 entry))
9515          (prev 1)
9516          (unread (sort (copy-sequence unread) '<))
9517          read)
9518     (if (or (not info) (not active))
9519         ;; There is no info on this group if it was, in fact,
9520         ;; killed.  Gnus stores no information on killed groups, so
9521         ;; there's nothing to be done.
9522         ;; One could store the information somewhere temporarily,
9523         ;; perhaps...  Hmmm...
9524         ()
9525       ;; Remove any negative articles numbers.
9526       (while (and unread (< (car unread) 0))
9527         (setq unread (cdr unread)))
9528       ;; Remove any expired article numbers
9529       (while (and unread (< (car unread) (car active)))
9530         (setq unread (cdr unread)))
9531       ;; Compute the ranges of read articles by looking at the list of
9532       ;; unread articles.
9533       (while unread
9534         (when (/= (car unread) prev)
9535           (push (if (= prev (1- (car unread))) prev
9536                   (cons prev (1- (car unread))))
9537                 read))
9538         (setq prev (1+ (car unread)))
9539         (setq unread (cdr unread)))
9540       (when (<= prev (cdr active))
9541         (push (cons prev (cdr active)) read))
9542       (setq read (if (> (length read) 1) (nreverse read) read))
9543       (if compute
9544           read
9545         (save-excursion
9546           (let (setmarkundo)
9547             ;; Propagate the read marks to the backend.
9548             (when (gnus-check-backend-function 'request-set-mark group)
9549               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9550                     (add (gnus-remove-from-range read (gnus-info-read info))))
9551                 (when (or add del)
9552                   (unless (gnus-check-group group)
9553                     (error "Can't open server for %s" group))
9554                   (gnus-request-set-mark
9555                    group (delq nil (list (if add (list add 'add '(read)))
9556                                          (if del (list del 'del '(read))))))
9557                   (setq setmarkundo
9558                         `(gnus-request-set-mark
9559                           ,group
9560                           ',(delq nil (list
9561                                        (if del (list del 'add '(read)))
9562                                        (if add (list add 'del '(read))))))))))
9563             (set-buffer gnus-group-buffer)
9564             (gnus-undo-register
9565               `(progn
9566                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9567                  (gnus-info-set-read ',info ',(gnus-info-read info))
9568                  (gnus-get-unread-articles-in-group ',info 
9569                                                     (gnus-active ,group))
9570                  (gnus-group-update-group ,group t)
9571                  ,setmarkundo))))
9572         ;; Enter this list into the group info.
9573         (gnus-info-set-read info read)
9574         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9575         (gnus-get-unread-articles-in-group info (gnus-active group))
9576         t))))
9577
9578 (defun gnus-offer-save-summaries ()
9579   "Offer to save all active summary buffers."
9580   (save-excursion
9581     (let ((buflist (buffer-list))
9582           buffers bufname)
9583       ;; Go through all buffers and find all summaries.
9584       (while buflist
9585         (and (setq bufname (buffer-name (car buflist)))
9586              (string-match "Summary" bufname)
9587              (save-excursion
9588                (set-buffer bufname)
9589                ;; We check that this is, indeed, a summary buffer.
9590                (and (eq major-mode 'gnus-summary-mode)
9591                     ;; Also make sure this isn't bogus.
9592                     gnus-newsgroup-prepared
9593                     ;; Also make sure that this isn't a dead summary buffer.
9594                     (not gnus-dead-summary-mode)))
9595              (push bufname buffers))
9596         (setq buflist (cdr buflist)))
9597       ;; Go through all these summary buffers and offer to save them.
9598       (when buffers
9599         (map-y-or-n-p
9600          "Update summary buffer %s? "
9601          (lambda (buf)
9602            (switch-to-buffer buf)
9603            (gnus-summary-exit))
9604          buffers)))))
9605
9606 (defun gnus-summary-setup-default-charset ()
9607   "Setup newsgroup default charset."
9608   (if (equal gnus-newsgroup-name "nndraft:drafts")
9609       (setq gnus-newsgroup-charset nil)
9610     (let* ((name (and gnus-newsgroup-name
9611                       (gnus-group-real-name gnus-newsgroup-name)))
9612            (ignored-charsets
9613             (or gnus-newsgroup-ephemeral-ignored-charsets
9614                 (append
9615                  (and gnus-newsgroup-name
9616                       (or (gnus-group-find-parameter gnus-newsgroup-name
9617                                                      'ignored-charsets t)
9618                           (let ((alist gnus-group-ignored-charsets-alist)
9619                                 elem (charsets nil))
9620                             (while (setq elem (pop alist))
9621                               (when (and name
9622                                          (string-match (car elem) name))
9623                                 (setq alist nil
9624                                       charsets (cdr elem))))
9625                             charsets)))
9626                  gnus-newsgroup-ignored-charsets))))
9627       (setq gnus-newsgroup-charset
9628             (or gnus-newsgroup-ephemeral-charset
9629                 (and gnus-newsgroup-name
9630                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9631                          (let ((alist gnus-group-charset-alist)
9632                                elem charset)
9633                            (while (setq elem (pop alist))
9634                              (when (and name
9635                                         (string-match (car elem) name))
9636                                (setq alist nil
9637                                      charset (cadr elem))))
9638                            charset)))
9639                 gnus-default-charset))
9640       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9641            ignored-charsets))))
9642
9643 ;;;
9644 ;;; Mime Commands
9645 ;;;
9646
9647 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9648   "Display the current article buffer fully MIME-buttonized.
9649 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9650 treated as multipart/mixed."
9651   (interactive "P")
9652   (require 'gnus-art)
9653   (let ((gnus-unbuttonized-mime-types nil)
9654         (gnus-mime-display-multipart-as-mixed show-all-parts))
9655     (gnus-summary-show-article)))
9656
9657 (defun gnus-summary-repair-multipart (article)
9658   "Add a Content-Type header to a multipart article without one."
9659   (interactive (list (gnus-summary-article-number)))
9660   (gnus-with-article article
9661     (message-narrow-to-head)
9662     (goto-char (point-max))
9663     (widen)
9664     (when (search-forward "\n--" nil t)
9665       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9666         (message-narrow-to-head)
9667         (message-remove-header "Mime-Version")
9668         (message-remove-header "Content-Type")
9669         (goto-char (point-max))
9670         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9671                         separator))
9672         (insert "Mime-Version: 1.0\n")
9673         (widen))))
9674   (let (gnus-mark-article-hook)
9675     (gnus-summary-select-article t t nil article)))
9676
9677 (defun gnus-summary-toggle-display-buttonized ()
9678   "Toggle the buttonizing of the article buffer."
9679   (interactive)
9680   (require 'gnus-art)
9681   (if (setq gnus-inhibit-mime-unbuttonizing
9682             (not gnus-inhibit-mime-unbuttonizing))
9683       (let ((gnus-unbuttonized-mime-types nil))
9684         (gnus-summary-show-article))
9685     (gnus-summary-show-article)))
9686
9687 ;;;
9688 ;;; Generic summary marking commands
9689 ;;;
9690
9691 (defvar gnus-summary-marking-alist
9692   '((read gnus-del-mark "d")
9693     (unread gnus-unread-mark "u")
9694     (ticked gnus-ticked-mark "!")
9695     (dormant gnus-dormant-mark "?")
9696     (expirable gnus-expirable-mark "e"))
9697   "An alist of names/marks/keystrokes.")
9698
9699 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9700 (defvar gnus-summary-mark-map)
9701
9702 (defun gnus-summary-make-all-marking-commands ()
9703   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9704   (dolist (elem gnus-summary-marking-alist)
9705     (apply 'gnus-summary-make-marking-command elem)))
9706
9707 (defun gnus-summary-make-marking-command (name mark keystroke)
9708   (let ((map (make-sparse-keymap)))
9709     (define-key gnus-summary-generic-mark-map keystroke map)
9710     (dolist (lway `((next "next" next nil "n")
9711                     (next-unread "next unread" next t "N")
9712                     (prev "previous" prev nil "p")
9713                     (prev-unread "previous unread" prev t "P")
9714                     (nomove "" nil nil ,keystroke)))
9715       (let ((func (gnus-summary-make-marking-command-1
9716                    mark (car lway) lway name)))
9717         (setq func (eval func))
9718         (define-key map (nth 4 lway) func)))))
9719       
9720 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9721   `(defun ,(intern
9722             (format "gnus-summary-put-mark-as-%s%s"
9723                     name (if (eq way 'nomove)
9724                              ""
9725                            (concat "-" (symbol-name way)))))
9726      (n)
9727      ,(format
9728        "Mark the current article as %s%s.
9729 If N, the prefix, then repeat N times.
9730 If N is negative, move in reverse order.
9731 The difference between N and the actual number of articles marked is
9732 returned."
9733        name (cadr lway))
9734      (interactive "p")
9735      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9736     
9737 (defun gnus-summary-generic-mark (n mark move unread)
9738   "Mark N articles with MARK."
9739   (unless (eq major-mode 'gnus-summary-mode)
9740     (error "This command can only be used in the summary buffer"))
9741   (gnus-summary-show-thread)
9742   (let ((nummove
9743          (cond
9744           ((eq move 'next) 1)
9745           ((eq move 'prev) -1)
9746           (t 0))))
9747     (if (zerop nummove)
9748         (setq n 1)
9749       (when (< n 0)
9750         (setq n (abs n)
9751               nummove (* -1 nummove))))
9752     (while (and (> n 0)
9753                 (gnus-summary-mark-article nil mark)
9754                 (zerop (gnus-summary-next-subject nummove unread t)))
9755       (setq n (1- n)))
9756     (when (/= 0 n)
9757       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9758     (gnus-summary-recenter)
9759     (gnus-summary-position-point)
9760     (gnus-set-mode-line 'summary)
9761     n))
9762
9763 (gnus-summary-make-all-marking-commands)
9764
9765 (gnus-ems-redefine)
9766
9767 (provide 'gnus-sum)
9768
9769 (run-hooks 'gnus-sum-load-hook)
9770
9771 ;;; gnus-sum.el ends here