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