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